View source
<?php
class Drupal_Sniffs_Array_ArraySniff implements PHP_CodeSniffer_Sniff {
public function register() {
return array(
T_ARRAY,
);
}
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile
->getTokens();
$lastItem = $phpcsFile
->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, $tokens[$stackPtr]['parenthesis_closer'] - 1, $stackPtr, true);
if ($lastItem === $tokens[$stackPtr]['parenthesis_opener']) {
return;
}
$isInlineArray = $tokens[$tokens[$stackPtr]['parenthesis_opener']]['line'] == $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line'];
if ($tokens[$lastItem]['code'] !== T_COMMA && $isInlineArray === false && $tokens[$lastItem + 1]['code'] !== T_CLOSE_PARENTHESIS) {
$phpcsFile
->addWarning('A comma should follow the last multiline array item. Found: ' . $tokens[$lastItem]['content'], $lastItem);
return;
}
if ($tokens[$lastItem]['code'] === T_COMMA && $isInlineArray === true) {
$phpcsFile
->addWarning('Last item of an inline array must not be followed by a comma', $lastItem);
}
if ($isInlineArray === true) {
if ($tokens[$tokens[$stackPtr]['parenthesis_closer']]['column'] > 80) {
$comma1 = $phpcsFile
->findNext(T_COMMA, $stackPtr + 1, $tokens[$stackPtr]['parenthesis_closer']);
if ($comma1 !== false) {
$comma2 = $phpcsFile
->findNext(T_COMMA, $comma1 + 1, $tokens[$stackPtr]['parenthesis_closer']);
if ($comma2 !== false) {
$error = 'If the line declaring an array spans longer than 80 characters, each element should be broken into its own line';
$phpcsFile
->addError($error, $stackPtr, 'LongLineDeclaration');
}
}
}
return;
}
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
end($tokens[$stackPtr]['nested_parenthesis']);
$outerNesting = key($tokens[$stackPtr]['nested_parenthesis']);
if ($tokens[$outerNesting]['line'] === $tokens[$stackPtr]['line']) {
return;
}
}
$firstLineColumn = $tokens[$stackPtr]['column'];
for ($i = $stackPtr; $i >= 0; $i--) {
if ($tokens[$i]['code'] !== T_WHITESPACE) {
$firstLineColumn = $tokens[$i]['column'];
$trimmed = ltrim($tokens[$i]['content']);
if ($trimmed !== $tokens[$i]['content']) {
$firstLineColumn = $firstLineColumn + strpos($tokens[$i]['content'], $trimmed);
}
}
if ($tokens[$i]['column'] === 1) {
break;
}
}
$lineStart = $stackPtr;
while ($lineStart < $tokens[$stackPtr]['parenthesis_closer']) {
$newLineStart = $lineStart;
while ($tokens[$newLineStart]['line'] == $tokens[$lineStart]['line']) {
$newLineStart = $phpcsFile
->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, $newLineStart + 1, $tokens[$stackPtr]['parenthesis_closer'] + 1, true);
if ($newLineStart === false) {
break 2;
}
}
if ($newLineStart === $tokens[$stackPtr]['parenthesis_closer']) {
if ($tokens[$newLineStart]['column'] !== $firstLineColumn) {
$error = 'Array closing indentation error, expected %s spaces but found %s';
$data = array(
$firstLineColumn - 1,
$tokens[$newLineStart]['column'] - 1,
);
$phpcsFile
->addError($error, $newLineStart, 'ArrayClosingIndentation', $data);
}
break;
}
$innerNesting = end($tokens[$newLineStart]['nested_parenthesis']);
if ($innerNesting === $tokens[$stackPtr]['parenthesis_closer'] && $tokens[$newLineStart]['column'] !== $firstLineColumn + 2) {
$error = 'Array indentation error, expected %s spaces but found %s';
$data = array(
$firstLineColumn + 1,
$tokens[$newLineStart]['column'] - 1,
);
$phpcsFile
->addError($error, $newLineStart, 'ArrayIndentation', $data);
}
$lineStart = $newLineStart;
}
}
}