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 InMemoryRepository implements RepositoryInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $entityName; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $collectionClass; |
||
25 | |||
26 | /** |
||
27 | * @var CriteriaFactory |
||
28 | */ |
||
29 | protected $criteriaFactory; |
||
30 | |||
31 | /** |
||
32 | * @var EntityFactoryInterface |
||
33 | */ |
||
34 | protected $entityFactory; |
||
35 | |||
36 | /** |
||
37 | * @var ArrayObject |
||
38 | */ |
||
39 | protected $identityMap; |
||
40 | |||
41 | /** |
||
42 | * @var ArrayObject |
||
43 | */ |
||
44 | protected $identityMapOriginal; |
||
45 | |||
46 | /** |
||
47 | * @var EventManagerInterface |
||
48 | */ |
||
49 | protected $eventManager; |
||
50 | |||
51 | /** |
||
52 | * @var EntityChangedEvent |
||
53 | */ |
||
54 | protected $event; |
||
55 | |||
56 | protected $primaryKey = 1; |
||
57 | |||
58 | /** |
||
59 | * @param string $entityName |
||
60 | * @param string $collectionClass |
||
61 | * @param CriteriaFactory $criteriaFactory |
||
62 | * @param EntityFactoryInterface $entityFactory |
||
63 | * @param EventManagerInterface $eventManager |
||
64 | */ |
||
65 | public function __construct( |
||
80 | |||
81 | /** |
||
82 | * @param EntityInterface $entity |
||
83 | * @return EntityInterface|int|null |
||
84 | */ |
||
85 | public function add(EntityInterface $entity) |
||
116 | |||
117 | /** |
||
118 | * @param EntityInterface $entity |
||
119 | * @return int|null |
||
120 | */ |
||
121 | public function remove(EntityInterface $entity) |
||
136 | |||
137 | /** |
||
138 | * @param CriteriaInterface|array $criteria |
||
139 | * @return EntityInterface|null |
||
140 | */ |
||
141 | public function find($criteria) |
||
179 | |||
180 | /** |
||
181 | * @param mixed $id |
||
182 | * @return EntityInterface|null |
||
183 | */ |
||
184 | public function findById($id) |
||
191 | |||
192 | /** |
||
193 | * @param CriteriaInterface|array $criteria |
||
194 | * @return EntityInterface[] |
||
195 | */ |
||
196 | public function findMany($criteria) |
||
230 | |||
231 | /** |
||
232 | * @param CriteriaInterface|array $criteria |
||
233 | * @return int |
||
234 | */ |
||
235 | public function count($criteria) |
||
246 | |||
247 | /** |
||
248 | * @param array $filter |
||
249 | * @return CriteriaInterface |
||
250 | */ |
||
251 | public function createCriteria(array $filter = []) |
||
255 | |||
256 | /** |
||
257 | * @param EntityInterface $entity |
||
258 | */ |
||
259 | protected function toIdentityMap(EntityInterface $entity) |
||
264 | |||
265 | /** |
||
266 | * @param EntityInterface $changedEntity |
||
267 | * @return bool |
||
268 | */ |
||
269 | protected function isEntityChanged(EntityInterface $changedEntity) |
||
274 | |||
275 | /** |
||
276 | * @return EntityChangedEvent |
||
277 | */ |
||
278 | View Code Duplication | protected function getEvent() |
|
287 | |||
288 | /** |
||
289 | * @param EntityInterface $createdEntity |
||
290 | */ |
||
291 | View Code Duplication | protected function triggerCreate(EntityInterface &$createdEntity) |
|
306 | |||
307 | /** |
||
308 | * @param EntityInterface $deletedEntity |
||
309 | */ |
||
310 | View Code Duplication | protected function triggerDelete(EntityInterface $deletedEntity) |
|
321 | |||
322 | /** |
||
323 | * @param EntityChangedEvent $e |
||
324 | */ |
||
325 | protected function triggerChanges(EntityChangedEvent $e) |
||
330 | |||
331 | /** |
||
332 | * @param EntityChangedEvent $e |
||
333 | */ |
||
334 | protected function triggerPreChanges(EntityChangedEvent $e) |
||
339 | |||
340 | /** |
||
341 | * @param EntityChangedEvent $e |
||
342 | */ |
||
343 | View Code Duplication | protected function triggerAttributesChange(EntityChangedEvent $e) |
|
354 | |||
355 | /** |
||
356 | * @param EntityInterface $changedEntity |
||
357 | * @return string |
||
358 | */ |
||
359 | protected function getEntityChangeEventName(EntityInterface $changedEntity) |
||
363 | |||
364 | /** |
||
365 | * @param EntityInterface $changedEntity |
||
366 | * @param $attributeName |
||
367 | * @return string |
||
368 | */ |
||
369 | protected function getAttributeChangeEventName(EntityInterface $changedEntity, $attributeName) |
||
373 | } |
||
374 |
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.