Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common::getSniffCode(): be more lenient about sniffs not following naming conventions #676

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 11 additions & 9 deletions src/Util/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,7 @@ public static function getSniffCode($sniffClass)

$parts = explode('\\', $sniffClass);
$partsCount = count($parts);
if ($partsCount < 4) {
throw new InvalidArgumentException(
'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received: '.$sniffClass
);
}

$sniff = $parts[($partsCount - 1)];
$sniff = $parts[($partsCount - 1)];

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

$standard = $parts[($partsCount - 4)];
$category = $parts[($partsCount - 2)];
$standard = '';
if (isset($parts[($partsCount - 4)]) === true) {
$standard = $parts[($partsCount - 4)];
}

$category = '';
if (isset($parts[($partsCount - 2)]) === true) {
$category = $parts[($partsCount - 2)];
}

return $standard.'.'.$category.'.'.$sniff;

}//end getSniffCode()
Expand Down
41 changes: 34 additions & 7 deletions tests/Core/Util/Common/GetSniffCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public static function dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTes
'Unqualified class name' => ['ClassName'],
'Fully qualified class name, not enough parts' => ['Fully\\Qualified\\ClassName'],
'Fully qualified class name, doesn\'t end on Sniff or UnitTest' => ['Fully\\Sniffs\\Qualified\\ClassName'],
'Fully qualified class name, ends on Sniff, but isn\'t' => ['Fully\\Sniffs\\AbstractSomethingSniff'],
];

}//end dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
Expand Down Expand Up @@ -141,30 +140,58 @@ public function testGetSniffCode($fqnClass, $expected)
public static function dataGetSniffCode()
{
return [
'PHPCS native sniff' => [
'PHPCS native sniff' => [
'fqnClass' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\Arrays\\ArrayIndentSniff',
'expected' => 'Generic.Arrays.ArrayIndent',
],
'Class is a PHPCS native test class' => [
'Class is a PHPCS native test class' => [
'fqnClass' => 'PHP_CodeSniffer\\Standards\\Generic\\Tests\\Arrays\\ArrayIndentUnitTest',
'expected' => 'Generic.Arrays.ArrayIndent',
],
'Sniff in external standard without namespace prefix' => [
'Sniff in external standard without namespace prefix' => [
'fqnClass' => 'MyStandard\\Sniffs\\PHP\\MyNameSniff',
'expected' => 'MyStandard.PHP.MyName',
],
'Test in external standard without namespace prefix' => [
'Test in external standard without namespace prefix' => [
'fqnClass' => 'MyStandard\\Tests\\PHP\\MyNameSniff',
'expected' => 'MyStandard.PHP.MyName',
],
'Sniff in external standard with namespace prefix' => [
'Sniff in external standard with namespace prefix' => [
'fqnClass' => 'Vendor\\Package\\MyStandard\\Sniffs\\Category\\AnalyzeMeSniff',
'expected' => 'MyStandard.Category.AnalyzeMe',
],
'Test in external standard with namespace prefix' => [
'Test in external standard with namespace prefix' => [
'fqnClass' => 'Vendor\\Package\\MyStandard\\Tests\\Category\\AnalyzeMeUnitTest',
'expected' => 'MyStandard.Category.AnalyzeMe',
],

/*
* These are not valid sniff codes and is an undesirable result, but can't be helped
* as changing this would be a BC-break.
* Supporting these to allow for <rule> tags directly including sniff files.
* See: https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/675
*/

'Fully qualified class name, ends on Sniff, but isn\'t' => [
'fqnClass' => 'Fully\\Sniffs\\AbstractSomethingSniff',
'expected' => '.Sniffs.AbstractSomething',
],
'Sniff provided via file include and doesn\'t comply with naming conventions [1]' => [
'fqnClass' => 'CheckMeSniff',
'expected' => '..CheckMe',
],
'Sniff provided via file include and doesn\'t comply with naming conventions [2]' => [
'fqnClass' => 'CompanyName\\CheckMeSniff',
'expected' => '.CompanyName.CheckMe',
],
'Sniff provided via file include and doesn\'t comply with naming conventions [3]' => [
'fqnClass' => 'CompanyName\\Sniffs\\CheckMeSniff',
'expected' => '.Sniffs.CheckMe',
],
'Sniff provided via file include and doesn\'t comply with naming conventions [4]' => [
'fqnClass' => 'CompanyName\\CustomSniffs\\Whatever\\CheckMeSniff',
'expected' => 'CompanyName.Whatever.CheckMe',
],
];

}//end dataGetSniffCode()
Expand Down