Skip to content

Commit a51096e

Browse files
committed
Make sure arrays are structured with one key per level.
1 parent ebbad2d commit a51096e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

PhpCollective/Sniffs/Formatting/ArrayDeclarationSniff.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,14 @@ public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, in
509509
$tokens[$searchIdx]['line'] > $tokens[$arrayStart]['line'] &&
510510
!in_array($tokens[$searchIdx]['code'], Tokens::$emptyTokens, true)
511511
) {
512-
// Extract actual indentation from the line
512+
// Extract actual indentation from the line (only leading whitespace)
513513
$lineStart = $phpcsFile->findFirstOnLine([], $searchIdx);
514514
if ($lineStart !== false && $lineStart < $searchIdx) {
515-
$baseIndent = $phpcsFile->getTokensAsString($lineStart, $searchIdx - $lineStart);
515+
$indentContent = $phpcsFile->getTokensAsString($lineStart, $searchIdx - $lineStart);
516+
// Only keep leading whitespace (tabs and spaces), remove any other characters
517+
if (preg_match('/^[\t ]*/', $indentContent, $matches)) {
518+
$baseIndent = $matches[0];
519+
}
516520
} else {
517521
// Fallback to column-based calculation
518522
$indentLevel = $tokens[$searchIdx]['column'] - 1;
@@ -571,6 +575,16 @@ public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, in
571575
}
572576

573577
$targetPtr = $p['key'] ?? $p['value'];
578+
579+
// Find any whitespace before the target token and remove it
580+
$prevToken = $targetPtr - 1;
581+
while ($prevToken >= $arrayStart && in_array($tokens[$prevToken]['code'], [T_WHITESPACE, T_COMMA], true)) {
582+
if ($tokens[$prevToken]['code'] === T_WHITESPACE) {
583+
$phpcsFile->fixer->replaceToken($prevToken, '');
584+
}
585+
$prevToken--;
586+
}
587+
574588
$phpcsFile->fixer->addContentBefore($targetPtr, "\n" . $baseIndent);
575589
}
576590
$phpcsFile->fixer->endChangeset();

0 commit comments

Comments
 (0)