Skip to content

Commit bd35bfb

Browse files
xellioRichard Prillwitz
andauthored
Avoid checking abstract functions (#13)
* - added sniff for abstract class naming convention; - added sniff for interface naming convention; - added basic tests (consistent to existing ones); * - code styling; * - code styling; * - enable worflow; * - added some debugging output for fixing/checking the github workflow; * - fix expected result; * - revert workflow changes; * - added return type hint; * - check only class on AbstractClassNameSniff; - optimized reading of the class name; * cleanup; * debugging testing output; * removed test debugging output; Co-authored-by: Richard Prillwitz <richard.prillwitz@proton.ch>
1 parent 7229e34 commit bd35bfb

File tree

5 files changed

+46
-21
lines changed

5 files changed

+46
-21
lines changed

Proton/Sniffs/Naming/AbstractClassNameSniff.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,32 @@ class AbstractClassNameSniff implements Sniff
1212
{
1313
public function register(): array
1414
{
15-
return [T_ABSTRACT];
15+
return [T_CLASS];
1616
}
1717

1818
public function process(File $phpcsFile, $stackPtr): void
1919
{
20+
if (!TokenHelper::findPrevious($phpcsFile, T_ABSTRACT, $stackPtr)) {
21+
return;
22+
}
23+
2024
$tokens = $phpcsFile->getTokens();
2125

22-
if ($tokens[$stackPtr]['code'] === T_ABSTRACT) {
23-
$namePointer = TokenHelper::findNext($phpcsFile, T_STRING, $stackPtr + 1);
26+
$opener = $tokens[$stackPtr]['scope_opener'];
27+
$nameStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $opener, true);
28+
$nameEnd = $phpcsFile->findNext([T_WHITESPACE, T_COLON], $nameStart, $opener);
29+
if ($nameEnd === false) {
30+
$name = $tokens[$nameStart]['content'];
31+
} else {
32+
$name = trim($phpcsFile->getTokensAsString($nameStart, ($nameEnd - $nameStart)));
33+
}
2434

25-
if (substr($tokens[$namePointer]['content'], 0, 8) !== 'Abstract') {
26-
$phpcsFile->addError(
27-
'An abstract class should always start with `Abstract`',
28-
$namePointer,
29-
'Found',
30-
);
31-
}
35+
if (substr($name, 0, 8) !== 'Abstract') {
36+
$phpcsFile->addError(
37+
'An abstract class should always start with `Abstract`',
38+
$nameStart,
39+
'Found',
40+
);
3241
}
3342
}
3443
}

Proton/Sniffs/Naming/InterfaceNameSniff.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,26 @@ public function process(File $phpcsFile, $stackPtr): void
1919
{
2020
$tokens = $phpcsFile->getTokens();
2121

22-
if ($tokens[$stackPtr]['code'] === T_INTERFACE) {
23-
$namePointer = TokenHelper::findNext($phpcsFile, T_STRING, $stackPtr + 1);
24-
$className = $tokens[$namePointer]['content'];
25-
26-
if (substr($className, strlen($className) - 9, 9) !== 'Interface') {
27-
$phpcsFile->addError(
28-
'An interface should always end with `Interface`',
29-
$namePointer,
30-
'Found',
31-
);
32-
}
22+
if ($tokens[$stackPtr]['code'] !== T_INTERFACE) {
23+
return;
3324
}
25+
26+
$opener = $tokens[$stackPtr]['scope_opener'];
27+
$nameStart = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), $opener, true);
28+
$nameEnd = $phpcsFile->findNext([T_WHITESPACE, T_COLON], $nameStart, $opener);
29+
if ($nameEnd === false) {
30+
$name = $tokens[$nameStart]['content'];
31+
} else {
32+
$name = trim($phpcsFile->getTokensAsString($nameStart, ($nameEnd - $nameStart)));
33+
}
34+
35+
if (substr($name, strlen($name) - 9, 9) !== 'Interface') {
36+
$phpcsFile->addError(
37+
'An interface should always end with `Interface`',
38+
$nameStart,
39+
'Found',
40+
);
41+
}
42+
3443
}
3544
}

tests/correct/AbstractClassOk.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
abstract class AbstractClassOk
88
{
9+
abstract public function setOne(int $one = 1): void;
10+
911
public function getOne(): int
1012
{
1113
return 1;

tests/expected_csv.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Line,Column,Type,Source
22
4,10,error,PSR2.Files.EndFileNewline.NoneFound
33
1,1,error,SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
44
7,11,error,Proton.Naming.InterfaceName.Found
5+
8,1,error,SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces.IncorrectEmptyLinesAfterOpeningBrace
6+
8,1,error,PSR12.Classes.OpeningBraceSpace.Found
57
7,16,error,Proton.Naming.AbstractClassName.Found
68
6,12,warning,Generic.Metrics.NestingLevel.TooHigh
79
6,12,error,SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint

tests/wrong/ClassWrongAbstract.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
abstract class ClassWrongAbstract
88
{
9+
10+
abstract public function setOne(int $one = 1): void;
11+
912
public function getOne(): int
1013
{
1114
return 1;

0 commit comments

Comments
 (0)