Skip to content

Commit 437e157

Browse files
committed
Created Unit Test for array autovivification sniff
1 parent f75830d commit 437e157

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

Diff for: Magento2/Sniffs/PHP/ArrayAutovivificationSniff.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,15 @@ public function process(File $phpcsFile, $stackPtr): void
4848

4949
if ($positionSquareBracket) {
5050
$tokens = $phpcsFile->getTokens();
51-
$propertyTokenKey = array_keys(array_column($tokens, 'content'), $tokens[$stackPtr]['content']);
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']);
5255

5356
arsort($propertyTokenKey);
5457

5558
foreach ($propertyTokenKey as $tokenKey) {
56-
if ($tokenKey < $stackPtr && $tokens[$tokenKey + 2]['content'] === '=') {
59+
if ($tokens[$tokenKey + 2]['content'] === '=') {
5760
if ($tokens[$tokenKey + 4]['content'] != 'false') {
5861
return;
5962
}

Diff for: Magento2/Tests/PHP/ArrayAutovivificationUnitTest.inc

+60
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+
}

Diff for: Magento2/Tests/PHP/ArrayAutovivificationUnitTest.php

+35
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+
}

Diff for: 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)