Skip to content

Commit 798e7aa

Browse files
committed
Check class, trait, interface naming in lowercase, change error codes to Missing, add Found info
1 parent 2fae0e8 commit 798e7aa

8 files changed

+20
-22
lines changed

src/Standards/Generic/Sniffs/NamingConventions/AbstractPrefixRequiredForAbstractClassSniff.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public function register()
3838
*/
3939
public function process(File $phpcsFile, $stackPtr)
4040
{
41-
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
42-
if ($prev === false || $phpcsFile->getTokens()[$prev]['code'] !== T_ABSTRACT) {
41+
if ($phpcsFile->getClassProperties($stackPtr)['is_abstract'] === false) {
4342
// This class is not abstract so we don't need to check it.
4443
return;
4544
}
@@ -51,8 +50,8 @@ public function process(File $phpcsFile, $stackPtr)
5150
}
5251

5352
$prefix = substr($className, 0, 8);
54-
if ($prefix !== 'Abstract') {
55-
$phpcsFile->addError('Abstract classes MUST be prefixed by Abstract: e.g. Psr\Foo\AbstractBar.', $stackPtr, 'RequiredAbstractPrefix');
53+
if (strtolower($prefix) !== 'abstract') {
54+
$phpcsFile->addError('Abstract classes MUST be prefixed by Abstract: e.g. AbstractBar. Found: %s', $stackPtr, 'Missing', [$className]);
5655
}
5756

5857
}//end process()

src/Standards/Generic/Sniffs/NamingConventions/InterfaceSuffixRequiredForInterfaceSniff.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ public function process(File $phpcsFile, $stackPtr)
4343
return;
4444
}
4545

46-
$interfaceNameLength = strlen($interfaceName);
47-
$suffix = substr($interfaceName, ($interfaceNameLength - 9), $interfaceNameLength);
48-
if ($suffix !== 'Interface') {
49-
$phpcsFile->addError('Interfaces MUST be suffixed by Interface: e.g. Psr\Foo\BarInterface.', $stackPtr, 'RequiredInterfaceSuffix');
46+
$suffix = substr($interfaceName, - 9);
47+
if (strtolower($suffix) !== 'interface') {
48+
$phpcsFile->addError('Interfaces MUST be suffixed by Interface: e.g. BarInterface. Found: %s', $stackPtr, 'Missing', [$interfaceName]);
5049
}
5150

5251
}//end process()

src/Standards/Generic/Sniffs/NamingConventions/TraitSuffixRequiredForTraitSniff.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ public function process(File $phpcsFile, $stackPtr)
4343
return;
4444
}
4545

46-
$traitNameLength = strlen($traitName);
47-
$suffix = substr($traitName, ($traitNameLength - 5), $traitNameLength);
48-
if ($suffix !== 'Trait') {
49-
$phpcsFile->addError('Traits MUST be suffixed by Trait: e.g. Psr\Foo\BarTrait.', $stackPtr, 'RequiredTraitSuffix');
46+
$suffix = substr($traitName, - 5);
47+
if (strtolower($suffix) !== 'trait') {
48+
$phpcsFile->addError('Traits MUST be suffixed by Trait: e.g. BarTrait. Found: %s', $stackPtr, 'Missing', [$traitName]);
5049
}
5150

5251
}//end process()

src/Standards/Generic/Tests/NamingConventions/AbstractPrefixRequiredForAbstractClassUnitTest.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ $var = 'abstract class IncorrectNameButOk';
5151

5252
$abstracVar = '';
5353

54-
class NameAbstractBar {}
54+
class NameAbstractBar {}
55+
56+
abstract class abstractOkName
57+
{
58+
59+
}

src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface ISomeNameInterface
1616
}
1717

1818

19-
interface ISomeOtherNameinterface // error
19+
interface ISomeOtherNameinterface
2020
{
2121

2222
}

src/Standards/Generic/Tests/NamingConventions/InterfaceSuffixRequiredForInterfaceUnitTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ class InterfaceSuffixRequiredForInterfaceUnitTest extends AbstractSniffUnitTest
2424
*/
2525
public function getErrorList()
2626
{
27-
return [
28-
8 => 1,
29-
19 => 1,
30-
];
27+
return [8 => 1];
3128

3229
}//end getErrorList()
3330

src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ trait GoodTraitTrait {}
88

99
trait BadTraitName {} // error
1010

11-
trait BadTraitNametrait {} // error
11+
trait BadTraitNametrait {}
1212

1313
trait NormalTraitNameTrait {}

src/Standards/Generic/Tests/NamingConventions/TraitSuffixRequiredForTraitUnitTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ class TraitSuffixRequiredForTraitUnitTest extends AbstractSniffUnitTest
2525
public function getErrorList()
2626
{
2727
return [
28-
3 => 1,
29-
9 => 1,
30-
11 => 1,
28+
3 => 1,
29+
9 => 1,
3130
];
3231

3332
}//end getErrorList()

0 commit comments

Comments
 (0)