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
/**

0 commit comments

Comments
 (0)