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 |
||
13 | class OpenApiSchema extends Schema |
||
14 | { |
||
15 | |||
16 | protected $serverVariables = []; |
||
17 | |||
18 | |||
19 | View Code Duplication | public function __construct($jsonFile) |
|
26 | |||
27 | public function getServerUrl() |
||
48 | |||
49 | public function getBasePath() |
||
54 | |||
55 | /** |
||
56 | * @param $parameterIn |
||
57 | * @param $parameters |
||
58 | * @param $arguments |
||
59 | * @throws DefinitionNotFoundException |
||
60 | * @throws InvalidDefinitionException |
||
61 | * @throws NotMatchedException |
||
62 | */ |
||
63 | protected function validateArguments($parameterIn, $parameters, $arguments) |
||
87 | |||
88 | /** |
||
89 | * @param $name |
||
90 | * @return mixed |
||
91 | * @throws DefinitionNotFoundException |
||
92 | * @throws InvalidDefinitionException |
||
93 | */ |
||
94 | public function getDefinition($name) |
||
95 | { |
||
96 | $nameParts = explode('/', $name); |
||
97 | |||
98 | View Code Duplication | if (count($nameParts) < 4 || $nameParts[0] !== '#') { |
|
99 | throw new InvalidDefinitionException('Invalid Component'); |
||
100 | } |
||
101 | |||
102 | View Code Duplication | if (!isset($this->jsonFile[$nameParts[1]][$nameParts[2]][$nameParts[3]])) { |
|
103 | throw new DefinitionNotFoundException("Component'$name' not found"); |
||
104 | } |
||
105 | |||
106 | return $this->jsonFile[$nameParts[1]][$nameParts[2]][$nameParts[3]]; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param $path |
||
111 | * @param $method |
||
112 | * @return OpenApiRequestBody |
||
113 | * @throws DefinitionNotFoundException |
||
114 | * @throws HttpMethodNotFoundException |
||
115 | * @throws InvalidDefinitionException |
||
116 | * @throws NotMatchedException |
||
117 | * @throws PathNotFoundException |
||
118 | */ |
||
119 | public function getRequestParameters($path, $method) |
||
128 | |||
129 | public function setServerVariable($var, $value) |
||
133 | } |
||
134 |
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.