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\Application\Data; |
||
26 | class ModelSchemaInfo implements ModelSchemaInfoInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $relationshipTypes = []; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private $reversedRelationships = []; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | private $reversedClasses = []; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | private $foreignKeys = []; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | private $belongsToMany = []; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $tableNames = []; |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | private $primaryKeys = []; |
||
62 | |||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | private $attributeTypes = []; |
||
67 | |||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | private $attributeLengths = []; |
||
72 | |||
73 | /** |
||
74 | * @var array |
||
75 | */ |
||
76 | private $attributes = []; |
||
77 | |||
78 | /** |
||
79 | * @return array |
||
80 | */ |
||
81 | 8 | public function getData(): array |
|
98 | |||
99 | /** |
||
100 | * @param array $data |
||
101 | * |
||
102 | * @return self |
||
103 | */ |
||
104 | 7 | public function setData(array $data): self |
|
112 | |||
113 | /** |
||
114 | * @inheritdoc |
||
115 | */ |
||
116 | 8 | public function registerClass( |
|
143 | |||
144 | /** |
||
145 | * @inheritdoc |
||
146 | */ |
||
147 | 1 | public function hasClass(string $class): bool |
|
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | 2 | public function getTable(string $class): string |
|
163 | |||
164 | /** |
||
165 | * @inheritdoc |
||
166 | */ |
||
167 | 2 | public function getPrimaryKey(string $class): string |
|
173 | |||
174 | /** |
||
175 | * @inheritdoc |
||
176 | */ |
||
177 | 1 | public function getAttributeTypes(string $class): array |
|
183 | |||
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | 1 | public function getAttributeType(string $class, string $name): string |
|
193 | |||
194 | /** |
||
195 | * @inheritdoc |
||
196 | */ |
||
197 | 1 | public function hasAttributeType(string $class, string $name): bool |
|
203 | |||
204 | /** |
||
205 | * @inheritdoc |
||
206 | */ |
||
207 | 1 | public function getAttributeLengths(string $class): array |
|
213 | |||
214 | /** |
||
215 | * @inheritdoc |
||
216 | */ |
||
217 | 1 | public function hasAttributeLength(string $class, string $name): bool |
|
223 | |||
224 | /** |
||
225 | * @inheritdoc |
||
226 | */ |
||
227 | 1 | public function getAttributeLength(string $class, string $name): int |
|
233 | |||
234 | /** |
||
235 | * @inheritdoc |
||
236 | */ |
||
237 | 1 | public function getAttributes(string $class): array |
|
243 | |||
244 | /** |
||
245 | * @inheritdoc |
||
246 | */ |
||
247 | 2 | public function hasRelationship(string $class, string $name): bool |
|
253 | |||
254 | /** |
||
255 | * @inheritdoc |
||
256 | */ |
||
257 | 2 | public function getRelationshipType(string $class, string $name): int |
|
263 | |||
264 | /** |
||
265 | * @inheritdoc |
||
266 | */ |
||
267 | 2 | public function getReverseRelationship(string $class, string $name): array |
|
273 | |||
274 | /** |
||
275 | * @inheritdoc |
||
276 | */ |
||
277 | 1 | View Code Duplication | public function getReversePrimaryKey(string $class, string $name): array |
286 | |||
287 | /** |
||
288 | * @inheritdoc |
||
289 | */ |
||
290 | 1 | View Code Duplication | public function getReverseForeignKey(string $class, string $name): array |
300 | |||
301 | /** |
||
302 | * @inheritdoc |
||
303 | */ |
||
304 | 1 | public function getReverseModelClass(string $class, string $name): string |
|
310 | |||
311 | /** |
||
312 | * @inheritdoc |
||
313 | */ |
||
314 | 1 | public function getForeignKey(string $class, string $name): string |
|
320 | |||
321 | /** |
||
322 | * @inheritdoc |
||
323 | */ |
||
324 | 1 | public function getBelongsToManyRelationship(string $class, string $name): array |
|
330 | |||
331 | /** |
||
332 | * @inheritdoc |
||
333 | */ |
||
334 | 8 | public function registerBelongsToOneRelationship( |
|
351 | |||
352 | /** @noinspection PhpTooManyParametersInspection |
||
353 | * @inheritdoc |
||
354 | */ |
||
355 | 8 | public function registerBelongsToManyRelationship( |
|
378 | |||
379 | /** |
||
380 | * @param int $type |
||
381 | * @param string $class |
||
382 | * @param string $name |
||
383 | * |
||
384 | * @return void |
||
385 | */ |
||
386 | 8 | private function registerRelationshipType(int $type, string $class, string $name): void |
|
396 | |||
397 | /** |
||
398 | * @param string $class |
||
399 | * @param string $name |
||
400 | * @param string $reverseClass |
||
401 | * @param string $reverseName |
||
402 | * |
||
403 | * @return void |
||
404 | */ |
||
405 | 8 | private function registerReversedRelationship( |
|
425 | } |
||
426 |
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.