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 |
||
14 | class OperationObject |
||
15 | { |
||
16 | /** |
||
17 | * @var SwaggerDocument |
||
18 | */ |
||
19 | private $document; |
||
20 | |||
21 | /** |
||
22 | * @var object |
||
23 | */ |
||
24 | private $definition; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $path; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $method; |
||
35 | |||
36 | /** |
||
37 | * @param SwaggerDocument $document |
||
38 | * @param string $path |
||
39 | * @param string $method |
||
40 | */ |
||
41 | public function __construct(SwaggerDocument $document, $path, $method) |
||
59 | |||
60 | /** |
||
61 | * @param object $definition |
||
62 | * @param string $path |
||
63 | * @param string $method |
||
64 | * |
||
65 | * @return static |
||
66 | */ |
||
67 | public static function createFromOperationDefinition($definition, $path = '/', $method = 'GET') |
||
81 | |||
82 | /** |
||
83 | * @return object |
||
84 | */ |
||
85 | public function getRequestSchema() |
||
89 | |||
90 | /** |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function hasParameters() |
||
97 | |||
98 | /** |
||
99 | * @return object |
||
100 | */ |
||
101 | public function getParameters() |
||
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | public function getPath() |
||
113 | |||
114 | /** |
||
115 | * @return string |
||
116 | */ |
||
117 | public function getMethod() |
||
121 | |||
122 | /** |
||
123 | * @return object |
||
124 | */ |
||
125 | public function getDefinition() |
||
129 | |||
130 | /** |
||
131 | * @param string $parameterName |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | View Code Duplication | public function createParameterPointer($parameterName) |
|
150 | |||
151 | /** |
||
152 | * @param string $parameterName |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | View Code Duplication | public function createParameterSchemaPointer($parameterName) |
|
172 | |||
173 | /** |
||
174 | * @return object |
||
175 | */ |
||
176 | private function assembleRequestSchema() |
||
206 | } |
||
207 |
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.