function ArrayDeclarationSniff::processSingleLineArray
Processes a single-line array definition.
Parameters
\PHP_CodeSniffer\Files\File $phpcsFile The current file being checked.:
int $stackPtr The position of the current token: in the stack passed in $tokens.
int $arrayStart The token that starts the array definition.:
int $arrayEnd The token that ends the array definition.:
Return value
void
1 call to ArrayDeclarationSniff::processSingleLineArray()
- ArrayDeclarationSniff::process in vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Squiz/ Sniffs/ Arrays/ ArrayDeclarationSniff.php - Processes this sniff, when one of its tokens is encountered.
File
-
vendor/
squizlabs/ php_codesniffer/ src/ Standards/ Squiz/ Sniffs/ Arrays/ ArrayDeclarationSniff.php, line 144
Class
Namespace
PHP_CodeSniffer\Standards\Squiz\Sniffs\ArraysCode
public function processSingleLineArray($phpcsFile, $stackPtr, $arrayStart, $arrayEnd) {
$tokens = $phpcsFile->getTokens();
// Check if there are multiple values. If so, then it has to be multiple lines
// unless it is contained inside a function call or condition.
$valueCount = 0;
$commas = [];
for ($i = $arrayStart + 1; $i < $arrayEnd; $i++) {
// Skip bracketed statements, like function calls.
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
$i = $tokens[$i]['parenthesis_closer'];
continue;
}
if ($tokens[$i]['code'] === T_COMMA) {
// Before counting this comma, make sure we are not
// at the end of the array.
$next = $phpcsFile->findNext(T_WHITESPACE, $i + 1, $arrayEnd, true);
if ($next !== false) {
$valueCount++;
$commas[] = $i;
}
else {
// There is a comma at the end of a single line array.
$error = 'Comma not allowed after last value in single-line array declaration';
$fix = $phpcsFile->addFixableError($error, $i, 'CommaAfterLast');
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($i, '');
}
}
}
}
//end for
// Now check each of the double arrows (if any).
$nextArrow = $arrayStart;
while (($nextArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $nextArrow + 1, $arrayEnd)) !== false) {
if ($tokens[$nextArrow - 1]['code'] !== T_WHITESPACE) {
$content = $tokens[$nextArrow - 1]['content'];
$error = 'Expected 1 space between "%s" and double arrow; 0 found';
$data = [
$content,
];
$fix = $phpcsFile->addFixableError($error, $nextArrow, 'NoSpaceBeforeDoubleArrow', $data);
if ($fix === true) {
$phpcsFile->fixer
->addContentBefore($nextArrow, ' ');
}
}
else {
$spaceLength = $tokens[$nextArrow - 1]['length'];
if ($spaceLength !== 1) {
$content = $tokens[$nextArrow - 2]['content'];
$error = 'Expected 1 space between "%s" and double arrow; %s found';
$data = [
$content,
$spaceLength,
];
$fix = $phpcsFile->addFixableError($error, $nextArrow, 'SpaceBeforeDoubleArrow', $data);
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($nextArrow - 1, ' ');
}
}
}
//end if
if ($tokens[$nextArrow + 1]['code'] !== T_WHITESPACE) {
$content = $tokens[$nextArrow + 1]['content'];
$error = 'Expected 1 space between double arrow and "%s"; 0 found';
$data = [
$content,
];
$fix = $phpcsFile->addFixableError($error, $nextArrow, 'NoSpaceAfterDoubleArrow', $data);
if ($fix === true) {
$phpcsFile->fixer
->addContent($nextArrow, ' ');
}
}
else {
$spaceLength = $tokens[$nextArrow + 1]['length'];
if ($spaceLength !== 1) {
$content = $tokens[$nextArrow + 2]['content'];
$error = 'Expected 1 space between double arrow and "%s"; %s found';
$data = [
$content,
$spaceLength,
];
$fix = $phpcsFile->addFixableError($error, $nextArrow, 'SpaceAfterDoubleArrow', $data);
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($nextArrow + 1, ' ');
}
}
}
//end if
}
//end while
if ($valueCount > 0) {
$nestedParenthesis = false;
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
$nested = $tokens[$stackPtr]['nested_parenthesis'];
$nestedParenthesis = array_pop($nested);
}
if ($nestedParenthesis === false || $tokens[$nestedParenthesis]['line'] !== $tokens[$stackPtr]['line']) {
$error = 'Array with multiple values cannot be declared on a single line';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SingleLineNotAllowed');
if ($fix === true) {
$phpcsFile->fixer
->beginChangeset();
$phpcsFile->fixer
->addNewline($arrayStart);
if ($tokens[$arrayEnd - 1]['code'] === T_WHITESPACE) {
$phpcsFile->fixer
->replaceToken($arrayEnd - 1, $phpcsFile->eolChar);
}
else {
$phpcsFile->fixer
->addNewlineBefore($arrayEnd);
}
$phpcsFile->fixer
->endChangeset();
}
return;
}
// We have a multiple value array that is inside a condition or
// function. Check its spacing is correct.
foreach ($commas as $comma) {
if ($tokens[$comma + 1]['code'] !== T_WHITESPACE) {
$content = $tokens[$comma + 1]['content'];
$error = 'Expected 1 space between comma and "%s"; 0 found';
$data = [
$content,
];
$fix = $phpcsFile->addFixableError($error, $comma, 'NoSpaceAfterComma', $data);
if ($fix === true) {
$phpcsFile->fixer
->addContent($comma, ' ');
}
}
else {
$spaceLength = $tokens[$comma + 1]['length'];
if ($spaceLength !== 1) {
$content = $tokens[$comma + 2]['content'];
$error = 'Expected 1 space between comma and "%s"; %s found';
$data = [
$content,
$spaceLength,
];
$fix = $phpcsFile->addFixableError($error, $comma, 'SpaceAfterComma', $data);
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($comma + 1, ' ');
}
}
}
//end if
if ($tokens[$comma - 1]['code'] === T_WHITESPACE) {
$content = $tokens[$comma - 2]['content'];
$spaceLength = $tokens[$comma - 1]['length'];
$error = 'Expected 0 spaces between "%s" and comma; %s found';
$data = [
$content,
$spaceLength,
];
$fix = $phpcsFile->addFixableError($error, $comma, 'SpaceBeforeComma', $data);
if ($fix === true) {
$phpcsFile->fixer
->replaceToken($comma - 1, '');
}
}
}
//end foreach
}
//end if
}