Skip to content
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
69 changes: 69 additions & 0 deletions Magento2/Sniffs/PHP/ArrayAutovivificationSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Sniffs\PHP;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

/**
* Sniff to validate array autovivification.
*/
class ArrayAutovivificationSniff implements Sniff
{
/**
* String representation of error.
*
* @var string
*/
private $warningMessage = 'Deprecated: Automatic conversion of false to array is deprecated.';

/**
* Warning violation code.
*
* @var string
*/
private $warningCode = 'Autovivification';

/**
* @inheritdoc
*/
public function register(): array
{
return [
T_VARIABLE
];
}

/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr): void
{
$positionSquareBracket = $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']);

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);
}
}
}
}
}
60 changes: 60 additions & 0 deletions Magento2/Tests/PHP/ArrayAutovivificationUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Tests\PHP;

/**
* Class to test array avtovivification.
*/
class Avtovivification
{
/**
* @return array
*/
public function testNullAvtovivification()
{
$productIds = null;

$productIds[] = 'test_array_value';

return $productIds;
}

/**
* @return array
*/
public function testArrayAvtovivification()
{
$productIds = [];

$productIds[] = 'test_array_value';

return $productIds;
}

/**
* @return array
*/
public function testFalseAvtovivification()
{
$productIds = false;

$productIds[] = 'test_array_value';

return $productIds;
}

/**
* @return array
*/
public function testUndefineAvtovivification()
{
$productIds[] = 'test_array_value';

return $productIds;
}
}
35 changes: 35 additions & 0 deletions Magento2/Tests/PHP/ArrayAutovivificationUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Tests\PHP;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class ArrayAutovivificationUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList(): array
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList($testFile = ''): array
{
if ($testFile === 'ArrayAutovivificationUnitTest.inc') {
return [
46 => 1
];
}

return [];
}
}
5 changes: 5 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,11 @@
<type>warning</type>
<exclude-pattern>*\.xml$</exclude-pattern>
</rule>
<rule ref="Magento2.PHP.ArrayAutovivification">
<severity>7</severity>
<type>warning</type>
<exclude-pattern>*\.xml$</exclude-pattern>
</rule>
<rule ref="Magento2.Performance.ForeachArrayMerge">
<severity>7</severity>
<type>warning</type>
Expand Down