Skip to content

Commit

Permalink
Performance: remove count() form the condition section of a loop
Browse files Browse the repository at this point in the history
  • Loading branch information
coderimus authored and dimonovp committed Mar 20, 2018
1 parent 447a24d commit 73099fd
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 9 deletions.
5 changes: 3 additions & 2 deletions app/code/Magento/Backend/Model/Widget/Grid/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Paypal/Model/Report/Settlement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Usps/Model/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Webapi/Model/Rest/Swagger/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . '}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 73099fd

Please sign in to comment.