Skip to content

Commit

Permalink
Fixed bug #1451 : Sniff exclusions/restrictions dont work with custom…
Browse files Browse the repository at this point in the history
… sniffs unless they use the PHP_CodeSniffer NS
  • Loading branch information
gsherwood committed May 9, 2017
1 parent d6f3c75 commit a7d530c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #1450 : Coding standard located under an installed_path with the same directory name throws an error
-- Thanks to Juliette Reinders Folmer for the patch
- Fixed bug #1451 : Sniff exclusions/restrictions dont work with custom sniffs unless they use the PHP_CodeSniffer NS
- Fixed bug #1454 : Squiz.WhiteSpace.OperatorSpacing is not checking spacing on either side of a short ternary operator
-- Thanks to Mponos George for the patch
</notes>
Expand Down
11 changes: 6 additions & 5 deletions src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ 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';
$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';
$sniffExclusions[$sniffName] = true;
}

Expand Down Expand Up @@ -1059,20 +1059,21 @@ public function registerSniffs($files, $restrictions, $exclusions)
continue;
}

$className = Autoload::loadFile($file);
$className = Autoload::loadFile($file);
$compareName = Util\Common::cleanSniffClass($className);

// If they have specified a list of sniffs to restrict to, check
// to see if this sniff is allowed.
if (empty($restrictions) === false
&& isset($restrictions[strtolower($className)]) === false
&& isset($restrictions[$compareName]) === false
) {
continue;
}

// If they have specified a list of sniffs to exclude, check
// to see if this sniff is allowed.
if (empty($exclusions) === false
&& isset($exclusions[strtolower($className)]) === true
&& isset($exclusions[$compareName]) === true
) {
continue;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Util/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,22 @@ public static function getSniffCode($sniffClass)
}//end getSniffCode()


/**
* Removes project-specific information from a sniff class name.
*
* @param string $sniffClass The fully qualified sniff class name.
*
* @return string
*/
public static function cleanSniffClass($sniffClass)
{
$newName = strtolower($sniffClass);
$end = (strlen($newName) - strrpos($newName, '\sniffs\\') + 1);
$start = strrpos($newName, '\\', ($end * -1));
$newName = substr($newName, ($start + 1));
return $newName;

}//end cleanSniffClass()


}//end class
1 change: 1 addition & 0 deletions tests/Standards/AbstractSniffUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ final public function testSniff()

$sniffClassName = substr(get_class($this), 0, -8).'Sniff';
$sniffClassName = str_replace('\Tests\\', '\Sniffs\\', $sniffClassName);
$sniffClassName = Common::cleanSniffClass($sniffClassName);

$restrictions = array(strtolower($sniffClassName) => true);
$ruleset->registerSniffs(array($sniffFile), $restrictions, array());
Expand Down

0 comments on commit a7d530c

Please sign in to comment.