diff --git a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php index 89cd7bd57f..efad97e4b3 100644 --- a/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php +++ b/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php @@ -45,6 +45,26 @@ public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); + // Prevent acting on short lists inside a foreach (see + // https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/527). + if ($tokens[$stackPtr]['code'] === T_OPEN_SHORT_ARRAY + && isset($tokens[$stackPtr]['nested_parenthesis']) === true + ) { + $nestedParens = $tokens[$stackPtr]['nested_parenthesis']; + $lastParenthesisCloser = end($nestedParens); + $lastParenthesisOpener = key($nestedParens); + + if (isset($tokens[$lastParenthesisCloser]['parenthesis_owner']) === true + && $tokens[$tokens[$lastParenthesisCloser]['parenthesis_owner']]['code'] === T_FOREACH + ) { + $asKeyword = $phpcsFile->findNext(T_AS, ($lastParenthesisOpener + 1), $lastParenthesisCloser); + + if ($asKeyword !== false && $asKeyword < $stackPtr) { + return; + } + } + } + if ($tokens[$stackPtr]['code'] === T_ARRAY) { $phpcsFile->recordMetric($stackPtr, 'Short array syntax used', 'no'); diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc index cdbfff6c33..6a25ab9072 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc @@ -19,7 +19,7 @@ class TestClass 'height' => '', ); - private $_bad = Array( + private $_bad = ARRAY( 'width' => '', 'height' => '' ); @@ -547,3 +547,14 @@ $x = array( 1, static::helloWorld(), $class instanceof static, 2, ); + +$noSpaceBeforeDoubleArrow = array( + 'width'=> '', + 'height' => '', + ); + +$newlineAfterDoubleArrow = array( + 'width' => + '', + 'height' => '', + ); diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed index 6f8fe216a3..048f898c8d 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.1.inc.fixed @@ -586,3 +586,13 @@ $x = array( $class instanceof static, 2, ); + +$noSpaceBeforeDoubleArrow = array( + 'width' => '', + 'height' => '', + ); + +$newlineAfterDoubleArrow = array( + 'width' => '', + 'height' => '', + ); diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc index 90b026f023..415042d89e 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc @@ -536,3 +536,20 @@ $x = [ 1, static::helloWorld(), $class instanceof static, 2, ]; + +$noSpaceBeforeDoubleArrow = [ + 'width'=> '', + 'height' => '', + ]; + +$newlineAfterDoubleArrow = [ + 'width' => + '', + 'height' => '', + ]; + +// Sniff should ignore short lists when inside a foreach. +// https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/527 +foreach ($data as [, , $value]) {} +foreach ($array as $k => [$v1, , $v3]) {} +foreach ([$a ,$b] as $c) {} // Not a short list. Sniff should handle it. diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed index 533be16a65..d835064ba1 100644 --- a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.2.inc.fixed @@ -573,3 +573,19 @@ $x = [ $class instanceof static, 2, ]; + +$noSpaceBeforeDoubleArrow = [ + 'width' => '', + 'height' => '', + ]; + +$newlineAfterDoubleArrow = [ + 'width' => '', + 'height' => '', + ]; + +// Sniff should ignore short lists when inside a foreach. +// https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/527 +foreach ($data as [, , $value]) {} +foreach ($array as $k => [$v1, , $v3]) {} +foreach ([$a, $b] as $c) {} // Not a short list. Sniff should handle it. diff --git a/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.3.inc b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.3.inc new file mode 100644 index 0000000000..beb5ae1aec --- /dev/null +++ b/src/Standards/Squiz/Tests/Arrays/ArrayDeclarationUnitTest.3.inc @@ -0,0 +1,7 @@ + 1, 540 => 1, 547 => 2, + 552 => 1, + 557 => 1, ]; case 'ArrayDeclarationUnitTest.2.inc': return [ @@ -229,7 +231,12 @@ public function getErrorList($testFile='') 526 => 1, 529 => 1, 536 => 2, + 541 => 1, + 546 => 1, + 555 => 2, ]; + case 'ArrayDeclarationUnitTest.4.inc': + return [8 => 1]; default: return []; }//end switch