Skip to content

Fixed code array autovivification #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions Magento2/Sniffs/PHP/ArrayAutovivificationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions Magento2/Tests/PHP/ArrayAutovivificationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}