Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
// Determine how far indented the entire array declaration should be.
$ignore = Tokens::$emptyTokens;
$ignore[] = T_DOUBLE_ARROW;
$ignore[] = T_COMMA;
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
$start = $phpcsFile->findStartOfStatement($prev);
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $start, true);
Expand Down Expand Up @@ -152,28 +151,27 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
$error = 'Closing brace of array declaration must be on a new line';
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceNotNewLine');
if ($fix === true) {
$padding = $phpcsFile->eolChar.str_repeat(' ', $expectedIndent);
$padding = $phpcsFile->eolChar.str_repeat(' ', $startIndent);
$phpcsFile->fixer->addContentBefore($arrayEnd, $padding);
}

return;
}

// The close brace must be indented one stop less.
$expectedIndent -= $this->indent;
$foundIndent = ($tokens[$arrayEnd]['column'] - 1);
if ($foundIndent === $expectedIndent) {
$foundIndent = ($tokens[$arrayEnd]['column'] - 1);
if ($foundIndent === $startIndent) {
return;
}

$pluralizeSpace = 's';
if ($expectedIndent === 1) {
if ($startIndent === 1) {
$pluralizeSpace = '';
}

$error = 'Array close brace not indented correctly; expected %s space%s but found %s';
$data = [
$expectedIndent,
$startIndent,
$pluralizeSpace,
$foundIndent,
];
Expand All @@ -182,7 +180,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
return;
}

$padding = str_repeat(' ', $expectedIndent);
$padding = str_repeat(' ', $startIndent);
if ($foundIndent === 0) {
$phpcsFile->fixer->addContentBefore($arrayEnd, $padding);
} else {
Expand Down
3 changes: 3 additions & 0 deletions src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,6 @@ $var = [
];

// phpcs:set Generic.Arrays.ArrayIndent indent 4

$array = [1,
];
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,6 @@ $var = [
];

// phpcs:set Generic.Arrays.ArrayIndent indent 4

$array = [1,
];