Skip to content

Commit f39e3fd

Browse files
committed
fix: cache count() results for loops
1 parent de5a599 commit f39e3fd

File tree

15 files changed

+37
-20
lines changed

15 files changed

+37
-20
lines changed

app/code/Magento/Paypal/Model/Report/Settlement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ public function parseCsv($localCsv, $format = 'new')
416416
private function getBodyItems(array $line, array $sectionColumns, array $rowMap)
417417
{
418418
$bodyItem = [];
419-
for ($i = 1, $count = count($line); $i < $count; $i++) {
419+
$lineCount = count($line);
420+
for ($i = 1, $count = $lineCount; $i < $count; $i++) {
420421
if(isset($rowMap[$sectionColumns[$i]])) {
421422
if (in_array($rowMap[$sectionColumns[$i]], $this->dateTimeColumns)) {
422423
$line[$i] = $this->formatDateTimeColumns($line[$i]);

dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Product/AddCompareProductsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public function tearDown()
7676
{
7777
$this->cmsIndex->open();
7878
$this->cmsIndex->getLinksBlock()->openLink("Compare Products");
79-
for ($i = 1; $i <= count($this->products); $i++) {
79+
$productsCount = count($this->products);
80+
for ($i = 1; $i <= $productsCount; $i++) {
8081
$this->catalogProductCompare->getCompareProductsBlock()->removeProduct();
8182
}
8283
}

dev/tests/functional/tests/app/Magento/Checkout/Test/TestCase/ShoppingCartPerCustomerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public function test(
9595

9696
$customers = [];
9797
$cartFixtures = [];
98-
for ($i = 0; $i < count($checkoutData); $i++) {
98+
$checkoutDataCount = count($checkoutData);
99+
for ($i = 0; $i < $checkoutDataCount; $i++) {
99100
$customers[$i] = $this->fixtureFactory->createByCode('customer', ['dataset' => $customerDataset]);
100101
$customers[$i]->persist();
101102

dev/tests/functional/tests/app/Magento/Multishipping/Test/TestStep/FillShippingInformationStep.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public function __construct(
5858
public function run()
5959
{
6060
$shippingMethods = [];
61-
for ($i = 0; $i < count($this->customer->getAddress()); $i++) {
61+
$addressCount = $this->customer->getAddress();
62+
for ($i = 0; $i < $addressCount; $i++) {
6263
$shippingMethods[] = $this->shippingMethod;
6364
}
6465
$this->shippingInformation->getShippingBlock()->selectShippingMethod($shippingMethods);

dev/tests/functional/tests/app/Magento/Sales/Test/Constraint/AssertProductsQtyAfterOrderCancel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public function processAssert(
5252
AssertProductForm $assertProductForm,
5353
AssertConfigurableProductForm $assertConfigurableProductForm
5454
) {
55-
for ($i = 0; $i < count($order->getEntityId()['products']); $i++) {
55+
$productsCount = count($order->getEntityId()['products']);
56+
for ($i = 0; $i < $productsCount; $i++) {
5657
$product = $order->getEntityId()['products'][$i];
5758
$productData = $product->getData();
5859
if ($product instanceof BundleProduct) {

dev/tests/integration/testsuite/Magento/Catalog/Model/Layer/Filter/Price/AlgorithmBaseTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public function testPricesSegmentation($categoryId, array $entityIds, array $int
112112
$items = $model->calculateSeparators($interval);
113113
$this->assertEquals(array_keys($intervalItems), array_keys($items));
114114

115-
for ($i = 0; $i < count($intervalItems); ++$i) {
115+
$intervalItemsCount = count($intervalItems);
116+
for ($i = 0; $i < $intervalItemsCount; ++$i) {
116117
$this->assertInternalType('array', $items[$i]);
117118
$this->assertEquals($intervalItems[$i]['from'], $items[$i]['from']);
118119
$this->assertEquals($intervalItems[$i]['to'], $items[$i]['to']);

dev/tests/static/framework/tests/unit/testsuite/Magento/Sniffs/Translation/ConstantUsageSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ private function tokenizeString($fileContent)
6767
$lineNumber = 1;
6868
$tokens = token_get_all($fileContent);
6969
$snifferTokens = [];
70-
for ($i = 0; $i < count($tokens); $i++) {
70+
$tokensCount = count($tokens);
71+
for ($i = 0; $i < $tokensCount; $i++) {
7172
$content = is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
7273
$snifferTokens[$i]['line'] = $lineNumber;
7374
$snifferTokens[$i]['content'] = $content;

dev/tests/static/testsuite/Magento/Test/Legacy/ObsoleteCodeTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,9 @@ private function _checkConstantWithClasspath($constant, $class, $replacement, $c
501501
{
502502
$classPathParts = explode('\\', $class);
503503
$classPartialPath = '';
504-
for ($i = count($classPathParts) - 1; $i >= 0; $i--) {
505-
if ($i === (count($classPathParts) - 1)) {
504+
$classPathPartsCount = count($classPathParts);
505+
for ($i = $classPathPartsCount - 1; $i >= 0; $i--) {
506+
if ($i === ($classPathPartsCount - 1)) {
506507
$classPartialPath = $classPathParts[$i] . $classPartialPath;
507508
} else {
508509
$classPartialPath = $classPathParts[$i] . '\\' . $classPartialPath;

lib/internal/Magento/Framework/App/Test/Unit/Language/DictionaryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public function testDictionaryGetter()
5252
}
5353

5454
$file = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\File\ReadInterface::class);
55-
for ($i = 0; $i < count($data); $i++) {
55+
$dataCount = count($data);
56+
for ($i = 0; $i < $dataCount; $i++) {
5657
$file->expects($this->at($i))->method('readCsv')->will($this->returnValue($data[$i]));
5758
}
5859
$file->expects($this->at($i))->method('readCsv')->will($this->returnValue(false));

lib/internal/Magento/Framework/Archive.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,15 @@ public function pack($source, $destination = 'packed.tgz', $skipRoot = false)
9696
{
9797
$archivers = $this->_getArchivers($destination);
9898
$interimSource = '';
99-
for ($i = 0; $i < count($archivers); $i++) {
100-
if ($i == count($archivers) - 1) {
99+
$archiversCount = count($archivers);
100+
for ($i = 0; $i < $archiversCount; $i++) {
101+
if ($i == $archiversCount - 1) {
101102
$packed = $destination;
102103
} else {
103104
$packed = dirname($destination) . '/~tmp-' . microtime(true) . $archivers[$i] . '.' . $archivers[$i];
104105
}
105106
$source = $this->_getArchiver($archivers[$i])->pack($source, $packed, $skipRoot);
106-
if ($interimSource && $i < count($archivers)) {
107+
if ($interimSource && $i < $archiversCount) {
107108
unlink($interimSource);
108109
}
109110
$interimSource = $source;

lib/internal/Magento/Framework/Cache/Backend/Memcached.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public function save($data, $id, $tags = [], $specificLifetime = false)
8484
if (is_string($data) && strlen($data) > $this->_options['slab_size']) {
8585
$dataChunks = str_split($data, $this->_options['slab_size']);
8686

87-
for ($i = 0, $cnt = count($dataChunks); $i < $cnt; $i++) {
87+
$dataChunksCount = count($dataChunks);
88+
for ($i = 0, $cnt = $dataChunksCount; $i < $cnt; $i++) {
8889
$chunkId = $this->_getChunkId($id, $i);
8990

9091
if (!parent::save($dataChunks[$i], $chunkId, $tags, $specificLifetime)) {

lib/internal/Magento/Framework/Filter/Template.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ protected function getVariable($value, $default = '{no_value_defined}')
313313
$stackVars = $tokenizer->tokenize();
314314
$result = $default;
315315
$last = 0;
316-
for ($i = 0; $i < count($stackVars); $i++) {
316+
$stackVarsCount = count($stackVars);
317+
for ($i = 0; $i < $stackVarsCount; $i++) {
317318
if ($i == 0 && isset($this->templateVars[$stackVars[$i]['name']])) {
318319
// Getting of template value
319320
$stackVars[$i]['variable'] = & $this->templateVars[$stackVars[$i]['name']];

lib/internal/Magento/Framework/System/Ftp.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public function mkdirRecursive($path, $mode = 0777)
5858
$dir = explode("/", $path);
5959
$path = "";
6060
$ret = true;
61-
for ($i = 0; $i < count($dir); $i++) {
61+
$dirCount = count($dir);
62+
for ($i = 0; $i < $dirCount; $i++) {
6263
$path .= "/" . $dir[$i];
6364
if (!@ftp_chdir($this->_conn, $path)) {
6465
@ftp_chdir($this->_conn, "/");

setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Html.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ protected function _parse()
3131

3232
$results = [];
3333
preg_match_all(Filter::CONSTRUCTION_PATTERN, $data, $results, PREG_SET_ORDER);
34-
for ($i = 0; $i < count($results); $i++) {
34+
$resultsCount = count($results);
35+
for ($i = 0; $i < $resultsCount; $i++) {
3536
if ($results[$i][1] === Filter::TRANS_DIRECTIVE_NAME) {
3637
$directive = [];
3738
if (preg_match(Filter::TRANS_DIRECTIVE_REGEX, $results[$i][2], $directive) !== 1) {
@@ -43,7 +44,8 @@ protected function _parse()
4344
}
4445

4546
preg_match_all(self::HTML_FILTER, $data, $results, PREG_SET_ORDER);
46-
for ($i = 0; $i < count($results); $i++) {
47+
$resultsCount = count($results);
48+
for ($i = 0; $i < $resultsCount; $i++) {
4749
if (!empty($results[$i]['value'])) {
4850
$this->_addPhrase($results[$i]['value']);
4951
}

setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Js.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ protected function _parse()
2222
$fileRow = fgets($fileHandle, 4096);
2323
$results = [];
2424
preg_match_all('/mage\.__\(\s*([\'"])(.*?[^\\\])\1.*?[),]/', $fileRow, $results, PREG_SET_ORDER);
25-
for ($i = 0; $i < count($results); $i++) {
25+
$resultsCount = count($results);
26+
for ($i = 0; $i < $resultsCount; $i++) {
2627
if (isset($results[$i][2])) {
2728
$quote = $results[$i][1];
2829
$this->_addPhrase($quote . $results[$i][2] . $quote, $lineNumber);
2930
}
3031
}
3132

3233
preg_match_all('/\\$t\(\s*([\'"])(.*?[^\\\])\1.*?[),]/', $fileRow, $results, PREG_SET_ORDER);
33-
for ($i = 0; $i < count($results); $i++) {
34+
$resultsCount = count($results);
35+
for ($i = 0; $i < $resultsCount; $i++) {
3436
if (isset($results[$i][2])) {
3537
$quote = $results[$i][1];
3638
$this->_addPhrase($quote . $results[$i][2] . $quote, $lineNumber);

0 commit comments

Comments
 (0)