Skip to content
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
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

RishiKulshreshtha
Copy link

Now phpdebt can be allowed to use the PHPCS config via:

phpdebt <path-of-dir/file-to-be-scanned> --standard=phpcs.xml.dist

$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.');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description is for the command, not the option.

$ php src/index.php --help
Description:
  Enables option of utilizing project based PHPCS Config.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, I totally missed that. It's fixed now.

*
* @package PHPDebt\Console\Command
*/
class CodeSnifferConfigCommand extends Command {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description says it's the whole phpdebt command, but the class is named like it's for configuring code sniffer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching it, I've written it initially as was thinking that I would be dealing with here code related only to the PHPCS, later my scope changed, but forgot to rename the class. This is fixed now.

@travis-bradbury
Copy link

travis-bradbury commented Feb 22, 2021

Is there a solution for scanning multiple paths, or does that still need to be discussed?

If you use --standard, the paths that phpcs scans are defined in that file, so you could have it scan web/modules/custom and web/themes/custom. The path, which is still required, would just be used for phpmd.

Example 1, the standard configures phpcs to scan two places:

# Scans two places with phpmd, but runs phpcs a second time for nothing.
$ phpdebt web/modules/custom --standard=phpcs.xml
$ phpdebt web/themes/custom --standard=phpcs.xml

Example 2, use two standards, one that just includes modules and one that just includes the theme:

# Requires invoking phpdebt twice, same as it does currently, but you need two standards, each matching the path we give to phpdebt.
$ phpdebt web/modules/custom --standard=phpcs-modules.xml
$ phpdebt web/themes/custom --standard=phpcs-theme.xml

I looked at a project that uses a custom phpcs configuration, and it looks like there's a potential additional problem. It sounds like phpcs only uses the paths specified in the standard if there isn't a path given on the command line. Perhaps it'd be more intuitive if the path given to phpdebt would also override the paths in the standard. The CI scripts we'd have to write would be like Example 1, except that the paths scanned would be the path we see in the command line.

@RishiKulshreshtha
Copy link
Author

@travis-bradbury, I've updated the code and it'll be using now the format of Example 1. I also found it more logical, thanks for the explanation with examples.

phpdebt <the-path> --standard=phpcs.xml.dist, now if this command is passed, then <the-path> will be used for phpcs just like phpmd & the rest configuration will be consumed by phpcs.xml.dist.

@travis-bradbury
Copy link

@RishiKulshreshtha cool, looks good to me now.

@smmccabe
Copy link
Owner

smmccabe commented May 3, 2021

@RishiKulshreshtha can you give me a quick summary of this PR? It looks like it switches the setup to use symfony console as the base and then moves some of the phpdebt commands into that?

@RishiKulshreshtha
Copy link
Author

can you give me a quick summary of this PR?

@smmccabe, as added above:
Using this, phpdebt can be allowed to use the PHPCS config via: phpdebt <path-of-dir/file-to-be-scanned> --standard=phpcs.xml.dist

It looks like it switches the setup to use symfony console as the base and then moves some of the phpdebt commands into that?

Yes, that's correct, but the actual reason to implement this is to allow the user/dev to use their existing/custom phpcs.xml.dist while calling the phpdebt. If provided then the values provided at the phpcs.xml.dist will be prioritized over the defaults of the phpdebt config.

Copy link

@DerekCresswell DerekCresswell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just need to fix up the actual phpcs running code to avoid repetition. Otherwise these changes seem good to me.

Comment on lines +59 to +65
$faults = $runner->run();
print "phpcs Drupal: " . $faults . "\n";
$standards_faults += $faults;

$faults = $runner->run();
print "phpcs DrupalPractice: " . $faults . "\n";
$standards_faults += $faults;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two are identical. Also the print lines should reflect $options instead of always being drupal.

Comment on lines +53 to +55
if ($option) {
define('PHP_CODESNIFFER_CBF', FALSE);
$runner->config = new Config(["--standard=$option"]);

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:

if (!$option) {
  $option = 'sane default';
}
$runner->config = new Config(["--standard=$option"]);
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants