Skip to content

fix: cache count() results for loops #15507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -416,7 +416,8 @@ public function parseCsv($localCsv, $format = 'new')
private function getBodyItems(array $line, array $sectionColumns, array $rowMap)
{
$bodyItem = [];
for ($i = 1, $count = count($line); $i < $count; $i++) {
$lineCount = count($line);
for ($i = 1, $count = $lineCount; $i < $count; $i++) {
if(isset($rowMap[$sectionColumns[$i]])) {
if (in_array($rowMap[$sectionColumns[$i]], $this->dateTimeColumns)) {
$line[$i] = $this->formatDateTimeColumns($line[$i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public function tearDown()
{
$this->cmsIndex->open();
$this->cmsIndex->getLinksBlock()->openLink("Compare Products");
for ($i = 1; $i <= count($this->products); $i++) {
$productsCount = count($this->products);
for ($i = 1; $i <= $productsCount; $i++) {
$this->catalogProductCompare->getCompareProductsBlock()->removeProduct();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ public function test(

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function __construct(
public function run()
{
$shippingMethods = [];
for ($i = 0; $i < count($this->customer->getAddress()); $i++) {
$addressCount = $this->customer->getAddress();
for ($i = 0; $i < $addressCount; $i++) {
$shippingMethods[] = $this->shippingMethod;
}
$this->shippingInformation->getShippingBlock()->selectShippingMethod($shippingMethods);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function processAssert(
AssertProductForm $assertProductForm,
AssertConfigurableProductForm $assertConfigurableProductForm
) {
for ($i = 0; $i < count($order->getEntityId()['products']); $i++) {
$productsCount = count($order->getEntityId()['products']);
for ($i = 0; $i < $productsCount; $i++) {
$product = $order->getEntityId()['products'][$i];
$productData = $product->getData();
if ($product instanceof BundleProduct) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public function testPricesSegmentation($categoryId, array $entityIds, array $int
$items = $model->calculateSeparators($interval);
$this->assertEquals(array_keys($intervalItems), array_keys($items));

for ($i = 0; $i < count($intervalItems); ++$i) {
$intervalItemsCount = count($intervalItems);
for ($i = 0; $i < $intervalItemsCount; ++$i) {
$this->assertInternalType('array', $items[$i]);
$this->assertEquals($intervalItems[$i]['from'], $items[$i]['from']);
$this->assertEquals($intervalItems[$i]['to'], $items[$i]['to']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ private function tokenizeString($fileContent)
$lineNumber = 1;
$tokens = token_get_all($fileContent);
$snifferTokens = [];
for ($i = 0; $i < count($tokens); $i++) {
$tokensCount = count($tokens);
for ($i = 0; $i < $tokensCount; $i++) {
$content = is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i];
$snifferTokens[$i]['line'] = $lineNumber;
$snifferTokens[$i]['content'] = $content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,9 @@ private function _checkConstantWithClasspath($constant, $class, $replacement, $c
{
$classPathParts = explode('\\', $class);
$classPartialPath = '';
for ($i = count($classPathParts) - 1; $i >= 0; $i--) {
if ($i === (count($classPathParts) - 1)) {
$classPathPartsCount = count($classPathParts);
for ($i = $classPathPartsCount - 1; $i >= 0; $i--) {
if ($i === ($classPathPartsCount - 1)) {
$classPartialPath = $classPathParts[$i] . $classPartialPath;
} else {
$classPartialPath = $classPathParts[$i] . '\\' . $classPartialPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function testDictionaryGetter()
}

$file = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\File\ReadInterface::class);
for ($i = 0; $i < count($data); $i++) {
$dataCount = count($data);
for ($i = 0; $i < $dataCount; $i++) {
$file->expects($this->at($i))->method('readCsv')->will($this->returnValue($data[$i]));
}
$file->expects($this->at($i))->method('readCsv')->will($this->returnValue(false));
Expand Down
7 changes: 4 additions & 3 deletions lib/internal/Magento/Framework/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,15 @@ public function pack($source, $destination = 'packed.tgz', $skipRoot = false)
{
$archivers = $this->_getArchivers($destination);
$interimSource = '';
for ($i = 0; $i < count($archivers); $i++) {
if ($i == count($archivers) - 1) {
$archiversCount = count($archivers);
for ($i = 0; $i < $archiversCount; $i++) {
if ($i == $archiversCount - 1) {
$packed = $destination;
} else {
$packed = dirname($destination) . '/~tmp-' . microtime(true) . $archivers[$i] . '.' . $archivers[$i];
}
$source = $this->_getArchiver($archivers[$i])->pack($source, $packed, $skipRoot);
if ($interimSource && $i < count($archivers)) {
if ($interimSource && $i < $archiversCount) {
unlink($interimSource);
}
$interimSource = $source;
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/Magento/Framework/Cache/Backend/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public function save($data, $id, $tags = [], $specificLifetime = false)
if (is_string($data) && strlen($data) > $this->_options['slab_size']) {
$dataChunks = str_split($data, $this->_options['slab_size']);

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

if (!parent::save($dataChunks[$i], $chunkId, $tags, $specificLifetime)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/Magento/Framework/Filter/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,8 @@ protected function getVariable($value, $default = '{no_value_defined}')
$stackVars = $tokenizer->tokenize();
$result = $default;
$last = 0;
for ($i = 0; $i < count($stackVars); $i++) {
$stackVarsCount = count($stackVars);
for ($i = 0; $i < $stackVarsCount; $i++) {
if ($i == 0 && isset($this->templateVars[$stackVars[$i]['name']])) {
// Getting of template value
$stackVars[$i]['variable'] = & $this->templateVars[$stackVars[$i]['name']];
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/Magento/Framework/System/Ftp.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public function mkdirRecursive($path, $mode = 0777)
$dir = explode("/", $path);
$path = "";
$ret = true;
for ($i = 0; $i < count($dir); $i++) {
$dirCount = count($dir);
for ($i = 0; $i < $dirCount; $i++) {
$path .= "/" . $dir[$i];
if (!@ftp_chdir($this->_conn, $path)) {
@ftp_chdir($this->_conn, "/");
Expand Down
6 changes: 4 additions & 2 deletions setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ protected function _parse()

$results = [];
preg_match_all(Filter::CONSTRUCTION_PATTERN, $data, $results, PREG_SET_ORDER);
for ($i = 0; $i < count($results); $i++) {
$resultsCount = count($results);
for ($i = 0; $i < $resultsCount; $i++) {
if ($results[$i][1] === Filter::TRANS_DIRECTIVE_NAME) {
$directive = [];
if (preg_match(Filter::TRANS_DIRECTIVE_REGEX, $results[$i][2], $directive) !== 1) {
Expand All @@ -43,7 +44,8 @@ protected function _parse()
}

preg_match_all(self::HTML_FILTER, $data, $results, PREG_SET_ORDER);
for ($i = 0; $i < count($results); $i++) {
$resultsCount = count($results);
for ($i = 0; $i < $resultsCount; $i++) {
if (!empty($results[$i]['value'])) {
$this->_addPhrase($results[$i]['value']);
}
Expand Down
6 changes: 4 additions & 2 deletions setup/src/Magento/Setup/Module/I18n/Parser/Adapter/Js.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ protected function _parse()
$fileRow = fgets($fileHandle, 4096);
$results = [];
preg_match_all('/mage\.__\(\s*([\'"])(.*?[^\\\])\1.*?[),]/', $fileRow, $results, PREG_SET_ORDER);
for ($i = 0; $i < count($results); $i++) {
$resultsCount = count($results);
for ($i = 0; $i < $resultsCount; $i++) {
if (isset($results[$i][2])) {
$quote = $results[$i][1];
$this->_addPhrase($quote . $results[$i][2] . $quote, $lineNumber);
}
}

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