Skip to content

Commit 9d1205c

Browse files
author
Lars Roettig
committed
#20 Impelement ArrayMerge sniff in foreach
1 parent fcf6094 commit 9d1205c

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento2\Sniffs\Performance;
8+
9+
use PHP_CodeSniffer\Files\File;
10+
use PHP_CodeSniffer\Sniffs\Sniff;
11+
12+
class ForeachArrayMergeSniff implements Sniff
13+
{
14+
/**
15+
* String representation of warning.
16+
*
17+
* @var string
18+
*/
19+
protected $warningMessage = 'Array merge is slow in each functions. ';
20+
21+
/**
22+
* Warning violation code.
23+
*
24+
* @var string
25+
*/
26+
protected $warningCode = 'ForeachArrayMerge';
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
public function register()
32+
{
33+
return [T_FOREACH, T_FOR];
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function process(File $phpcsFile, $stackPtr)
40+
{
41+
$tokens = $phpcsFile->getTokens();
42+
43+
$scopeOpener = $tokens[$stackPtr]['scope_opener'];
44+
$scopeCloser = $tokens[$stackPtr]['scope_closer'] ;
45+
46+
for ($i = $scopeOpener; $i < $scopeCloser; $i++)
47+
{
48+
$tag = $tokens[$i];
49+
if ($tag['code'] !== T_STRING) {
50+
continue;
51+
}
52+
if ($tag['content'] !== 'array_merge') {
53+
continue;
54+
}
55+
56+
$phpcsFile->addWarning($this->warningMessage, $i, $this->warningCode);
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
$configurationSources = [
3+
0 => 'value',
4+
1 => 'value2',
5+
];
6+
7+
$options = [];
8+
foreach ($configurationSources as $source) {
9+
$options = array_merge($options, $source);
10+
}
11+
12+
$options = [];
13+
$itemCount = count($configurationSources);
14+
for ($i = 0; $i <= $itemCount; $i++) {
15+
$source = $options[$itemCount];
16+
$options = array_merge($options, $source));
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento2\Tests\Performance;
7+
8+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
9+
10+
/**
11+
* Class EmptyCheckUnitTest
12+
*/
13+
class ForeachArrayMergeUnitTest extends AbstractSniffUnitTest
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function getErrorList()
19+
{
20+
return [];
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
public function getWarningList()
27+
{
28+
return [
29+
9 => 1,
30+
16 => 1,
31+
];
32+
}
33+
}

Magento2/ruleset.xml

+4
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@
230230
<severity>7</severity>
231231
<type>warning</type>
232232
</rule>
233+
<rule ref="Magento2.Performance.ForeachArrayMerge">
234+
<severity>7</severity>
235+
<type>warning</type>
236+
</rule>
233237
<rule ref="Magento2.Strings.StringConcat">
234238
<severity>7</severity>
235239
<type>warning</type>

0 commit comments

Comments
 (0)