Skip to content

Commit 89863d3

Browse files
committed
Merge branch 'improvements/sniff-to-check-array-autovivification' of github.com:anzin/magento-coding-standard into improvements/sniff-to-check-array-autovivification
2 parents f4974da + 437e157 commit 89863d3

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Sniffs\PHP;
9+
10+
use PHP_CodeSniffer\Files\File;
11+
use PHP_CodeSniffer\Sniffs\Sniff;
12+
13+
/**
14+
* Sniff to validate array autovivification.
15+
*/
16+
class ArrayAutovivificationSniff implements Sniff
17+
{
18+
/**
19+
* String representation of error.
20+
*
21+
* @var string
22+
*/
23+
private $warningMessage = 'Deprecated: Automatic conversion of false to array is deprecated.';
24+
25+
/**
26+
* Warning violation code.
27+
*
28+
* @var string
29+
*/
30+
private $warningCode = 'Autovivification';
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function register(): array
36+
{
37+
return [
38+
T_VARIABLE
39+
];
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function process(File $phpcsFile, $stackPtr): void
46+
{
47+
$positionSquareBracket = $phpcsFile->findNext(T_OPEN_SQUARE_BRACKET, $stackPtr, $stackPtr + 2);
48+
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']);
55+
56+
arsort($propertyTokenKey);
57+
58+
foreach ($propertyTokenKey as $tokenKey) {
59+
if ($tokens[$tokenKey + 2]['content'] === '=') {
60+
if ($tokens[$tokenKey + 4]['content'] != 'false') {
61+
return;
62+
}
63+
64+
$phpcsFile->addWarning($this->warningMessage, $positionSquareBracket, $this->warningCode);
65+
}
66+
}
67+
}
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\PHP;
9+
10+
/**
11+
* Class to test array avtovivification.
12+
*/
13+
class Avtovivification
14+
{
15+
/**
16+
* @return array
17+
*/
18+
public function testNullAvtovivification()
19+
{
20+
$productIds = null;
21+
22+
$productIds[] = 'test_array_value';
23+
24+
return $productIds;
25+
}
26+
27+
/**
28+
* @return array
29+
*/
30+
public function testArrayAvtovivification()
31+
{
32+
$productIds = [];
33+
34+
$productIds[] = 'test_array_value';
35+
36+
return $productIds;
37+
}
38+
39+
/**
40+
* @return array
41+
*/
42+
public function testFalseAvtovivification()
43+
{
44+
$productIds = false;
45+
46+
$productIds[] = 'test_array_value';
47+
48+
return $productIds;
49+
}
50+
51+
/**
52+
* @return array
53+
*/
54+
public function testUndefineAvtovivification()
55+
{
56+
$productIds[] = 'test_array_value';
57+
58+
return $productIds;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Tests\PHP;
9+
10+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
11+
12+
class ArrayAutovivificationUnitTest extends AbstractSniffUnitTest
13+
{
14+
/**
15+
* @inheritdoc
16+
*/
17+
public function getErrorList(): array
18+
{
19+
return [];
20+
}
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function getWarningList($testFile = ''): array
26+
{
27+
if ($testFile === 'ArrayAutovivificationUnitTest.inc') {
28+
return [
29+
46 => 1
30+
];
31+
}
32+
33+
return [];
34+
}
35+
}

Magento2/ruleset.xml

+5
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@
400400
<type>warning</type>
401401
<exclude-pattern>*\.xml$</exclude-pattern>
402402
</rule>
403+
<rule ref="Magento2.PHP.ArrayAutovivification">
404+
<severity>7</severity>
405+
<type>warning</type>
406+
<exclude-pattern>*\.xml$</exclude-pattern>
407+
</rule>
403408
<rule ref="Magento2.Performance.ForeachArrayMerge">
404409
<severity>7</severity>
405410
<type>warning</type>

0 commit comments

Comments
 (0)