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); |
||
18 | abstract class Description implements Visitee |
||
19 | { |
||
20 | use VisiteeMixin; |
||
21 | |||
22 | /** |
||
23 | * @var Path[] |
||
24 | */ |
||
25 | protected $paths; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $host; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $schemes = []; |
||
36 | |||
37 | /** |
||
38 | * @var Document |
||
39 | */ |
||
40 | protected $document; |
||
41 | |||
42 | /** |
||
43 | * @return array |
||
44 | */ |
||
45 | public function getSchemes(): array |
||
49 | |||
50 | /** |
||
51 | * @return string|null |
||
52 | */ |
||
53 | public function getHost() |
||
57 | |||
58 | /** |
||
59 | * @param string $path |
||
60 | * |
||
61 | * @return Path |
||
62 | */ |
||
63 | public function getPath(string $path): Path |
||
73 | |||
74 | /** |
||
75 | * @param string $path |
||
76 | * @param string $method |
||
77 | * |
||
78 | * @return Schema |
||
79 | */ |
||
80 | public function getRequestSchema(string $path, string $method): Schema |
||
84 | |||
85 | /** |
||
86 | * @param string $path |
||
87 | * @param string $method |
||
88 | * |
||
89 | * @param int $code |
||
90 | * |
||
91 | * @return Schema |
||
92 | */ |
||
93 | public function getResponseSchema(string $path, string $method, int $code): Schema |
||
97 | |||
98 | /** |
||
99 | * @return Path[] |
||
100 | */ |
||
101 | public function getPaths(): array |
||
105 | |||
106 | /** |
||
107 | * @return Document |
||
108 | */ |
||
109 | public function getDocument(): Document |
||
113 | } |
||
114 |
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.