From 8e30d5a9bc32a934fdb93705a247b2b60e7b1425 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Mon, 8 Apr 2019 14:15:05 -0500 Subject: [PATCH 1/3] - added exclude for `*/Fixtures/*` files; - added `ForeachArrayMergeSniff` documentation with `HowTo Fix` example; - major version bump. --- .../Performance/ForeachArrayMergeSniff.md | 26 +++++++++++++++++++ Magento2/ruleset.xml | 14 ++++++++++ composer.json | 2 +- composer.lock | 2 +- 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 Magento2/Sniffs/Performance/ForeachArrayMergeSniff.md 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..d7ad057e 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -31,7 +31,10 @@ 10 error + */_files/* + */Fixtures/* */Test/* + *Test.php 10 @@ -57,6 +60,7 @@ 10 error */_files/* + */Fixtures/* */Test/* *Test.php @@ -68,6 +72,7 @@ 10 error */_files/* + */Fixtures/* */lib/* */Test/* *Test.php @@ -103,6 +108,7 @@ 9 warning */_files/* + */Fixtures/* */lib/* */Test/* *Test.php @@ -116,6 +122,7 @@ 9 warning */_files/* + */Fixtures/* */lib/* */Test/* *Test.php @@ -134,6 +141,7 @@ 8 warning */_files/* + */Fixtures/* */Test/* *Test.php @@ -145,6 +153,7 @@ 8 warning */_files/* + */Fixtures/* */lib/* */Test/* *Test.php @@ -153,6 +162,7 @@ 8 warning */_files/* + */Fixtures/* */lib/* */Setup/* */Test/* @@ -224,6 +234,7 @@ 7 warning */_files/* + */Fixtures/* */Test/* *Test.php @@ -251,6 +262,7 @@ 7 warning */_files/* + */Fixtures/* */Test/* *Test.php @@ -266,6 +278,7 @@ 7 warning */_files/* + */Fixtures/* */Test/* *Test.php @@ -329,6 +342,7 @@ 6 warning */_files/* + */Fixtures/* */Test/* *Test.php diff --git a/composer.json b/composer.json index 90b0f1a7..db7ee2be 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.3.0" diff --git a/composer.lock b/composer.lock index 53e29e78..869fb283 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": "dce173b9ca738da750bd8aaca1b88d6a", + "content-hash": "ac1b585821a1b7fcb6575fdc0e972a5d", "packages": [ { "name": "squizlabs/php_codesniffer", From e9cdc055f71bcfe2716cac946cb57cf3a99d53e4 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Tue, 16 Apr 2019 13:35:13 -0500 Subject: [PATCH 2/3] - added excludes --- Magento2/ruleset.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index d7ad057e..999b6f20 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -61,6 +61,7 @@ error */_files/* */Fixtures/* + */lib/* */Test/* *Test.php @@ -68,6 +69,9 @@ 10 error + + *.phtml + 10 error From 38b77579c40be3fbdc9d1dca10b69c914a97e218 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Wed, 17 Apr 2019 09:35:04 -0500 Subject: [PATCH 3/3] - resolved conflicts - version bump --- composer.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",