|
24 | 24 | */ |
25 | 25 | class ArrayDeclarationSniff implements Sniff |
26 | 26 | { |
| 27 | + /** |
| 28 | + * Controls when multi-line indentation rules are applied. |
| 29 | + * |
| 30 | + * Options: |
| 31 | + * - 'assoc' (default): Only enforce one item per line for associative arrays |
| 32 | + * - 'all': Enforce one item per line for all arrays (both associative and indexed) |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + public string $multiLineIndentationMode = 'assoc'; |
| 37 | + |
27 | 38 | /** |
28 | 39 | * @inheritDoc |
29 | 40 | */ |
@@ -331,7 +342,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $array |
331 | 342 | } |
332 | 343 | } |
333 | 344 |
|
334 | | - public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, int $arrayEnd): void |
| 345 | + protected function processMultiLineIndentation(File $phpcsFile, int $arrayStart, int $arrayEnd): void |
335 | 346 | { |
336 | 347 | $tokens = $phpcsFile->getTokens(); |
337 | 348 | $pairs = []; |
@@ -414,6 +425,7 @@ public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, in |
414 | 425 | 'value' => $valueStart, |
415 | 426 | 'value_end' => $valueEnd, |
416 | 427 | 'line' => $tokens[$keyStart]['line'], |
| 428 | + 'is_associative' => true, |
417 | 429 | ]; |
418 | 430 |
|
419 | 431 | $i = $phpcsFile->findNext([T_COMMA], $valueEnd + 1, $arrayEnd); |
@@ -466,6 +478,7 @@ public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, in |
466 | 478 | 'value' => $i, |
467 | 479 | 'value_end' => $valueEnd, |
468 | 480 | 'line' => $tokens[$i]['line'], |
| 481 | + 'is_associative' => false, |
469 | 482 | ]; |
470 | 483 |
|
471 | 484 | $i = $phpcsFile->findNext([T_COMMA], $valueEnd + 1, $arrayEnd); |
@@ -494,7 +507,31 @@ public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, in |
494 | 507 | continue; |
495 | 508 | } |
496 | 509 |
|
| 510 | + // Check if we should process these items based on configuration |
| 511 | + $shouldProcess = false; |
| 512 | + if ($this->multiLineIndentationMode === 'all') { |
| 513 | + $shouldProcess = true; |
| 514 | + } else { |
| 515 | + // In 'assoc' mode, only process if at least one item on this line is associative |
| 516 | + foreach ($items as $item) { |
| 517 | + if ($item['is_associative']) { |
| 518 | + $shouldProcess = true; |
| 519 | + |
| 520 | + break; |
| 521 | + } |
| 522 | + } |
| 523 | + } |
| 524 | + |
| 525 | + if (!$shouldProcess) { |
| 526 | + continue; |
| 527 | + } |
| 528 | + |
497 | 529 | foreach ($items as $i => $pair) { |
| 530 | + // In 'assoc' mode, only flag associative items |
| 531 | + if ($this->multiLineIndentationMode === 'assoc' && !$pair['is_associative']) { |
| 532 | + continue; |
| 533 | + } |
| 534 | + |
498 | 535 | $ptr = $pair['key'] ?? $pair['value']; |
499 | 536 | $error = 'Each array item must be on its own line in a multi-line array'; |
500 | 537 | $fix = $phpcsFile->addFixableError($error, $ptr, 'MultipleItemsPerLine'); |
@@ -574,6 +611,11 @@ public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, in |
574 | 611 | continue; |
575 | 612 | } |
576 | 613 |
|
| 614 | + // In 'assoc' mode, only fix associative items |
| 615 | + if ($this->multiLineIndentationMode === 'assoc' && !$p['is_associative']) { |
| 616 | + continue; |
| 617 | + } |
| 618 | + |
577 | 619 | $targetPtr = $p['key'] ?? $p['value']; |
578 | 620 |
|
579 | 621 | // Find any whitespace before the target token and remove it |
|
0 commit comments