Skip to content

Commit e07a290

Browse files
committed
Revert "Common::getSniffCode(): be more lenient about sniffs not following naming conventions"
This reverts commit 71b4524. Includes further enhancements to strictly apply the naming conventions by: 1. Making sure that the FQN contains a `Sniffs` or `Tests` sub-namespace in the correct position. 2. Making sure that the category is not called `Sniffs`. 3. Making sure that a class ends on `Sniff` or `UnitTest`, and that that is a suffix, not the sole name of the class. Includes updating the tests. Related to 689 Related to 6
1 parent d27a8c8 commit e07a290

File tree

3 files changed

+31
-63
lines changed

3 files changed

+31
-63
lines changed

src/Runner.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,6 @@ public function processFile($file)
643643
try {
644644
if (empty($nextStack) === false
645645
&& isset($nextStack['class']) === true
646-
&& substr($nextStack['class'], -5) === 'Sniff'
647646
) {
648647
$sniffCode = 'the '.Common::getSniffCode($nextStack['class']).' sniff';
649648
}

src/Util/Common.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ public static function suggestType($varType)
532532
* @return string
533533
*
534534
* @throws \InvalidArgumentException When $sniffClass is not a non-empty string.
535-
* @throws \InvalidArgumentException When $sniffClass is not a FQN for a sniff(test) class.
535+
* @throws \InvalidArgumentException When $sniffClass is not a valid FQN for a sniff(test) class.
536536
*/
537537
public static function getSniffCode($sniffClass)
538538
{
@@ -542,12 +542,22 @@ public static function getSniffCode($sniffClass)
542542

543543
$parts = explode('\\', $sniffClass);
544544
$partsCount = count($parts);
545-
$sniff = $parts[($partsCount - 1)];
545+
if ($partsCount < 4
546+
|| ($parts[($partsCount - 3)] !== 'Sniffs'
547+
&& $parts[($partsCount - 3)] !== 'Tests')
548+
|| $parts[($partsCount - 2)] === 'Sniffs'
549+
) {
550+
throw new InvalidArgumentException(
551+
'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received: '.$sniffClass
552+
);
553+
}
554+
555+
$sniff = $parts[($partsCount - 1)];
546556

547-
if (substr($sniff, -5) === 'Sniff') {
557+
if ($sniff !== 'Sniff' && substr($sniff, -5) === 'Sniff') {
548558
// Sniff class name.
549559
$sniff = substr($sniff, 0, -5);
550-
} else if (substr($sniff, -8) === 'UnitTest') {
560+
} else if ($sniff !== 'UnitTest' && substr($sniff, -8) === 'UnitTest') {
551561
// Unit test class name.
552562
$sniff = substr($sniff, 0, -8);
553563
} else {
@@ -556,16 +566,8 @@ public static function getSniffCode($sniffClass)
556566
);
557567
}
558568

559-
$standard = '';
560-
if (isset($parts[($partsCount - 4)]) === true) {
561-
$standard = $parts[($partsCount - 4)];
562-
}
563-
564-
$category = '';
565-
if (isset($parts[($partsCount - 2)]) === true) {
566-
$category = $parts[($partsCount - 2)];
567-
}
568-
569+
$standard = $parts[($partsCount - 4)];
570+
$category = $parts[($partsCount - 2)];
569571
return $standard.'.'.$category.'.'.$sniff;
570572

571573
}//end getSniffCode()

tests/Core/Util/Common/GetSniffCodeTest.php

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,16 @@ public function testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass(
105105
public static function dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
106106
{
107107
return [
108-
'Unqualified class name' => ['ClassName'],
109-
'Fully qualified class name, not enough parts' => ['Fully\\Qualified\\ClassName'],
108+
'Unqualified class name' => ['ClassNameSniff'],
109+
'Fully qualified sniff class name, not enough parts [1]' => ['Fully\\Qualified\\ClassNameSniff'],
110+
'Fully qualified sniff class name, not enough parts [2]' => ['CompanyName\\CheckMeSniff'],
111+
'Fully qualified test class name, not enough parts' => ['Fully\\Qualified\\ClassNameUnitTest'],
110112
'Fully qualified class name, doesn\'t end on Sniff or UnitTest' => ['Fully\\Sniffs\\Qualified\\ClassName'],
113+
'Fully qualified class name, ends on Sniff, but isn\'t' => ['Fully\\Sniffs\\AbstractSomethingSniff'],
114+
'Fully qualified class name, last part *is* Sniff' => ['CompanyName\\Sniffs\\Category\\Sniff'],
115+
'Fully qualified class name, last part *is* UnitTest' => ['CompanyName\\Tests\\Category\\UnitTest'],
116+
'Fully qualified class name, no Sniffs or Tests leaf' => ['CompanyName\\CustomSniffs\\Whatever\\CheckMeSniff'],
117+
'Fully qualified class name, category called Sniffs' => ['CompanyName\\Sniffs\\Sniffs\\InvalidCategorySniff'],
111118
];
112119

113120
}//end dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
@@ -140,70 +147,30 @@ public function testGetSniffCode($fqnClass, $expected)
140147
public static function dataGetSniffCode()
141148
{
142149
return [
143-
'PHPCS native sniff' => [
150+
'PHPCS native sniff' => [
144151
'fqnClass' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\Arrays\\ArrayIndentSniff',
145152
'expected' => 'Generic.Arrays.ArrayIndent',
146153
],
147-
'Class is a PHPCS native test class' => [
154+
'Class is a PHPCS native test class' => [
148155
'fqnClass' => 'PHP_CodeSniffer\\Standards\\Generic\\Tests\\Arrays\\ArrayIndentUnitTest',
149156
'expected' => 'Generic.Arrays.ArrayIndent',
150157
],
151-
'Sniff in external standard without namespace prefix' => [
158+
'Sniff in external standard without namespace prefix' => [
152159
'fqnClass' => 'MyStandard\\Sniffs\\PHP\\MyNameSniff',
153160
'expected' => 'MyStandard.PHP.MyName',
154161
],
155-
'Test in external standard without namespace prefix' => [
162+
'Test in external standard without namespace prefix' => [
156163
'fqnClass' => 'MyStandard\\Tests\\PHP\\MyNameUnitTest',
157164
'expected' => 'MyStandard.PHP.MyName',
158165
],
159-
'Sniff in external standard with namespace prefix' => [
166+
'Sniff in external standard with namespace prefix' => [
160167
'fqnClass' => 'Vendor\\Package\\MyStandard\\Sniffs\\Category\\AnalyzeMeSniff',
161168
'expected' => 'MyStandard.Category.AnalyzeMe',
162169
],
163-
'Test in external standard with namespace prefix' => [
170+
'Test in external standard with namespace prefix' => [
164171
'fqnClass' => 'Vendor\\Package\\MyStandard\\Tests\\Category\\AnalyzeMeUnitTest',
165172
'expected' => 'MyStandard.Category.AnalyzeMe',
166173
],
167-
168-
/*
169-
* These are not valid sniff codes and is an undesirable result, but can't be helped
170-
* as changing this would be a BC-break.
171-
* Supporting these to allow for <rule> tags directly including sniff files.
172-
* See: https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/675
173-
*/
174-
175-
'Fully qualified class name, ends on Sniff, but isn\'t' => [
176-
'fqnClass' => 'Fully\\Sniffs\\AbstractSomethingSniff',
177-
'expected' => '.Sniffs.AbstractSomething',
178-
],
179-
'Sniff provided via file include and doesn\'t comply with naming conventions [1]' => [
180-
'fqnClass' => 'CheckMeSniff',
181-
'expected' => '..CheckMe',
182-
],
183-
'Sniff provided via file include and doesn\'t comply with naming conventions [2]' => [
184-
'fqnClass' => 'CompanyName\\CheckMeSniff',
185-
'expected' => '.CompanyName.CheckMe',
186-
],
187-
'Sniff provided via file include and doesn\'t comply with naming conventions [3]' => [
188-
'fqnClass' => 'CompanyName\\Sniffs\\CheckMeSniff',
189-
'expected' => '.Sniffs.CheckMe',
190-
],
191-
'Sniff provided via file include and doesn\'t comply with naming conventions [4]' => [
192-
'fqnClass' => 'CompanyName\\CustomSniffs\\Whatever\\CheckMeSniff',
193-
'expected' => 'CompanyName.Whatever.CheckMe',
194-
],
195-
'Sniff provided via file include and doesn\'t comply with naming conventions [5]' => [
196-
'fqnClass' => 'CompanyName\\Sniffs\\Category\\Sniff',
197-
'expected' => 'CompanyName.Category.',
198-
],
199-
'Sniff provided via file include and doesn\'t comply with naming conventions [6]' => [
200-
'fqnClass' => 'CompanyName\\Tests\\Category\\UnitTest',
201-
'expected' => 'CompanyName.Category.',
202-
],
203-
'Sniff provided via file include and doesn\'t comply with naming conventions [7]' => [
204-
'fqnClass' => 'Sniffs\\Category\\NamedSniff',
205-
'expected' => '.Category.Named',
206-
],
207174
];
208175

209176
}//end dataGetSniffCode()

0 commit comments

Comments
 (0)