Skip to content

Commit a64d755

Browse files
authored
ENGCOM-8260: [Performance] Fix array merge in loop #30153
2 parents 0661641 + e1c29e9 commit a64d755

File tree

35 files changed

+228
-237
lines changed

35 files changed

+228
-237
lines changed

app/code/Magento/AdvancedPricingImportExport/Model/Export/AdvancedPricing.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ public function __construct(
158158
protected function initTypeModels()
159159
{
160160
$productTypes = $this->_exportConfig->getEntityTypes(CatalogProduct::ENTITY);
161+
$disabledAttrs = [];
162+
$indexValueAttributes = [];
161163
foreach ($productTypes as $productTypeName => $productTypeConfig) {
162164
if (!($model = $this->_typeFactory->create($productTypeConfig['model']))) {
163165
throw new \Magento\Framework\Exception\LocalizedException(
@@ -174,21 +176,19 @@ protected function initTypeModels()
174176
}
175177
if ($model->isSuitable()) {
176178
$this->_productTypeModels[$productTypeName] = $model;
177-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
178-
$this->_disabledAttrs = array_merge($this->_disabledAttrs, $model->getDisabledAttrs());
179-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
180-
$this->_indexValueAttributes = array_merge(
181-
$this->_indexValueAttributes,
182-
$model->getIndexValueAttributes()
183-
);
179+
$disabledAttrs[] = $model->getDisabledAttrs();
180+
$indexValueAttributes[] = $model->getIndexValueAttributes();
184181
}
185182
}
186183
if (!$this->_productTypeModels) {
187184
throw new \Magento\Framework\Exception\LocalizedException(
188185
__('There are no product types available for export')
189186
);
190187
}
191-
$this->_disabledAttrs = array_unique($this->_disabledAttrs);
188+
$this->_disabledAttrs = array_unique(array_merge([], $this->_disabledAttrs, ...$disabledAttrs));
189+
$this->_indexValueAttributes = array_unique(
190+
array_merge([], $this->_indexValueAttributes, ...$indexValueAttributes)
191+
);
192192
return $this;
193193
}
194194

@@ -518,6 +518,8 @@ protected function getTierPrices(array $listSku, $table)
518518
if (isset($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP])) {
519519
$exportFilter = $this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP];
520520
}
521+
$selectFields = [];
522+
$exportData = false;
521523
if ($table == ImportAdvancedPricing::TABLE_TIER_PRICE) {
522524
$selectFields = [
523525
ImportAdvancedPricing::COL_SKU => 'cpe.sku',

app/code/Magento/AsynchronousOperations/Model/OperationProcessor.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function process(string $encodedMessage)
117117
$status = OperationInterface::STATUS_TYPE_COMPLETE;
118118
$errorCode = null;
119119
$messages = [];
120+
$entityParams = [];
120121
$topicName = $operation->getTopicName();
121122
$handlers = $this->configuration->getHandlers($topicName);
122123
try {
@@ -127,7 +128,7 @@ public function process(string $encodedMessage)
127128
$this->logger->error($e->getMessage());
128129
$status = OperationInterface::STATUS_TYPE_NOT_RETRIABLY_FAILED;
129130
$errorCode = $e->getCode();
130-
$messages[] = $e->getMessage();
131+
$messages[] = [$e->getMessage()];
131132
}
132133

133134
$outputData = null;
@@ -136,9 +137,7 @@ public function process(string $encodedMessage)
136137
$result = $this->executeHandler($callback, $entityParams);
137138
$status = $result['status'];
138139
$errorCode = $result['error_code'];
139-
// phpcs:disable Magento2.Performance.ForeachArrayMerge
140-
$messages = array_merge($messages, $result['messages']);
141-
// phpcs:enable Magento2.Performance.ForeachArrayMerge
140+
$messages[] = $result['messages'];
142141
$outputData = $result['output_data'];
143142
}
144143
}
@@ -157,7 +156,7 @@ public function process(string $encodedMessage)
157156
);
158157
$outputData = $this->jsonHelper->serialize($outputData);
159158
} catch (\Exception $e) {
160-
$messages[] = $e->getMessage();
159+
$messages[] = [$e->getMessage()];
161160
}
162161
}
163162

@@ -167,7 +166,7 @@ public function process(string $encodedMessage)
167166
$operation->getId(),
168167
$status,
169168
$errorCode,
170-
implode('; ', $messages),
169+
implode('; ', array_merge([], ...$messages)),
171170
$serializedData,
172171
$outputData
173172
);

