/system/application/libraries/cleaner/class.cleaner.php

https://github.com/geekbuntu/Microweber · PHP · 379 lines · 301 code · 70 blank · 8 comment · 232 complexity · 3de3601662884d0ec90cd81243098a80 MD5 · raw file

  1. <?
  2. class codeCleaner{
  3. var $standardCurly = false;
  4. var $tabSpaces = false;
  5. var $removeempty = true;
  6. var $functionComm = true;
  7. var $ifComm = true;
  8. var $loopComm = true;
  9. var $classComm = true;
  10. //var $ifComm = true;
  11. //var $lookComm = true;
  12. //var $commLines = 4;
  13. function cleanFile($filename,$contents=""){
  14. if($filename) {
  15. $codelines = file($filename);
  16. echo "Going to Indent Code of : <b>$filename</b> <br>";
  17. }
  18. if($contents){
  19. $codelines = explode("\n" , $contents);
  20. }
  21. $newlines = "";
  22. foreach($codelines as $oneline){
  23. if(trim($oneline) || $this->removeempty == false){
  24. //$oneline = ltrim($oneline)
  25. $newlines .= $oneline;
  26. }
  27. }
  28. $newlines = $this->parseIt($newlines);
  29. $newlines = $this->addComments($newlines);
  30. return trim($newlines);
  31. }
  32. function addComments($contents){
  33. if(!$this->functionComm && !$this->classComm) return $contents;
  34. $class_comm = "// -- Class Name : {class_name}\n";
  35. $class_comm .= "// -- Purpose : \n";
  36. $class_comm .= "// -- Created On : ";
  37. $func_comm = "// -- Function Name : {func_name}\n";
  38. $func_comm .= "// -- Params : {func_params}\n";
  39. $func_comm .= "// -- Purpose : ";
  40. $newstr = "";
  41. $tokens = token_get_all($contents);
  42. foreach($tokens as $ind => $token){
  43. $name = @token_name($token[0]);
  44. $value = $token[1];
  45. $line = $token[2];
  46. if(!trim($name)){
  47. $value = $token[0];
  48. if($token[0] == "{") $name = "CURLY_START";
  49. if($token[0] == "}") $name = "CURLY_END";
  50. if($token[0] == "(") $name = "SMALL_START";
  51. if($token[0] == ")") $name = "SMALL_END";
  52. if($token[0] == "[") $name = "SQ_START";
  53. if($token[0] == "]") $name = "SQ_END";
  54. if($token[0] == ",") $name = "COMMA";
  55. if($token[0] == ";") $name = "COLON";
  56. if($token[0] == "=") $name = "EQUAL";
  57. if($token[0] == ".") $name = "DOT";
  58. }
  59. if(!trim($name)){
  60. $name = "UNKNOWN";
  61. }
  62. $comments = "";
  63. $fname = "";
  64. $cname = "";
  65. if($name == "T_CLASS" && $lastone != "T_COMMENT" && $this->classComm ){
  66. for($i=1 ; $i < 100 ; $i++){
  67. $nexttok = $tokens[($ind+$i)];
  68. if(@token_name($nexttok[0]) == "T_STRING"){
  69. $cname = $nexttok[1];
  70. break;
  71. }
  72. }
  73. $comments = str_replace("{class_name}" , $cname , $class_comm);
  74. }
  75. if($name == "T_FUNCTION" && $lastone != "T_COMMENT" && $this->functionComm){
  76. for($i=1 ; $i < 100 ; $i++){
  77. $nexttok = $tokens[($ind+$i)];
  78. if(@token_name($nexttok[0]) == "T_STRING"){
  79. $fname = $nexttok[1];
  80. break;
  81. }
  82. }
  83. $params = "";
  84. $so = 0;
  85. $sc = 0;
  86. for($x=($i+1) ; $x < 100 ; $x++){
  87. $nexttok = $tokens[($ind+$x)];
  88. if(!$nexttok[1]) $v = $nexttok[0];
  89. else $v = $nexttok[1];
  90. $params .= $v;
  91. if($v == "(") $so++;
  92. if($v == ")") $sc++;
  93. if($v == ")" && ($so > 0 && $so == $sc)){
  94. break;
  95. }
  96. }
  97. $comments = str_replace("{func_name}" , $fname , $func_comm);
  98. $comments = str_replace("{func_params}" , substr(trim($params) , 1 , -1) , $comments);
  99. }
  100. if($comments){
  101. $tabs = "";
  102. for($i=0 ; $i < 1000 ; $i++){
  103. $pos = ((strlen($newstr)-1)-$i);
  104. $chr = $newstr[$pos];
  105. if(ord($chr) == 9) {
  106. if($this->tabSpaces == false )$tabs .= "\t";
  107. else $tabs .= " ";
  108. }
  109. if(ord($chr) == 10) {
  110. //$pos--;
  111. break;
  112. }
  113. }
  114. $part1 = substr($newstr , 0 , $pos);
  115. $part2 = substr($newstr , ($pos+1));
  116. $newstr = $part1."\n\n".str_replace("//" , "$tabs//" , $comments)."\n".$part2;
  117. }
  118. $newstr .= $value;
  119. if($name != "T_WHITESPACE") $lastone = $name;
  120. }
  121. return $newstr;
  122. }
  123. function parseIt($contents){
  124. $tokens = token_get_all($contents);
  125. $newstr = "";
  126. //if($this->tabSpaces == false ) $currtab = "\t";
  127. //else $currtab = " ";
  128. $currtab = "\t";
  129. $incond = false;
  130. $smallso = 0;
  131. $smallsc = 0;
  132. $scs = 0;
  133. $sce = 0;
  134. foreach($tokens as $ind => $token){
  135. $name = @token_name($token[0]);
  136. $value = $token[1];
  137. if(!trim($name)){
  138. $value = $token[0];
  139. if($token[0] == "{") $name = "CURLY_START";
  140. if($token[0] == "}") $name = "CURLY_END";
  141. if($token[0] == "(") $name = "SMALL_START";
  142. if($token[0] == ")") $name = "SMALL_END";
  143. if($token[0] == "[") $name = "SQ_START";
  144. if($token[0] == "]") $name = "SQ_END";
  145. if($token[0] == ",") $name = "COMMA";
  146. if($token[0] == ";") $name = "COLON";
  147. if($token[0] == ":") $name = "COLON2";
  148. if($token[0] == "=") $name = "EQUAL";
  149. if($token[0] == ".") $name = "DOT";
  150. }
  151. if(!trim($name)){
  152. $name = "UNKNOWN";
  153. }
  154. $extra = 0;
  155. if($name == "T_WHITESPACE") {
  156. $nv = "";
  157. for($i=0 ; $i < strlen($value) ; $i++){
  158. $extra = ord($value[$i]);
  159. if($extra != 10 && $extra != 13 && $extra != 9) $nv .= $value[$i];
  160. }
  161. $value = $nv;
  162. if($value=="") continue;
  163. }
  164. if($name == "T_SWITCH"){
  165. $switch = true;
  166. }
  167. if($switch){
  168. if($name == "CURLY_START"){
  169. $scs++;
  170. }
  171. if($name == "CURLY_END"){
  172. $sce++;
  173. }
  174. }
  175. if($name == "CURLY_START") {
  176. $currtab .= "\t";
  177. }
  178. if($name == "CURLY_END" ) {
  179. $currtab = substr($currtab , 0 , -1);
  180. if($switch){
  181. if($scs >0 && $scs == $sce){
  182. $currtab = substr($currtab , 0 , -1);
  183. $switch = false;
  184. $lookforcol2 = false;
  185. $caseend = true;
  186. }
  187. }
  188. }
  189. if($name == "T_CASE" || $name == "T_DEFAULT"){
  190. $lookforcol2 = true;
  191. }
  192. if($name == "COLON2" && $lookforcol2){
  193. $currtab .= "\t";
  194. }
  195. if($caseend && $name == "COLON"){
  196. $currtab = substr($currtab , 0 , -1);
  197. $caseend = false;
  198. }
  199. if($name == "T_BREAK" && $lookforcol2){
  200. $caseend = true;
  201. $lookforcol2 = false;
  202. }else{
  203. $caseend = false;
  204. }
  205. if($lastone == "CURLY_END" && $name == "T_WHITESPACE") $value = str_replace(" " , "" , $value);
  206. if($name == "T_OPEN_TAG" && $value == "<?") $value = "<?php";
  207. if($incond && $name== "SMALL_START") $smallso++;
  208. if($incond && $name== "SMALL_END") $smallsc++;
  209. if($incond && ($smallso > 0) && $smallso == $smallsc) $incond =false;
  210. if(($lastone == "COLON" && $incond == false) || $lastone == "COLON2" || $lastone == "CURLY_START" || $lastone == "T_OPEN_TAG" || $name == "T_FUNCTION" || $name == "T_CLASS" || $lastone == "T_COMMENT") {
  211. if($name == "T_COMMENT") {
  212. if($lastone != "T_COMMENT") $value = "\n".$currtab.trim($value," ");
  213. else $value = $currtab.trim($value," ");
  214. }else {
  215. if($name == "T_FUNCTION") {
  216. if($lastone == "T_COMMENT") $value = $currtab.trim($value," ");
  217. else $value = "\n".$currtab.trim($value," ");
  218. }else{
  219. $value = "\n".$currtab.trim($value," ");
  220. }
  221. }
  222. }
  223. if($name == "T_IF") $value = "\n".$currtab.$value;
  224. if($name == "CURLY_START") {
  225. if($this->standardCurly == true) $value = "\n".substr($currtab,0,-1).$value;
  226. }
  227. if($name == "CURLY_END") {
  228. if(substr($newstr , -1) == chr(9)) {
  229. $newstr = substr($newstr , 0 , -1);
  230. }
  231. $value .= "\n\n";
  232. }
  233. if($lastone == "CURLY_END") $value = $currtab.$value;
  234. if($name == "T_FOR") {
  235. $incond = true;
  236. $smallso = 0;
  237. $smallsc = 0;
  238. }
  239. if($name == "T_ELSE") {
  240. $value = trim($value);
  241. $rnewstr = $newstr;
  242. for($i=0 ; $i < 1000 ; $i++){
  243. $pos = ((strlen($rnewstr)-1)-$i);
  244. $chr = ord($newstr[$pos]);
  245. if($chr != 9 && $chr != 10 && $chr != 13 && $chr != 32) {
  246. break;
  247. }else{
  248. $newstr = substr($newstr , 0 , -1);
  249. }
  250. }
  251. if($this->standardCurly == true) $value = "\n".$currtab.$value;
  252. else $value = " ".$value;
  253. }
  254. if($lastone == "T_ELSE" && $name != "T_WHITESPACE") $value = " ".$value;
  255. $newstr .= $value;
  256. //echo "$name -- $value -- $extra<hr>";
  257. $lastone = $name;
  258. }
  259. $tokens = token_get_all($newstr);
  260. $newstr = "";
  261. foreach($tokens as $ind => $token){
  262. $name = @token_name($token[0]);
  263. $value = $token[1];
  264. $line = $token[2];
  265. if($value == '') $value = $token[0];
  266. if($name == "T_WHITESPACE" && substr_count($value , chr(10))){
  267. $value = str_replace(" " , "" , $value);
  268. if($this->tabSpaces == true ) $value = str_replace(chr(9) , " " , $value);
  269. }
  270. $newstr .= $value;
  271. }
  272. $tokens = token_get_all($newstr);
  273. $newstr = "";
  274. foreach($tokens as $ind => $token){
  275. $name = @token_name($token[0]);
  276. $value = $token[1];
  277. $line = $token[2];
  278. if(trim($name) == '') $value = $token[0];
  279. if($name == "T_OPEN_TAG"){
  280. $lb = 0;
  281. $found = false;
  282. for($i=1 ; $i < 1000 ; $i++){
  283. $pret = $tokens[($ind+$i)];
  284. $pname = @token_name($pret[0]);
  285. $pvalue = $pret[1];
  286. if($pname == "T_WHITESPACE"){
  287. if(substr_count($pvalue , "\n")) $lb++;
  288. }
  289. if($lb > 2) break;
  290. if($pname == "T_CLOSE_TAG") {
  291. $found = true;
  292. break;
  293. }
  294. }
  295. if($found) $value .= " ";
  296. }
  297. if($name == "T_CLOSE_TAG" && $found) $value = " ".$value;
  298. if($name == "T_CLOSE_TAG") $found = false;
  299. if($found ){
  300. if($this->tabSpaces == false ) $value = str_replace(array("\n" , "\t") , "" , $value);
  301. else $value = str_replace(array("\n" , "\t" , " ") , "" , $value);
  302. $value = str_replace(array("\n" , "\t") , "" , $value);
  303. }
  304. $newstr .= $value;
  305. }
  306. return $newstr;
  307. }
  308. }
  309. ?>