Skip to content

Commit 44dd259

Browse files
authored
Merge pull request #892 from PHPCSStandards/feature/689-ruleset-hard-deprecate-sniffs-breaking-naming-conventions
Ruleset: hard deprecate support for sniffs not following the naming conventions
2 parents e207df2 + d23fa30 commit 44dd259

File tree

12 files changed

+263
-37
lines changed

12 files changed

+263
-37
lines changed

src/Ruleset.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,19 @@ public function populateTokenListeners()
14671467
$this->sniffs[$sniffClass] = new $sniffClass();
14681468

14691469
$sniffCode = Common::getSniffCode($sniffClass);
1470+
1471+
if (substr($sniffCode, 0, 1) === '.'
1472+
|| substr($sniffCode, -1) === '.'
1473+
|| strpos($sniffCode, '..') !== false
1474+
|| preg_match('`(^|\.)Sniffs\.`', $sniffCode) === 1
1475+
|| preg_match('`[^\s\.-]+\\\\Sniffs\\\\[^\s\.-]+\\\\[^\s\.-]+Sniff`', $sniffClass) !== 1
1476+
) {
1477+
$message = "The sniff $sniffClass does not comply with the PHP_CodeSniffer naming conventions.";
1478+
$message .= ' This will no longer be supported in PHPCS 4.0.'.PHP_EOL;
1479+
$message .= 'Contact the sniff author to fix the sniff.';
1480+
$this->msgCache->add($message, MessageCollector::DEPRECATED);
1481+
}
1482+
14701483
$this->sniffCodes[$sniffCode] = $sniffClass;
14711484

14721485
if ($this->sniffs[$sniffClass] instanceof DeprecatedSniff) {

tests/Core/Ruleset/ExpandSniffDirectoryTest.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,9 @@ public function testExpandSniffDirectory()
4949
$this->assertNotFalse($expectedPathToRuleset, 'Ruleset file could not be found');
5050
$this->assertContains($expectedPathToRuleset, $ruleset->paths, 'Ruleset file not included in the "seen ruleset paths"');
5151

52-
/*
53-
* Take note: the expectation includes some "undesirables" related to the convoluted directory structure
54-
* in the "standard" used as a test fixture.
55-
*
56-
* That is okay as (for now) non-standard directory layouts are supported.
57-
*
58-
* This test is not about the standard directory layout.
59-
*/
60-
6152
$expectedSniffCodes = [
62-
'.Sniffs.IncorrectLevelShouldStillBeFound' => 'MyStandard\\Sniffs\\IncorrectLevelShouldStillBeFoundSniff',
63-
'MyStandard.CategoryA.FindMe' => 'MyStandard\\Sniffs\\CategoryA\\FindMeSniff',
64-
'MyStandard.CategoryB.FindMe' => 'MyStandard\\Sniffs\\CategoryB\\FindMeSniff',
65-
'Sniffs.SubDir.IncorrectLevelShouldStillBeFound' => 'MyStandard\\Sniffs\\CategoryA\\SubDir\\IncorrectLevelShouldStillBeFoundSniff',
53+
'MyStandard.CategoryA.FindMe' => 'MyStandard\\Sniffs\\CategoryA\\FindMeSniff',
54+
'MyStandard.CategoryB.FindMe' => 'MyStandard\\Sniffs\\CategoryB\\FindMeSniff',
6655
];
6756

6857
// Sort the value to make the tests stable as different OSes will read directories
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\RulesetPopulateTokenListenersNamingConventionsTest
6+
*/
7+
8+
namespace BrokenNamingConventions\Sniffs\Category;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff as PHPCS_Sniff;
12+
13+
final class Sniff implements PHPCS_Sniff
14+
{
15+
public function register()
16+
{
17+
return [T_OPEN_TAG];
18+
}
19+
20+
public function process(File $phpcsFile, $stackPtr)
21+
{
22+
// Do something.
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\RulesetPopulateTokenListenersNamingConventionsTest
6+
*/
7+
8+
namespace BrokenNamingConventions\Sniffs\Category\SubDir;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
final class TooDeeplyNestedSniff implements Sniff
14+
{
15+
public function register()
16+
{
17+
return [T_OPEN_TAG];
18+
}
19+
20+
public function process(File $phpcsFile, $stackPtr)
21+
{
22+
// Do something.
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\RulesetPopulateTokenListenersNamingConventionsTest
6+
*/
7+
8+
namespace BrokenNamingConventions\Sniffs;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
final class MissingCategoryDirSniff implements Sniff
14+
{
15+
public function register()
16+
{
17+
return [T_OPEN_TAG];
18+
}
19+
20+
public function process(File $phpcsFile, $stackPtr)
21+
{
22+
// Do something.
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\PopulateTokenListenersNamingConventionsTest
6+
*/
7+
8+
use PHP_CodeSniffer\Files\File;
9+
use PHP_CodeSniffer\Sniffs\Sniff;
10+
11+
final class NoNamespaceSniff implements Sniff
12+
{
13+
public function register()
14+
{
15+
return [T_OPEN_TAG];
16+
}
17+
18+
public function process(File $phpcsFile, $stackPtr)
19+
{
20+
// Do something.
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\Ruleset\PopulateTokenListenersNamingConventionsTest
6+
*/
7+
8+
namespace Sniffs;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
final class PartialNamespaceSniff implements Sniff
14+
{
15+
public function register()
16+
{
17+
return [T_OPEN_TAG];
18+
}
19+
20+
public function process(File $phpcsFile, $stackPtr)
21+
{
22+
// Do something.
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Test fixture.
4+
*
5+
* @see \PHP_CodeSniffer\Tests\Core\RulesetPopulateTokenListenersNamingConventionsTest
6+
*/
7+
8+
namespace BrokenNamingConventions\Sniffs\Sniffs;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
final class CategoryCalledSniffsSniff implements Sniff
14+
{
15+
public function register()
16+
{
17+
return [T_OPEN_TAG];
18+
}
19+
20+
public function process(File $phpcsFile, $stackPtr)
21+
{
22+
// Do something.
23+
}
24+
}

tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/Subdir/IncorrectLevelShouldStillBeFoundSniff.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/IncorrectLevelShouldStillBeFoundSniff.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)