Skip to content

Commit 6c31390

Browse files
committed
Try to remove whitespace when fixing single line array
Also refactor whitespace a little
1 parent 2914011 commit 6c31390

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,13 @@ public function processSingleLineArray($phpcsFile, $stackPtr, $arrayStart, $arra
243243
if ($fix === true) {
244244
$phpcsFile->fixer->beginChangeset();
245245
$phpcsFile->fixer->addNewline($arrayStart);
246-
$phpcsFile->fixer->addNewlineBefore($arrayEnd);
246+
247+
if ($tokens[($arrayEnd - 1)]['code'] === T_WHITESPACE) {
248+
$phpcsFile->fixer->replaceToken(($arrayEnd - 1), $phpcsFile->eolChar);
249+
} else {
250+
$phpcsFile->fixer->addNewlineBefore($arrayEnd);
251+
}
252+
247253
$phpcsFile->fixer->endChangeset();
248254
}
249255

@@ -394,9 +400,7 @@ public function processMultiLineArray($phpcsFile, $stackPtr, $arrayStart, $array
394400
continue;
395401
}//end if
396402

397-
if ($tokens[$nextToken]['code'] !== T_DOUBLE_ARROW
398-
&& $tokens[$nextToken]['code'] !== T_COMMA
399-
) {
403+
if ($tokens[$nextToken]['code'] !== T_DOUBLE_ARROW && $tokens[$nextToken]['code'] !== T_COMMA) {
400404
continue;
401405
}
402406

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,12 @@ $a = array
370370
// comment
371371
(
372372
'a',
373-
'b',
373+
'b',
374374
);
375375

376376
$a = array /* comment */ (
377377
'a',
378-
'b',
378+
'b',
379379
);
380380

381381
$x = array('a' => false);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ $listItems[$aliasPath] = [
179179
'itemContent' => implode('<br/>', $aliases)
180180
];
181181

182-
$x =
182+
$x =
183183
[
184184
$x,
185185
$y,

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ $listItems[$aliasPath] = [
186186
'itemContent' => implode('<br/>', $aliases),
187187
];
188188

189-
$x =
189+
$x =
190190
[
191191
$x,
192192
$y,

0 commit comments

Comments
 (0)