Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
14 | class DefaultMagentoImportStrategy extends ConfigurableAddOrReplaceStrategy |
||
15 | { |
||
16 | /** @var PropertyAccessor */ |
||
17 | protected $propertyAccessor; |
||
18 | |||
19 | /** |
||
20 | * @param PropertyAccessor $propertyAccessor |
||
21 | */ |
||
22 | public function setPropertyAccessor(PropertyAccessor $propertyAccessor) |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | protected function updateContextCounters($entity) |
||
42 | |||
43 | /** |
||
44 | * Specify channel as identity field |
||
45 | * |
||
46 | * For local entities created from not existing in Magento entities (as guest customer - without originId) |
||
47 | * should be specified additional identities in appropriate strategy |
||
48 | * |
||
49 | * @param string $entityName |
||
50 | * @param array $identityValues |
||
51 | * @return null|object |
||
52 | */ |
||
53 | View Code Duplication | protected function findEntityByIdentityValues($entityName, array $identityValues) |
|
61 | |||
62 | /** |
||
63 | * Combine channel with identity values for entity search on local new entities storage |
||
64 | * |
||
65 | * For local entities created from not existing in Magento entities (as guest customer - without originId) |
||
66 | * should be configured special identity fields or search context in appropriate strategy |
||
67 | * |
||
68 | * @param $entity |
||
69 | * @param $entityClass |
||
70 | * @param array $searchContext |
||
71 | * |
||
72 | * @return array|null |
||
73 | */ |
||
74 | View Code Duplication | protected function combineIdentityValues($entity, $entityClass, array $searchContext) |
|
82 | |||
83 | /** |
||
84 | * @return PropertyAccessor |
||
85 | */ |
||
86 | protected function getPropertyAccessor() |
||
93 | |||
94 | /** |
||
95 | * Get existing registered customer or existing guest customer |
||
96 | * Find existing customer using entity data for entities containing customer like Order and Cart |
||
97 | * |
||
98 | * @param object $entity |
||
99 | * |
||
100 | * @return null|Customer |
||
101 | */ |
||
102 | protected function findExistingCustomerByContext($entity) |
||
113 | |||
114 | /** |
||
115 | * Get search context for entity customer |
||
116 | * |
||
117 | * @param object $entity |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | protected function getEntityCustomerSearchContext($entity) |
||
134 | |||
135 | /** |
||
136 | * Get customer search context by channel and website if exists |
||
137 | * |
||
138 | * @param Customer $customer |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | protected function getCustomerSearchContext(Customer $customer) |
||
163 | |||
164 | /** |
||
165 | * Find existing Magento Region entity |
||
166 | * Find by Code if parameter $regionId not passed |
||
167 | * |
||
168 | * @param Region $entity |
||
169 | * @param string|null $regionId |
||
170 | * |
||
171 | * @return null|MagentoRegion |
||
172 | */ |
||
173 | protected function findRegionEntity(Region $entity, $regionId = null) |
||
199 | } |
||
200 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.