Skip to content

Commit

Permalink
Generic/ArrayIndent: avoid extra calls to ArrayIndent::processMultiLi…
Browse files Browse the repository at this point in the history
…neArray()

This commit fixes a small inefficiency when fixing the
`CloseBraceNotNewLine` error. Previously, the sniff would add the new
line and the number of spaces used for an array element (which is more
tha what should be used for the array closing brace). Then on a
subsequent call to processMultiLineArray(), it would detect the wrong
number of spaces for the closing brace and it would fix it. Now, the
sniff will use the correct number of spaces when adding the newline.
  • Loading branch information
rodrigoprimo committed May 9, 2024
1 parent 16e9b04 commit 12645d7
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,20 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
}
}//end foreach

$expectedCloseBraceIndent = ($expectedElementIndent - $this->indent);
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($arrayEnd - 1), null, true);
if ($tokens[$prev]['line'] === $tokens[$arrayEnd]['line']) {
$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(' ', $expectedElementIndent);
$padding = $phpcsFile->eolChar.str_repeat(' ', $expectedCloseBraceIndent);
$phpcsFile->fixer->addContentBefore($arrayEnd, $padding);
}

return;
}

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

0 comments on commit 12645d7

Please sign in to comment.