Skip to content

Commit d49e703

Browse files
committed
Added lower camel case variable name sniff
1 parent 4619ac1 commit d49e703

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace flyeralarm\CodingGuidelines\Flyeralarm\Sniffs\Variable;
4+
5+
use PHP_CodeSniffer\Sniffs\Sniff;
6+
use PHP_CodeSniffer\Files\File;
7+
use PHP_CodeSniffer\Util\Common;
8+
9+
class LowerCamelCaseSniff implements Sniff
10+
{
11+
/**
12+
* @return array
13+
*/
14+
public function register()
15+
{
16+
return array(T_OPEN_TAG);
17+
}
18+
19+
/**
20+
* @param File $phpcsFile
21+
* @param int $stackPtr
22+
* @return void
23+
*/
24+
public function process(File $phpcsFile, $stackPtr)
25+
{
26+
$startPtr = $stackPtr;
27+
$tokens = $phpcsFile->getTokens();
28+
29+
while ($variablePtr = $phpcsFile->findNext(T_VARIABLE, $startPtr)) {
30+
$startPtr = $variablePtr + 1;
31+
32+
$variableName = $tokens[$variablePtr]['content'];
33+
$variableName = substr($variableName, 1);
34+
35+
$isLowerCamelCase = Common::isCamelCaps($variableName, false, true, true);
36+
if ($isLowerCamelCase) {
37+
continue;
38+
}
39+
if (strtolower($variableName) == $variableName) {
40+
continue;
41+
}
42+
43+
$phpcsFile->addError(
44+
'Variable names must be specified in lower camel case',
45+
$variablePtr,
46+
'CamelCase'
47+
);
48+
}
49+
}
50+
}

ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
<rule ref="./custom-standards/Flyeralarm/Sniffs/File/NoClassKindSuffixSniff.php"/>
1313
<rule ref="./custom-standards/Flyeralarm/Sniffs/Docblock/ReturnTypeSniff.php"/>
1414
<rule ref="./custom-standards/Flyeralarm/Sniffs/Docblock/ExpectedExceptionMessageSniff.php"/>
15+
<rule ref="./custom-standards/Flyeralarm/Sniffs/Variable/LowerCamelCaseSniff.php"/>
1516
<rule ref="./custom-standards/Flyeralarm/Sniffs/ControlStructures/YodaSniff.php"/>
1617
</ruleset>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// @expectedError Variable names must be specified in lower camel case
4+
5+
$UpperCamelCase = true;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// @expectedError Variable names must be specified in lower camel case
4+
5+
$fooBAr = true;

0 commit comments

Comments
 (0)