diff --git a/Magento2/Sniffs/Performance/ForeachArrayMergeSniff.md b/Magento2/Sniffs/Performance/ForeachArrayMergeSniff.md new file mode 100644 index 00000000..90bc6211 --- /dev/null +++ b/Magento2/Sniffs/Performance/ForeachArrayMergeSniff.md @@ -0,0 +1,26 @@ +# Rule: array_merge(...) is used in a loop and is a resources greedy construction + +## Reason +Merging arrays in a loop is slow and causes high CPU usage. + +## How to Fix +Typical example when `array_merge` is being used in the loop: +``` php + $options = []; + foreach ($configurationSources as $source) { + // code here + $options = array_merge($options, $source->getOptions()); + } +``` + +In order to reduce execution time `array_merge` can be called only once: +``` php + $options = [[]]; + foreach ($configurationSources as $source) { + // code here + $options[] = $source->getOptions(); + } + + // PHP 5.6+ + $options = array_merge(...$options); +``` diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index ec705c70..999b6f20 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -31,7 +31,10 @@ 10 error + */_files/* + */Fixtures/* */Test/* + *Test.php 10 @@ -57,6 +60,8 @@ 10 error */_files/* + */Fixtures/* + */lib/* */Test/* *Test.php @@ -64,10 +69,14 @@ 10 error + + *.phtml + 10 error */_files/* + */Fixtures/* */lib/* */Test/* *Test.php @@ -103,6 +112,7 @@ 9 warning */_files/* + */Fixtures/* */lib/* */Test/* *Test.php @@ -116,6 +126,7 @@ 9 warning */_files/* + */Fixtures/* */lib/* */Test/* *Test.php @@ -134,6 +145,7 @@ 8 warning */_files/* + */Fixtures/* */Test/* *Test.php @@ -145,6 +157,7 @@ 8 warning */_files/* + */Fixtures/* */lib/* */Test/* *Test.php @@ -153,6 +166,7 @@ 8 warning */_files/* + */Fixtures/* */lib/* */Setup/* */Test/* @@ -224,6 +238,7 @@ 7 warning */_files/* + */Fixtures/* */Test/* *Test.php @@ -251,6 +266,7 @@ 7 warning */_files/* + */Fixtures/* */Test/* *Test.php @@ -266,6 +282,7 @@ 7 warning */_files/* + */Fixtures/* */Test/* *Test.php @@ -329,6 +346,7 @@ 6 warning */_files/* + */Fixtures/* */Test/* *Test.php diff --git a/composer.json b/composer.json index 643c21cd..e809b3ee 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "AFL-3.0" ], "type": "phpcodesniffer-standard", - "version": "1.0.2", + "version": "2.0.0", "require": { "php": ">=5.6.0", "squizlabs/php_codesniffer": "^3.4" diff --git a/composer.lock b/composer.lock index e75272ca..0310b0bb 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "fb234e25bac2100e5226c8304c0f0c4c", + "content-hash": "b4b39922ceb5120fe91cae1cd3d06df8", "packages": [ { "name": "squizlabs/php_codesniffer",