Skip to content

Commit 0d969c9

Browse files
authored
Merge pull request #748 from rodrigoprimo/fix-array-declaration-short-list-bug
Squiz/ArrayDeclaration: fixes false positive when handling short lists inside `foreach`
2 parents aab0862 + 8e0074a commit 0d969c9

9 files changed

+105
-1
lines changed

src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ public function process(File $phpcsFile, $stackPtr)
4545
{
4646
$tokens = $phpcsFile->getTokens();
4747

48+
// Prevent acting on short lists inside a foreach (see
49+
// https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/527).
50+
if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY
51+
&& isset($tokens[$stackPtr]['nested_parenthesis']) === true
52+
) {
53+
$nestedParens = $tokens[$stackPtr]['nested_parenthesis'];
54+
$lastParenthesisCloser = end($nestedParens);
55+
$lastParenthesisOpener = key($nestedParens);
56+
57+
if (isset($tokens[$lastParenthesisCloser]['parenthesis_owner']) === true
58+
&& $tokens[$tokens[$lastParenthesisCloser]['parenthesis_owner']]['code'] === T_FOREACH
59+
) {
60+
$asKeyword = $phpcsFile->findNext(T_AS, ($lastParenthesisOpener + 1), $lastParenthesisCloser);
61+
62+
if ($asKeyword !== false && $asKeyword < $stackPtr) {
63+
return;
64+
}
65+
}
66+
}
67+
4868
if ($tokens[$stackPtr]['code'] === T_ARRAY) {
4969
$phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no');
5070

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TestClass
1919
'height' => '',
2020
);
2121

22-
private $_bad = Array(
22+
private $_bad = ARRAY(
2323
'width' => '',
2424
'height' => ''
2525
);
@@ -547,3 +547,14 @@ $x = array(
547547
1, static::helloWorld(), $class instanceof static,
548548
2,
549549
);
550+
551+
$noSpaceBeforeDoubleArrow = array(
552+
'width'=> '',
553+
'height' => '',
554+
);
555+
556+
$newlineAfterDoubleArrow = array(
557+
'width' =>
558+
'',
559+
'height' => '',
560+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,3 +586,13 @@ $x = array(
586586
$class instanceof static,
587587
2,
588588
);
589+
590+
$noSpaceBeforeDoubleArrow = array(
591+
'width' => '',
592+
'height' => '',
593+
);
594+
595+
$newlineAfterDoubleArrow = array(
596+
'width' => '',
597+
'height' => '',
598+
);

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,20 @@ $x = [
536536
1, static::helloWorld(), $class instanceof static,
537537
2,
538538
];
539+
540+
$noSpaceBeforeDoubleArrow = [
541+
'width'=> '',
542+
'height' => '',
543+
];
544+
545+
$newlineAfterDoubleArrow = [
546+
'width' =>
547+
'',
548+
'height' => '',
549+
];
550+
551+
// Sniff should ignore short lists when inside a foreach.
552+
// https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/527
553+
foreach ($data as [, , $value]) {}
554+
foreach ($array as $k => [$v1, , $v3]) {}
555+
foreach ([$a ,$b] as $c) {} // Not a short list. Sniff should handle it.

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,19 @@ $x = [
573573
$class instanceof static,
574574
2,
575575
];
576+
577+
$noSpaceBeforeDoubleArrow = [
578+
'width' => '',
579+
'height' => '',
580+
];
581+
582+
$newlineAfterDoubleArrow = [
583+
'width' => '',
584+
'height' => '',
585+
];
586+
587+
// Sniff should ignore short lists when inside a foreach.
588+
// https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/527
589+
foreach ($data as [, , $value]) {}
590+
foreach ($array as $k => [$v1, , $v3]) {}
591+
foreach ([$a, $b] as $c) {} // Not a short list. Sniff should handle it.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (missing closing parenthesis).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
$array = array(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Intentional parse error (missing T_AS in foreach).
4+
// This should be the only test in this file.
5+
// Testing that the code preventing the sniff to act on short lists inside a foreach doesn't
6+
// interfere with the rest of sniff when the `as` keyword is missing.
7+
8+
foreach ([$a , $b])
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Intentional parse error (missing T_AS in foreach).
4+
// This should be the only test in this file.
5+
// Testing that the code preventing the sniff to act on short lists inside a foreach doesn't
6+
// interfere with the rest of sniff when the `as` keyword is missing.
7+
8+
foreach ([$a, $b])

src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ public function getErrorList($testFile='')
136136
537 => 1,
137137
540 => 1,
138138
547 => 2,
139+
552 => 1,
140+
557 => 1,
139141
];
140142
case 'ArrayDeclarationUnitTest.2.inc':
141143
return [
@@ -229,7 +231,12 @@ public function getErrorList($testFile='')
229231
526 => 1,
230232
529 => 1,
231233
536 => 2,
234+
541 => 1,
235+
546 => 1,
236+
555 => 2,
232237
];
238+
case 'ArrayDeclarationUnitTest.4.inc':
239+
return [8 => 1];
233240
default:
234241
return [];
235242
}//end switch

0 commit comments

Comments
 (0)