diff --git a/Magento2/Sniffs/PHP/ArrayAutovivificationSniff.php b/Magento2/Sniffs/PHP/ArrayAutovivificationSniff.php index f5893a3d..852aed30 100644 --- a/Magento2/Sniffs/PHP/ArrayAutovivificationSniff.php +++ b/Magento2/Sniffs/PHP/ArrayAutovivificationSniff.php @@ -20,14 +20,14 @@ class ArrayAutovivificationSniff implements Sniff * * @var string */ - private $warningMessage = 'Deprecated: Automatic conversion of false to array is deprecated.'; + private const WARNING_MESSAGE = 'Deprecated: Automatic conversion of false to array is deprecated.'; /** - * Warning violation code. + * Error violation code. * * @var string */ - private $warningCode = 'Autovivification'; + private const WARNING_CODE = 'Autovivification'; /** * @inheritdoc @@ -44,25 +44,37 @@ public function register(): array */ public function process(File $phpcsFile, $stackPtr): void { - $positionSquareBracket = $phpcsFile->findNext(T_OPEN_SQUARE_BRACKET, $stackPtr, $stackPtr + 2); + $openSquareBracketKey = $phpcsFile->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']); + if (!$openSquareBracketKey) { + return; + } + + $closeSquareBracketKey = $phpcsFile->findNext(T_CLOSE_SQUARE_BRACKET, $openSquareBracketKey); + $hasEqualKey = $phpcsFile->findNext(T_EQUAL, $closeSquareBracketKey, $closeSquareBracketKey + 3); + + if (!$hasEqualKey) { + return; + } - arsort($propertyTokenKey); + $tokens = $phpcsFile->getTokens(); + $functionKey = $phpcsFile->findPrevious(T_FUNCTION, $openSquareBracketKey) ?: 0; + $sliceToken = array_slice(array_column($tokens, 'content'), $functionKey, $stackPtr - $functionKey, true); + $propertyTokenKey = array_keys($sliceToken, $tokens[$stackPtr]['content']); - foreach ($propertyTokenKey as $tokenKey) { - if ($tokens[$tokenKey + 2]['content'] === '=') { - if ($tokens[$tokenKey + 4]['content'] != 'false') { - return; - } + arsort($propertyTokenKey); - $phpcsFile->addWarning($this->warningMessage, $positionSquareBracket, $this->warningCode); + foreach ($propertyTokenKey as $propertyKey) { + $positionEqualKey = $phpcsFile->findNext(T_EQUAL, $propertyKey, $propertyKey + 3); + + if ($positionEqualKey) { + $falseKey = $phpcsFile->findNext(T_FALSE, $positionEqualKey, $positionEqualKey + 3); + + if (!($falseKey && $phpcsFile->findNext(T_SEMICOLON, $falseKey, $falseKey + 2))) { + return; } + + $phpcsFile->addWarning(self::WARNING_MESSAGE, $openSquareBracketKey, self::WARNING_CODE); } } } diff --git a/Magento2/Tests/PHP/ArrayAutovivificationUnitTest.inc b/Magento2/Tests/PHP/ArrayAutovivificationUnitTest.inc index 82b92c00..72e57499 100644 --- a/Magento2/Tests/PHP/ArrayAutovivificationUnitTest.inc +++ b/Magento2/Tests/PHP/ArrayAutovivificationUnitTest.inc @@ -57,4 +57,44 @@ class Avtovivification return $productIds; } + + /** + * @return array + */ + public function testAvtovivification($testData) + { + $productIds = false !== $testData ? $testData : null; + + $productIds[] = 'test_array_value'; + + return $productIds; + } + + /** + * @param array $productIds + * + * @return array + */ + public function testWithParameterArray($productIds) + { + if (!empty($productIds['test_array_key'])) { + $productIds['test_array_key'] = 'test_array_value'; + } + + return $productIds; + } + + /** + * @param false|array $productIds + * + * @return false|array + */ + public function testWithParameterFalse($productIds = false) + { + if ($productIds !== false) { + $productIds[] = 'test_array_value'; + } + + return $productIds; + } }