Skip to content

Commit a0e4677

Browse files
authored
Merge pull request #121 from magento-commerce/improvements/sniff-to-check-array-autovivification-fix
Fixed code array autovivification
2 parents 159e0fc + 795a005 commit a0e4677

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

Magento2/Sniffs/PHP/ArrayAutovivificationSniff.php

+29-17
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ class ArrayAutovivificationSniff implements Sniff
2020
*
2121
* @var string
2222
*/
23-
private $warningMessage = 'Deprecated: Automatic conversion of false to array is deprecated.';
23+
private const WARNING_MESSAGE = 'Deprecated: Automatic conversion of false to array is deprecated.';
2424

2525
/**
26-
* Warning violation code.
26+
* Error violation code.
2727
*
2828
* @var string
2929
*/
30-
private $warningCode = 'Autovivification';
30+
private const WARNING_CODE = 'Autovivification';
3131

3232
/**
3333
* @inheritdoc
@@ -44,25 +44,37 @@ public function register(): array
4444
*/
4545
public function process(File $phpcsFile, $stackPtr): void
4646
{
47-
$positionSquareBracket = $phpcsFile->findNext(T_OPEN_SQUARE_BRACKET, $stackPtr, $stackPtr + 2);
47+
$openSquareBracketKey = $phpcsFile->findNext(T_OPEN_SQUARE_BRACKET, $stackPtr, $stackPtr + 2);
4848

49-
if ($positionSquareBracket) {
50-
$tokens = $phpcsFile->getTokens();
51-
$positionFunction = $phpcsFile->findPrevious(T_FUNCTION, $positionSquareBracket) ?: 0;
52-
$sliceLength = $stackPtr - $positionFunction;
53-
$sliceToken = array_slice(array_column($tokens, 'content'), $positionFunction, $sliceLength, true);
54-
$propertyTokenKey = array_keys($sliceToken, $tokens[$stackPtr]['content']);
49+
if (!$openSquareBracketKey) {
50+
return;
51+
}
52+
53+
$closeSquareBracketKey = $phpcsFile->findNext(T_CLOSE_SQUARE_BRACKET, $openSquareBracketKey);
54+
$hasEqualKey = $phpcsFile->findNext(T_EQUAL, $closeSquareBracketKey, $closeSquareBracketKey + 3);
55+
56+
if (!$hasEqualKey) {
57+
return;
58+
}
5559

56-
arsort($propertyTokenKey);
60+
$tokens = $phpcsFile->getTokens();
61+
$functionKey = $phpcsFile->findPrevious(T_FUNCTION, $openSquareBracketKey) ?: 0;
62+
$sliceToken = array_slice(array_column($tokens, 'content'), $functionKey, $stackPtr - $functionKey, true);
63+
$propertyTokenKey = array_keys($sliceToken, $tokens[$stackPtr]['content']);
5764

58-
foreach ($propertyTokenKey as $tokenKey) {
59-
if ($tokens[$tokenKey + 2]['content'] === '=') {
60-
if ($tokens[$tokenKey + 4]['content'] != 'false') {
61-
return;
62-
}
65+
arsort($propertyTokenKey);
6366

64-
$phpcsFile->addWarning($this->warningMessage, $positionSquareBracket, $this->warningCode);
67+
foreach ($propertyTokenKey as $propertyKey) {
68+
$positionEqualKey = $phpcsFile->findNext(T_EQUAL, $propertyKey, $propertyKey + 3);
69+
70+
if ($positionEqualKey) {
71+
$falseKey = $phpcsFile->findNext(T_FALSE, $positionEqualKey, $positionEqualKey + 3);
72+
73+
if (!($falseKey && $phpcsFile->findNext(T_SEMICOLON, $falseKey, $falseKey + 2))) {
74+
return;
6575
}
76+
77+
$phpcsFile->addWarning(self::WARNING_MESSAGE, $openSquareBracketKey, self::WARNING_CODE);
6678
}
6779
}
6880
}

Magento2/Tests/PHP/ArrayAutovivificationUnitTest.inc

+40
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,44 @@ class Avtovivification
5757

5858
return $productIds;
5959
}
60+
61+
/**
62+
* @return array
63+
*/
64+
public function testAvtovivification($testData)
65+
{
66+
$productIds = false !== $testData ? $testData : null;
67+
68+
$productIds[] = 'test_array_value';
69+
70+
return $productIds;
71+
}
72+
73+
/**
74+
* @param array $productIds
75+
*
76+
* @return array
77+
*/
78+
public function testWithParameterArray($productIds)
79+
{
80+
if (!empty($productIds['test_array_key'])) {
81+
$productIds['test_array_key'] = 'test_array_value';
82+
}
83+
84+
return $productIds;
85+
}
86+
87+
/**
88+
* @param false|array $productIds
89+
*
90+
* @return false|array
91+
*/
92+
public function testWithParameterFalse($productIds = false)
93+
{
94+
if ($productIds !== false) {
95+
$productIds[] = 'test_array_value';
96+
}
97+
98+
return $productIds;
99+
}
60100
}

0 commit comments

Comments
 (0)