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 BelongsTo extends AbstractRelation implements RelationInterface |
||
27 | { |
||
28 | /** |
||
29 | * Relations utility methods |
||
30 | */ |
||
31 | use RelationsUtilityMethods; |
||
32 | |||
33 | /** |
||
34 | * @readwrite |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $dependent = true; |
||
38 | |||
39 | /** |
||
40 | * BelongsTo relation |
||
41 | * |
||
42 | * @param array|object $options The parameters from annotation |
||
43 | */ |
||
44 | 26 | public function __construct($options) |
|
57 | |||
58 | /** |
||
59 | * Handles the before select callback |
||
60 | * |
||
61 | * @param Select $event |
||
62 | */ |
||
63 | 4 | View Code Duplication | public function beforeSelect(Select $event) |
|
|||
64 | { |
||
65 | 4 | if ($this->isLazyLoaded()) { |
|
66 | 2 | return; |
|
67 | } |
||
68 | |||
69 | 2 | $fields = $this->getFieldsPrefixed(); |
|
70 | 2 | $table = $this->entityDescriptor->getTableName(); |
|
71 | 2 | $relateTable = $this->getParentTableName(); |
|
72 | 2 | $pmk = $this->getParentPrimaryKey(); |
|
73 | |||
74 | 2 | $onClause = "{$table}.{$this->getForeignKey()} = ". |
|
75 | 2 | "{$relateTable}.{$pmk}"; |
|
76 | |||
77 | 2 | $query = $event->getQuery(); |
|
78 | 2 | $query->join($relateTable, $onClause, $fields, $relateTable); |
|
79 | 2 | } |
|
80 | |||
81 | /** |
||
82 | * Handles the after select callback |
||
83 | * |
||
84 | * @param Select $event |
||
85 | */ |
||
86 | 6 | public function afterSelect(Select $event) |
|
96 | |||
97 | 2 | public function beforeSave(Save $event) |
|
108 | |||
109 | /** |
||
110 | * Registers the listener for before select event |
||
111 | */ |
||
112 | 14 | protected function registerListeners() |
|
138 | |||
139 | /** |
||
140 | * Prefixed fields for join |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | 4 | protected function getFieldsPrefixed() |
|
155 | |||
156 | /** |
||
157 | * Check if entity is already loaded and uses it. |
||
158 | * |
||
159 | * If not loaded the entity will be created and loaded to the repository's |
||
160 | * identity map so that it can be reused next time. |
||
161 | * |
||
162 | * @param array $dataRow |
||
163 | * |
||
164 | * @return null|EntityCollection|EntityInterface|EntityInterface[] |
||
165 | */ |
||
166 | 2 | protected function getFromMap($dataRow) |
|
176 | |||
177 | /** |
||
178 | * Creates and maps related entity |
||
179 | * |
||
180 | * @param array $dataRow |
||
181 | * |
||
182 | * @return null|EntityCollection|EntityInterface|EntityInterface[] |
||
183 | */ |
||
184 | 4 | protected function map($dataRow) |
|
193 | |||
194 | /** |
||
195 | * Gets a data array with fields and values for parent entity creation |
||
196 | * |
||
197 | * @param array $dataRow |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | 4 | protected function getData($dataRow) |
|
213 | |||
214 | /** |
||
215 | * Loads the entity or entity collection for this relation |
||
216 | * |
||
217 | * @param EntityInterface $entity |
||
218 | * |
||
219 | * @return null|EntityInterface |
||
220 | */ |
||
221 | 2 | public function load(EntityInterface $entity) |
|
248 | } |
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.