Skip to content

Commit c2636f5

Browse files
committed
Ruleset: remove support for sniffs not following the naming conventions
The [About Standards for PHP_CodeSniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/About-Standards-for-PHP_CodeSniffer) wiki page outlines exactly what the naming conventions are. This PR changes the previously added Ruleset deprecation notice to an error and starts rejecting sniffs which do not comply with the naming conventions. Includes updated tests. Closes 689 Related to 6
1 parent e07a290 commit c2636f5

File tree

2 files changed

+36
-51
lines changed

2 files changed

+36
-51
lines changed

src/Ruleset.php

+11-14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace PHP_CodeSniffer;
1313

14+
use InvalidArgumentException;
1415
use PHP_CodeSniffer\Exceptions\RuntimeException;
1516
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
1617
use PHP_CodeSniffer\Util\Common;
@@ -1461,23 +1462,19 @@ public function populateTokenListeners()
14611462
$this->tokenListeners = [];
14621463

14631464
foreach ($this->sniffs as $sniffClass => $sniffObject) {
1464-
$this->sniffs[$sniffClass] = null;
1465-
$this->sniffs[$sniffClass] = new $sniffClass();
1466-
1467-
$sniffCode = Common::getSniffCode($sniffClass);
1468-
1469-
if (substr($sniffCode, 0, 1) === '.'
1470-
|| substr($sniffCode, -1) === '.'
1471-
|| strpos($sniffCode, '..') !== false
1472-
|| preg_match('`(^|\.)Sniffs\.`', $sniffCode) === 1
1473-
|| preg_match('`[^\s\.-]+\\\\Sniffs\\\\[^\s\.-]+\\\\[^\s\.-]+Sniff`', $sniffClass) !== 1
1474-
) {
1475-
$message = "The sniff $sniffClass does not comply with the PHP_CodeSniffer naming conventions.";
1476-
$message .= ' This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
1465+
try {
1466+
$sniffCode = Common::getSniffCode($sniffClass);
1467+
} catch (InvalidArgumentException $e) {
1468+
$message = "The sniff $sniffClass does not comply with the PHP_CodeSniffer naming conventions.".PHP_EOL;
14771469
$message .= 'Contact the sniff author to fix the sniff.';
1478-
$this->msgCache->add($message, MessageCollector::DEPRECATED);
1470+
$this->msgCache->add($message, MessageCollector::ERROR);
1471+
1472+
// Unregister the sniff.
1473+
unset($this->sniffs[$sniffClass]);
1474+
continue;
14791475
}
14801476

1477+
$this->sniffs[$sniffClass] = new $sniffClass();
14811478
$this->sniffCodes[$sniffCode] = $sniffClass;
14821479

14831480
$isDeprecated = false;

tests/Core/Ruleset/PopulateTokenListenersNamingConventionsTest.php

+25-37
Original file line numberDiff line numberDiff line change
@@ -11,70 +11,58 @@
1111

1212
use PHP_CodeSniffer\Ruleset;
1313
use PHP_CodeSniffer\Tests\ConfigDouble;
14-
use PHPUnit\Framework\TestCase;
14+
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;
1515

1616
/**
1717
* Test handling of sniffs not following the PHPCS naming conventions in the Ruleset::populateTokenListeners() method.
1818
*
1919
* @covers \PHP_CodeSniffer\Ruleset::populateTokenListeners
2020
*/
21-
final class PopulateTokenListenersNamingConventionsTest extends TestCase
21+
final class PopulateTokenListenersNamingConventionsTest extends AbstractRulesetTestCase
2222
{
2323

2424

2525
/**
2626
* Verify a warning is shown for sniffs not complying with the PHPCS naming conventions.
2727
*
2828
* Including sniffs which do not comply with the PHPCS naming conventions is soft deprecated since
29-
* PHPCS 3.12.0, hard deprecated since PHPCS 3.13.0 and support will be removed in PHPCS 4.0.0.
29+
* PHPCS 3.12.0, hard deprecated since PHPCS 3.13.0 and support has been removed in PHPCS 4.0.0.
3030
*
3131
* @return void
3232
*/
3333
public function testBrokenNamingConventions()
3434
{
35+
$expectedMessage = 'ERROR: The sniff BrokenNamingConventions\\Sniffs\\MissingCategoryDirSniff does not comply';
36+
$expectedMessage .= ' with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
37+
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
38+
$expectedMessage .= 'ERROR: The sniff NoNamespaceSniff does not comply with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
39+
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
40+
$expectedMessage .= 'ERROR: The sniff Sniffs\PartialNamespaceSniff does not comply with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
41+
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
42+
$expectedMessage .= 'ERROR: The sniff BrokenNamingConventions\Sniffs\Category\Sniff does not comply';
43+
$expectedMessage .= ' with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
44+
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
45+
$expectedMessage .= 'ERROR: The sniff BrokenNamingConventions\Sniffs\Sniffs\CategoryCalledSniffsSniff does not';
46+
$expectedMessage .= ' comply with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
47+
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
48+
$expectedMessage .= 'ERROR: The sniff BrokenNamingConventions\Sniffs\Category\SubDir\TooDeeplyNestedSniff';
49+
$expectedMessage .= ' does not comply with the PHP_CodeSniffer naming conventions.'.PHP_EOL;
50+
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL.PHP_EOL;
51+
52+
$this->expectRuntimeExceptionMessage($expectedMessage);
53+
3554
// Set up the ruleset.
3655
$standard = __DIR__.'/PopulateTokenListenersNamingConventionsTest.xml';
3756
$config = new ConfigDouble(["--standard=$standard"]);
3857
$ruleset = new Ruleset($config);
3958

4059
// The "Generic.PHP.BacktickOperator" sniff is the only valid sniff.
4160
$expectedSniffCodes = [
42-
'..NoNamespace' => 'NoNamespaceSniff',
43-
'.Sniffs.MissingCategoryDir' => 'BrokenNamingConventions\\Sniffs\\MissingCategoryDirSniff',
44-
'.Sniffs.PartialNamespace' => 'Sniffs\\PartialNamespaceSniff',
45-
'BrokenNamingConventions.Category.' => 'BrokenNamingConventions\\Sniffs\\Category\\Sniff',
46-
'BrokenNamingConventions.Sniffs.CategoryCalledSniffs' => 'BrokenNamingConventions\\Sniffs\\Sniffs\\CategoryCalledSniffsSniff',
47-
'Generic.PHP.BacktickOperator' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\BacktickOperatorSniff',
48-
'Sniffs.SubDir.TooDeeplyNested' => 'BrokenNamingConventions\\Sniffs\\Category\\SubDir\\TooDeeplyNestedSniff',
61+
'Generic.PHP.BacktickOperator' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\PHP\\BacktickOperatorSniff',
4962
];
5063

51-
// Sort the value to make the tests stable as different OSes will read directories
52-
// in a different order and the order is not relevant for these tests. Just the values.
53-
$actual = $ruleset->sniffCodes;
54-
ksort($actual);
55-
56-
$this->assertSame($expectedSniffCodes, $actual, 'Registered sniffs do not match expectation');
57-
58-
$expectedMessage = 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\MissingCategoryDirSniff does not comply';
59-
$expectedMessage .= ' with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
60-
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
61-
$expectedMessage .= 'DEPRECATED: The sniff NoNamespaceSniff does not comply with the PHP_CodeSniffer naming conventions.';
62-
$expectedMessage .= ' This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
63-
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
64-
$expectedMessage .= 'DEPRECATED: The sniff Sniffs\\PartialNamespaceSniff does not comply with the PHP_CodeSniffer naming conventions.';
65-
$expectedMessage .= ' This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
66-
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
67-
$expectedMessage .= 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\Category\\Sniff does not comply';
68-
$expectedMessage .= ' with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
69-
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
70-
$expectedMessage .= 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\Sniffs\\CategoryCalledSniffsSniff does not';
71-
$expectedMessage .= ' comply with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
72-
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL;
73-
$expectedMessage .= 'DEPRECATED: The sniff BrokenNamingConventions\\Sniffs\\Category\\SubDir\\TooDeeplyNestedSniff';
74-
$expectedMessage .= ' does not comply with the PHP_CodeSniffer naming conventions. This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
75-
$expectedMessage .= 'Contact the sniff author to fix the sniff.'.PHP_EOL.PHP_EOL;
76-
77-
$this->expectOutputString($expectedMessage);
64+
// This assertion will only take effect for PHPUnit 10+.
65+
$this->assertSame($expectedSniffCodes, $ruleset->sniffCodes, 'Registered sniffs do not match expectation');
7866

7967
}//end testBrokenNamingConventions()
8068

0 commit comments

Comments
 (0)