apstats.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file dev/tools/apstats.php
  21. * \brief Script to report Advanced Statistics on a coding project
  22. */
  23. $sapi_type = php_sapi_name();
  24. $script_file = basename(__FILE__);
  25. $path = dirname(__FILE__) . '/';
  26. // Test si mode batch
  27. $sapi_type = php_sapi_name();
  28. if (substr($sapi_type, 0, 3) == 'cgi') {
  29. echo "Error: You are using PHP for CGI. To execute " . $script_file . " from command line, you must use PHP for CLI mode.\n";
  30. exit();
  31. }
  32. error_reporting(E_ALL & ~ E_DEPRECATED);
  33. define('PRODUCT', "apstats");
  34. define('VERSION', "1.0");
  35. print '***** '.constant('PRODUCT').' - '.constant('VERSION').' *****'."\n";
  36. if (empty($argv[1])) {
  37. print 'You must run this tool being into the root of the project.'."\n";
  38. print 'Usage: '.constant('PRODUCT').'.php pathto/index.html [--dir-scc=pathtoscc] [--dir-phpstan=pathtophpstan]'."\n";
  39. print 'Example: '.constant('PRODUCT').'.php dev/tools/apstats.php documents/apstats/index.html --dir-phpstan=~/git/phpstan/htdocs/includes/bin';
  40. exit(0);
  41. }
  42. $outputpath = $argv[1];
  43. $outputdir = dirname($outputpath);
  44. $outputfile = basename($outputpath);
  45. if (! is_dir($outputdir)) {
  46. print 'Error: dir '.$outputdir.' does not exists or is not writable'."\n";
  47. exit(1);
  48. }
  49. $dirscc = '';
  50. $dirphpstan = '';
  51. $i = 0;
  52. while ($i < $argc) {
  53. $reg = array();
  54. if (preg_match('/--dir-scc=(.*)$/', $argv[$i], $reg)) {
  55. $dirphpstan = $reg[1];
  56. }
  57. if (preg_match('/--dir-phpstan=(.*)$/', $argv[$i], $reg)) {
  58. $dirphpstan = $reg[1];
  59. }
  60. $i++;
  61. }
  62. // Count lines of code of Dolibarr itself
  63. /*
  64. $commandcheck = 'cloc . --exclude-dir=includes --exclude-dir=custom --ignore-whitespace --vcs=git';
  65. $resexec = shell_exec($commandcheck);
  66. $resexec = (int) (empty($resexec) ? 0 : trim($resexec));
  67. // Count lines of code of external dependencies
  68. $commandcheck = 'cloc htdocs/includes --ignore-whitespace --vcs=git';
  69. $resexec = shell_exec($commandcheck);
  70. $resexec = (int) (empty($resexec) ? 0 : trim($resexec));
  71. */
  72. // Count lines of code of application
  73. $commandcheck = ($dirscc ? $dirscc.'/' : '').'scc . --exclude-dir=includes,custom';
  74. print 'Execute SCC to count lines of code in project: '.$commandcheck."\n";
  75. $output_arrproj = array();
  76. $resexecproj = 0;
  77. exec($commandcheck, $output_arrproj, $resexecproj);
  78. // Count lines of code of dependencies
  79. $commandcheck = ($dirscc ? $dirscc.'/' : '').'scc htdocs/includes';
  80. print 'Execute SCC to count lines of code in dependencies: '.$commandcheck."\n";
  81. $output_arrdep = array();
  82. $resexecdep = 0;
  83. exec($commandcheck, $output_arrdep, $resexecdep);
  84. // Get technical debt
  85. $commandcheck = ($dirphpstan ? $dirphpstan.'/' : '').'phpstan -v analyze -a build/phpstan/bootstrap.php --memory-limit 5G --error-format=github';
  86. print 'Execute PHPStan to get the technical debt: '.$commandcheck."\n";
  87. $output_arrtd = array();
  88. $resexectd = 0;
  89. exec($commandcheck, $output_arrtd, $resexectd);
  90. $arrayoflineofcode = array();
  91. $arraycocomo = array();
  92. $arrayofmetrics = array(
  93. 'proj'=>array('Bytes'=>0, 'Files'=>0, 'Lines'=>0, 'Blanks'=>0, 'Comments'=>0, 'Code'=>0, 'Complexity'=>0),
  94. 'dep'=>array('Bytes'=>0, 'Files'=>0, 'Lines'=>0, 'Blanks'=>0, 'Comments'=>0, 'Code'=>0, 'Complexity'=>0)
  95. );
  96. // Analyse $output_arrproj
  97. foreach (array('proj', 'dep') as $source) {
  98. print 'Analyze SCC result for lines of code for '.$source."\n";
  99. if ($source == 'proj') {
  100. $output_arr = &$output_arrproj;
  101. } elseif ($source == 'dep') {
  102. $output_arr = &$output_arrdep;
  103. } else {
  104. print 'Bad value for $source';
  105. die();
  106. }
  107. foreach ($output_arr as $line) {
  108. if (preg_match('/^(───|Language|Total)/', $line)) {
  109. continue;
  110. }
  111. //print $line."<br>\n";
  112. if (preg_match('/^Estimated Cost.*\$(.*)/i', $line, $reg)) {
  113. $arraycocomo[$source]['currency'] = preg_replace('/[^\d\.]/', '', str_replace(array(',', ' '), array('', ''), $reg[1]));
  114. }
  115. if (preg_match('/^Estimated Schedule Effort.*\s([\d\s,]+)/i', $line, $reg)) {
  116. $arraycocomo[$source]['effort'] = str_replace(array(',', ' '), array('.', ''), $reg[1]);
  117. }
  118. if (preg_match('/^Estimated People.*\s([\d\s,]+)/i', $line, $reg)) {
  119. $arraycocomo[$source]['people'] = str_replace(array(',', ' '), array('.', ''), $reg[1]);
  120. }
  121. if (preg_match('/^Processed\s(\d+)\s/i', $line, $reg)) {
  122. $arrayofmetrics[$source]['Bytes'] = $reg[1];
  123. }
  124. if (preg_match('/^(.*)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/', $line, $reg)) {
  125. $arrayoflineofcode[$source][$reg[1]]['Files'] = $reg[2];
  126. $arrayoflineofcode[$source][$reg[1]]['Lines'] = $reg[3];
  127. $arrayoflineofcode[$source][$reg[1]]['Blanks'] = $reg[4];
  128. $arrayoflineofcode[$source][$reg[1]]['Comments'] = $reg[5];
  129. $arrayoflineofcode[$source][$reg[1]]['Code'] = $reg[6];
  130. $arrayoflineofcode[$source][$reg[1]]['Complexity'] = $reg[7];
  131. }
  132. }
  133. foreach ($arrayoflineofcode[$source] as $key => $val) {
  134. $arrayofmetrics[$source]['Files'] += $val['Files'];
  135. $arrayofmetrics[$source]['Lines'] += $val['Lines'];
  136. $arrayofmetrics[$source]['Blanks'] += $val['Blanks'];
  137. $arrayofmetrics[$source]['Comments'] += $val['Comments'];
  138. $arrayofmetrics[$source]['Code'] += $val['Code'];
  139. $arrayofmetrics[$source]['Complexity'] += $val['Complexity'];
  140. }
  141. }
  142. /*
  143. * View
  144. */
  145. $html = '<html>';
  146. $html .= '
  147. <style>
  148. body {
  149. margin: 10px;
  150. margin-left: 50px;
  151. margin-right: 50px;
  152. }
  153. h1 {
  154. font-size: 1.5em;
  155. font-weight: bold;
  156. padding-top: 5px;
  157. padding-bottom: 5px;
  158. margin-top: 5px;
  159. margin-bottom: 5px;
  160. }
  161. header {
  162. text-align: center;
  163. }
  164. header, section.chapter {
  165. margin-top: 10px;
  166. margin-bottom: 10px;
  167. padding: 10px;
  168. }
  169. table {
  170. border-collapse: collapse;
  171. }
  172. th,td {
  173. padding-top: 5px;
  174. padding-bottom: 5px;
  175. padding-left: 10px;
  176. padding-right: 10px;
  177. }
  178. .left {
  179. text-align: left;
  180. }
  181. .right {
  182. text-align: right;
  183. }
  184. .opacity {
  185. opacity: 0.5;
  186. }
  187. .centpercent {
  188. width: 100%;
  189. }
  190. .trgroup {
  191. background-color: #EEE;
  192. }
  193. </style>';
  194. $html .= '<body>';
  195. $html .= '<header>';
  196. $html .= '<h1>Advanced Project Statistics</h1>';
  197. $currentDate = date("Y-m-d H:i:s"); // Format: Year-Month-Day Hour:Minute:Second
  198. $html .= '<span class="opacity">Generated on '.$currentDate.'</span>';
  199. $html .= '</header>';
  200. $html .= '<section class="chapter">';
  201. $html .= '<h2>Lines of code</h2>';
  202. $html .= '<table class="centpercent">';
  203. $html .= '<tr class="loc">';
  204. $html .= '<th class="left">Language</td>';
  205. $html .= '<th class="right">Bytes</th>';
  206. $html .= '<th class="right">Files</th>';
  207. $html .= '<th class="right">Lines</th>';
  208. $html .= '<th class="right">Blanks</th>';
  209. $html .= '<th class="right">Comments</th>';
  210. $html .= '<th class="right">Code</th>';
  211. //$html .= '<td class="right">'.$val['Complexity'].'</td>';
  212. $html .= '</th>';
  213. foreach (array('proj', 'dep') as $source) {
  214. $html .= '<tr class="trgroup" id="source'.$source.'">';
  215. if ($source == 'proj') {
  216. $html .= '<td>All files from project only</td>';
  217. } elseif ($source == 'dep') {
  218. $html .= '<td>All files from dependencies</td>';
  219. }
  220. $html .= '<td class="right">'.$arrayofmetrics[$source]['Bytes'].'</td>';
  221. $html .= '<td class="right">'.$arrayofmetrics[$source]['Files'].'</td>';
  222. $html .= '<td class="right">'.$arrayofmetrics[$source]['Lines'].'</td>';
  223. $html .= '<td class="right">'.$arrayofmetrics[$source]['Blanks'].'</td>';
  224. $html .= '<td class="right">'.$arrayofmetrics[$source]['Comments'].'</td>';
  225. $html .= '<td class="right">'.$arrayofmetrics[$source]['Code'].'</td>';
  226. $html .= '<td>See detail per file type...</td>';
  227. $html .= '</tr>';
  228. foreach ($arrayoflineofcode[$source] as $key => $val) {
  229. $html .= '<tr class="loc source'.$source.' language'.str_replace(' ', '', $key).'">';
  230. $html .= '<td>'.$key.'</td>';
  231. $html .= '<td class="right"></td>';
  232. $html .= '<td class="right">'.(empty($val['Files']) ? '' : $val['Files']).'</td>';
  233. $html .= '<td class="right">'.(empty($val['Lines']) ? '' : $val['Lines']).'</td>';
  234. $html .= '<td class="right">'.(empty($val['Blanks']) ? '' : $val['Blanks']).'</td>';
  235. $html .= '<td class="right">'.(empty($val['Comments']) ? '' : $val['Comments']).'</td>';
  236. $html .= '<td class="right">'.(empty($val['Code']) ? '' : $val['Code']).'</td>';
  237. //$html .= '<td class="right">'.(empty($val['Complexity']) ? '' : $val['Complexity']).'</td>';
  238. $html .= '<td>graph here...</td>';
  239. $html .= '</tr>';
  240. }
  241. }
  242. $html .= '<tr class="trgroup">';
  243. $html .= '<td class="left">Total</td>';
  244. $html .= '<td class="right">'.($arrayofmetrics['proj']['Bytes'] + $arrayofmetrics['dep']['Bytes']).'</td>';
  245. $html .= '<td class="right">'.($arrayofmetrics['proj']['Files'] + $arrayofmetrics['dep']['Files']).'</td>';
  246. $html .= '<td class="right">'.($arrayofmetrics['proj']['Lines'] + $arrayofmetrics['dep']['Lines']).'</td>';
  247. $html .= '<td class="right">'.($arrayofmetrics['proj']['Blanks'] + $arrayofmetrics['dep']['Blanks']).'</td>';
  248. $html .= '<td class="right">'.($arrayofmetrics['proj']['Comments'] + $arrayofmetrics['dep']['Comments']).'</td>';
  249. $html .= '<td class="right">'.($arrayofmetrics['proj']['Code'] + $arrayofmetrics['dep']['Code']).'</td>';
  250. //$html .= '<td>'.$arrayofmetrics['Complexity'].'</td>';
  251. $html .= '<td></td>';
  252. $html .= '</tr>';
  253. $html .= '</table>';
  254. $html .= '</section>';
  255. $html .= '<section class="chapter">';
  256. $html .= '<h2>Project value:</h2><br>';
  257. $html .= 'CODOMO (Basic organic model) value: $'.($arraycocomo['proj']['currency'] + $arraycocomo['dep']['currency']).'<br>';
  258. $html .= 'CODOMO (Basic organic model) effort: '.($arraycocomo['proj']['people'] * $arraycocomo['proj']['effort'] + $arraycocomo['dep']['people'] * $arraycocomo['dep']['effort']).' Month people<br>';
  259. $html .= '</section>';
  260. $html .= '<section class="chapter">';
  261. $html .= '<h2>Technical debt: ('.count($output_arrtd).')</h2><br>';
  262. $html .= join('<br>'."\n", $output_arrtd);
  263. $html .= '</section>';
  264. $html .= '</boby>';
  265. $html .= '</html>';
  266. $fh = fopen($outputpath, 'w');
  267. if ($fh) {
  268. fwrite($fh, $html);
  269. fclose($fh);
  270. print 'Generation of output file '.$outputfile.' done.'."\n";
  271. } else {
  272. print 'Failed to open '.$outputfile.' for ouput.'."\n";
  273. }