diff --git a/Magento2/Sniffs/PHP/ArrayAutovivificationSniff.php b/Magento2/Sniffs/PHP/ArrayAutovivificationSniff.php new file mode 100644 index 00000000..f5893a3d --- /dev/null +++ b/Magento2/Sniffs/PHP/ArrayAutovivificationSniff.php @@ -0,0 +1,69 @@ +findNext(T_OPEN_SQUARE_BRACKET, $stackPtr, $stackPtr + 2); + + if ($positionSquareBracket) { + $tokens = $phpcsFile->getTokens(); + $positionFunction = $phpcsFile->findPrevious(T_FUNCTION, $positionSquareBracket) ?: 0; + $sliceLength = $stackPtr - $positionFunction; + $sliceToken = array_slice(array_column($tokens, 'content'), $positionFunction, $sliceLength, true); + $propertyTokenKey = array_keys($sliceToken, $tokens[$stackPtr]['content']); + + arsort($propertyTokenKey); + + foreach ($propertyTokenKey as $tokenKey) { + if ($tokens[$tokenKey + 2]['content'] === '=') { + if ($tokens[$tokenKey + 4]['content'] != 'false') { + return; + } + + $phpcsFile->addWarning($this->warningMessage, $positionSquareBracket, $this->warningCode); + } + } + } + } +} diff --git a/Magento2/Tests/PHP/ArrayAutovivificationUnitTest.inc b/Magento2/Tests/PHP/ArrayAutovivificationUnitTest.inc new file mode 100644 index 00000000..82b92c00 --- /dev/null +++ b/Magento2/Tests/PHP/ArrayAutovivificationUnitTest.inc @@ -0,0 +1,60 @@ + 1 + ]; + } + + return []; + } +} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 442d9867..5ea68ff3 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -400,6 +400,11 @@ warning *\.xml$ + + 7 + warning + *\.xml$ + 7 warning