Skip to content

Commit 2fae0e8

Browse files
committed
Added AbstractPrefixRequiredForAbstractClass, InterfaceSuffixRequiredForInterface, TraitSuffixRequiredForTrait sniffs to Generic.NamingConventions
1 parent d33a6a9 commit 2fae0e8

13 files changed

+500
-0
lines changed

package.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
253253
<file baseinstalldir="PHP/CodeSniffer" name="NestingLevelStandard.xml" role="php" />
254254
</dir>
255255
<dir name="NamingConventions">
256+
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassStandard.xml" role="php" />
256257
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameStandard.xml" role="php" />
257258
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameStandard.xml" role="php" />
259+
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceStandard.xml" role="php" />
260+
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitStandard.xml" role="php" />
258261
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameStandard.xml" role="php" />
259262
</dir>
260263
<dir name="PHP">
@@ -358,8 +361,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
358361
<file baseinstalldir="PHP/CodeSniffer" name="NestingLevelSniff.php" role="php" />
359362
</dir>
360363
<dir name="NamingConventions">
364+
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassSniff.php" role="php" />
361365
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameSniff.php" role="php" />
362366
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameSniff.php" role="php" />
367+
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceSniff.php" role="php" />
368+
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitSniff.php" role="php" />
363369
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameSniff.php" role="php" />
364370
</dir>
365371
<dir name="PHP">
@@ -603,10 +609,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
603609
<file baseinstalldir="PHP/CodeSniffer" name="NestingLevelUnitTest.php" role="test" />
604610
</dir>
605611
<dir name="NamingConventions">
612+
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassUnitTest.inc" role="test" />
613+
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassUnitTest.php" role="test" />
606614
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameUnitTest.inc" role="test" />
607615
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameUnitTest.php" role="test" />
608616
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameUnitTest.inc" role="test" />
609617
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameUnitTest.php" role="test" />
618+
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceUnitTest.inc" role="test" />
619+
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceUnitTest.php" role="test" />
620+
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitUnitTest.inc" role="test" />
621+
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitUnitTest.php" role="test" />
610622
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameUnitTest.inc" role="test" />
611623
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameUnitTest.php" role="test" />
612624
</dir>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<documentation title="Abstract class name">
2+
<standard>
3+
<![CDATA[
4+
Abstract classes MUST be prefixed by Abstract: e.g. Psr\Foo\AbstractBar.
5+
]]>
6+
</standard>
7+
<code_comparison>
8+
<code title="Valid: ">
9+
<![CDATA[
10+
abstract class AbstractFoo
11+
{
12+
}
13+
]]>
14+
</code>
15+
<code title="Invalid: ">
16+
<![CDATA[
17+
abstract class Foo
18+
{
19+
}
20+
]]>
21+
</code>
22+
</code_comparison>
23+
</documentation>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<documentation title="Interface name">
2+
<standard>
3+
<![CDATA[
4+
Interfaces MUST be suffixed by Interface: e.g. Psr\Foo\BarInterface.
5+
]]>
6+
</standard>
7+
<code_comparison>
8+
<code title="Valid: ">
9+
<![CDATA[
10+
interface FooInterface
11+
{
12+
}
13+
]]>
14+
</code>
15+
<code title="Invalid: ">
16+
<![CDATA[
17+
interface IFoo
18+
{
19+
}
20+
]]>
21+
</code>
22+
</code_comparison>
23+
</documentation>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<documentation title="Trait name">
2+
<standard>
3+
<![CDATA[
4+
Traits MUST be suffixed by Trait: e.g. Psr\Foo\BarTrait.
5+
]]>
6+
</standard>
7+
<code_comparison>
8+
<code title="Valid: ">
9+
<![CDATA[
10+
trait BarTrait
11+
{
12+
}
13+
]]>
14+
</code>
15+
<code title="Invalid: ">
16+
<![CDATA[
17+
trait Bar
18+
{
19+
}
20+
]]>
21+
</code>
22+
</code_comparison>
23+
</documentation>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Checks that abstract classes are prefixed by Abstract.
4+
*
5+
* @author Anna Borzenko <[email protected]>
6+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions;
10+
11+
use PHP_CodeSniffer\Files\File;
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
14+
class AbstractPrefixRequiredForAbstractClassSniff implements Sniff
15+
{
16+
17+
18+
/**
19+
* Registers the tokens that this sniff wants to listen for.
20+
*
21+
* @return int[]
22+
*/
23+
public function register()
24+
{
25+
return [T_CLASS];
26+
27+
}//end register()
28+
29+
30+
/**
31+
* Processes this sniff, when one of its tokens is encountered.
32+
*
33+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
34+
* @param int $stackPtr The position of the current token
35+
* in the stack passed in $tokens.
36+
*
37+
* @return void
38+
*/
39+
public function process(File $phpcsFile, $stackPtr)
40+
{
41+
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
42+
if ($prev === false || $phpcsFile->getTokens()[$prev]['code'] !== T_ABSTRACT) {
43+
// This class is not abstract so we don't need to check it.
44+
return;
45+
}
46+
47+
$className = $phpcsFile->getDeclarationName($stackPtr);
48+
if ($className === null) {
49+
// We are not interested in anonymous classes.
50+
return;
51+
}
52+
53+
$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');
56+
}
57+
58+
}//end process()
59+
60+
61+
}//end class
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Checks that interfaces are suffixed by Interface.
4+
*
5+
* @author Anna Borzenko <[email protected]>
6+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions;
10+
11+
use PHP_CodeSniffer\Files\File;
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
14+
class InterfaceSuffixRequiredForInterfaceSniff implements Sniff
15+
{
16+
17+
18+
/**
19+
* Registers the tokens that this sniff wants to listen for.
20+
*
21+
* @return int[]
22+
*/
23+
public function register()
24+
{
25+
return [T_INTERFACE];
26+
27+
}//end register()
28+
29+
30+
/**
31+
* Processes this sniff, when one of its tokens is encountered.
32+
*
33+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
34+
* @param int $stackPtr The position of the current token
35+
* in the stack passed in $tokens.
36+
*
37+
* @return void
38+
*/
39+
public function process(File $phpcsFile, $stackPtr)
40+
{
41+
$interfaceName = $phpcsFile->getDeclarationName($stackPtr);
42+
if ($interfaceName === null) {
43+
return;
44+
}
45+
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');
50+
}
51+
52+
}//end process()
53+
54+
55+
}//end class
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Checks that traits are suffixed by Trait.
4+
*
5+
* @author Anna Borzenko <[email protected]>
6+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\NamingConventions;
10+
11+
use PHP_CodeSniffer\Files\File;
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
14+
class TraitSuffixRequiredForTraitSniff implements Sniff
15+
{
16+
17+
18+
/**
19+
* Registers the tokens that this sniff wants to listen for.
20+
*
21+
* @return int[]
22+
*/
23+
public function register()
24+
{
25+
return [T_TRAIT];
26+
27+
}//end register()
28+
29+
30+
/**
31+
* Processes this sniff, when one of its tokens is encountered.
32+
*
33+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
34+
* @param int $stackPtr The position of the current token
35+
* in the stack passed in $tokens.
36+
*
37+
* @return void
38+
*/
39+
public function process(File $phpcsFile, $stackPtr)
40+
{
41+
$traitName = $phpcsFile->getDeclarationName($stackPtr);
42+
if ($traitName === null) {
43+
return;
44+
}
45+
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');
50+
}
51+
52+
}//end process()
53+
54+
55+
}//end class
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
abstract class IncorrectName // error
4+
{
5+
6+
}
7+
8+
abstract class AbstractCorrectName
9+
{
10+
11+
}
12+
13+
abstract class IncorrectNameAbstract // error
14+
{
15+
16+
}
17+
18+
abstract class InvalidNameabstract // error
19+
{
20+
21+
}
22+
23+
abstract class IncorrectAbstractName // error
24+
{
25+
26+
}
27+
28+
$anon = new class {};
29+
30+
class AbstractClassName
31+
{
32+
33+
}
34+
35+
if (!class_exists('AbstractClassCorrectName')) {
36+
abstract class AbstractClassCorrectName
37+
{
38+
39+
}
40+
}
41+
if (!class_exists('ClassAbstractIncorrectName')) {
42+
abstract class ClassAbstractIncorrectName // error
43+
{
44+
45+
}
46+
}
47+
48+
$abstractVar = '';
49+
50+
$var = 'abstract class IncorrectNameButOk';
51+
52+
$abstracVar = '';
53+
54+
class NameAbstractBar {}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Unit test class for the AbstractPrefixRequiredForAbstractClass sniff.
4+
*
5+
* @author Anna Borzenko <[email protected]>
6+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Standards\Generic\Tests\NamingConventions;
10+
11+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
12+
13+
class AbstractPrefixRequiredForAbstractClassUnitTest extends AbstractSniffUnitTest
14+
{
15+
16+
17+
/**
18+
* Returns the lines where errors should occur.
19+
*
20+
* The key of the array should represent the line number and the value
21+
* should represent the number of errors that should occur on that line.
22+
*
23+
* @return array<int, int>
24+
*/
25+
public function getErrorList()
26+
{
27+
return [
28+
3 => 1,
29+
13 => 1,
30+
18 => 1,
31+
23 => 1,
32+
42 => 1,
33+
];
34+
35+
}//end getErrorList()
36+
37+
38+
/**
39+
* Returns the lines where warnings should occur.
40+
*
41+
* The key of the array should represent the line number and the value
42+
* should represent the number of warnings that should occur on that line.
43+
*
44+
* @return array<int, int>
45+
*/
46+
public function getWarningList()
47+
{
48+
return [];
49+
50+
}//end getWarningList()
51+
52+
53+
}//end class

0 commit comments

Comments
 (0)