Skip to content

Commit 7d33bcd

Browse files
authored
Merge pull request #676 from PHPCSStandards/feature/675-common-sniffcode-be-more-lenient
Common::getSniffCode(): be more lenient about sniffs not following naming conventions
2 parents 26ddb35 + 71b4524 commit 7d33bcd

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

src/Util/Common.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -542,13 +542,7 @@ public static function getSniffCode($sniffClass)
542542

543543
$parts = explode('\\', $sniffClass);
544544
$partsCount = count($parts);
545-
if ($partsCount < 4) {
546-
throw new InvalidArgumentException(
547-
'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received: '.$sniffClass
548-
);
549-
}
550-
551-
$sniff = $parts[($partsCount - 1)];
545+
$sniff = $parts[($partsCount - 1)];
552546

553547
if (substr($sniff, -5) === 'Sniff') {
554548
// Sniff class name.
@@ -562,8 +556,16 @@ public static function getSniffCode($sniffClass)
562556
);
563557
}
564558

565-
$standard = $parts[($partsCount - 4)];
566-
$category = $parts[($partsCount - 2)];
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+
567569
return $standard.'.'.$category.'.'.$sniff;
568570

569571
}//end getSniffCode()

tests/Core/Util/Common/GetSniffCodeTest.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ public static function dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTes
108108
'Unqualified class name' => ['ClassName'],
109109
'Fully qualified class name, not enough parts' => ['Fully\\Qualified\\ClassName'],
110110
'Fully qualified class name, doesn\'t end on Sniff or UnitTest' => ['Fully\\Sniffs\\Qualified\\ClassName'],
111-
'Fully qualified class name, ends on Sniff, but isn\'t' => ['Fully\\Sniffs\\AbstractSomethingSniff'],
112111
];
113112

114113
}//end dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
@@ -141,30 +140,58 @@ public function testGetSniffCode($fqnClass, $expected)
141140
public static function dataGetSniffCode()
142141
{
143142
return [
144-
'PHPCS native sniff' => [
143+
'PHPCS native sniff' => [
145144
'fqnClass' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\Arrays\\ArrayIndentSniff',
146145
'expected' => 'Generic.Arrays.ArrayIndent',
147146
],
148-
'Class is a PHPCS native test class' => [
147+
'Class is a PHPCS native test class' => [
149148
'fqnClass' => 'PHP_CodeSniffer\\Standards\\Generic\\Tests\\Arrays\\ArrayIndentUnitTest',
150149
'expected' => 'Generic.Arrays.ArrayIndent',
151150
],
152-
'Sniff in external standard without namespace prefix' => [
151+
'Sniff in external standard without namespace prefix' => [
153152
'fqnClass' => 'MyStandard\\Sniffs\\PHP\\MyNameSniff',
154153
'expected' => 'MyStandard.PHP.MyName',
155154
],
156-
'Test in external standard without namespace prefix' => [
155+
'Test in external standard without namespace prefix' => [
157156
'fqnClass' => 'MyStandard\\Tests\\PHP\\MyNameSniff',
158157
'expected' => 'MyStandard.PHP.MyName',
159158
],
160-
'Sniff in external standard with namespace prefix' => [
159+
'Sniff in external standard with namespace prefix' => [
161160
'fqnClass' => 'Vendor\\Package\\MyStandard\\Sniffs\\Category\\AnalyzeMeSniff',
162161
'expected' => 'MyStandard.Category.AnalyzeMe',
163162
],
164-
'Test in external standard with namespace prefix' => [
163+
'Test in external standard with namespace prefix' => [
165164
'fqnClass' => 'Vendor\\Package\\MyStandard\\Tests\\Category\\AnalyzeMeUnitTest',
166165
'expected' => 'MyStandard.Category.AnalyzeMe',
167166
],
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+
],
168195
];
169196

170197
}//end dataGetSniffCode()

0 commit comments

Comments
 (0)