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 |
||
17 | class Repository implements RepositoryInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $entityName; |
||
23 | |||
24 | /** |
||
25 | * @var CriteriaFactory |
||
26 | */ |
||
27 | protected $criteriaFactory; |
||
28 | |||
29 | /** |
||
30 | * @var TableGateway |
||
31 | */ |
||
32 | protected $tableGateway; |
||
33 | |||
34 | /** |
||
35 | * @var Mapper |
||
36 | */ |
||
37 | protected $mapper; |
||
38 | |||
39 | /** |
||
40 | * @var EntityFactoryInterface |
||
41 | */ |
||
42 | protected $entityFactory; |
||
43 | |||
44 | /** |
||
45 | * @var ArrayObject |
||
46 | */ |
||
47 | protected $identityMap; |
||
48 | |||
49 | /** |
||
50 | * @var ArrayObject |
||
51 | */ |
||
52 | protected $identityMapOriginal; |
||
53 | |||
54 | /** |
||
55 | * @var EventManagerInterface |
||
56 | */ |
||
57 | protected $eventManager; |
||
58 | |||
59 | /** |
||
60 | * @var EntityChangedEvent |
||
61 | */ |
||
62 | protected $event; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $tablePrimaryKey; |
||
68 | |||
69 | /** |
||
70 | * @param string $entityName |
||
71 | * @param CriteriaFactory $criteriaFactory |
||
72 | * @param TableGateway $tableGateway |
||
73 | * @param Mapper $mapper |
||
74 | * @param EntityFactoryInterface $entityFactory |
||
75 | * @param EventManagerInterface $eventManager |
||
76 | * @param string $tablePrimaryKey |
||
77 | */ |
||
78 | public function __construct( |
||
98 | |||
99 | /** |
||
100 | * @param EntityInterface $entity |
||
101 | * @return EntityInterface|int|null |
||
102 | */ |
||
103 | public function add(EntityInterface $entity) |
||
140 | |||
141 | /** |
||
142 | * @param EntityInterface $entity |
||
143 | * @return int|null |
||
144 | */ |
||
145 | public function remove(EntityInterface $entity) |
||
159 | |||
160 | /** |
||
161 | * @param CriteriaInterface|array $criteria |
||
162 | * @return EntityInterface|null |
||
163 | */ |
||
164 | public function find($criteria) |
||
188 | |||
189 | /** |
||
190 | * @param mixed $id |
||
191 | * @return EntityInterface|null |
||
192 | */ |
||
193 | public function findById($id) |
||
200 | |||
201 | /** |
||
202 | * @param CriteriaInterface|array $criteria |
||
203 | * @return EntityInterface[] |
||
204 | */ |
||
205 | public function findMany($criteria) |
||
226 | |||
227 | /** |
||
228 | * @param CriteriaInterface|array $criteria |
||
229 | * @return int |
||
230 | */ |
||
231 | public function count($criteria) |
||
254 | |||
255 | /** |
||
256 | * @param array $filter |
||
257 | * @return CriteriaInterface |
||
258 | */ |
||
259 | public function createCriteria(array $filter = []) |
||
263 | |||
264 | /** |
||
265 | * @param EntityInterface $entity |
||
266 | */ |
||
267 | protected function toIdentityMap(EntityInterface $entity) |
||
272 | |||
273 | /** |
||
274 | * @param EntityInterface $changedEntity |
||
275 | * @return bool |
||
276 | */ |
||
277 | protected function isEntityChanged(EntityInterface $changedEntity) |
||
282 | |||
283 | /** |
||
284 | * @return EntityChangedEvent |
||
285 | */ |
||
286 | View Code Duplication | protected function getEvent() |
|
295 | |||
296 | /** |
||
297 | * @param EntityInterface $createdEntity |
||
298 | */ |
||
299 | View Code Duplication | protected function triggerCreate(EntityInterface &$createdEntity) |
|
314 | |||
315 | /** |
||
316 | * @param EntityInterface $deletedEntity |
||
317 | */ |
||
318 | View Code Duplication | protected function triggerDelete(EntityInterface $deletedEntity) |
|
329 | |||
330 | /** |
||
331 | * @param EntityChangedEvent $e |
||
332 | */ |
||
333 | View Code Duplication | protected function triggerChanges(EntityChangedEvent $e) |
|
342 | |||
343 | /** |
||
344 | * @param EntityChangedEvent $e |
||
345 | */ |
||
346 | View Code Duplication | protected function triggerPreChanges(EntityChangedEvent $e) |
|
354 | |||
355 | /** |
||
356 | * @param EntityChangedEvent $e |
||
357 | */ |
||
358 | View Code Duplication | protected function triggerAttributesChange(EntityChangedEvent $e) |
|
372 | |||
373 | /** |
||
374 | * @param EntityInterface $changedEntity |
||
375 | * @return string |
||
376 | */ |
||
377 | protected function getEntityChangeEventName(EntityInterface $changedEntity) |
||
381 | |||
382 | /** |
||
383 | * @param EntityInterface $changedEntity |
||
384 | * @param $attributeName |
||
385 | * @return string |
||
386 | */ |
||
387 | protected function getAttributeChangeEventName(EntityInterface $changedEntity, $attributeName) |
||
391 | } |
||
392 |
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.