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 newline
and the number of spaces used for an array element (which is more
than 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 authored and jrfnl committed May 14, 2024
1 parent d74b846 commit 3a2230c
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,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 @@ -181,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

0 comments on commit 3a2230c

Please sign in to comment.