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 |
||
23 | trait Asserts |
||
24 | { |
||
25 | /** |
||
26 | * Assert request matches against declared specification. |
||
27 | * |
||
28 | * The list of constraints: |
||
29 | * |
||
30 | * - Assert request method defined |
||
31 | * - Assert request URI declared by host, basePath, schemes and parameters (path, query) |
||
32 | * - Assert content-type declared by consumes |
||
33 | * - Assert headers declared by parameters (header) |
||
34 | * - Assert body declared by parameters (body) |
||
35 | * |
||
36 | * @param Spec $spec |
||
37 | * @param string $template |
||
38 | * @param Request $request |
||
39 | * @param string $msg |
||
40 | */ |
||
41 | 4 | final protected static function assertRequest(Spec $spec, $template, Request $request, $msg = '') |
|
48 | |||
49 | /** |
||
50 | * Assert response matches against declared specification. |
||
51 | * |
||
52 | * The list of constraints: |
||
53 | * |
||
54 | * - Assert response status code or default is defined |
||
55 | * - Assert content-type declared by produces from operation |
||
56 | * - Assert headers |
||
57 | * - Assert body |
||
58 | * |
||
59 | * @param Spec $spec |
||
60 | * @param string $template |
||
61 | * @param string $method |
||
62 | * @param Response $response |
||
63 | * @param string $msg |
||
64 | */ |
||
65 | 3 | final protected static function assertResponse(Spec $spec, $template, $method, Response $response, $msg = '') |
|
85 | |||
86 | /** |
||
87 | * Assert URI matches against declared host, basePath, schemes and parameters (path, query). |
||
88 | * |
||
89 | * The list of constraints: |
||
90 | * |
||
91 | * - Assert URI scheme matches allowed schemes |
||
92 | * - Assert URI host matches defined |
||
93 | * - Assert URI path starts with defined base path |
||
94 | * - Assert URI path matches defined template and path parameters |
||
95 | * - Assert URI path matches defined query parameters |
||
96 | * |
||
97 | * @param Spec $spec |
||
98 | * @param string $template |
||
99 | * @param string $method |
||
100 | * @param Uri $uri |
||
101 | * @param string $msg |
||
102 | */ |
||
103 | 3 | final protected static function assertUri(Spec $spec, $template, $method, Uri $uri, $msg = '') |
|
118 | |||
119 | /** |
||
120 | * Assert the endpoint supports given operation. |
||
121 | * |
||
122 | * @param Spec $spec |
||
123 | * @param string $template |
||
124 | * @param string $method |
||
125 | * @param string $msg |
||
126 | */ |
||
127 | 4 | final protected static function assertMethodAllowed(Spec $spec, $template, $method, $msg = '') |
|
135 | |||
136 | /** |
||
137 | * Assert the response status code defined. |
||
138 | * |
||
139 | * @param Spec $spec |
||
140 | * @param string $template |
||
141 | * @param string $method |
||
142 | * @param string $status |
||
143 | * @param string $msg |
||
144 | */ |
||
145 | 3 | final protected static function assertResponseDefined(Spec $spec, $template, $method, $status, $msg = '') |
|
152 | |||
153 | /** |
||
154 | * Assert the endpoint supports given operation. |
||
155 | * |
||
156 | * @param Spec $spec |
||
157 | * @param string $template |
||
158 | * @param string $method |
||
159 | * @param string $contentType |
||
160 | * @param string $msg |
||
161 | */ |
||
162 | 3 | final protected static function assertRequestContentType(Spec $spec, $template, $method, $contentType, $msg = '') |
|
170 | |||
171 | /** |
||
172 | * Assert the endpoint supports given operation. |
||
173 | * |
||
174 | * @param Spec $spec |
||
175 | * @param string $template |
||
176 | * @param string $method |
||
177 | * @param string $contentType |
||
178 | * @param string $msg |
||
179 | */ |
||
180 | 3 | final protected static function assertResponseContentType(Spec $spec, $template, $method, $contentType, $msg = '') |
|
188 | |||
189 | /** |
||
190 | * @param Spec $spec |
||
191 | * @param string $template |
||
192 | * @param string $method |
||
193 | * @param array $headers |
||
194 | * @param string $msg |
||
195 | */ |
||
196 | 3 | final protected static function assertRequestHeaders(Spec $spec, $template, $method, array $headers, $msg = '') |
|
214 | |||
215 | /** |
||
216 | * @param Spec $spec |
||
217 | * @param string $template |
||
218 | * @param string $method |
||
219 | * @param string $status |
||
220 | * @param array $headers |
||
221 | * @param string $msg |
||
222 | */ |
||
223 | 3 | final protected static function assertResponseHeaders(Spec $spec, $template, $method, $status, array $headers, $msg = '') |
|
255 | |||
256 | /** |
||
257 | * @param Spec $spec |
||
258 | * @param string $template |
||
259 | * @param string $method |
||
260 | * @param Stream|null $body |
||
261 | * @param string $msg |
||
262 | */ |
||
263 | 2 | View Code Duplication | final protected static function assertRequestBody(Spec $spec, $template, $method, Stream $body = null, $msg = '') |
277 | |||
278 | /** |
||
279 | * @param Spec $spec |
||
280 | * @param string $template |
||
281 | * @param string $method |
||
282 | * @param string $status |
||
283 | * @param Stream|null $body |
||
284 | * @param string $msg |
||
285 | */ |
||
286 | 3 | View Code Duplication | final protected static function assertResponseBody(Spec $spec, $template, $method, $status, Stream $body = null, $msg = '') |
300 | |||
301 | /** |
||
302 | * @param Spec $spec |
||
303 | * @param string $class |
||
304 | * @param mixed $actual |
||
305 | * @param string $msg |
||
306 | */ |
||
307 | 1 | final protected static function assertDefinitionSchema(Spec $spec, $class, $actual, $msg = '') |
|
315 | } |
||
316 |
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.