Skip to content

Commit

Permalink
Fixed bug #2882 : Generic.Arrays.ArrayIndent can request close brace …
Browse files Browse the repository at this point in the history
…indent to be less than the statement indent level
  • Loading branch information
gsherwood committed Aug 19, 2020
1 parent cee37ff commit 0425342
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
37 changes: 35 additions & 2 deletions src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,41 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
{
$tokens = $phpcsFile->getTokens();

$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
$expectedIndent = ($tokens[$first]['column'] - 1 + $this->indent);
// Determine how far indented the entire array declaration should be.
$ignore = Tokens::$emptyTokens;
$ignore[] = T_DOUBLE_ARROW;
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
$start = $phpcsFile->findStartOfStatement($prev);
$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $start, true);
$baseIndent = ($tokens[$first]['column'] - 1);

$first = $phpcsFile->findFirstOnLine(T_WHITESPACE, $stackPtr, true);
$startIndent = ($tokens[$first]['column'] - 1);

// If the open brace is not indented to at least to the level of the start
// of the statement, the sniff will conflict with other sniffs trying to
// check indent levels because it's not valid. But we don't enforce exactly
// how far indented it should be.
if ($startIndent < $baseIndent) {
$error = 'Array open brace not indented correctly; expected at least %s spaces but found %s';
$data = [
$baseIndent,
$startIndent,
];
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'OpenBraceIncorrect', $data);
if ($fix === true) {
$padding = str_repeat(' ', $baseIndent);
if ($startIndent === 0) {
$phpcsFile->fixer->addContentBefore($first, $padding);
} else {
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
}
}

return;
}//end if

$expectedIndent = ($startIndent + $this->indent);

foreach ($indices as $index) {
if (isset($index['index_start']) === true) {
Expand Down
7 changes: 7 additions & 0 deletions src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ $array = [
$c ? $d : $e,
];

$foo =
[
'bar' =>
[
],
];

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

$var = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ $array = [
$c ? $d : $e,
];

$foo =
[
'bar' =>
[
],
];

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

$var = [
Expand Down
7 changes: 4 additions & 3 deletions src/Standards/Generic/Tests/Arrays/ArrayIndentUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function getErrorList()
62 => 1,
63 => 1,
69 => 1,
70 => 1,
71 => 1,
72 => 1,
76 => 1,
77 => 1,
78 => 1,
79 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit 0425342

Please sign in to comment.