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 declare(strict_types=1); |
||
37 | class ModelSchemaInfo implements ModelSchemaInfoInterface |
||
38 | { |
||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | private $relationshipTypes = []; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | private $reversedRelationships = []; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | private $reversedClasses = []; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private $foreignKeys = []; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | private $belongsToMany = []; |
||
63 | |||
64 | /** |
||
65 | * @var array |
||
66 | */ |
||
67 | private $tableNames = []; |
||
68 | |||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | private $primaryKeys = []; |
||
73 | |||
74 | /** |
||
75 | * @var array |
||
76 | */ |
||
77 | private $attributeTypes = []; |
||
78 | |||
79 | /** |
||
80 | * @var array |
||
81 | */ |
||
82 | private $attributeLengths = []; |
||
83 | |||
84 | /** |
||
85 | * @var array |
||
86 | */ |
||
87 | private $attributes = []; |
||
88 | 8 | ||
89 | /** |
||
90 | * @var array |
||
91 | 8 | */ |
|
92 | 8 | private $rawAttributes = []; |
|
93 | 8 | ||
94 | 8 | /** |
|
95 | 8 | * @return array |
|
96 | 8 | */ |
|
97 | 8 | public function getData(): array |
|
98 | 8 | { |
|
99 | 8 | $result = [ |
|
100 | 8 | $this->foreignKeys, |
|
101 | 8 | $this->belongsToMany, |
|
102 | $this->relationshipTypes, |
||
103 | $this->reversedRelationships, |
||
104 | 8 | $this->tableNames, |
|
105 | $this->primaryKeys, |
||
106 | $this->attributeTypes, |
||
107 | $this->attributeLengths, |
||
108 | $this->attributes, |
||
109 | $this->rawAttributes, |
||
110 | $this->reversedClasses, |
||
111 | ]; |
||
112 | 7 | ||
113 | return $result; |
||
114 | 7 | } |
|
115 | 7 | ||
116 | 7 | /** |
|
117 | 7 | * @param array $data |
|
118 | * |
||
119 | 7 | * @return self |
|
120 | */ |
||
121 | public function setData(array $data): self |
||
122 | { |
||
123 | list($this->foreignKeys, $this->belongsToMany, $this->relationshipTypes, |
||
124 | $this->reversedRelationships,$this->tableNames, $this->primaryKeys, |
||
125 | $this->attributeTypes, $this->attributeLengths, $this->attributes, $this->rawAttributes, |
||
126 | $this->reversedClasses) = $data; |
||
127 | 8 | ||
128 | return $this; |
||
129 | } |
||
130 | |||
131 | /** @noinspection PhpTooManyParametersInspection |
||
132 | * @inheritdoc |
||
133 | * |
||
134 | * @throws ReflectionException |
||
135 | 8 | */ |
|
136 | 1 | public function registerClass( |
|
137 | string $class, |
||
138 | string $tableName, |
||
139 | 8 | string $primaryKey, |
|
140 | 1 | array $attributeTypes, |
|
141 | array $attributeLengths, |
||
142 | array $rawAttributes = [] |
||
143 | 8 | ): ModelSchemaInfoInterface { |
|
144 | 1 | if (empty($class) === true) { |
|
145 | throw new InvalidArgumentException('class'); |
||
146 | } |
||
147 | 8 | ||
148 | 8 | if (empty($tableName) === true) { |
|
149 | 8 | throw new InvalidArgumentException('tableName'); |
|
150 | } |
||
151 | |||
152 | 8 | if (empty($primaryKey) === true) { |
|
153 | 8 | throw new InvalidArgumentException('primaryKey'); |
|
154 | 8 | } |
|
155 | 8 | ||
156 | 8 | assert( |
|
157 | 8 | (new ReflectionClass($class))->getName() === $class, |
|
158 | "Please check name for class `$class`. It should be case sensitive." |
||
159 | 8 | ); |
|
160 | |||
161 | $this->tableNames[$class] = $tableName; |
||
162 | $this->primaryKeys[$class] = $primaryKey; |
||
163 | $this->attributeTypes[$class] = $attributeTypes; |
||
164 | $this->attributeLengths[$class] = $attributeLengths; |
||
165 | 2 | $this->attributes[$class] = array_keys($attributeTypes); |
|
166 | $this->rawAttributes[$class] = $rawAttributes; |
||
167 | 2 | ||
168 | return $this; |
||
169 | } |
||
170 | 2 | ||
171 | 2 | /** |
|
172 | 2 | * @inheritdoc |
|
173 | */ |
||
174 | public function hasClass(string $class): bool |
||
175 | 2 | { |
|
176 | $result = array_key_exists($class, $this->tableNames); |
||
177 | |||
178 | // check if not found it cannot be found case insensitive (protection from case insensitive values) |
||
179 | assert( |
||
180 | $result === true || |
||
181 | 2 | in_array(strtolower($class), array_change_key_case($this->tableNames, CASE_LOWER)) === false |
|
182 | ); |
||
183 | 2 | ||
184 | return $result; |
||
185 | 2 | } |
|
186 | |||
187 | 2 | /** |
|
188 | * @inheritdoc |
||
189 | */ |
||
190 | public function getTable(string $class): string |
||
198 | |||
199 | 2 | /** |
|
200 | * @inheritdoc |
||
201 | */ |
||
202 | public function getPrimaryKey(string $class): string |
||
210 | |||
211 | 1 | /** |
|
212 | * @inheritdoc |
||
213 | */ |
||
214 | public function getAttributeTypes(string $class): array |
||
215 | { |
||
216 | assert($this->hasClass($class)); |
||
217 | 1 | ||
218 | $result = $this->attributeTypes[$class]; |
||
219 | 1 | ||
220 | 1 | return $result; |
|
221 | 1 | } |
|
222 | |||
223 | /** |
||
224 | 1 | * @inheritdoc |
|
225 | */ |
||
226 | 1 | public function getAttributeType(string $class, string $name): string |
|
227 | { |
||
228 | assert( |
||
229 | $this->hasAttributeType($class, $name), |
||
230 | "Type is not defined for attribute `$name` in class `$class`." |
||
231 | ); |
||
232 | 1 | ||
233 | $result = $this->attributeTypes[$class][$name]; |
||
234 | 1 | ||
235 | return $result; |
||
236 | 1 | } |
|
237 | |||
238 | 1 | /** |
|
239 | * @inheritdoc |
||
240 | */ |
||
241 | View Code Duplication | public function hasAttributeType(string $class, string $name): bool |
|
249 | |||
250 | 1 | /** |
|
251 | * @inheritdoc |
||
252 | */ |
||
253 | public function getAttributeLengths(string $class): array |
||
261 | |||
262 | 1 | /** |
|
263 | * @inheritdoc |
||
264 | */ |
||
265 | View Code Duplication | public function hasAttributeLength(string $class, string $name): bool |
|
266 | { |
||
267 | assert($this->hasClass($class)); |
||
268 | 1 | ||
269 | $result = isset($this->attributeLengths[$class][$name]); |
||
270 | 1 | ||
271 | 1 | return $result; |
|
272 | 1 | } |
|
273 | |||
274 | /** |
||
275 | 1 | * @inheritdoc |
|
276 | */ |
||
277 | 1 | View Code Duplication | public function getAttributeLength(string $class, string $name): int |
278 | { |
||
279 | assert( |
||
280 | $this->hasAttributeLength($class, $name) === true, |
||
281 | "Length not found for column `$name` in class `$class`." |
||
282 | ); |
||
283 | 1 | ||
284 | $result = $this->attributeLengths[$class][$name]; |
||
285 | 1 | ||
286 | return $result; |
||
287 | 1 | } |
|
288 | |||
289 | 1 | /** |
|
290 | * @inheritdoc |
||
291 | */ |
||
292 | public function getAttributes(string $class): array |
||
300 | |||
301 | 1 | /** |
|
302 | * @inheritdoc |
||
303 | */ |
||
304 | public function getRawAttributes(string $class): array |
||
312 | |||
313 | /** |
||
314 | * @inheritdoc |
||
315 | */ |
||
316 | public function hasRelationship(string $class, string $name): bool |
||
317 | 2 | { |
|
318 | $result = isset($this->relationshipTypes[$class][$name]); |
||
319 | 2 | ||
320 | 2 | return $result; |
|
322 | |||
323 | /** |
||
324 | 2 | * @inheritdoc |
|
325 | */ |
||
326 | 2 | View Code Duplication | public function getRelationshipType(string $class, string $name): int |
337 | |||
338 | /** |
||
339 | * @inheritdoc |
||
340 | */ |
||
341 | public function getReverseRelationship(string $class, string $name): array |
||
347 | 1 | ||
348 | /** |
||
349 | 1 | * @inheritdoc |
|
350 | */ |
||
351 | View Code Duplication | public function getReversePrimaryKey(string $class, string $name): array |
|
360 | |||
361 | 1 | /** |
|
362 | * @inheritdoc |
||
363 | 1 | */ |
|
364 | View Code Duplication | public function getReverseForeignKey(string $class, string $name): array |
|
374 | |||
375 | /** |
||
376 | * @inheritdoc |
||
377 | */ |
||
378 | public function getReverseModelClass(string $class, string $name): string |
||
384 | |||
385 | /** |
||
386 | * @inheritdoc |
||
387 | */ |
||
388 | public function getForeignKey(string $class, string $name): string |
||
394 | |||
395 | /** |
||
396 | * @inheritdoc |
||
397 | */ |
||
398 | public function getBelongsToManyRelationship(string $class, string $name): array |
||
404 | |||
405 | /** |
||
406 | 8 | * @inheritdoc |
|
407 | 8 | */ |
|
408 | public function registerBelongsToOneRelationship( |
||
425 | |||
426 | /** @noinspection PhpTooManyParametersInspection |
||
427 | * @inheritdoc |
||
428 | */ |
||
429 | 8 | public function registerBelongsToManyRelationship( |
|
452 | |||
453 | 8 | /** |
|
454 | 8 | * @param int $type |
|
455 | 8 | * @param string $class |
|
456 | 8 | * @param string $name |
|
457 | * |
||
458 | * @return void |
||
459 | 8 | */ |
|
460 | private function registerRelationshipType(int $type, string $class, string $name): void |
||
470 | 8 | ||
471 | /** |
||
472 | * @param string $class |
||
473 | * @param string $name |
||
474 | * @param string $reverseClass |
||
475 | * @param string $reverseName |
||
476 | 8 | * |
|
477 | 8 | * @return void |
|
478 | 8 | */ |
|
479 | 8 | private function registerReversedRelationship( |
|
499 | } |
||
500 |
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.