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 |
||
41 | class EavAttributeJoiner |
||
42 | { |
||
43 | /** |
||
44 | * @string |
||
45 | */ |
||
46 | const VALUE_FIELD = 'value'; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | private $aliasIndex; |
||
53 | |||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private $joinReturnData = []; |
||
59 | |||
60 | |||
61 | /** |
||
62 | * @var \Magento\Eav\Model\Entity\Type |
||
63 | */ |
||
64 | private $entityType; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | private $entityTable; |
||
71 | |||
72 | |||
73 | /** |
||
74 | * @var \Magento\Framework\Model\ResourceModel\AbstractResource |
||
75 | */ |
||
76 | private $entityBasicModel; |
||
77 | |||
78 | |||
79 | /** |
||
80 | * @var int |
||
81 | */ |
||
82 | private $entityBasicModelIdField; |
||
83 | |||
84 | |||
85 | /** |
||
86 | * @var \Magento\Eav\Api\Data\AttributeInterface |
||
87 | */ |
||
88 | private $attributeModel; |
||
89 | |||
90 | |||
91 | /** |
||
92 | * @var string |
||
93 | */ |
||
94 | private $backendType; |
||
95 | |||
96 | |||
97 | /** Dependencies */ |
||
98 | |||
99 | /** |
||
100 | * @var \Magento\Eav\Api\AttributeRepositoryInterface |
||
101 | */ |
||
102 | private $attributeRepository; |
||
103 | |||
104 | |||
105 | /** |
||
106 | * @var \Magento\Eav\Model\Entity\Type |
||
107 | */ |
||
108 | private $entityTypeModel; |
||
109 | |||
110 | |||
111 | /** |
||
112 | * @var \Magento\Store\Model\StoreManagerInterface |
||
113 | */ |
||
114 | private $storeManager; |
||
115 | |||
116 | |||
117 | /** |
||
118 | * @var \Psr\Log\LoggerInterface |
||
119 | */ |
||
120 | private $logger; |
||
121 | |||
122 | |||
123 | /** |
||
124 | * @param \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepository |
||
125 | * @param \Magento\Eav\Model\Entity\Type $entityTypeModel |
||
126 | * @param \Magento\Store\Model\StoreManagerInterface $storeManager |
||
127 | * @param \Psr\Log\LoggerInterface $logger |
||
128 | */ |
||
129 | public function __construct( |
||
140 | |||
141 | |||
142 | /** |
||
143 | * @param \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection $collection |
||
144 | * @param string $foreignKeyName |
||
145 | * @param string $entityTypeCode |
||
146 | * @param mixed string | array $attributes |
||
147 | * @param string $entityAlternativeKeyName |
||
148 | * @param string $mainTable |
||
149 | * |
||
150 | * @throws \Magento\Framework\Exception\NoSuchEntityException |
||
151 | * @throws \Magento\Framework\Exception\LocalizedException |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | public function join( |
||
197 | |||
198 | |||
199 | /** |
||
200 | * @param \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection $collection |
||
201 | * @param string $foreignKeyName |
||
202 | * @param string $entityTypeCode |
||
203 | * @param mixed string | array $attributes |
||
204 | * @param int $storeId |
||
205 | * @param string $entityAlternativeKeyName |
||
206 | * @param string $mainTable |
||
207 | * |
||
208 | * @throws \Magento\Framework\Exception\NoSuchEntityException |
||
209 | * @throws \Magento\Framework\Exception\LocalizedException |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | public function joinScopeable( |
||
269 | |||
270 | |||
271 | /** |
||
272 | * @param \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection $collection |
||
273 | * @param string $entityTypeCode |
||
274 | * @param string $entityAlternativeKeyName |
||
275 | * |
||
276 | * @return void |
||
277 | */ |
||
278 | private function prepareEntityInformation( |
||
292 | |||
293 | |||
294 | /** |
||
295 | * @param mixed string | array $attributes |
||
296 | * |
||
297 | * @return array $attributesTable |
||
298 | */ |
||
299 | private function convertAttributesArgument($attributes) : array |
||
310 | |||
311 | |||
312 | /** |
||
313 | * @param string $entityTypeCode |
||
314 | * @param string $attributeCode |
||
315 | * |
||
316 | * @throws \Magento\Framework\Exception\NoSuchEntityException |
||
317 | * |
||
318 | * @return void |
||
319 | */ |
||
320 | private function prepareAttributeInformation(string $entityTypeCode, string $attributeCode) |
||
325 | |||
326 | |||
327 | /** |
||
328 | * @param string $entityTypeCode |
||
329 | * @param string $attributeCode |
||
330 | * |
||
331 | * @throws \Magento\Framework\Exception\NoSuchEntityException |
||
332 | * |
||
333 | * @return \Magento\Eav\Api\Data\AttributeInterface |
||
334 | */ |
||
335 | private function getAttributeByCode($entityTypeCode, $attributeCode) |
||
339 | |||
340 | |||
341 | /** |
||
342 | * @return string |
||
343 | */ |
||
344 | private function buildTypeTableName() |
||
348 | |||
349 | |||
350 | /** |
||
351 | * @param string $tableName |
||
352 | * @param bool $useDefaultSuffix |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | private function generateAlias(string $tableName, bool $useDefaultSuffix = false) : string |
||
366 | |||
367 | |||
368 | /** |
||
369 | * @param string $entityTypeCode |
||
370 | * @param string $attributeCode |
||
371 | * @param string $tableAlias |
||
372 | * @param string $fieldName |
||
373 | * |
||
374 | * @return void |
||
375 | */ |
||
376 | private function addRecordToJoinReturnData( |
||
385 | } |
||
386 |
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.