11<?php
22
3+ /**
4+ * @file
5+ * PHP Technical Debt Calculator.
6+ */
7+
38require __DIR__ . '/../vendor/autoload.php ' ;
49require __DIR__ . '/../vendor/squizlabs/php_codesniffer/autoload.php ' ;
510
611use PHPMD \PHPMD ;
12+ use PHPMD \Report ;
713use PHPMD \RuleSetFactory ;
814
9- use SebastianBergmann \FinderFacade \FinderFacade ;
1015use SebastianBergmann \PHPLOC \Analyser ;
1116
1217use PHP_CodeSniffer \Runner ;
1318use PHP_CodeSniffer \Config ;
1419
20+ use Symfony \Component \Finder \Finder ;
21+
1522$ code_faults = 0 ;
1623$ standards_faults = 0 ;
1724$ path = $ argv [1 ];
1825
19- function phpMD ($ path , $ type ) {
20- $ ruleSetFactory = new RuleSetFactory ();
21- $ phpmd = new PHPMD ();
22-
23- $ report = $ phpmd ->runReport (
26+ /**
27+ * Mess Detector function.
28+ */
29+ function php_md ($ path , $ type ) {
30+ $ ruleSetFactory = new RuleSetFactory ();
31+ $ phpmd = new PHPMD ();
32+ $ report = new Report ();
33+ $ phpmd ->processFiles (
2434 $ path ,
2535 $ type ,
26- $ ruleSetFactory
36+ $ render = [],
37+ $ ruleSetFactory ,
38+ $ report ,
2739 );
2840
29- $ faults = count ($ report ->getRuleViolations ());
41+ $ faults = count ($ report ->getRuleViolations ());
3042
31- print "phpmd " . $ type . ": " . $ faults . "\n" ;
43+ print "phpmd " . $ type . ": " . $ faults . "\n" ;
3244
33- return count ($ report ->getRuleViolations ());
45+ return count ($ report ->getRuleViolations ());
3446}
3547
36- $ code_faults += phpMD ($ path , 'cleancode ' );
37- $ code_faults += phpMD ($ path , 'codesize ' );
38- $ code_faults += phpMD ($ path , 'design ' );
39- $ code_faults += phpMD ($ path , 'naming ' );
40- $ code_faults += phpMD ($ path , 'unusedcode ' );
48+ $ code_faults += php_md ($ path , 'cleancode ' );
49+ $ code_faults += php_md ($ path , 'codesize ' );
50+ $ code_faults += php_md ($ path , 'design ' );
51+ $ code_faults += php_md ($ path , 'naming ' );
52+ $ code_faults += php_md ($ path , 'unusedcode ' );
4153
4254
4355$ runner = new Runner ();
@@ -46,16 +58,16 @@ function phpMD($path, $type) {
4658$ runner ->config ->files = [$ path ];
4759$ runner ->config ->standards = ['Drupal ' ];
4860$ runner ->config ->extensions = [
49- 'php ' => 'PHP ' ,
50- 'module ' => 'PHP ' ,
51- 'inc ' => 'PHP ' ,
52- 'install ' => 'PHP ' ,
53- 'test ' => 'PHP ' ,
54- 'profile ' => 'PHP ' ,
55- 'theme ' => 'PHP ' ,
56- 'css ' => 'CSS ' ,
57- 'info ' => 'PHP ' ,
58- 'js ' => 'JS '
61+ 'php ' => 'PHP ' ,
62+ 'module ' => 'PHP ' ,
63+ 'inc ' => 'PHP ' ,
64+ 'install ' => 'PHP ' ,
65+ 'test ' => 'PHP ' ,
66+ 'profile ' => 'PHP ' ,
67+ 'theme ' => 'PHP ' ,
68+ 'css ' => 'CSS ' ,
69+ 'info ' => 'PHP ' ,
70+ 'js ' => 'JS ' ,
5971];
6072$ runner ->init ();
6173
@@ -69,15 +81,22 @@ function phpMD($path, $type) {
6981print "phpcs DrupalPractice: " . $ faults . "\n" ;
7082$ standards_faults += $ faults ;
7183
72- // Lines of Code
73- $ files = (new FinderFacade ([$ path ], [], ['*.php ' , '*.module ' , '*.install ' , '*.inc ' , '*.js ' , '*.scss ' ], []))->findFiles ();
74- $ count = (new Analyser )->countFiles ($ files , true );
84+ // Lines of code.
85+ $ finder = Finder::create ();
86+ $ finder ->files ()->in ($ path )->filter (static function (SplFileInfo $ file ) {
87+ return $ file ->isDir () || \preg_match ('/\.(php|module|install|inc|js|scss)$/ ' , $ file ->getPathname ());
88+ });
89+ $ files = [];
90+ foreach ($ finder as $ file ) {
91+ $ files [] = $ file ->getRealPath ();
92+ }
93+ $ count = (new Analyser )->countFiles ($ files , TRUE );
7594$ total_lines = $ count ['ncloc ' ];
7695
7796$ faults = $ code_faults + $ standards_faults ;
7897$ percent = ($ faults / $ total_lines ) * 100 ;
7998
80- // Summary
99+ // Summary.
81100print "Total Faults: " . $ faults . "\n" ;
82101print "Total Lines: " . $ total_lines . "\n" ;
83102print "Quality Score: " . number_format ($ percent , 2 ) . " faults per 100 lines \n" ;
@@ -86,21 +105,21 @@ function phpMD($path, $type) {
86105$ base = ($ total_lines / $ ratio ) * sqrt ($ percent );
87106
88107if ($ percent > 25 ) {
89- $ hours = ($ total_lines / $ ratio ) * sqrt ($ percent - ($ percent - 25 ));
90- print "Estimate to get to 25: " . number_format ($ base - $ hours , 2 ) . " hours \n" ;
108+ $ hours = ($ total_lines / $ ratio ) * sqrt ($ percent - ($ percent - 25 ));
109+ print "Estimate to get to 25: " . number_format ($ base - $ hours , 2 ) . " hours \n" ;
91110}
92111
93112if ($ percent > 10 ) {
94- $ hours = ($ total_lines / $ ratio ) * sqrt ($ percent - ($ percent - 10 ));
95- print "Estimate to get to 10: " . number_format ($ base - $ hours , 2 ) . " hours \n" ;
113+ $ hours = ($ total_lines / $ ratio ) * sqrt ($ percent - ($ percent - 10 ));
114+ print "Estimate to get to 10: " . number_format ($ base - $ hours , 2 ) . " hours \n" ;
96115}
97116
98117if ($ percent > 5 ) {
99- $ hours = ($ total_lines / $ ratio ) * sqrt ($ percent - ($ percent - 5 ));
100- print "Estimate to get to 5: " . number_format ($ base - $ hours , 2 ) . " hours \n" ;
118+ $ hours = ($ total_lines / $ ratio ) * sqrt ($ percent - ($ percent - 5 ));
119+ print "Estimate to get to 5: " . number_format ($ base - $ hours , 2 ) . " hours \n" ;
101120}
102121
103122if ($ percent > 3 ) {
104- $ hours = ($ total_lines / $ ratio ) * sqrt ($ percent - ($ percent - 3 ));
105- print "Estimate to get to 3: " . number_format ($ base - $ hours , 2 ) . " hours \n" ;
106- }
123+ $ hours = ($ total_lines / $ ratio ) * sqrt ($ percent - ($ percent - 3 ));
124+ print "Estimate to get to 3: " . number_format ($ base - $ hours , 2 ) . " hours \n" ;
125+ }
0 commit comments