Skip to content

Commit d6fec81

Browse files
committed
Fix false positive.
1 parent 32bc36a commit d6fec81

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

PhpCollective/Sniffs/Formatting/ArrayDeclarationSniff.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ protected function processMultiLineIndentation(File $phpcsFile, int $arrayStart,
363363

364364
continue;
365365
}
366+
// Note: Nested arrays are processed separately by their own process() call,
367+
// so we should skip them here to avoid double-processing
366368
if ($token['code'] === T_OPEN_SHORT_ARRAY && isset($token['bracket_closer'])) {
367369
$i = $token['bracket_closer'] + 1;
368370

@@ -440,6 +442,15 @@ protected function processMultiLineIndentation(File $phpcsFile, int $arrayStart,
440442

441443
// Handle single value (non-associative)
442444
if ($token['code'] !== T_COMMA) {
445+
// Check if this might be a key by looking ahead for =>
446+
$nextNonEmpty = $phpcsFile->findNext(Tokens::$emptyTokens, $i + 1, $arrayEnd, true);
447+
if ($nextNonEmpty !== false && $tokens[$nextNonEmpty]['code'] === T_DOUBLE_ARROW) {
448+
// This is actually a key, not a standalone value
449+
// The T_DOUBLE_ARROW will be handled in the next iteration
450+
$i++;
451+
452+
continue;
453+
}
443454
// Find the end of this value expression (handles function calls, etc.)
444455
$valueEnd = $i;
445456
$depth = 0;
@@ -487,6 +498,7 @@ protected function processMultiLineIndentation(File $phpcsFile, int $arrayStart,
487498
}
488499
$i++;
489500
} else {
501+
// Skip the comma and continue
490502
$i++;
491503
}
492504
}

tests/_data/ArrayDeclaration/after.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public function test(): void
1313
'z' => 'a',
1414
],
1515
'c' => __FILE__,
16-
'd' => Xyz::class,
17-
'r',
16+
'd' => Xyz::class, 'r',
1817
'content' => $this->getContent($tokens, $i, $tagEnd),
1918
];
2019
}

0 commit comments

Comments
 (0)