Skip to content

Commit 998f1da

Browse files
committed
Merge branch 'feature/psr-naming-conventions' of https://github.com/annechko/PHP_CodeSniffer
2 parents e5e129e + 88c8dac commit 998f1da

13 files changed

+498
-0
lines changed

package.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
320320
<file baseinstalldir="PHP/CodeSniffer" name="NestingLevelStandard.xml" role="php" />
321321
</dir>
322322
<dir name="NamingConventions">
323+
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassStandard.xml" role="php" />
323324
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameStandard.xml" role="php" />
324325
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameStandard.xml" role="php" />
326+
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceStandard.xml" role="php" />
327+
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitStandard.xml" role="php" />
325328
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameStandard.xml" role="php" />
326329
</dir>
327330
<dir name="PHP">
@@ -425,8 +428,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
425428
<file baseinstalldir="PHP/CodeSniffer" name="NestingLevelSniff.php" role="php" />
426429
</dir>
427430
<dir name="NamingConventions">
431+
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassSniff.php" role="php" />
428432
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameSniff.php" role="php" />
429433
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameSniff.php" role="php" />
434+
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceSniff.php" role="php" />
435+
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitSniff.php" role="php" />
430436
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameSniff.php" role="php" />
431437
</dir>
432438
<dir name="PHP">
@@ -670,10 +676,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
670676
<file baseinstalldir="PHP/CodeSniffer" name="NestingLevelUnitTest.php" role="test" />
671677
</dir>
672678
<dir name="NamingConventions">
679+
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassUnitTest.inc" role="test" />
680+
<file baseinstalldir="PHP/CodeSniffer" name="AbstractPrefixRequiredForAbstractClassUnitTest.php" role="test" />
673681
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameUnitTest.inc" role="test" />
674682
<file baseinstalldir="PHP/CodeSniffer" name="CamelCapsFunctionNameUnitTest.php" role="test" />
675683
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameUnitTest.inc" role="test" />
676684
<file baseinstalldir="PHP/CodeSniffer" name="ConstructorNameUnitTest.php" role="test" />
685+
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceUnitTest.inc" role="test" />
686+
<file baseinstalldir="PHP/CodeSniffer" name="InterfaceSuffixRequiredForInterfaceUnitTest.php" role="test" />
687+
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitUnitTest.inc" role="test" />
688+
<file baseinstalldir="PHP/CodeSniffer" name="TraitSuffixRequiredForTraitUnitTest.php" role="test" />
677689
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameUnitTest.inc" role="test" />
678690
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantNameUnitTest.php" role="test" />
679691
</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. AbstractBar.
5+
]]>
6+
</standard>
7+
<code_comparison>
8+
<code title="Valid: ">
9+
<![CDATA[
10+
abstract class <em>AbstractBar</em>
11+
{
12+
}
13+
]]>
14+
</code>
15+
<code title="Invalid: ">
16+
<![CDATA[
17+
abstract class <em>Bar</em>
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. BarInterface.
5+
]]>
6+
</standard>
7+
<code_comparison>
8+
<code title="Valid: ">
9+
<![CDATA[
10+
interface <em>BarInterface</em>
11+
{
12+
}
13+
]]>
14+
</code>
15+
<code title="Invalid: ">
16+
<![CDATA[
17+
interface <em>Bar</em>
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. BarTrait.
5+
]]>
6+
</standard>
7+
<code_comparison>
8+
<code title="Valid: ">
9+
<![CDATA[
10+
trait <em>BarTrait</em>
11+
{
12+
}
13+
]]>
14+
</code>
15+
<code title="Invalid: ">
16+
<![CDATA[
17+
trait <em>Bar</em>
18+
{
19+
}
20+
]]>
21+
</code>
22+
</code_comparison>
23+
</documentation>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
if ($phpcsFile->getClassProperties($stackPtr)['is_abstract'] === false) {
42+
// This class is not abstract so we don't need to check it.
43+
return;
44+
}
45+
46+
$className = $phpcsFile->getDeclarationName($stackPtr);
47+
if ($className === null) {
48+
// We are not interested in anonymous classes.
49+
return;
50+
}
51+
52+
$prefix = substr($className, 0, 8);
53+
if (strtolower($prefix) !== 'abstract') {
54+
$phpcsFile->addError('Abstract classes MUST be prefixed by Abstract: e.g. AbstractBar. Found: %s', $stackPtr, 'Missing', [$className]);
55+
}
56+
57+
}//end process()
58+
59+
60+
}//end class
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
$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]);
49+
}
50+
51+
}//end process()
52+
53+
54+
}//end class
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
$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]);
49+
}
50+
51+
}//end process()
52+
53+
54+
}//end class
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 {}
55+
56+
abstract class abstractOkName
57+
{
58+
59+
}
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)