From 73099fdecc8e5ef2c5eaa4d47f5fb5cb26bb3738 Mon Sep 17 00:00:00 2001 From: Alexander Shkurko Date: Sun, 14 Jan 2018 14:59:34 +0100 Subject: [PATCH 1/3] Performance: remove count() form the condition section of a loop --- app/code/Magento/Backend/Model/Widget/Grid/Parser.php | 5 +++-- .../Catalog/Model/Layer/Filter/DataProvider/Price.php | 3 ++- .../Config/Structure/Element/Dependency/MapperTest.php | 6 ++++-- app/code/Magento/Paypal/Model/Report/Settlement.php | 3 ++- app/code/Magento/Usps/Model/Carrier.php | 3 ++- app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php | 3 ++- .../Magento/Webapi/Model/Soap/Wsdl/ComplexTypeStrategy.php | 3 ++- 7 files changed, 17 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Backend/Model/Widget/Grid/Parser.php b/app/code/Magento/Backend/Model/Widget/Grid/Parser.php index e2b77fb471acd..ea6fadf49e253 100644 --- a/app/code/Magento/Backend/Model/Widget/Grid/Parser.php +++ b/app/code/Magento/Backend/Model/Widget/Grid/Parser.php @@ -30,8 +30,9 @@ public function parseExpression($expression) $expression = trim($expression); foreach ($this->_operations as $operation) { $splittedExpr = preg_split('/\\' . $operation . '/', $expression, -1, PREG_SPLIT_DELIM_CAPTURE); - if (count($splittedExpr) > 1) { - for ($i = 0; $i < count($splittedExpr); $i++) { + if (!empty($splittedExpr)) { + $count = count($splittedExpr); + for ($i = 0; $i < $count; $i++) { $stack = array_merge($stack, $this->parseExpression($splittedExpr[$i])); if ($i > 0) { $stack[] = $operation; diff --git a/app/code/Magento/Catalog/Model/Layer/Filter/DataProvider/Price.php b/app/code/Magento/Catalog/Model/Layer/Filter/DataProvider/Price.php index 8a17a3b6c8cfa..d1aee8c4c5ba6 100644 --- a/app/code/Magento/Catalog/Model/Layer/Filter/DataProvider/Price.php +++ b/app/code/Magento/Catalog/Model/Layer/Filter/DataProvider/Price.php @@ -282,7 +282,8 @@ public function getMaxPrice() public function getPriorFilters($filterParams) { $priorFilters = []; - for ($i = 1; $i < count($filterParams); ++$i) { + $count = count($filterParams); + for ($i = 1; $i < $count; ++$i) { $priorFilter = $this->validateFilter($filterParams[$i]); if ($priorFilter) { $priorFilters[] = $priorFilter; diff --git a/app/code/Magento/Config/Test/Unit/Model/Config/Structure/Element/Dependency/MapperTest.php b/app/code/Magento/Config/Test/Unit/Model/Config/Structure/Element/Dependency/MapperTest.php index 1c758bfcbefaa..f532e3f3d64f6 100644 --- a/app/code/Magento/Config/Test/Unit/Model/Config/Structure/Element/Dependency/MapperTest.php +++ b/app/code/Magento/Config/Test/Unit/Model/Config/Structure/Element/Dependency/MapperTest.php @@ -98,7 +98,8 @@ public function testGetDependenciesWhenDependentIsInvisible($isValueSatisfy) { $expected = []; $rowData = array_values($this->_testData); - for ($i = 0; $i < count($this->_testData); ++$i) { + $count = $this->_testData; + for ($i = 0; $i < $count; ++$i) { $data = $rowData[$i]; $dependentPath = 'some path ' . $i; $field = $this->_getField( @@ -158,7 +159,8 @@ public function testGetDependenciesIsVisible() { $expected = []; $rowData = array_values($this->_testData); - for ($i = 0; $i < count($this->_testData); ++$i) { + $count = count($this->_testData); + for ($i = 0; $i < $count; ++$i) { $data = $rowData[$i]; $field = $this->_getField( true, diff --git a/app/code/Magento/Paypal/Model/Report/Settlement.php b/app/code/Magento/Paypal/Model/Report/Settlement.php index 12307977d7e36..5dc51518f0b11 100644 --- a/app/code/Magento/Paypal/Model/Report/Settlement.php +++ b/app/code/Magento/Paypal/Model/Report/Settlement.php @@ -369,7 +369,8 @@ public function parseCsv($localCsv, $format = 'new') // Section columns. // In case ever the column order is changed, we will have the items recorded properly // anyway. We have named, not numbered columns. - for ($i = 1; $i < count($line); $i++) { + $count = count($line); + for ($i = 1; $i < $count; $i++) { $sectionColumns[$line[$i]] = $i; } $flippedSectionColumns = array_flip($sectionColumns); diff --git a/app/code/Magento/Usps/Model/Carrier.php b/app/code/Magento/Usps/Model/Carrier.php index 2b46fd884f651..f56f490099bb9 100644 --- a/app/code/Magento/Usps/Model/Carrier.php +++ b/app/code/Magento/Usps/Model/Carrier.php @@ -2054,7 +2054,8 @@ protected function _parseZip($zipString, $returnFull = false) if (preg_match('/[\\d\\w]{5}\\-[\\d\\w]{4}/', $zipString) != 0) { $zip = explode('-', $zipString); } - for ($i = 0; $i < count($zip); ++$i) { + $count = count($zip); + for ($i = 0; $i < $count; ++$i) { if (strlen($zip[$i]) == 5) { $zip5 = $zip[$i]; } elseif (strlen($zip[$i]) == 4) { diff --git a/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php b/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php index 6609040b040d0..8d27ed796f437 100644 --- a/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php +++ b/app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php @@ -749,7 +749,8 @@ private function handlePrimitive($name, $prefix) private function convertPathParams($uri) { $parts = explode('/', $uri); - for ($i=0; $i < count($parts); $i++) { + $count = count($parts); + for ($i=0; $i < $count; $i++) { if (strpos($parts[$i], ':') === 0) { $parts[$i] = '{' . substr($parts[$i], 1) . '}'; } diff --git a/app/code/Magento/Webapi/Model/Soap/Wsdl/ComplexTypeStrategy.php b/app/code/Magento/Webapi/Model/Soap/Wsdl/ComplexTypeStrategy.php index 401440684165c..3884a0ef026e1 100644 --- a/app/code/Magento/Webapi/Model/Soap/Wsdl/ComplexTypeStrategy.php +++ b/app/code/Magento/Webapi/Model/Soap/Wsdl/ComplexTypeStrategy.php @@ -229,7 +229,8 @@ public function addAnnotation(\DOMElement $element, $documentation, $default = n $this->_processElementType($elementType, $documentation, $appInfoNode); if (preg_match_all('/{([a-z]+):(.+)}/Ui', $documentation, $matches)) { - for ($i = 0; $i < count($matches[0]); $i++) { + $count = count($matches[0]); + for ($i = 0; $i < $count; $i++) { $appinfoTag = $matches[0][$i]; $tagName = $matches[1][$i]; $tagValue = $matches[2][$i]; From b4eede9d69f96449e3a44a2352424a578c0f998d Mon Sep 17 00:00:00 2001 From: Alexander Shkurko Date: Mon, 15 Jan 2018 14:11:50 +0100 Subject: [PATCH 2/3] Hotfix: change the way of checking array size and condition check --- app/code/Magento/Backend/Model/Widget/Grid/Parser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Backend/Model/Widget/Grid/Parser.php b/app/code/Magento/Backend/Model/Widget/Grid/Parser.php index ea6fadf49e253..fbed7149aa565 100644 --- a/app/code/Magento/Backend/Model/Widget/Grid/Parser.php +++ b/app/code/Magento/Backend/Model/Widget/Grid/Parser.php @@ -30,8 +30,8 @@ public function parseExpression($expression) $expression = trim($expression); foreach ($this->_operations as $operation) { $splittedExpr = preg_split('/\\' . $operation . '/', $expression, -1, PREG_SPLIT_DELIM_CAPTURE); - if (!empty($splittedExpr)) { - $count = count($splittedExpr); + $count = count($splittedExpr); + if ($count > 1) { for ($i = 0; $i < $count; $i++) { $stack = array_merge($stack, $this->parseExpression($splittedExpr[$i])); if ($i > 0) { From a478f9d9c5db7a6f35f554e97d066ea7c44ba136 Mon Sep 17 00:00:00 2001 From: Alexander Shkurko Date: Mon, 15 Jan 2018 15:03:32 +0100 Subject: [PATCH 3/3] Hotfix: add missed count() --- .../Model/Config/Structure/Element/Dependency/MapperTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Config/Test/Unit/Model/Config/Structure/Element/Dependency/MapperTest.php b/app/code/Magento/Config/Test/Unit/Model/Config/Structure/Element/Dependency/MapperTest.php index f532e3f3d64f6..2f081ea4285b9 100644 --- a/app/code/Magento/Config/Test/Unit/Model/Config/Structure/Element/Dependency/MapperTest.php +++ b/app/code/Magento/Config/Test/Unit/Model/Config/Structure/Element/Dependency/MapperTest.php @@ -98,7 +98,7 @@ public function testGetDependenciesWhenDependentIsInvisible($isValueSatisfy) { $expected = []; $rowData = array_values($this->_testData); - $count = $this->_testData; + $count = count($this->_testData); for ($i = 0; $i < $count; ++$i) { $data = $rowData[$i]; $dependentPath = 'some path ' . $i;