app/code/Magento/Bundle/Test/Unit/Pricing/Adjustment/CalculatorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ public function testGetterAmount($amountForBundle, $optionList, $expectedResult)
166166

167167
$optionSelections = [];
168168
foreach ($options as $option) {
169-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
170-
$optionSelections = array_merge($optionSelections, $option->getSelections());
169+
$optionSelections[] = $option->getSelections();
171170
}
171+
$optionSelections = array_merge([], ...$optionSelections);
172+
172173
$this->selectionPriceListProvider->expects($this->any())->method('getPriceList')->willReturn($optionSelections);
173174

174175
$price = $this->createMock(BundleOptionPrice::class);

app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -603,19 +603,15 @@ protected function populateInsertOptionValues(array $optionIds): array
603603
if ($assoc['position'] == $this->_cachedOptions[$entityId][$key]['index']
604604
&& $assoc['parent_id'] == $entityId) {
605605
$option['parent_id'] = $entityId;
606-
//phpcs:ignore Magento2.Performance.ForeachArrayMerge
607-
$optionValues = array_merge(
608-
$optionValues,
609-
$this->populateOptionValueTemplate($option, $optionId)
610-
);
606+
$optionValues[] = $this->populateOptionValueTemplate($option, $optionId);
611607
$this->_cachedOptions[$entityId][$key]['option_id'] = $optionId;
612608
break;
613609
}
614610
}
615611
}
616612
}
617613

618-
return $optionValues;
614+
return array_merge([], ...$optionValues);
619615
}
620616

