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); |
||
29 | trait EncoderPropertiesTrait |
||
30 | { |
||
31 | /** |
||
32 | * @var SchemaContainerInterface |
||
33 | */ |
||
34 | private $container; |
||
35 | |||
36 | /** |
||
37 | * @var FactoryInterface |
||
38 | */ |
||
39 | private $factory; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $urlPrefix; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $includePaths; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private $fieldSets; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | private $encodeOptions; |
||
60 | |||
61 | /** |
||
62 | * @var int |
||
63 | */ |
||
64 | private $encodeDepth; |
||
65 | |||
66 | /** |
||
67 | * @var iterable |
||
68 | */ |
||
69 | private $links; |
||
70 | |||
71 | /** |
||
72 | * @var iterable |
||
73 | */ |
||
74 | private $profile; |
||
75 | |||
76 | /** |
||
77 | * @var bool |
||
78 | */ |
||
79 | private $hasMeta; |
||
80 | |||
81 | /** |
||
82 | * @var mixed |
||
83 | */ |
||
84 | private $meta; |
||
85 | |||
86 | /** |
||
87 | * @var string|null |
||
88 | */ |
||
89 | private $jsonApiVersion; |
||
90 | |||
91 | /** |
||
92 | * @var mixed |
||
93 | */ |
||
94 | private $jsonApiMeta; |
||
95 | |||
96 | /** |
||
97 | * @var bool |
||
98 | */ |
||
99 | private $hasJsonApiMeta; |
||
100 | |||
101 | /** |
||
102 | * Reset to initial state. |
||
103 | * |
||
104 | * @param string $urlPrefix |
||
105 | * @param iterable $includePaths |
||
106 | * @param array $fieldSets |
||
107 | * @param int $encodeOptions |
||
108 | * @param int $encodeDepth |
||
109 | * |
||
110 | * @return EncoderPropertiesTrait |
||
|
|||
111 | */ |
||
112 | 81 | protected function reset( |
|
136 | |||
137 | /** |
||
138 | * @return SchemaContainerInterface |
||
139 | */ |
||
140 | 70 | protected function getSchemaContainer(): SchemaContainerInterface |
|
144 | |||
145 | /** |
||
146 | * @param SchemaContainerInterface $container |
||
147 | * |
||
148 | * @return self |
||
149 | */ |
||
150 | 81 | public function setContainer(SchemaContainerInterface $container): self |
|
156 | |||
157 | /** |
||
158 | * @return FactoryInterface |
||
159 | */ |
||
160 | 79 | protected function getFactory(): FactoryInterface |
|
164 | |||
165 | /** |
||
166 | * @param FactoryInterface $factory |
||
167 | * |
||
168 | * @return self |
||
169 | */ |
||
170 | 81 | public function setFactory(FactoryInterface $factory): self |
|
176 | |||
177 | /** |
||
178 | * @param string $prefix |
||
179 | * |
||
180 | * @return self|EncoderInterface |
||
181 | */ |
||
182 | 81 | public function withUrlPrefix(string $prefix): EncoderInterface |
|
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | 79 | protected function getUrlPrefix(): string |
|
195 | |||
196 | /** |
||
197 | * @param iterable $paths |
||
198 | * |
||
199 | * @return self|EncoderInterface |
||
200 | */ |
||
201 | 81 | public function withIncludedPaths(iterable $paths): EncoderInterface |
|
221 | |||
222 | /** |
||
223 | * @return array |
||
224 | */ |
||
225 | 70 | protected function getIncludePaths(): array |
|
229 | |||
230 | /** |
||
231 | * @param array $fieldSets |
||
232 | * |
||
233 | * @return self|EncoderInterface |
||
234 | */ |
||
235 | 81 | public function withFieldSets(array $fieldSets): EncoderInterface |
|
241 | |||
242 | |||
243 | /** |
||
244 | * @return array |
||
245 | */ |
||
246 | 70 | protected function getFieldSets(): array |
|
250 | |||
251 | /** |
||
252 | * @param int $options |
||
253 | * |
||
254 | * @return self|EncoderInterface |
||
255 | */ |
||
256 | 81 | public function withEncodeOptions(int $options): EncoderInterface |
|
262 | |||
263 | /** |
||
264 | * @return int |
||
265 | */ |
||
266 | 73 | protected function getEncodeOptions(): int |
|
270 | |||
271 | /** |
||
272 | * @param int $depth |
||
273 | * |
||
274 | * @return self|EncoderInterface |
||
275 | */ |
||
276 | 81 | public function withEncodeDepth(int $depth): EncoderInterface |
|
284 | |||
285 | /** |
||
286 | * @return int |
||
287 | */ |
||
288 | 73 | protected function getEncodeDepth(): int |
|
292 | |||
293 | /** |
||
294 | * @param iterable $links |
||
295 | * |
||
296 | * @return self|EncoderInterface |
||
297 | */ |
||
298 | 5 | public function withLinks(iterable $links): EncoderInterface |
|
304 | |||
305 | /** |
||
306 | * @return bool |
||
307 | */ |
||
308 | 79 | protected function hasLinks(): bool |
|
312 | |||
313 | /** |
||
314 | * @return iterable |
||
315 | */ |
||
316 | 5 | protected function getLinks(): iterable |
|
320 | |||
321 | /** |
||
322 | * @param iterable $links |
||
323 | * |
||
324 | * @return self|EncoderInterface |
||
325 | */ |
||
326 | 1 | public function withProfile(iterable $links): EncoderInterface |
|
332 | |||
333 | /** |
||
334 | * @return bool |
||
335 | */ |
||
336 | 79 | protected function hasProfile(): bool |
|
340 | |||
341 | /** |
||
342 | * @return iterable |
||
343 | */ |
||
344 | 1 | protected function getProfile(): iterable |
|
348 | |||
349 | /** |
||
350 | * @param mixed $meta |
||
351 | * |
||
352 | * @return self|EncoderInterface |
||
353 | */ |
||
354 | 6 | public function withMeta($meta): EncoderInterface |
|
361 | |||
362 | /** |
||
363 | * @return bool |
||
364 | */ |
||
365 | 79 | protected function hasMeta(): bool |
|
369 | |||
370 | /** |
||
371 | * @return mixed |
||
372 | */ |
||
373 | 6 | public function getMeta() |
|
377 | |||
378 | /** |
||
379 | * @param string $version |
||
380 | * |
||
381 | * @return self|EncoderInterface |
||
382 | */ |
||
383 | 2 | public function withJsonApiVersion(string $version): EncoderInterface |
|
389 | |||
390 | /** |
||
391 | * @return bool |
||
392 | */ |
||
393 | 79 | protected function hasJsonApiVersion(): bool |
|
397 | |||
398 | /** |
||
399 | * @return string |
||
400 | */ |
||
401 | 2 | protected function getJsonApiVersion(): string |
|
405 | |||
406 | /** |
||
407 | * @param mixed $meta |
||
408 | * |
||
409 | * @return self|EncoderInterface |
||
410 | */ |
||
411 | 2 | public function withJsonApiMeta($meta): EncoderInterface |
|
418 | |||
419 | /** |
||
420 | * @return bool |
||
421 | */ |
||
422 | 79 | protected function hasJsonApiMeta(): bool |
|
426 | |||
427 | /** |
||
428 | * @return mixed |
||
429 | */ |
||
430 | 2 | protected function getJsonApiMeta() |
|
434 | |||
435 | /** |
||
436 | * @param mixed $resource |
||
437 | * @param string $relationshipName |
||
438 | * |
||
439 | * @return self|EncoderInterface |
||
440 | */ |
||
441 | 1 | View Code Duplication | public function withRelationshipSelfLink($resource, string $relationshipName): EncoderInterface |
451 | |||
452 | /** |
||
453 | * @param mixed $resource |
||
454 | * @param string $relationshipName |
||
455 | * |
||
456 | * @return self|EncoderInterface |
||
457 | */ |
||
458 | 1 | View Code Duplication | public function withRelationshipRelatedLink($resource, string $relationshipName): EncoderInterface |
468 | |||
469 | /** |
||
470 | * @param iterable $iterable1 |
||
471 | * @param iterable $iterable2 |
||
472 | * |
||
473 | * @return iterable |
||
474 | */ |
||
475 | 1 | private function mergeIterables(iterable $iterable1, iterable $iterable2): iterable |
|
480 | } |
||
481 |
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.