Skip to content

Commit 7229e34

Browse files
authored
Naming sniffs (#11)
- added sniff for abstract class naming convention - added sniff for interface naming convention - added basic tests (consistent to existing ones) - enable worflow - added some debugging output for fixing/checking the github workflow - fix expected result - added return type hint
1 parent 665b259 commit 7229e34

File tree

11 files changed

+129
-6
lines changed

11 files changed

+129
-6
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.vscode
33
.phpintel
44
/vendor/
5-
out.csv
5+
out.csv
6+
composer.lock

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018
3+
Copyright (c) 2022
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Proton\Sniffs\Naming;
6+
7+
use PHP_CodeSniffer\Files\File;
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use SlevomatCodingStandard\Helpers\TokenHelper;
10+
11+
class AbstractClassNameSniff implements Sniff
12+
{
13+
public function register(): array
14+
{
15+
return [T_ABSTRACT];
16+
}
17+
18+
public function process(File $phpcsFile, $stackPtr): void
19+
{
20+
$tokens = $phpcsFile->getTokens();
21+
22+
if ($tokens[$stackPtr]['code'] === T_ABSTRACT) {
23+
$namePointer = TokenHelper::findNext($phpcsFile, T_STRING, $stackPtr + 1);
24+
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+
}
32+
}
33+
}
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Proton\Sniffs\Naming;
6+
7+
use PHP_CodeSniffer\Files\File;
8+
use PHP_CodeSniffer\Sniffs\Sniff;
9+
use SlevomatCodingStandard\Helpers\TokenHelper;
10+
11+
class InterfaceNameSniff implements Sniff
12+
{
13+
public function register(): array
14+
{
15+
return [T_INTERFACE];
16+
}
17+
18+
public function process(File $phpcsFile, $stackPtr): void
19+
{
20+
$tokens = $phpcsFile->getTokens();
21+
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+
}
33+
}
34+
}
35+
}

Proton/ruleset.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@
247247
</rule>
248248

249249
<rule ref="Proton.Spacing.ArrowFunctionSpacing"/>
250+
<rule ref="Proton.Naming.AbstractClassName"/>
251+
<rule ref="Proton.Naming.InterfaceName"/>
250252

251253
<exclude-pattern>*/node_modules/*</exclude-pattern>
252254
<exclude-pattern>*/vendor/*</exclude-pattern>

tests/correct/AbstractClassOk.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Proton\Test;
6+
7+
abstract class AbstractClassOk
8+
{
9+
public function getOne(): int
10+
{
11+
return 1;
12+
}
13+
}

tests/correct/ValidInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Proton\Test;
6+
7+
interface ValidInterface
8+
{
9+
public function example(): void;
10+
}

tests/expected.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ PHP CODE SNIFFER REPORT SUMMARY
33
------------------------------------------------------------------------------------------------
44
FILE ERRORS WARNINGS
55
------------------------------------------------------------------------------------------------
6-
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 39 4
7-
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassMetrics.php 4 2
8-
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/file.php 2 0
6+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/Class1.php 39 4
7+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassMetrics.php 4 2
8+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/ClassWrongAbstract.php 1 0
9+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/file.php 2 0
10+
/Users/Username/dev/Proton/php-coding-standard/tests/wrong/InterfaceWrong.php 1 0
911
------------------------------------------------------------------------------------------------
10-
A TOTAL OF 37 ERRORS AND 5 WARNINGS WERE FOUND IN 3 FILES
12+
A TOTAL OF 39 ERRORS AND 5 WARNINGS WERE FOUND IN 5 FILES
1113
------------------------------------------------------------------------------------------------
1214
PHPCBF CAN FIX 29 OF THESE SNIFF VIOLATIONS AUTOMATICALLY
1315
------------------------------------------------------------------------------------------------

tests/expected_csv.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Line,Column,Type,Source
22
4,10,error,PSR2.Files.EndFileNewline.NoneFound
33
1,1,error,SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing
4+
7,11,error,Proton.Naming.InterfaceName.Found
5+
7,16,error,Proton.Naming.AbstractClassName.Found
46
6,12,warning,Generic.Metrics.NestingLevel.TooHigh
57
6,12,error,SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint
68
6,12,error,SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingAnyTypeHint
@@ -65,4 +67,5 @@ Line,Column,Type,Source
6567
10,27,error,SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces.IncorrectEmptyLinesAfterOpeningBrace
6668
10,27,error,PSR2.Classes.ClassDeclaration.OpenBraceNewLine
6769
10,27,error,PSR12.Classes.OpeningBraceSpace.Found
70+
10,22,error,Proton.Naming.AbstractClassName.Found
6871
1,1,error,SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing

tests/wrong/ClassWrongAbstract.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace wrong;
6+
7+
abstract class ClassWrongAbstract
8+
{
9+
public function getOne(): int
10+
{
11+
return 1;
12+
}
13+
}

0 commit comments

Comments
 (0)