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:
Complex classes like SwaggerSchema often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SwaggerSchema, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class SwaggerSchema |
||
18 | { |
||
19 | protected $jsonFile; |
||
20 | protected $allowNullValues; |
||
21 | protected $specificationVersion; |
||
22 | |||
23 | const SWAGGER_PATHS="paths"; |
||
24 | const SWAGGER_PARAMETERS="parameters"; |
||
25 | const SWAGGER_COMPONENTS="components"; |
||
26 | |||
27 | public function __construct($jsonFile, $allowNullValues = false) |
||
33 | |||
34 | /** |
||
35 | * Returns the major specification version |
||
36 | * @return string |
||
37 | */ |
||
38 | public function getSpecificationVersion() |
||
42 | |||
43 | public function getServerUrl() |
||
47 | |||
48 | public function getHttpSchema() |
||
52 | |||
53 | public function getHost() |
||
57 | |||
58 | public function getBasePath() |
||
67 | |||
68 | /** |
||
69 | * @param $path |
||
70 | * @param $method |
||
71 | * @return mixed |
||
72 | * @throws DefinitionNotFoundException |
||
73 | * @throws HttpMethodNotFoundException |
||
74 | * @throws InvalidDefinitionException |
||
75 | * @throws NotMatchedException |
||
76 | * @throws PathNotFoundException |
||
77 | */ |
||
78 | public function getPathDefinition($path, $method) |
||
128 | |||
129 | /** |
||
130 | * @param $parameterIn |
||
131 | * @param $parameters |
||
132 | * @param $arguments |
||
133 | * @throws DefinitionNotFoundException |
||
134 | * @throws InvalidDefinitionException |
||
135 | * @throws NotMatchedException |
||
136 | */ |
||
137 | private function validateArguments($parameterIn, $parameters, $arguments) |
||
172 | |||
173 | /** |
||
174 | * @param $name |
||
175 | * @return mixed |
||
176 | * @throws DefinitionNotFoundException |
||
177 | * @throws InvalidDefinitionException |
||
178 | */ |
||
179 | public function getDefintion($name) |
||
205 | |||
206 | /** |
||
207 | * @param $path |
||
208 | * @param $method |
||
209 | * @return \ByJG\Swagger\SwaggerRequestBody |
||
210 | * @throws DefinitionNotFoundException |
||
211 | * @throws HttpMethodNotFoundException |
||
212 | * @throws InvalidDefinitionException |
||
213 | * @throws NotMatchedException |
||
214 | * @throws PathNotFoundException |
||
215 | */ |
||
216 | public function getRequestParameters($path, $method) |
||
232 | |||
233 | /** |
||
234 | * @param $path |
||
235 | * @param $method |
||
236 | * @param $status |
||
237 | * @return \ByJG\Swagger\SwaggerResponseBody |
||
238 | * @throws \ByJG\Swagger\Exception\HttpMethodNotFoundException |
||
239 | * @throws \ByJG\Swagger\Exception\InvalidDefinitionException |
||
240 | * @throws \ByJG\Swagger\Exception\NotMatchedException |
||
241 | * @throws \ByJG\Swagger\Exception\PathNotFoundException |
||
242 | * @throws DefinitionNotFoundException |
||
243 | */ |
||
244 | public function getResponseParameters($path, $method, $status) |
||
254 | |||
255 | /** |
||
256 | * OpenApi 2.0 doesn't describe null values, so this flag defines, |
||
257 | * if match is ok when one of property |
||
258 | * |
||
259 | * @return bool |
||
260 | */ |
||
261 | public function isAllowNullValues() |
||
265 | |||
266 | /** |
||
267 | * OpenApi 2.0 doesn't describe null values, so this flag defines, |
||
268 | * if match is ok when one of property |
||
269 | * |
||
270 | * @param $value |
||
271 | */ |
||
272 | public function setAllowNullValues($value) |
||
276 | } |
||
277 |
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.