Skip to content

Commit 906f7a2

Browse files
committed
Generic/ArrayIndent: avoid extra calls to ArrayIndent::processMultiLineArray()
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.
1 parent 94cd19a commit 906f7a2

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/Standards/Generic/Sniffs/Arrays/ArrayIndentSniff.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,28 +151,27 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
151151
$error = 'Closing brace of array declaration must be on a new line';
152152
$fix = $phpcsFile->addFixableError($error, $arrayEnd, 'CloseBraceNotNewLine');
153153
if ($fix === true) {
154-
$padding = $phpcsFile->eolChar.str_repeat(' ', $expectedIndent);
154+
$padding = $phpcsFile->eolChar.str_repeat(' ', $startIndent);
155155
$phpcsFile->fixer->addContentBefore($arrayEnd, $padding);
156156
}
157157

158158
return;
159159
}
160160

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

168167
$pluralizeSpace = 's';
169-
if ($expectedIndent === 1) {
168+
if ($startIndent === 1) {
170169
$pluralizeSpace = '';
171170
}
172171

173172
$error = 'Array close brace not indented correctly; expected %s space%s but found %s';
174173
$data = [
175-
$expectedIndent,
174+
$startIndent,
176175
$pluralizeSpace,
177176
$foundIndent,
178177
];
@@ -181,7 +180,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
181180
return;
182181
}
183182

184-
$padding = str_repeat(' ', $expectedIndent);
183+
$padding = str_repeat(' ', $startIndent);
185184
if ($foundIndent === 0) {
186185
$phpcsFile->fixer->addContentBefore($arrayEnd, $padding);
187186
} else {

0 commit comments

Comments
 (0)