Skip to content

Commit 75f2c2f

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

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

PhpCollective/Sniffs/Formatting/ArrayDeclarationSniff.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
*/
2525
class ArrayDeclarationSniff implements Sniff
2626
{
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+
2738
/**
2839
* @inheritDoc
2940
*/
@@ -331,7 +342,7 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $array
331342
}
332343
}
333344

334-
public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, int $arrayEnd): void
345+
protected function processMultiLineIndentation(File $phpcsFile, int $arrayStart, int $arrayEnd): void
335346
{
336347
$tokens = $phpcsFile->getTokens();
337348
$pairs = [];
@@ -414,6 +425,7 @@ public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, in
414425
'value' => $valueStart,
415426
'value_end' => $valueEnd,
416427
'line' => $tokens[$keyStart]['line'],
428+
'is_associative' => true,
417429
];
418430

419431
$i = $phpcsFile->findNext([T_COMMA], $valueEnd + 1, $arrayEnd);
@@ -466,6 +478,7 @@ public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, in
466478
'value' => $i,
467479
'value_end' => $valueEnd,
468480
'line' => $tokens[$i]['line'],
481+
'is_associative' => false,
469482
];
470483

471484
$i = $phpcsFile->findNext([T_COMMA], $valueEnd + 1, $arrayEnd);
@@ -494,7 +507,31 @@ public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, in
494507
continue;
495508
}
496509

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+
497529
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+
498535
$ptr = $pair['key'] ?? $pair['value'];
499536
$error = 'Each array item must be on its own line in a multi-line array';
500537
$fix = $phpcsFile->addFixableError($error, $ptr, 'MultipleItemsPerLine');
@@ -574,6 +611,11 @@ public function processMultiLineIndentation(File $phpcsFile, int $arrayStart, in
574611
continue;
575612
}
576613

614+
// In 'assoc' mode, only fix associative items
615+
if ($this->multiLineIndentationMode === 'assoc' && !$p['is_associative']) {
616+
continue;
617+
}
618+
577619
$targetPtr = $p['key'] ?? $p['value'];
578620

579621
// Find any whitespace before the target token and remove it

0 commit comments

Comments
 (0)