-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding support of passing project-specific configuration to code sniffer #6
Open
RishiKulshreshtha
wants to merge
5
commits into
smmccabe:master
Choose a base branch
from
RishiKulshreshtha:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
73d67c2
Adding `$argv[1]` to the accept the config file.
7b3ecfd
Replacing `getopt` with `$argv` as cleaner approach.
ddb8670
Introducing `symfony/console` as better approach for options based co…
26c8b1e
Fixing undefined constant PHP_CODESNIFFER_CBF
5c57b2f
Fixing misc stuffs as per the MR suggestions.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
namespace PHPDebt\Console\Command; | ||
|
||
use PHP_CodeSniffer\Config; | ||
use PHP_CodeSniffer\Runner; | ||
use PHPDebt\PHPDebtMD; | ||
use SebastianBergmann\FinderFacade\FinderFacade; | ||
use SebastianBergmann\PHPLOC\Analyser; | ||
use Symfony\Component\Console\{ | ||
Command\Command, | ||
Input\InputArgument, | ||
Input\InputInterface, | ||
Input\InputOption, | ||
Output\OutputInterface | ||
}; | ||
|
||
/** | ||
* The class for all `phpdebt` command. | ||
* | ||
* @package PHPDebt\Console\Command | ||
*/ | ||
class PHPDebtCommand extends Command { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
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('This tool provides code quality score based on a number of standards from existing code analysis tools.'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) { | ||
$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) { | ||
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; | ||
|
||
$faults = $runner->run(); | ||
print "phpcs DrupalPractice: " . $faults . "\n"; | ||
$standards_faults += $faults; | ||
Comment on lines
+59
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two are identical. Also the print lines should reflect |
||
} | ||
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 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']; | ||
|
||
$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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace PHPDebt; | ||
|
||
use PHPMD\PHPMD; | ||
use PHPMD\RuleSetFactory; | ||
|
||
/** | ||
* The main facade of the PHPDebt's PHPMD application. | ||
* | ||
* @package PHPDebt | ||
*/ | ||
class PHPDebtMD { | ||
|
||
/** | ||
* This methods does the job of returning the rule violations as per PHPMD. | ||
* | ||
* @param string $path | ||
* The provided path that needs to be scanned. | ||
* @param string $type | ||
* The received ruleset. | ||
* | ||
* @return int|void | ||
*/ | ||
public function process(string $path, string $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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of having this if be running phpcs in two contexts, we should move the running until after and have the if just for determining if we need a default for
$option
.I mean to say: