Advertisement
Guest User

Untitled

a guest
Jun 29th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. <?php
  2. /**
  3. * A very simple code analysis stored in a single php file.
  4. *
  5. * Edit The variables just below to customize to your needs.
  6. */
  7.  
  8. // Variables. Change these to meet your needs.
  9. $file_types = array('php','inc','js');
  10. $skip_directories = array('.git', 'files', 'external', 'scripts','.idea');
  11. $starting_directory = './';
  12.  
  13. // Initialize. No need to change anything below this line.
  14. $stats = array();
  15. $stats['gen'] = array();
  16. $stats['gen']['commented_lines'] = 0;
  17. $stats['gen']['blank_lines'] = 0;
  18. $stats['gen']['bracket_lines'] = 0;
  19. $stats['gen']['comment_blocks'] = 0;
  20. $stats['gen']['classes'] = 0;
  21. $stats['gen']['functions'] = 0;
  22. $stats['included_files'] = array();
  23. $stats['excluded_files'] = array();
  24. $stats['skip'] = $skip_directories;
  25. $stats['file_types'] = '(' . implode('|', $file_types) . ')';
  26.  
  27. // Execute.
  28. $totalLines = countLines($starting_directory, $stats);
  29.  
  30. // Print results.
  31. echo 'Total lines: ' . $totalLines . '<br />';
  32. echo 'Adjusted lines: ' . ($totalLines - $stats['gen']['commented_lines'] - $stats['gen']['blank_lines'] - $stats['gen']['bracket_lines']) . '<br />';
  33. foreach($stats['gen'] as $key=>$val)
  34. {
  35. echo ucfirst($key) . ": " . $val . "<br>";
  36. }
  37.  
  38. echo "<br />";
  39. echo 'Included Files (' . count($stats['included_files']) . '): <br />';
  40. foreach($stats['included_files'] as $file_name)
  41. {
  42. echo $file_name . '<br />';
  43. }
  44.  
  45. echo "<br />";
  46. echo 'Excluded Files & Directories (' . count($stats['excluded_files']) . '): <br />';
  47. foreach($stats['excluded_files'] as $file_name)
  48. {
  49. echo $file_name . '<br />';
  50. }
  51.  
  52. function countLines($dir, &$stats)
  53. {
  54. $lineCounter = 0;
  55. $dirHandle = opendir($dir);
  56. $path = realpath($dir);
  57. $nextLineIsComment = false;
  58.  
  59. if($dirHandle)
  60. {
  61. while($file = readdir($dirHandle))
  62. {
  63. if(is_dir($path."/".$file) && ($file !== '.' && $file !== '..') && !in_array($file, $stats['skip']))
  64. {
  65. $lineCounter += countLines($path . '/' . $file, $stats);
  66. }
  67. elseif(in_array($file, $stats['skip']))
  68. {
  69. $stats['excluded_files'][] = $path . '/' . $file;
  70. }
  71. elseif($file !== '.' && $file !== '..')
  72. {
  73.  
  74. // Check if we have a valid file
  75. $ext = _findExtension($file);
  76. if(preg_match("/" . $stats['file_types'] . "$/i", $ext))
  77. {
  78. $realFile = realpath($path)."/".$file;
  79. $fileArray = file($realFile);
  80.  
  81. // Check content of file:
  82. for($i=0; $i<count($fileArray); $i++)
  83. {
  84. if($nextLineIsComment)
  85. {
  86. $stats['gen']['commented_lines']++;
  87. // Look for the end of the comment block
  88. if(strpos($fileArray[$i], '*/'))
  89. {
  90. $nextLineIsComment = false;
  91. }
  92. }
  93. else
  94. {
  95.  
  96. // Look for a function
  97. if(strpos($fileArray[$i], 'function'))
  98. {
  99. $stats['gen']['functions']++;
  100. }
  101.  
  102. // Look for a commented line
  103. if(strpos(trim($fileArray[$i]), '//') === 0)
  104. {
  105. $stats['gen']['commented_lines']++;
  106. }
  107.  
  108. // Look for a class
  109. if(substr(trim($fileArray[$i]), 0, 5) == 'class')
  110. {
  111. $stats['gen']['classes']++;
  112. }
  113.  
  114. // Look for a comment block
  115. if(strpos($fileArray[$i], '/*'))
  116. {
  117. $nextLineIsComment = true;
  118. $stats['gen']['commented_lines']++;
  119. $stats['gen']['comment_blocks']++;
  120. }
  121.  
  122. //Look for a blank line
  123. if(trim($fileArray[$i]) == '')
  124. {
  125. $stats['gen']['blank_lines']++;
  126. }
  127.  
  128. // Look for lines that have an open or close bracket and nothing else.
  129. if (trim($fileArray[$i]) == '{' || trim($fileArray[$i]) == '}')
  130. {
  131. $stats['gen']['bracket_lines']++;
  132. }
  133. }
  134. }
  135. $lineCounter += count($fileArray);
  136.  
  137. // Mark as an included file.
  138. $stats['included_files'][] = $path . '/' . $file;
  139. }
  140. else
  141. {
  142. $stats['excluded_files'][] = $path . '/' . $file;
  143. }
  144. }
  145. }
  146. }
  147. else
  148. {
  149. echo 'Could not enter folder: ' . $dir;
  150. }
  151.  
  152. return $lineCounter;
  153. }
  154.  
  155. function _findExtension($filename)
  156. {
  157. $filename = strtolower($filename) ;
  158. $exts = preg_split("[/\\.]", $filename) ;
  159. $n = count($exts)-1;
  160. $exts = $exts[$n];
  161. return $exts;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement