View source
<?php
namespace Drupal\Sniffs\Arrays;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
class ArraySniff implements Sniff {
public function register() {
return array(
T_ARRAY,
T_OPEN_SHORT_ARRAY,
);
}
public function process(File $phpcsFile, $stackPtr) {
$tokens = $phpcsFile
->getTokens();
$parenthesis_opener = 'parenthesis_opener';
$parenthesis_closer = 'parenthesis_closer';
if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY) {
$parenthesis_opener = 'bracket_opener';
$parenthesis_closer = 'bracket_closer';
}
if ($tokens[$stackPtr][$parenthesis_closer] === null) {
return;
}
$lastItem = $phpcsFile
->findPrevious(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 && $tokens[$lastItem + 1]['code'] !== T_CLOSE_SHORT_ARRAY && isset(Tokens::$heredocTokens[$tokens[$lastItem]['code']]) === false) {
$data = array(
$tokens[$lastItem]['content'],
);
$fix = $phpcsFile
->addFixableWarning('A comma should follow the last multiline array item. Found: %s', $lastItem, 'CommaLastItem', $data);
if ($fix === true) {
$phpcsFile->fixer
->addContent($lastItem, ',');
}
return;
}
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;
}
$firstLineColumn = $tokens[$stackPtr]['column'];
for ($i = $stackPtr; $i >= 0; $i--) {
if ($tokens[$i]['code'] === T_OPEN_TAG) {
return;
}
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;
$current_line = $tokens[$newLineStart]['line'];
while ($current_line >= $tokens[$newLineStart]['line']) {
$newLineStart = $phpcsFile
->findNext(Tokens::$emptyTokens, $newLineStart + 1, $tokens[$stackPtr][$parenthesis_closer] + 1, true);
if ($newLineStart === false) {
break 2;
}
if ($tokens[$newLineStart]['code'] === T_ARRAY) {
$newLineStart = $tokens[$newLineStart]['parenthesis_closer'];
$current_line = $tokens[$newLineStart]['line'];
}
if ($tokens[$newLineStart]['code'] === T_OPEN_SHORT_ARRAY) {
$newLineStart = $tokens[$newLineStart]['bracket_closer'];
$current_line = $tokens[$newLineStart]['line'];
}
if ($tokens[$newLineStart]['conditions'] !== $tokens[$stackPtr]['conditions']) {
$current_line++;
}
}
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,
);
$fix = $phpcsFile
->addFixableError($error, $newLineStart, 'ArrayClosingIndentation', $data);
if ($fix === true) {
if ($tokens[$newLineStart]['column'] === 1) {
$phpcsFile->fixer
->addContentBefore($newLineStart, str_repeat(' ', $firstLineColumn - 1));
}
else {
$phpcsFile->fixer
->replaceToken($newLineStart - 1, str_repeat(' ', $firstLineColumn - 1));
}
}
}
break;
}
$expectedColumn = $firstLineColumn + 2;
if ($tokens[$newLineStart]['code'] === T_OBJECT_OPERATOR) {
$expectedColumn += 2;
}
if ($tokens[$newLineStart]['column'] !== $expectedColumn) {
$innerNesting = empty($tokens[$newLineStart]['nested_parenthesis']) === false && end($tokens[$newLineStart]['nested_parenthesis']) < $tokens[$stackPtr][$parenthesis_closer];
$isMultiLineString = $tokens[$newLineStart - 1]['code'] === T_CONSTANT_ENCAPSED_STRING && substr($tokens[$newLineStart - 1]['content'], -1) === $phpcsFile->eolChar;
$nowDoc = isset(Tokens::$heredocTokens[$tokens[$newLineStart]['code']]);
if ($innerNesting === false && $isMultiLineString === false && $nowDoc === false) {
$error = 'Array indentation error, expected %s spaces but found %s';
$data = array(
$expectedColumn - 1,
$tokens[$newLineStart]['column'] - 1,
);
$fix = $phpcsFile
->addFixableError($error, $newLineStart, 'ArrayIndentation', $data);
if ($fix === true) {
if ($tokens[$newLineStart]['column'] === 1) {
$phpcsFile->fixer
->addContentBefore($newLineStart, str_repeat(' ', $expectedColumn - 1));
}
else {
$phpcsFile->fixer
->replaceToken($newLineStart - 1, str_repeat(' ', $expectedColumn - 1));
}
}
}
}
$lineStart = $newLineStart;
}
}
}