Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace PHP_CodeSniffer;

use InvalidArgumentException;
use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
use PHP_CodeSniffer\Util\Common;
Expand Down Expand Up @@ -1461,23 +1462,19 @@ public function populateTokenListeners()
$this->tokenListeners = [];

foreach ($this->sniffs as $sniffClass => $sniffObject) {
$this->sniffs[$sniffClass] = null;
$this->sniffs[$sniffClass] = new $sniffClass();

$sniffCode = Common::getSniffCode($sniffClass);

if (substr($sniffCode, 0, 1) === '.'
|| substr($sniffCode, -1) === '.'
|| strpos($sniffCode, '..') !== false
|| preg_match('`(^|\.)Sniffs\.`', $sniffCode) === 1
|| preg_match('`[^\s\.-]+\\\\Sniffs\\\\[^\s\.-]+\\\\[^\s\.-]+Sniff`', $sniffClass) !== 1
) {
$message = "The sniff $sniffClass does not comply with the PHP_CodeSniffer naming conventions.";
$message .= ' This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
try {
$sniffCode = Common::getSniffCode($sniffClass);
} catch (InvalidArgumentException $e) {
$message = "The sniff $sniffClass does not comply with the PHP_CodeSniffer naming conventions.".PHP_EOL;
$message .= 'Contact the sniff author to fix the sniff.';
$this->msgCache->add($message, MessageCollector::DEPRECATED);
$this->msgCache->add($message, MessageCollector::ERROR);

// Unregister the sniff.
unset($this->sniffs[$sniffClass]);
continue;
}

$this->sniffs[$sniffClass] = new $sniffClass();
$this->sniffCodes[$sniffCode] = $sniffClass;

$isDeprecated = false;
Expand Down
62 changes: 25 additions & 37 deletions tests/Core/Ruleset/PopulateTokenListenersNamingConventionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,58 @@

use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Tests\ConfigDouble;
use PHPUnit\Framework\TestCase;
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;

/**
* Test handling of sniffs not following the PHPCS naming conventions in the Ruleset::populateTokenListeners() method.
*
* @covers \PHP_CodeSniffer\Ruleset::populateTokenListeners
*/
final class PopulateTokenListenersNamingConventionsTest extends TestCase
final class PopulateTokenListenersNamingConventionsTest extends AbstractRulesetTestCase
{


/**
* Verify a warning is shown for sniffs not complying with the PHPCS naming conventions.
*
* Including sniffs which do not comply with the PHPCS naming conventions is soft deprecated since
* PHPCS 3.12.0, hard deprecated since PHPCS 3.13.0 and support will be removed in PHPCS 4.0.0.
* PHPCS 3.12.0, hard deprecated since PHPCS 3.13.0 and support has been removed in PHPCS 4.0.0.
*
* @return void
*/
public function testBrokenNamingConventions()
{
$expectedMessage = 'ERROR: The sniff BrokenNamingConventions\\Sniffs\\MissingCategoryDirSniff does not comply';
$expectedMessage .= ' with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
$expectedMessage .= 'ERROR: The sniff NoNamespaceSniff does not comply with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
$expectedMessage .= 'ERROR: The sniff Sniffs\PartialNamespaceSniff does not comply with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
$expectedMessage .= 'ERROR: The sniff BrokenNamingConventions\Sniffs\Category\Sniff does not comply';
$expectedMessage .= ' with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
$expectedMessage .= 'ERROR: The sniff BrokenNamingConventions\Sniffs\Sniffs\CategoryCalledSniffsSniff does not';
$expectedMessage .= ' comply with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
$expectedMessage .= 'ERROR: The sniff BrokenNamingConventions\Sniffs\Category\SubDir\TooDeeplyNestedSniff';
$expectedMessage .= ' does not comply with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL.PHP_EOL;

$this->expectRuntimeExceptionMessage($expectedMessage);

// Set up the ruleset.
$standard = __DIR__.'/PopulateTokenListenersNamingConventionsTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);

// The "Generic.PHP.BacktickOperator" sniff is the only valid sniff.
$expectedSniffCodes = [
'..NoNamespace' => 'NoNamespaceSniff',
'.Sniffs.MissingCategoryDir' => 'BrokenNamingConventions\\Sniffs\\MissingCategoryDirSniff',
'.Sniffs.PartialNamespace' => 'Sniffs\\PartialNamespaceSniff',
'BrokenNamingConventions.Category.' => 'BrokenNamingConventions\\Sniffs\\Category\\Sniff',
'BrokenNamingConventions.Sniffs.CategoryCalledSniffs' => 'BrokenNamingConventions\\Sniffs\\Sniffs\\CategoryCalledSniffsSniff',
'Generic.PHP.BacktickOperator' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\BacktickOperatorSniff',
'Sniffs.SubDir.TooDeeplyNested' => 'BrokenNamingConventions\\Sniffs\\Category\\SubDir\\TooDeeplyNestedSniff',
'Generic.PHP.BacktickOperator' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\BacktickOperatorSniff',
];

// Sort the value to make the tests stable as different OSes will read directories
// in a different order and the order is not relevant for these tests. Just the values.
$actual = $ruleset->sniffCodes;
ksort($actual);

$this->assertSame($expectedSniffCodes, $actual, 'Registered sniffs do not match expectation');

$expectedMessage = 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\MissingCategoryDirSniff does not comply';
$expectedMessage .= ' with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
$expectedMessage .= 'DEPRECATED: The sniff NoNamespaceSniff does not comply with the PHP_CodeSniffer naming conventions.';
$expectedMessage .= ' This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
$expectedMessage .= 'DEPRECATED: The sniff Sniffs\\PartialNamespaceSniff does not comply with the PHP_CodeSniffer naming conventions.';
$expectedMessage .= ' This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
$expectedMessage .= 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\Category\\Sniff does not comply';
$expectedMessage .= ' with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
$expectedMessage .= 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\Sniffs\\CategoryCalledSniffsSniff does not';
$expectedMessage .= ' comply with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
$expectedMessage .= 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\Category\\SubDir\\TooDeeplyNestedSniff';
$expectedMessage .= ' does not comply with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL.PHP_EOL;

$this->expectOutputString($expectedMessage);
// This assertion will only take effect for PHPUnit 10+.
$this->assertSame($expectedSniffCodes, $ruleset->sniffCodes, 'Registered sniffs do not match expectation');

}//end testBrokenNamingConventions()

Expand Down