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 namespace Limoncello\Models; |
||
25 | class ModelSchemes implements ModelSchemesInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $relationshipTypes; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $reversedRelationships; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $reversedClasses; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $foreignKeys; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $belongsToMany; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | private $tableNames; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | private $primaryKeys; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | private $attributeTypes; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | private $attributeLengths; |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | private $attributes; |
||
76 | |||
77 | /** |
||
78 | * @inheritdoc |
||
79 | */ |
||
80 | 5 | public function getData() |
|
97 | |||
98 | /** |
||
99 | * @inheritdoc |
||
100 | */ |
||
101 | 5 | public function setData(array $data) |
|
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | 6 | public function registerClass($class, $tableName, $primaryKey, array $attributeTypes, array $attributeLengths) |
|
121 | |||
122 | /** |
||
123 | * @inheritdoc |
||
124 | */ |
||
125 | 1 | public function hasClass($class) |
|
131 | |||
132 | /** |
||
133 | * @inheritdoc |
||
134 | */ |
||
135 | 2 | public function getTable($class) |
|
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | 3 | public function getPrimaryKey($class) |
|
151 | |||
152 | /** |
||
153 | * @inheritdoc |
||
154 | */ |
||
155 | 1 | public function getAttributeTypes($class) |
|
161 | |||
162 | /** |
||
163 | * @inheritdoc |
||
164 | */ |
||
165 | 1 | public function getAttributeType($class, $name) |
|
171 | |||
172 | /** |
||
173 | * @inheritdoc |
||
174 | */ |
||
175 | 1 | public function hasAttributeType($class, $name) |
|
181 | |||
182 | /** |
||
183 | * @inheritdoc |
||
184 | */ |
||
185 | 1 | public function getAttributeLengths($class) |
|
191 | |||
192 | /** |
||
193 | * @inheritdoc |
||
194 | */ |
||
195 | 1 | public function hasAttributeLength($class, $name) |
|
201 | |||
202 | /** |
||
203 | * @inheritdoc |
||
204 | */ |
||
205 | 1 | public function getAttributeLength($class, $name) |
|
211 | |||
212 | /** |
||
213 | * @inheritdoc |
||
214 | */ |
||
215 | 1 | public function getAttributes($class) |
|
221 | |||
222 | /** |
||
223 | * @inheritdoc |
||
224 | */ |
||
225 | 2 | public function hasRelationship($class, $name) |
|
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | */ |
||
235 | 2 | public function getRelationshipType($class, $name) |
|
241 | |||
242 | /** |
||
243 | * @inheritdoc |
||
244 | */ |
||
245 | 2 | public function getReverseRelationship($class, $name) |
|
251 | |||
252 | /** |
||
253 | * @inheritdoc |
||
254 | */ |
||
255 | 1 | View Code Duplication | public function getReversePrimaryKey($class, $name) |
264 | |||
265 | /** |
||
266 | * @inheritdoc |
||
267 | */ |
||
268 | 1 | View Code Duplication | public function getReverseForeignKey($class, $name) |
278 | |||
279 | /** |
||
280 | * @inheritdoc |
||
281 | */ |
||
282 | 1 | public function getReverseModelClass($class, $name) |
|
288 | |||
289 | /** |
||
290 | * @inheritdoc |
||
291 | */ |
||
292 | 1 | public function getForeignKey($class, $name) |
|
298 | |||
299 | /** |
||
300 | * @inheritdoc |
||
301 | */ |
||
302 | 1 | public function getBelongsToManyRelationship($class, $name) |
|
308 | |||
309 | /** |
||
310 | * @inheritdoc |
||
311 | */ |
||
312 | 5 | public function registerBelongsToOneRelationship($class, $name, $foreignKey, $reverseClass, $reverseName) |
|
324 | |||
325 | /** |
||
326 | * @inheritdoc |
||
327 | */ |
||
328 | 5 | public function registerBelongsToManyRelationship( |
|
351 | |||
352 | /** |
||
353 | * @param int $type |
||
354 | * @param string $class |
||
355 | * @param string $name |
||
356 | */ |
||
357 | 5 | private function registerRelationshipType($type, $class, $name) |
|
366 | |||
367 | /** |
||
368 | * @param string $class |
||
369 | * @param string $name |
||
370 | * @param string $reverseClass |
||
371 | * @param string $reverseName |
||
372 | */ |
||
373 | 5 | private function registerReversedRelationship($class, $name, $reverseClass, $reverseName) |
|
382 | } |
||
383 |
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.