Skip to content

Commit 84cac76

Browse files
committed
Add none option or array declaration.
1 parent d6fec81 commit 84cac76

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

PhpCollective/Sniffs/Formatting/ArrayDeclarationSniff.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ArrayDeclarationSniff implements Sniff
3030
* Options:
3131
* - 'assoc' (default): Only enforce one item per line for associative arrays
3232
* - 'all': Enforce one item per line for all arrays (both associative and indexed)
33+
* - 'none': Disable multi-line indentation checks
3334
*
3435
* @var string
3536
*/
@@ -344,8 +345,16 @@ public function processMultiLineArray(File $phpcsFile, int $stackPtr, int $array
344345

345346
protected function processMultiLineIndentation(File $phpcsFile, int $arrayStart, int $arrayEnd): void
346347
{
348+
// Early return if mode is 'none'
349+
if ($this->multiLineIndentationMode === 'none') {
350+
return;
351+
}
352+
347353
$tokens = $phpcsFile->getTokens();
348354
$pairs = [];
355+
356+
// Debug: identify which array we're processing
357+
// file_put_contents('/tmp/array_debug.log', "\nProcessing array from {$tokens[$arrayStart]['line']} to {$tokens[$arrayEnd]['line']}\n", FILE_APPEND);
349358

350359
$i = $arrayStart + 1;
351360
while ($i < $arrayEnd) {
@@ -403,6 +412,18 @@ protected function processMultiLineIndentation(File $phpcsFile, int $arrayStart,
403412
continue;
404413
}
405414

415+
// Handle nested arrays - if we hit an array opener, skip to its closer
416+
if ($currentToken['code'] === T_OPEN_SHORT_ARRAY && isset($currentToken['bracket_closer'])) {
417+
$valueEnd = $currentToken['bracket_closer'];
418+
$j = $valueEnd;
419+
continue;
420+
}
421+
if ($currentToken['code'] === T_ARRAY && isset($currentToken['parenthesis_closer'])) {
422+
$valueEnd = $currentToken['parenthesis_closer'];
423+
$j = $valueEnd;
424+
continue;
425+
}
426+
406427
// Track parentheses depth
407428
if ($currentToken['code'] === T_OPEN_PARENTHESIS) {
408429
$depth++;
@@ -465,6 +486,18 @@ protected function processMultiLineIndentation(File $phpcsFile, int $arrayStart,
465486
continue;
466487
}
467488

489+
// Handle nested arrays - if we hit an array opener, skip to its closer
490+
if ($currentToken['code'] === T_OPEN_SHORT_ARRAY && isset($currentToken['bracket_closer'])) {
491+
$valueEnd = $currentToken['bracket_closer'];
492+
$j = $valueEnd;
493+
continue;
494+
}
495+
if ($currentToken['code'] === T_ARRAY && isset($currentToken['parenthesis_closer'])) {
496+
$valueEnd = $currentToken['parenthesis_closer'];
497+
$j = $valueEnd;
498+
continue;
499+
}
500+
468501
// Track parentheses depth
469502
if ($currentToken['code'] === T_OPEN_PARENTHESIS) {
470503
$depth++;

tests/PhpCollective/Sniffs/Formatting/ArrayDeclarationSniffTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ArrayDeclarationSniffTest extends TestCase
1717
*/
1818
public function testDocBlockConstSniffer(): void
1919
{
20-
$this->assertSnifferFindsErrors(new ArrayDeclarationSniff(), 2);
20+
$this->assertSnifferFindsErrors(new ArrayDeclarationSniff(), 3);
2121
}
2222

2323
/**

tests/_data/ArrayDeclaration/after.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,20 @@ public function test(): void
1313
'z' => 'a',
1414
],
1515
'c' => __FILE__,
16-
'd' => Xyz::class, 'r',
16+
'd' => Xyz::class,
1717
'content' => $this->getContent($tokens, $i, $tagEnd),
1818
];
19+
20+
// Test case for nested arrays - should NOT be flagged
21+
$config = [
22+
'levels' => ['notice', 'info', 'debug'],
23+
'other' => 'value',
24+
];
25+
26+
// Multiple items with nested arrays - SHOULD be flagged
27+
$multi = [
28+
'first' => ['a', 'b'],
29+
'second' => ['c', 'd'],
30+
];
1931
}
2032
}

tests/_data/ArrayDeclaration/before.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,18 @@ public function test(): void
1111
'x' => [
1212
'y' => 'z', 'z' => 'a',
1313
],
14-
'c' => __FILE__, 'd' => Xyz::class, 'r', 'content' => $this->getContent($tokens, $i, $tagEnd),
14+
'c' => __FILE__, 'd' => Xyz::class, 'content' => $this->getContent($tokens, $i, $tagEnd),
15+
];
16+
17+
// Test case for nested arrays - should NOT be flagged
18+
$config = [
19+
'levels' => ['notice', 'info', 'debug'],
20+
'other' => 'value',
21+
];
22+
23+
// Multiple items with nested arrays - SHOULD be flagged
24+
$multi = [
25+
'first' => ['a', 'b'], 'second' => ['c', 'd'],
1526
];
1627
}
1728
}

0 commit comments

Comments
 (0)