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 |
||
16 | final class Schema |
||
17 | { |
||
18 | /** |
||
19 | * Schema definition. |
||
20 | * |
||
21 | * @var object |
||
22 | */ |
||
23 | private $schema; |
||
24 | |||
25 | /** |
||
26 | * @param object $schema |
||
27 | */ |
||
28 | 15 | public function __construct($schema) |
|
36 | |||
37 | /** |
||
38 | * @return string |
||
39 | */ |
||
40 | 5 | public function getHost() |
|
44 | |||
45 | /** |
||
46 | * @return string |
||
47 | */ |
||
48 | 4 | public function getBasePath() |
|
52 | |||
53 | /** |
||
54 | * @param string $name |
||
55 | * |
||
56 | * @return object |
||
57 | */ |
||
58 | 2 | public function getDefinition($name) |
|
62 | |||
63 | /** |
||
64 | * @return string[] |
||
65 | */ |
||
66 | 1 | public function getDefinitionNames() |
|
70 | |||
71 | /** |
||
72 | * @param string $template |
||
73 | * |
||
74 | * @return object |
||
75 | */ |
||
76 | 6 | public function getPathSchema($template) |
|
80 | |||
81 | /** |
||
82 | * @return string[] |
||
83 | */ |
||
84 | 1 | public function getAvailablePaths() |
|
88 | |||
89 | /** |
||
90 | * @param string $template Schema path template. |
||
91 | * |
||
92 | * @return string[] |
||
93 | */ |
||
94 | 6 | public function getAllowedMethods($template) |
|
109 | |||
110 | /** |
||
111 | * The transfer protocol for the operation. |
||
112 | * The value overrides the top-level schemes definition. |
||
113 | * |
||
114 | * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operationObject |
||
115 | * |
||
116 | * @param string $template |
||
117 | * @param string $method |
||
118 | * |
||
119 | * @return string[] |
||
120 | */ |
||
121 | 4 | View Code Duplication | public function getSupportedSchemes($template, $method) |
137 | |||
138 | /** |
||
139 | * @param string $template |
||
140 | * @param string $method |
||
141 | * |
||
142 | * @return object[] |
||
143 | */ |
||
144 | 6 | public function getRequestHeaderSchemas($template, $method) |
|
148 | |||
149 | /** |
||
150 | * @param string $template |
||
151 | * @param string $method |
||
152 | * |
||
153 | * @return object|null |
||
154 | */ |
||
155 | 5 | public function getRequestBodySchema($template, $method) |
|
176 | |||
177 | /** |
||
178 | * @param string $template |
||
179 | * @param string $method |
||
180 | * |
||
181 | * @return object[] |
||
182 | */ |
||
183 | 4 | public function getRequestPathParameters($template, $method) |
|
187 | |||
188 | /** |
||
189 | * @param string $template |
||
190 | * @param string $method |
||
191 | * |
||
192 | * @return object[] |
||
193 | */ |
||
194 | 4 | public function getRequestQueryParameters($template, $method) |
|
198 | |||
199 | /** |
||
200 | * @param string $template |
||
201 | * @param string $method |
||
202 | * |
||
203 | * @return string[] |
||
204 | */ |
||
205 | 4 | View Code Duplication | public function getRequestContentTypes($template, $method) |
215 | |||
216 | /** |
||
217 | * @param string $template |
||
218 | * @param string $method |
||
219 | * |
||
220 | * @return string[] |
||
221 | */ |
||
222 | 4 | public function getResponseCodes($template, $method) |
|
229 | |||
230 | /** |
||
231 | * @param string $template |
||
232 | * @param string $method |
||
233 | * |
||
234 | * @return string[] |
||
235 | */ |
||
236 | 4 | View Code Duplication | public function getResponseContentTypes($template, $method) |
246 | |||
247 | /** |
||
248 | * @param string $template |
||
249 | * @param string $method |
||
250 | * @param string $status |
||
251 | * |
||
252 | * TODO: Normalize headers list to JSON schema (seems it is validator deals) |
||
253 | * TODO: If status does not defined, check default response declaration |
||
254 | * |
||
255 | * @return object[] |
||
256 | */ |
||
257 | 4 | public function getResponseHeaderSchemas($template, $method, $status) |
|
269 | |||
270 | /** |
||
271 | * Returns body schema. |
||
272 | * |
||
273 | * @param string $template |
||
274 | * @param string $method |
||
275 | * @param string $status |
||
276 | * |
||
277 | * @return object|null |
||
278 | */ |
||
279 | 4 | public function getResponseBodySchema($template, $method, $status) |
|
291 | |||
292 | /** |
||
293 | * @param $schema |
||
294 | * @param array ...$paths |
||
295 | * |
||
296 | * @return mixed |
||
297 | */ |
||
298 | 14 | private static function fetch($schema, ...$paths) |
|
310 | |||
311 | /** |
||
312 | * A list of parameters that are applicable for this operation. |
||
313 | * |
||
314 | * If a parameter is already defined at the Path Item, |
||
315 | * the new definition will override it, but can never remove it. |
||
316 | * |
||
317 | * @param string $template |
||
318 | * @param string $method |
||
319 | * @param string $location |
||
320 | * |
||
321 | * @return object[] |
||
322 | */ |
||
323 | 8 | private function getRequestParameters($template, $method, $location) |
|
339 | |||
340 | /** |
||
341 | * Normalizes parameters definitions. |
||
342 | * |
||
343 | * Filter parameters by location, and use name as list index. |
||
344 | * |
||
345 | * @param array $parameters |
||
346 | * @param string $location |
||
347 | * |
||
348 | * @return object[] |
||
349 | */ |
||
350 | 8 | private function normalizeRequestParameters(array $parameters, $location) |
|
369 | } |
||
370 |
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.