Skip to content

Commit 47222b9

Browse files
Daniel MüllerDaniel Müller
authored andcommitted
Merge pull request #3 in CFA/coding-guidelines from CORRECT_NAMESPACES to master
* commit '21635939b8222007fa2271a3ea534e9c78c64c4d': Improved error message namespace declarations must be in UpperCamelCase
2 parents c484cfe + 2163593 commit 47222b9

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace flyeralarm\CodingGuidelines\Flyeralarm\Sniffs\File;
4+
5+
use PHP_CodeSniffer\Sniffs\Sniff;
6+
use PHP_CodeSniffer\Files\File;
7+
8+
class NamespacesSniff implements Sniff
9+
{
10+
/**
11+
* @return array
12+
*/
13+
public function register()
14+
{
15+
return array(T_CLASS, T_ABSTRACT, T_TRAIT, T_INTERFACE);
16+
}
17+
18+
/**
19+
* @param File $phpcsFile
20+
* @param int $stackPtr
21+
* @return void
22+
*/
23+
public function process(File $phpcsFile, $stackPtr)
24+
{
25+
$tokens = $phpcsFile->getTokens();
26+
$ptr = -1;
27+
while($ptr = $phpcsFile->findNext(T_NS_SEPARATOR, $ptr + 1)) {
28+
if (ctype_upper($tokens[$ptr + 1]['content'][0]) === false) {
29+
$phpcsFile->addError(
30+
'Namespace declarations after vendor name must be in UpperCamelCase',
31+
$stackPtr,
32+
'NamespaceDeclarationWithInvalidCapitalization'
33+
);
34+
}
35+
36+
if (strpos($tokens[$ptr + 1]['content'], '_') !== false) {
37+
$phpcsFile->addError(
38+
'Namespace declarations after vendor name must be in UpperCamelCase',
39+
$stackPtr,
40+
'NamespaceDeclarationWithInvalidCapitalization'
41+
);
42+
}
43+
}
44+
}
45+
}

ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
99
<rule ref="./custom-standards/Flyeralarm/Sniffs/File/ExceptionMessageSniff.php"/>
1010
<rule ref="./custom-standards/Flyeralarm/Sniffs/File/ForbiddenKeywordsSniff.php"/>
11+
<rule ref="./custom-standards/Flyeralarm/Sniffs/File/NamespacesSniff.php"/>
1112
<rule ref="./custom-standards/Flyeralarm/Sniffs/File/NoClassKindSuffixSniff.php"/>
1213
<rule ref="./custom-standards/Flyeralarm/Sniffs/Docblock/ReturnTypeSniff.php"/>
1314
<rule ref="./custom-standards/Flyeralarm/Sniffs/Docblock/ExpectedExceptionMessageSniff.php"/>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
// @expectedError Namespace declarations after vendor name must be in UpperCamelCase
4+
5+
namespace flyeralarm\lowerCase;
6+
7+
class LowercaseNamespaceCapitalization
8+
{
9+
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
// @expectedError Namespace declarations after vendor name must be in UpperCamelCase
4+
5+
namespace flyeralarm\under_score;
6+
7+
class UnderscoreNamespace
8+
{
9+
10+
}

0 commit comments

Comments
 (0)