621617
/**

app/code/Magento/Catalog/Model/Indexer/Product/Flat/TableBuilder.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ public function build($storeId, $changedIds, $valueFieldSuffix)
8686
//Create list of temporary tables based on available attributes attributes
8787
$valueTables = [];
8888
foreach ($temporaryEavAttributes as $tableName => $columns) {
89-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
90-
$valueTables = array_merge(
91-
$valueTables,
92-
$this->_createTemporaryTable($this->_getTemporaryTableName($tableName), $columns, $valueFieldSuffix)
89+
$valueTables[] = $this->_createTemporaryTable(
90+
$this->_getTemporaryTableName($tableName),
91+
$columns,
92+
$valueFieldSuffix
9393
);
9494
}
95+
$valueTables = array_merge([], ...$valueTables);
9596

9697
//Fill "base" table which contains all available products
9798
$this->_fillTemporaryEntityTable($entityTableName, $entityTableColumns, $changedIds);

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CategoryLinkTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ public function testSaveCategoryLinks($newCategoryLinks, $dbCategoryLinks, $affe
145145
$expectedResult = [];
146146

147147
foreach ($affectedIds as $type => $ids) {
148-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
149-
$expectedResult = array_merge($expectedResult, $ids);
148+
$expectedResult[] = $ids;
150149
// Verify if the correct insert, update and/or delete actions are performed:
151150
$this->setupExpectationsForConnection($type, $ids);
152151
}
152+
$expectedResult = array_merge([], ...$expectedResult);
153153

154154
$actualResult = $this->model->saveCategoryLinks($product, $newCategoryLinks);
155155

app/code/Magento/CatalogImportExport/Model/Export/Product.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ protected function initCategories()
478478
protected function initTypeModels()
479479
{
480480
$productTypes = $this->_exportConfig->getEntityTypes($this->getEntityTypeCode());
481+
$disabledAttrs = [];
482+
$indexValueAttributes = [];
481483
foreach ($productTypes as $productTypeName => $productTypeConfig) {
482484
if (!($model = $this->_typeFactory->create($productTypeConfig['model']))) {
483485
throw new \Magento\Framework\Exception\LocalizedException(
@@ -494,21 +496,19 @@ protected function initTypeModels()
494496
}
495497
if ($model->isSuitable()) {
496498
$this->_productTypeModels[$productTypeName] = $model;
497-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
498-
$this->_disabledAttrs = array_merge($this->_disabledAttrs, $model->getDisabledAttrs());
499-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
500-
$this->_indexValueAttributes = array_merge(
501-
$this->_indexValueAttributes,
502-
$model->getIndexValueAttributes()
503-
);
499+
$disabledAttrs[] = $model->getDisabledAttrs();
500+
$indexValueAttributes[] = $model->getIndexValueAttributes();
504501
}
505502
}
506503
if (!$this->_productTypeModels) {
507504
throw new \Magento\Framework\Exception\LocalizedException(
508505
__('There are no product types available for export.')
509506
);
510507
}
511-
$this->_disabledAttrs = array_unique($this->_disabledAttrs);
508+
$this->_disabledAttrs = array_unique(array_merge([], $this->_disabledAttrs, ...$disabledAttrs));
509+
$this->_indexValueAttributes = array_unique(
510+
array_merge([], $this->_indexValueAttributes, ...$indexValueAttributes)
511+
);
512512

513513
return $this;
514514
}
@@ -1128,7 +1128,7 @@ private function wrapValue($value)
11281128
protected function collectMultirawData()
11291129
{
11301130
$data = [];
1131-
$productIds = [];
1131+
$productLinkIds = [];
11321132
$rowWebsites = [];
11331133
$rowCategories = [];
11341134

@@ -1138,7 +1138,6 @@ protected function collectMultirawData()
11381138
/** @var \Magento\Catalog\Model\Product $item */
11391139
foreach ($collection as $item) {
11401140
$productLinkIds[] = $item->getData($this->getProductEntityLinkField());
1141-
$productIds[] = $item->getId();
11421141
$rowWebsites[$item->getId()] = array_intersect(
11431142
array_keys($this->_websiteIdToCode),
11441143
$item->getWebsites()

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,8 @@ private function initImagesArrayKeys()
12121212
protected function _initTypeModels()
12131213
{
12141214
$productTypes = $this->_importConfig->getEntityTypes($this->getEntityTypeCode());
1215+
$fieldsMap = [];
1216+
$specialAttributes = [];
12151217
foreach ($productTypes as $productTypeName => $productTypeConfig) {
12161218
$params = [$this, $productTypeName];
12171219
if (!($model = $this->_productTypeFactory->create($productTypeConfig['model'], ['params' => $params]))
@@ -1231,14 +1233,13 @@ protected function _initTypeModels()
12311233
if ($model->isSuitable()) {
12321234
$this->_productTypeModels[$productTypeName] = $model;
12331235
}
1234-
// phpcs:disable Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
1235-
$this->_fieldsMap = array_merge($this->_fieldsMap, $model->getCustomFieldsMapping());
1236-
$this->_specialAttributes = array_merge($this->_specialAttributes, $model->getParticularAttributes());
1237-
// phpcs:enable
1236+
$fieldsMap[] = $model->getCustomFieldsMapping();
1237+
$specialAttributes[] = $model->getParticularAttributes();
12381238
}
1239+
$this->_fieldsMap = array_merge([], $this->_fieldsMap, ...$fieldsMap);
12391240
$this->_initErrorTemplates();
12401241
// remove doubles
1241-
$this->_specialAttributes = array_unique($this->_specialAttributes);
1242+
$this->_specialAttributes = array_unique(array_merge([], $this->_specialAttributes, ...$specialAttributes));
12421243

12431244
return $this;
12441245
}

app/code/Magento/Config/Model/Config.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,9 @@ public function save()
207207
$deleteTransaction
208208
);
209209

210-
$groupChangedPaths = $this->getChangedPaths($sectionId, $groupId, $groupData, $oldConfig, $extraOldGroups);
211-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
212-
$changedPaths = \array_merge($changedPaths, $groupChangedPaths);
210+
$changedPaths[] = $this->getChangedPaths($sectionId, $groupId, $groupData, $oldConfig, $extraOldGroups);
213211
}
212+
$changedPaths = array_merge([], ...$changedPaths);
214213

215214
try {
216215
$deleteTransaction->delete();
@@ -356,7 +355,7 @@ private function getChangedPaths(
356355
$field = $this->getField($sectionId, $groupId, $fieldId);
357356
$path = $this->getFieldPath($field, $fieldId, $oldConfig, $extraOldGroups);
358357
if ($this->isValueChanged($oldConfig, $path, $fieldData)) {
359-
$changedPaths[] = $path;
358+
$changedPaths[] = [$path];
360359
}
361360
}
362361
}
@@ -371,12 +370,11 @@ private function getChangedPaths(
371370
$oldConfig,
372371
$extraOldGroups
373372
);
374-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
375-
$changedPaths = \array_merge($changedPaths, $subGroupChangedPaths);
373+
$changedPaths[] = $subGroupChangedPaths;
376374
}
377375
}
378376

379-
return $changedPaths;
377+
return \array_merge([], ...$changedPaths);
380378
}
381379

