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); |
||
20 | class Description implements Visitee, ClosureVisitorScope |
||
21 | { |
||
22 | use VisiteeMixin; |
||
23 | |||
24 | /** |
||
25 | * @var Path[] |
||
26 | */ |
||
27 | protected $paths; |
||
28 | |||
29 | /** |
||
30 | * @var ComplexType[] |
||
31 | */ |
||
32 | protected $complexTypes; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $host; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $schemes = []; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $extensions = []; |
||
48 | |||
49 | /** |
||
50 | * @var Document |
||
51 | */ |
||
52 | protected $document; |
||
53 | |||
54 | /** |
||
55 | * Description constructor. |
||
56 | * |
||
57 | * @param Path[] $paths |
||
58 | * @param ComplexType[] $complexTypes |
||
59 | * @param string $host |
||
60 | * @param array $schemes |
||
61 | * @param array $extensions |
||
62 | * @param Document $document |
||
63 | */ |
||
64 | public function __construct( |
||
79 | |||
80 | /** |
||
81 | * @param string $name |
||
82 | * |
||
83 | * @return mixed |
||
84 | */ |
||
85 | public function getExtension(string $name) |
||
89 | |||
90 | /** |
||
91 | * @return ComplexType[] |
||
92 | */ |
||
93 | public function getComplexTypes(): array |
||
97 | |||
98 | /** |
||
99 | * @return array |
||
100 | */ |
||
101 | public function getSchemes(): array |
||
105 | |||
106 | /** |
||
107 | * @return string|null |
||
108 | */ |
||
109 | public function getHost() |
||
113 | |||
114 | /** |
||
115 | * @param string $path |
||
116 | * |
||
117 | * @return Path |
||
118 | */ |
||
119 | public function getPath(string $path): Path |
||
129 | |||
130 | /** |
||
131 | * @param string $path |
||
132 | * @param string $method |
||
133 | * |
||
134 | * @return Schema |
||
135 | */ |
||
136 | public function getRequestSchema(string $path, string $method): Schema |
||
140 | |||
141 | /** |
||
142 | * @param string $path |
||
143 | * @param string $method |
||
144 | * |
||
145 | * @return Parameter|null |
||
146 | */ |
||
147 | public function getRequestBodyParameter(string $path, string $method) |
||
155 | |||
156 | /** |
||
157 | * @param string $path |
||
158 | * @param string $method |
||
159 | * |
||
160 | * @param int $code |
||
161 | * |
||
162 | * @return Schema |
||
163 | */ |
||
164 | public function getResponseSchema(string $path, string $method, int $code): Schema |
||
168 | |||
169 | /** |
||
170 | * @return Path[] |
||
171 | */ |
||
172 | public function getPaths(): array |
||
176 | |||
177 | /** |
||
178 | * @return Document |
||
179 | */ |
||
180 | public function getDocument(): Document |
||
184 | } |
||
185 |
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.