Skip to content

Commit

Permalink
PHPCS: Added support for short array syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
lolautruche committed Jan 13, 2015
1 parent 0a9bc13 commit b592de6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
17 changes: 11 additions & 6 deletions php/ezcs/Sniffs/Arrays/ArrayDeclarationSniff.php
Expand Up @@ -8,7 +8,7 @@ class ezcs_Sniffs_Arrays_ArrayDeclarationSniff implements PHP_CodeSniffer_Sniff
*/
public function register()
{
return array(T_ARRAY);
return array(T_ARRAY, T_OPEN_SHORT_ARRAY);

}

Expand All @@ -31,13 +31,18 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$phpcsFile->addError('Array keyword should be lower case; expected "array" but found "%s"', $stackPtr, 'NotLowerCase', array($tokens[$stackPtr]['content']));
}

$arrayStart = $tokens[$stackPtr]['parenthesis_opener'];
$arrayEnd = $tokens[$arrayStart]['parenthesis_closer'];
if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY) {
$arrayStart = $tokens[$stackPtr]['bracket_opener'];
$arrayEnd = $tokens[$arrayStart]['bracket_closer'];
} else { // T_ARRAY
$arrayStart = $tokens[$stackPtr]['parenthesis_opener'];
$arrayEnd = $tokens[$arrayStart]['parenthesis_closer'];
if ($arrayStart != ($stackPtr + 1)) {
$phpcsFile->addError('There must be no space between the "array" keyword and the opening parenthesis', $stackPtr, 'SpaceAfterKeyword');
}
}
$keywordStart = $tokens[$stackPtr]['column'];

if ($arrayStart != ($stackPtr + 1)) {
$phpcsFile->addError('There must be no space between the "array" keyword and the opening parenthesis', $stackPtr, 'SpaceAfterKeyword');
}

// Check for empty arrays.
$content = $phpcsFile->findNext(array(T_WHITESPACE), ($arrayStart + 1), ($arrayEnd + 1), true);
Expand Down
10 changes: 8 additions & 2 deletions php/ezcs/Sniffs/Functions/FunctionCallSignatureSniff.php
Expand Up @@ -10,7 +10,7 @@ class ezcs_Sniffs_Functions_FunctionCallSignatureSniff implements PHP_CodeSniffe
*/
public function register()
{
return array(T_STRING, T_ARRAY);
return array(T_STRING, T_ARRAY, T_OPEN_SHORT_ARRAY);

}//end register()

Expand Down Expand Up @@ -145,12 +145,18 @@ public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr,
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
$lastLine = $tokens[$openBracket]['line'];
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
// Skip nested function calls.
// Skip nested function calls (including arrays).
if ($tokens[$i]["code"] === T_OPEN_PARENTHESIS) {
$i = $tokens[$i]['parenthesis_closer'];
$lastLine = $tokens[$i]['line'];
continue;
}
// Skip nested short arrays
if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
$i = $tokens[$i]['bracket_closer'];
$lastLine = $tokens[$i]['line'];
continue;
}
$code = $tokens[$i]["code"];
$line = $tokens[$i]["line"];

Expand Down

0 comments on commit b592de6

Please sign in to comment.