Skip to content

#20 Impelement ArrayMerge sniff in foreach #72

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
merged 8 commits into from
Apr 8, 2019
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
71 changes: 71 additions & 0 deletions Magento2/Sniffs/Performance/ForeachArrayMergeSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Sniffs\Performance;

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

/**
* Detects array_merge(...) is used in a loop and is a resources greedy construction.
*/
class ForeachArrayMergeSniff implements Sniff
{
/**
* String representation of warning.
*
* @var string
*/
protected $warningMessage = 'array_merge(...) is used in a loop and is a resources greedy construction.';

/**
* Warning violation code.
*
* @var string
*/
protected $warningCode = 'ForeachArrayMerge';

/**
* @var array
*/
protected $foreachCache = [];

/**
* @inheritdoc
*/
public function register()
{
return [T_FOREACH, T_FOR];
}

/**
* @inheritdoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

$scopeOpener = $tokens[$stackPtr]['scope_opener'];
$scopeCloser = $tokens[$stackPtr]['scope_closer'];

for ($i = $scopeOpener; $i < $scopeCloser; $i++) {
$tag = $tokens[$i];
if ($tag['code'] !== T_STRING) {
continue;
}
if ($tag['content'] !== 'array_merge') {
continue;
}

$cacheKey = $phpcsFile->getFilename() . $i;
if (isset($this->foreachCache[$cacheKey])) {
continue;
}

$this->foreachCache[$cacheKey] = '';
$phpcsFile->addWarning($this->warningMessage, $i, $this->warningCode);
}
}
}
43 changes: 43 additions & 0 deletions Magento2/Tests/Performance/ForeachArrayMergeUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
$configurationSources = [
0 => 'value',
1 => 'value2',
];

$options = [];

foreach ([] as $collection) {
foreach ($configurationSources as $source) {
$options = array_merge($options, $source);
}
}

$options = [];
$itemCount = count($configurationSources);
for ($i = 0; $i <= $itemCount; $i++) {
$source = $options[$itemCount];
$options = array_merge($options, $source);
}

class SelectBuilder
{
private $columns = [];

public function getColumns()
{
return $this->columns;
}

public function setColumns(array $columns)
{
$this->columns = $columns;
}
}

$selectBuilder = new SelectBuilder();

foreach ([] as $collection) {
foreach ($configurationSources as $source) {
$selectBuilder->setColumns(array_merge($selectBuilder->getColumns(), $source));
}
}
35 changes: 35 additions & 0 deletions Magento2/Tests/Performance/ForeachArrayMergeUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Tests\Performance;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Class EmptyCheckUnitTest
*/
class ForeachArrayMergeUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList()
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList()
{
return [
11 => 1,
19 => 1,
41 => 1
];
}
}
4 changes: 4 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@
<severity>7</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Performance.ForeachArrayMerge">
<severity>7</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Strings.StringConcat">
<severity>7</severity>
<type>warning</type>
Expand Down