From 73d67c20f2d086f4edb18ada002132244fc9d8bb Mon Sep 17 00:00:00 2001 From: Rishi Kulshreshtha Date: Wed, 17 Feb 2021 21:10:27 +0530 Subject: [PATCH 1/5] Adding `$argv[1]` to the accept the config file. --- src/index.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/index.php b/src/index.php index 71c9a36..a330be2 100644 --- a/src/index.php +++ b/src/index.php @@ -57,6 +57,12 @@ function phpMD($path, $type) { 'info' => 'PHP', 'js' => 'JS' ]; +// Accepting an argument here, this can be used to accept the PHPCS config file +// that will override the above options. +// Example: phpdebt module/path --standard=web/core/phpcs.xml.dist +if (isset($argv[1])) { + $runner->config = new Config([$argv[1]]); +} $runner->init(); $faults = $runner->run(); @@ -103,4 +109,4 @@ function phpMD($path, $type) { if ($percent > 3) { $hours = ($total_lines / $ratio) * sqrt($percent - ($percent - 3)); print "Estimate to get to 3: " . number_format($base - $hours, 2) . " hours\n"; -} \ No newline at end of file +} From 7b3ecfd04ba73b6ca3f9de14b504d5accb106e61 Mon Sep 17 00:00:00 2001 From: Rishi Kulshreshtha Date: Thu, 18 Feb 2021 13:34:17 +0530 Subject: [PATCH 2/5] Replacing `getopt` with `$argv` as cleaner approach. --- src/index.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/index.php b/src/index.php index a330be2..6837328 100644 --- a/src/index.php +++ b/src/index.php @@ -12,6 +12,7 @@ use PHP_CodeSniffer\Runner; use PHP_CodeSniffer\Config; +$options = getopt(NULL, ['standard:']); $code_faults = 0; $standards_faults = 0; $path = $argv[1]; @@ -59,9 +60,9 @@ function phpMD($path, $type) { ]; // Accepting an argument here, this can be used to accept the PHPCS config file // that will override the above options. -// Example: phpdebt module/path --standard=web/core/phpcs.xml.dist -if (isset($argv[1])) { - $runner->config = new Config([$argv[1]]); +// Example: phpdebt module/path --standard=./phpcs.xml.dist +if (isset($options['standard'])) { + $runner->config = new Config($options); } $runner->init(); From ddb867052406d8a5539dad6460eef587a732c94d Mon Sep 17 00:00:00 2001 From: Rishi Kulshreshtha Date: Sat, 20 Feb 2021 00:34:55 +0530 Subject: [PATCH 3/5] Introducing `symfony/console` as better approach for options based command-line operations. --- composer.json | 6 +- .../Command/CodeSnifferConfigCommand.php | 131 ++++++++++++++++++ src/PHPDebt/PHPDebtMD.php | 42 ++++++ src/index.php | 116 ++-------------- 4 files changed, 186 insertions(+), 109 deletions(-) create mode 100644 src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php create mode 100644 src/PHPDebt/PHPDebtMD.php diff --git a/composer.json b/composer.json index f4583fe..8310e61 100644 --- a/composer.json +++ b/composer.json @@ -23,10 +23,14 @@ "squizlabs/php_codesniffer": "dev-api-friendly as 3.x-dev", "drupal/coder": "^8.3", "phpmd/phpmd": "dev-api-friendly", - "phploc/phploc": "^6.0" + "phploc/phploc": "^6.0", + "symfony/console": "^5.2" }, "require-dev": { "clue/phar-composer": "^1.1" }, + "autoload": { + "psr-0": { "": "src/" } + }, "bin": ["src/index.php"] } diff --git a/src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php b/src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php new file mode 100644 index 0000000..1cf1892 --- /dev/null +++ b/src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php @@ -0,0 +1,131 @@ +setName('phpdebt') + ->addArgument('path', InputArgument::REQUIRED, 'The path of the file or directory that needs to be scanned.') + ->addOption('standard', 's', InputOption::VALUE_REQUIRED, 'The path of the phpcs.xml.dist') + ->setDescription('Enables option of utilizing project based PHPCS Config.'); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output): int { + $code_faults = 0; + $standards_faults = 0; + $path = $input->getArgument('path'); + $option = $input->getOption('standard'); + + $debt_md = new PHPDebtMD(); + $code_faults += $debt_md->process($path, 'cleancode'); + $code_faults += $debt_md->process($path, 'codesize'); + $code_faults += $debt_md->process($path, 'design'); + $code_faults += $debt_md->process($path, 'naming'); + $code_faults += $debt_md->process($path, 'unusedcode'); + + $runner = new Runner(); + // If PHPCS config is provided, then execute this. + if ($option) { + $runner->config = new Config(["--standard=$option"]); + $runner->init(); + + $faults = $runner->run(); + print "phpcs Drupal: " . $faults . "\n"; + $standards_faults += $faults; + + $faults = $runner->run(); + print "phpcs DrupalPractice: " . $faults . "\n"; + $standards_faults += $faults; + } + else { + $runner->config = new Config(); + $runner->config->files = [$path]; + $runner->config->standards = ['Drupal']; + $runner->config->extensions = [ + 'php' => 'PHP', + 'module' => 'PHP', + 'inc' => 'PHP', + 'install' => 'PHP', + 'test' => 'PHP', + 'profile' => 'PHP', + 'theme' => 'PHP', + 'css' => 'CSS', + 'info' => 'PHP', + 'js' => 'JS', + ]; + $runner->init(); + $faults = $runner->run(); + print "phpcs Drupal: " . $faults . "\n"; + $standards_faults += $faults; + + $runner->config->standards = ['DrupalPractice']; + $runner->init(); + $faults = $runner->run(); + print "phpcs DrupalPractice: " . $faults . "\n"; + $standards_faults += $faults; + } + + // Lines of Code + $files = (new FinderFacade([$path], [], ['*.php', '*.module', '*.install', '*.inc', '*.js', '*.scss'], []))->findFiles(); + $count = (new Analyser)->countFiles($files, TRUE); + $total_lines = $count['ncloc']; + + $faults = $code_faults + $standards_faults; + $percent = ($faults / $total_lines) * 100; + + // Summary + print "Total Faults: " . $faults . "\n"; + print "Total Lines: " . $total_lines . "\n"; + print "Quality Score: " . number_format($percent, 2) . " faults per 100 lines\n"; + + $ratio = 200; + $base = ($total_lines / $ratio) * sqrt($percent); + + if ($percent > 25) { + $hours = ($total_lines / $ratio) * sqrt($percent - ($percent - 25)); + print "Estimate to get to 25: " . number_format($base - $hours, 2) . " hours\n"; + } + + if ($percent > 10) { + $hours = ($total_lines / $ratio) * sqrt($percent - ($percent - 10)); + print "Estimate to get to 10: " . number_format($base - $hours, 2) . " hours\n"; + } + + if ($percent > 5) { + $hours = ($total_lines / $ratio) * sqrt($percent - ($percent - 5)); + print "Estimate to get to 5: " . number_format($base - $hours, 2) . " hours\n"; + } + + if ($percent > 3) { + $hours = ($total_lines / $ratio) * sqrt($percent - ($percent - 3)); + print "Estimate to get to 3: " . number_format($base - $hours, 2) . " hours\n"; + } + return 0; + } + +} diff --git a/src/PHPDebt/PHPDebtMD.php b/src/PHPDebt/PHPDebtMD.php new file mode 100644 index 0000000..209d874 --- /dev/null +++ b/src/PHPDebt/PHPDebtMD.php @@ -0,0 +1,42 @@ +runReport( + $path, + $type, + $ruleSetFactory + ); + + $faults = count($report->getRuleViolations()); + + print "phpmd " . $type . ": " . $faults . "\n"; + + return count($report->getRuleViolations()); + } + +} diff --git a/src/index.php b/src/index.php index 6837328..d6e558f 100644 --- a/src/index.php +++ b/src/index.php @@ -3,111 +3,11 @@ require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/squizlabs/php_codesniffer/autoload.php'; -use PHPMD\PHPMD; -use PHPMD\RuleSetFactory; - -use SebastianBergmann\FinderFacade\FinderFacade; -use SebastianBergmann\PHPLOC\Analyser; - -use PHP_CodeSniffer\Runner; -use PHP_CodeSniffer\Config; - -$options = getopt(NULL, ['standard:']); -$code_faults = 0; -$standards_faults = 0; -$path = $argv[1]; - -function phpMD($path, $type) { - $ruleSetFactory = new RuleSetFactory(); - $phpmd = new PHPMD(); - - $report = $phpmd->runReport( - $path, - $type, - $ruleSetFactory - ); - - $faults = count($report->getRuleViolations()); - - print "phpmd " . $type . ": " . $faults . "\n"; - - return count($report->getRuleViolations()); -} - -$code_faults += phpMD($path, 'cleancode'); -$code_faults += phpMD($path, 'codesize'); -$code_faults += phpMD($path, 'design'); -$code_faults += phpMD($path, 'naming'); -$code_faults += phpMD($path, 'unusedcode'); - - -$runner = new Runner(); - -$runner->config = new Config(); -$runner->config->files = [$path]; -$runner->config->standards = ['Drupal']; -$runner->config->extensions = [ - 'php' => 'PHP', - 'module' => 'PHP', - 'inc' => 'PHP', - 'install' => 'PHP', - 'test' => 'PHP', - 'profile' => 'PHP', - 'theme' => 'PHP', - 'css' => 'CSS', - 'info' => 'PHP', - 'js' => 'JS' -]; -// Accepting an argument here, this can be used to accept the PHPCS config file -// that will override the above options. -// Example: phpdebt module/path --standard=./phpcs.xml.dist -if (isset($options['standard'])) { - $runner->config = new Config($options); -} -$runner->init(); - -$faults = $runner->run(); -print "phpcs Drupal: " . $faults . "\n"; -$standards_faults += $faults; - -$runner->config->standards = ['DrupalPractice']; -$runner->init(); -$faults = $runner->run(); -print "phpcs DrupalPractice: " . $faults . "\n"; -$standards_faults += $faults; - -// Lines of Code -$files = (new FinderFacade([$path], [], ['*.php', '*.module', '*.install', '*.inc', '*.js', '*.scss'], []))->findFiles(); -$count = (new Analyser)->countFiles($files, true); -$total_lines = $count['ncloc']; - -$faults = $code_faults + $standards_faults; -$percent = ($faults / $total_lines) * 100; - -// Summary -print "Total Faults: " . $faults . "\n"; -print "Total Lines: " . $total_lines . "\n"; -print "Quality Score: " . number_format($percent, 2) . " faults per 100 lines\n"; - -$ratio = 200; -$base = ($total_lines / $ratio) * sqrt($percent); - -if ($percent > 25) { - $hours = ($total_lines / $ratio) * sqrt($percent - ($percent - 25)); - print "Estimate to get to 25: " . number_format($base - $hours, 2) . " hours\n"; -} - -if ($percent > 10) { - $hours = ($total_lines / $ratio) * sqrt($percent - ($percent - 10)); - print "Estimate to get to 10: " . number_format($base - $hours, 2) . " hours\n"; -} - -if ($percent > 5) { - $hours = ($total_lines / $ratio) * sqrt($percent - ($percent - 5)); - print "Estimate to get to 5: " . number_format($base - $hours, 2) . " hours\n"; -} - -if ($percent > 3) { - $hours = ($total_lines / $ratio) * sqrt($percent - ($percent - 3)); - print "Estimate to get to 3: " . number_format($base - $hours, 2) . " hours\n"; -} +use PHPDebt\Console\Command\CodeSnifferConfigCommand; +use Symfony\Component\Console\Application; + +$command = new CodeSnifferConfigCommand(); +$app = new Application(); +$app->add($command); +$app->setDefaultCommand($command->getName(), true); +$app->run(); From 26c8b1e23e5268bfa7649c1ccc5e0b28c72b10fc Mon Sep 17 00:00:00 2001 From: Rishi Kulshreshtha Date: Mon, 22 Feb 2021 12:04:25 +0530 Subject: [PATCH 4/5] Fixing undefined constant PHP_CODESNIFFER_CBF --- src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php | 8 +++++--- src/index.php | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php b/src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php index 1cf1892..ae8a2b2 100644 --- a/src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php +++ b/src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php @@ -35,7 +35,7 @@ protected function configure() { /** * {@inheritdoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int { + protected function execute(InputInterface $input, OutputInterface $output) { $code_faults = 0; $standards_faults = 0; $path = $input->getArgument('path'); @@ -51,13 +51,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int $runner = new Runner(); // If PHPCS config is provided, then execute this. if ($option) { + define('PHP_CODESNIFFER_CBF', FALSE); $runner->config = new Config(["--standard=$option"]); - $runner->init(); + $runner->init(); $faults = $runner->run(); print "phpcs Drupal: " . $faults . "\n"; $standards_faults += $faults; + $runner->init(); $faults = $runner->run(); print "phpcs DrupalPractice: " . $faults . "\n"; $standards_faults += $faults; @@ -90,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $standards_faults += $faults; } - // Lines of Code + // Lines of Code should be working on and anything unique about getting his local back up to date? $files = (new FinderFacade([$path], [], ['*.php', '*.module', '*.install', '*.inc', '*.js', '*.scss'], []))->findFiles(); $count = (new Analyser)->countFiles($files, TRUE); $total_lines = $count['ncloc']; diff --git a/src/index.php b/src/index.php index d6e558f..7e17bb9 100644 --- a/src/index.php +++ b/src/index.php @@ -9,5 +9,5 @@ $command = new CodeSnifferConfigCommand(); $app = new Application(); $app->add($command); -$app->setDefaultCommand($command->getName(), true); +$app->setDefaultCommand($command->getName(), TRUE); $app->run(); From 5c57b2feeba065ba553436f2ce386cd1804eca82 Mon Sep 17 00:00:00 2001 From: Rishi Kulshreshtha Date: Mon, 22 Feb 2021 23:28:46 +0530 Subject: [PATCH 5/5] Fixing misc stuffs as per the MR suggestions. --- .../{CodeSnifferConfigCommand.php => PHPDebtCommand.php} | 6 +++--- src/index.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) rename src/PHPDebt/Console/Command/{CodeSnifferConfigCommand.php => PHPDebtCommand.php} (94%) diff --git a/src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php b/src/PHPDebt/Console/Command/PHPDebtCommand.php similarity index 94% rename from src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php rename to src/PHPDebt/Console/Command/PHPDebtCommand.php index ae8a2b2..b9cb37d 100644 --- a/src/PHPDebt/Console/Command/CodeSnifferConfigCommand.php +++ b/src/PHPDebt/Console/Command/PHPDebtCommand.php @@ -20,7 +20,7 @@ * * @package PHPDebt\Console\Command */ -class CodeSnifferConfigCommand extends Command { +class PHPDebtCommand extends Command { /** * {@inheritdoc} @@ -29,7 +29,7 @@ protected function configure() { $this->setName('phpdebt') ->addArgument('path', InputArgument::REQUIRED, 'The path of the file or directory that needs to be scanned.') ->addOption('standard', 's', InputOption::VALUE_REQUIRED, 'The path of the phpcs.xml.dist') - ->setDescription('Enables option of utilizing project based PHPCS Config.'); + ->setDescription('This tool provides code quality score based on a number of standards from existing code analysis tools.'); } /** @@ -53,13 +53,13 @@ protected function execute(InputInterface $input, OutputInterface $output) { if ($option) { define('PHP_CODESNIFFER_CBF', FALSE); $runner->config = new Config(["--standard=$option"]); + $runner->config->files = [$path]; $runner->init(); $faults = $runner->run(); print "phpcs Drupal: " . $faults . "\n"; $standards_faults += $faults; - $runner->init(); $faults = $runner->run(); print "phpcs DrupalPractice: " . $faults . "\n"; $standards_faults += $faults; diff --git a/src/index.php b/src/index.php index 7e17bb9..5e3005f 100644 --- a/src/index.php +++ b/src/index.php @@ -3,10 +3,10 @@ require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/squizlabs/php_codesniffer/autoload.php'; -use PHPDebt\Console\Command\CodeSnifferConfigCommand; +use PHPDebt\Console\Command\PHPDebtCommand; use Symfony\Component\Console\Application; -$command = new CodeSnifferConfigCommand(); +$command = new PHPDebtCommand(); $app = new Application(); $app->add($command); $app->setDefaultCommand($command->getName(), TRUE);