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 |
||
26 | class HasOne extends BelongsTo |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * Handles the before select callback |
||
31 | * |
||
32 | * @param Select $event |
||
33 | */ |
||
34 | View Code Duplication | public function beforeSelect(Select $event) |
|
51 | |||
52 | /** |
||
53 | * Gets the foreign key field name |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | View Code Duplication | public function getForeignKey() |
|
66 | |||
67 | /** |
||
68 | * Loads the entity or entity collection for this relation |
||
69 | * |
||
70 | * @param EntityInterface $entity |
||
71 | * |
||
72 | * @return null|EntityInterface |
||
73 | */ |
||
74 | public function load(EntityInterface $entity) |
||
94 | |||
95 | /** |
||
96 | * Deletes the related entity for before deleting current entity |
||
97 | * |
||
98 | * @param Delete $event |
||
99 | */ |
||
100 | public function beforeDelete(Delete $event) |
||
107 | |||
108 | /** |
||
109 | * Registers the listener for before select event |
||
110 | */ |
||
111 | protected function registerListeners() |
||
131 | |||
132 | /** |
||
133 | * Check if entity is already loaded and uses it. |
||
134 | * |
||
135 | * If not loaded the entity will be created and loaded to the repository's |
||
136 | * identity map so that it can be reused next time. |
||
137 | * |
||
138 | * @param array $dataRow |
||
139 | * |
||
140 | * @return null|EntityCollection|EntityInterface|EntityInterface[] |
||
141 | */ |
||
142 | protected function getFromMap($dataRow) |
||
156 | |||
157 | } |
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.