382380
/**

app/code/Magento/Config/Model/Config/Structure.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,20 +292,16 @@ public function getFieldPathsByAttribute($attributeName, $attributeValue)
292292
foreach ($section['children'] as $group) {
293293
if (isset($group['children'])) {
294294
$path = $section['id'] . '/' . $group['id'];
295-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
296-
$result = array_merge(
297-
$result,
298-
$this->_getGroupFieldPathsByAttribute(
299-
$group['children'],
300-
$path,
301-
$attributeName,
302-
$attributeValue
303-
)
295+
$result[] = $this->_getGroupFieldPathsByAttribute(
296+
$group['children'],
297+
$path,
298+
$attributeName,
299+
$attributeValue
304300
);
305301
}
306302
}
307303
}
308-
return $result;
304+
return array_merge([], ...$result);
309305
}
310306

311307
/**

app/code/Magento/Deploy/Package/Processor/PreProcessor/Css.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
use Magento\Deploy\Package\Package;
1111
use Magento\Deploy\Package\PackageFile;
1212
use Magento\Deploy\Package\Processor\ProcessorInterface;
13+
use Magento\Framework\App\Filesystem\DirectoryList;
1314
use Magento\Framework\Css\PreProcessor\Instruction\Import;
1415
use Magento\Framework\Filesystem;
1516
use Magento\Framework\Filesystem\Directory\ReadInterface;
16-
use Magento\Framework\App\Filesystem\DirectoryList;
17-
use Magento\Framework\View\Url\CssResolver;
1817
use Magento\Framework\View\Asset\Minification;
18+
use Magento\Framework\View\Url\CssResolver;
1919

2020
/**
2121
* Pre-processor for speeding up deployment of CSS files
@@ -137,7 +137,7 @@ private function buildMap($packagePath, $filePath, $fullPath)
137137

138138
$content = $this->staticDir->readFile($this->minification->addMinifiedSign($fullPath));
139139

140-
$callback = function ($matchContent) use ($packagePath, $filePath, & $imports) {
140+
$callback = function ($matchContent) use ($packagePath, $filePath, &$imports) {
141141
$importRelPath = $this->normalize(pathinfo($filePath, PATHINFO_DIRNAME) . '/' . $matchContent['path']);
142142
$imports[$importRelPath] = $this->normalize(
143143
$packagePath . '/' . pathinfo($filePath, PATHINFO_DIRNAME) . '/' . $matchContent['path']
@@ -175,15 +175,17 @@ private function buildMap($packagePath, $filePath, $fullPath)
175175
*
176176
* @param string $fileName
177177
* @return array
178-
* phpcs:disable Magento2.Performance.ForeachArrayMerge
179178
*/
180-
private function collectFileMap($fileName)
179+
private function collectFileMap(string $fileName): array
181180
{
182-
$result = isset($this->map[$fileName]) ? $this->map[$fileName] : [];
183-
foreach ($result as $path) {
184-
$result = array_merge($result, $this->collectFileMap($path));
181+
$valueFromMap = $this->map[$fileName] ?? [];
182+
$result = [$valueFromMap];
183+
184+
foreach ($valueFromMap as $path) {
185+
$result[] = $this->collectFileMap($path);
185186
}
186-
return array_unique($result);
187+
188+
return array_unique(array_merge([], ...$result));
187189
}
188190

189191
/**

app/code/Magento/MediaContentApi/Model/Composite/GetAssetIdsByContentField.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ public function execute(string $field, string $value): array
4242
$ids = [];
4343
/** @var GetAssetIdsByContentFieldInterface $fieldHandler */
4444
foreach ($this->fieldHandlers[$field] as $fieldHandler) {
45-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
46-
$ids = array_merge($ids, $fieldHandler->execute($value));
45+
$ids[] = $fieldHandler->execute($value);
4746
}
48-
return array_unique($ids);
47+
return array_unique(array_merge([], ...$ids));
4948
}
5049
}

app/code/Magento/Sales/Model/AdminOrder/Create.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,15 +1999,17 @@ protected function _validate()
19991999
$this->_errors[] = __('Please specify order items.');
20002000
}
20012001

2002+
$errors = [];
20022003
foreach ($items as $item) {
20032004
/** @var \Magento\Quote\Model\Quote\Item $item */
20042005
$messages = $item->getMessage(false);
20052006
if ($item->getHasError() && is_array($messages) && !empty($messages)) {
2006-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
2007-
$this->_errors = array_merge($this->_errors, $messages);
2007+
$errors[] = $messages;
20082008
}
20092009
}
20102010

2011+
$this->_errors = array_merge([], $this->_errors, ...$errors);
2012+
20112013
if (!$this->getQuote()->isVirtual()) {
20122014
if (!$this->getQuote()->getShippingAddress()->getShippingMethod()) {
20132015
$this->_errors[] = __('The shipping method is missing. Select the shipping method and try again.');

0 commit comments

Comments
 (0)