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 |
||
39 | class EntityAttributeObserver extends AbstractAttributeImportObserver |
||
40 | { |
||
41 | |||
42 | /** |
||
43 | * The attribute processor instance. |
||
44 | * |
||
45 | * @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
||
46 | */ |
||
47 | protected $attributeBunchProcessor; |
||
48 | |||
49 | /** |
||
50 | * The collection with entity merger instances. |
||
51 | * |
||
52 | * @var \Doctrine\Common\Collections\Collection |
||
53 | */ |
||
54 | protected $entityMergers; |
||
55 | |||
56 | /** |
||
57 | * Initializes the observer with the passed subject instance. |
||
58 | * |
||
59 | * @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance |
||
60 | * @param \TechDivision\Import\Observers\EntityMergers\EntityMergerInterface $entityMerger The entity merger instance |
||
61 | * @param \TechDivision\Import\Observers\StateDetectorInterface|null $stateDetector The state detector instance to use |
||
62 | */ |
||
63 | View Code Duplication | public function __construct( |
|
76 | |||
77 | /** |
||
78 | * Process the observer's business logic. |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | protected function process() |
||
126 | |||
127 | /** |
||
128 | * Merge's and return's the entity with the passed attributes and set's the |
||
129 | * passed status. |
||
130 | * |
||
131 | * @param array $entity The entity to merge the attributes into |
||
132 | * @param array $attr The attributes to be merged |
||
133 | * @param string|null $changeSetName The change set name to use |
||
134 | * |
||
135 | * @return array The merged entity |
||
136 | * @todo https://github.com/techdivision/import/issues/179 |
||
137 | */ |
||
138 | View Code Duplication | protected function mergeEntity(array $entity, array $attr, $changeSetName = null) |
|
146 | |||
147 | /** |
||
148 | * Prepare the attributes of the entity that has to be persisted. |
||
149 | * |
||
150 | * @return array The prepared attributes |
||
151 | */ |
||
152 | protected function prepareAttributes() |
||
176 | |||
177 | /** |
||
178 | * Initialize the attribute with the passed attributes and returns an instance. |
||
179 | * |
||
180 | * @param array $attr The attribute attributes |
||
181 | * |
||
182 | * @return array The initialized attribute |
||
183 | */ |
||
184 | protected function initializeAttribute(array $attr) |
||
188 | |||
189 | /** |
||
190 | * Return's the attribute bunch processor instance. |
||
191 | * |
||
192 | * @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
||
193 | */ |
||
194 | protected function getAttributeBunchProcessor() |
||
198 | |||
199 | /** |
||
200 | * Queries whether or not the attribute with the passed code has already been processed. |
||
201 | * |
||
202 | * @param string $attributeCode The attribute code to check |
||
203 | * |
||
204 | * @return boolean TRUE if the path has been processed, else FALSE |
||
205 | */ |
||
206 | protected function hasBeenProcessed($attributeCode) |
||
210 | |||
211 | /** |
||
212 | * Return's the ID of the attribute that has been created recently. |
||
213 | * |
||
214 | * @return integer The attribute ID |
||
215 | */ |
||
216 | protected function getLastAttributeId() |
||
220 | |||
221 | /** |
||
222 | * Return's the entity type for the passed code. |
||
223 | * |
||
224 | * @param string $entityTypeCode The entity type code |
||
225 | * |
||
226 | * @return array The requested entity type |
||
227 | * @throws \Exception Is thrown, if the entity type with the passed code is not available |
||
228 | */ |
||
229 | protected function getEntityType($entityTypeCode) |
||
233 | |||
234 | /** |
||
235 | * Return's the attribute set with the passed attribute set name. |
||
236 | * |
||
237 | * @param string $attributeSetName The name of the requested attribute set |
||
238 | * @param string $entityTypeCode The entity type code of the requested attribute set |
||
239 | * |
||
240 | * @return array The EAV attribute set |
||
241 | * @throws \Exception Is thrown, if the attribute set with the passed name is not available |
||
242 | */ |
||
243 | protected function getAttributeSetByAttributeSetNameAndEntityTypeCode($attributeSetName, $entityTypeCode) |
||
247 | |||
248 | /** |
||
249 | * Return's the attribute group with the passed attribute set/group name. |
||
250 | * |
||
251 | * @param string $entityTypeCode The entity type code of the requested attribute group |
||
252 | * @param string $attributeSetName The name of the requested attribute group's attribute set |
||
253 | * @param string $attributeGroupName The name of the requested attribute group |
||
254 | * |
||
255 | * @return array The EAV attribute group |
||
256 | * @throws \Exception Is thrown, if the attribute group with the passed attribute set/group name is not available |
||
257 | */ |
||
258 | protected function getAttributeGroupByEntityTypeCodeAndAttributeSetNameAndAttributeGroupName($entityTypeCode, $attributeSetName, $attributeGroupName) |
||
262 | |||
263 | /** |
||
264 | * Persist the passed entity attribute. |
||
265 | * |
||
266 | * @param array $entityAttribute The entity attribute to persist |
||
267 | * |
||
268 | * @return void |
||
269 | */ |
||
270 | protected function persistEntityAttribute(array $entityAttribute) |
||
274 | } |
||
275 |
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.