Skip to content

Commit

Permalink
🔥 Fix broken sniff exclusions/restrictions in combination with custom…
Browse files Browse the repository at this point in the history
… standards in PHPCS 3.x

When running PHPCS with a custom standard, the command-line `--sniffs` and `--exclude` options no longer work.

This is caused by the sniffNames being prefixed with PHPCS's own `php_codesniffer\standards\` namespace which custom standard should not use in the first place.

This PR fixes this by only prefixing the sniffname with the PHPCS native namespace when it is a sniff from one of the build-in standards.
  • Loading branch information
jrfnl committed May 8, 2017
1 parent 979a5dd commit d187210
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ class Ruleset
*/
private $config = null;

/**
* PHPCS native standards.
*
* @var array<string, bool>
*/
private $nativeStandards = array(
'generic' => true,
'mysource' => true,
'pear' => true,
'psr1' => true,
'psr2' => true,
'squiz' => true,
'zend' => true,
);


/**
* Initialise the ruleset that the run will use.
Expand Down Expand Up @@ -186,14 +201,22 @@ public function __construct(Config $config)
$sniffRestrictions = array();
foreach ($restrictions as $sniffCode) {
$parts = explode('.', strtolower($sniffCode));
$sniffName = 'php_codesniffer\standards\\'.$parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
$sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
if (isset($this->nativeStandards[$parts[0]]) === true) {
$sniffName = 'php_codesniffer\standards\\'.$sniffName;
}

$sniffRestrictions[$sniffName] = true;
}

$sniffExclusions = array();
foreach ($exclusions as $sniffCode) {
$parts = explode('.', strtolower($sniffCode));
$sniffName = 'php_codesniffer\standards\\'.$parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
$sniffName = $parts[0].'\sniffs\\'.$parts[1].'\\'.$parts[2].'sniff';
if (isset($this->nativeStandards[$parts[0]]) === true) {
$sniffName = 'php_codesniffer\standards\\'.$sniffName;
}

$sniffExclusions[$sniffName] = true;
}

Expand Down

0 comments on commit d187210

Please sign in to comment.