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 |
||
9 | abstract class AbstractResourceCest |
||
10 | { |
||
11 | /** |
||
12 | * @var UrlManager url manager used to parse Route's for the services. |
||
13 | */ |
||
14 | protected $urlManager; |
||
15 | |||
16 | /** |
||
17 | * Initializes the `$urlManager` object |
||
18 | */ |
||
19 | public function __construct() |
||
31 | |||
32 | /** |
||
33 | * Authenticates a user identified by 'authUser' index in the `$example`. |
||
34 | * |
||
35 | * @param Tester $I |
||
36 | * @param Example $example data used for the testing. It uses the keys |
||
|
|||
37 | * - tokenName: (optional) string name of the token used to authenticate. |
||
38 | */ |
||
39 | protected function authUser(Tester $I, Example $example) |
||
45 | |||
46 | /** |
||
47 | * Parses the Route for the test using `$urlManager` and `getRoutePattern()` |
||
48 | * |
||
49 | * @param Example $example data used for the testing. It uses the keys |
||
50 | * - url: (optional) string if provided it will be used directly. |
||
51 | * - urlParams: (optional) string[] GET params to parse the url rule with |
||
52 | * `getRoutePattern()`. |
||
53 | * @return string the url which will be used for the service. |
||
54 | */ |
||
55 | protected function parseUrl(Example $example): string |
||
65 | |||
66 | /** |
||
67 | * Handles the internal logic when running a test on a collection resource. |
||
68 | * |
||
69 | * @param Tester $I |
||
70 | * @param Example $example data used for the testing. It uses the keys |
||
71 | * - tokenName: (optional) string name of the token used to authenticate. |
||
72 | * - url: (optional) string if provided it will be used directly. |
||
73 | * - urlParams: (optional) string[] GET params to parse the url rule with |
||
74 | * `getRoutePattern()`. |
||
75 | * - headers: (optional) string[] pairs of header => value expected in the |
||
76 | * response. |
||
77 | */ |
||
78 | View Code Duplication | protected function internalIndex(Tester $I, Example $example) |
|
92 | |||
93 | /** |
||
94 | * Handles the internal logic when running a test on a creating a record. |
||
95 | * |
||
96 | * @param Tester $I |
||
97 | * @param Example $example data used for the testing. It uses the keys |
||
98 | * - tokenName: (optional) string name of the token used to authenticate. |
||
99 | * - url: (optional) string if provided it will be used directly. |
||
100 | * - urlParams: (optional) string[] GET params to parse the url rule with |
||
101 | * `getRoutePattern()`. |
||
102 | * - data: (optional) POST data sent on the request. |
||
103 | * - headers: (optional) string[] pairs of header => value expected in the |
||
104 | * response. |
||
105 | */ |
||
106 | protected function internalCreate(Tester $I, Example $example) |
||
123 | |||
124 | /** |
||
125 | * Handles the internal logic when running a test on a record resource. |
||
126 | * |
||
127 | * @param Tester $I |
||
128 | * @param Example $example data used for the testing. It uses the keys |
||
129 | * - tokenName: (optional) string name of the token used to authenticate. |
||
130 | * - url: string if provided it will be used directly. |
||
131 | * - urlParams: string[] GET params to parse the url rule with |
||
132 | * `getRoutePattern()`. |
||
133 | * - httpCode: integer expected Http Code response. If the httpCode is |
||
134 | * a key in `$responses` parameter then the method defined there will be |
||
135 | * used to analyze the response. Otherwise `checkErrorResponse()` will be |
||
136 | * used. |
||
137 | * - error: string|string[] expected json being contained in the response. |
||
138 | * - headers: (optional) string[] pairs of header => value expected in the |
||
139 | * response. |
||
140 | */ |
||
141 | View Code Duplication | protected function internalView(Tester $I, Example $example) |
|
154 | |||
155 | /** |
||
156 | * Handles the internal logic when running a test on a updating a record. |
||
157 | * |
||
158 | * @param Tester $I |
||
159 | * @param Example $example data used for the testing. It uses the keys |
||
160 | * - tokenName: (optional) string name of the token used to authenticate. |
||
161 | * - url: (optional) string if provided it will be used directly. |
||
162 | * - urlParams: (optional) string[] GET params to parse the url rule with |
||
163 | * `getRoutePattern()`. |
||
164 | * - data: (optional) POST data sent on the request. |
||
165 | * - expected: (optional) expected in the JSON response body. |
||
166 | * - headers: (optional) string[] pairs of header => value expected in the |
||
167 | * response. |
||
168 | */ |
||
169 | protected function internalUpdate(Tester $I, Example $example) |
||
187 | |||
188 | /** |
||
189 | * Handles the internal logic when running a test on a updating a record. |
||
190 | * |
||
191 | * @param Tester $I |
||
192 | * @param Example $example data used for the testing. It uses the keys |
||
193 | * - tokenName: (optional) string name of the token used to authenticate. |
||
194 | * - url: (optional) string if provided it will be used directly. |
||
195 | * - urlParams: (optional) string[] GET params to parse the url rule with |
||
196 | * `getRoutePattern()`. |
||
197 | * - httpCode: integer expected Http Code response. |
||
198 | */ |
||
199 | protected function internalDelete(Tester $I, Example $example) |
||
210 | |||
211 | /** |
||
212 | * Checks the response Http code and the response based on the Http code and |
||
213 | * a list of pairs 'httpCode' => 'responseMethod' provided in the |
||
214 | * `$responses` parameter. |
||
215 | * |
||
216 | * If the Http Code in the response doesn't match any of the keys in |
||
217 | * `$responses` then `checkErrorResponse` will be used instead. |
||
218 | * |
||
219 | * @param array $responses pairs of 'httpCode' => 'responseMethod' which |
||
220 | * will determine how to check the response. |
||
221 | * @param Tester $I |
||
222 | * @param Example $example data used for the testing. It uses the keys |
||
223 | * - httpCode: integer expected Http Code response. If the httpCode is |
||
224 | * a key in `$responses` parameter then the method defined there will be |
||
225 | * used to analyze the response. Otherwise `checkErrorResponse()` will be |
||
226 | * used. |
||
227 | * - error: (optional) mixed expected json being contained in the response. |
||
228 | * - headers: (optional) string[] pairs of header => value expected in the |
||
229 | * response. |
||
230 | */ |
||
231 | protected function checkResponse( |
||
249 | |||
250 | /** |
||
251 | * Checks an expected success collection response. |
||
252 | * |
||
253 | * @param Tester $I |
||
254 | * @param Example $example |
||
255 | */ |
||
256 | protected function checkSuccessIndexResponse(Tester $I, Example $example) |
||
262 | |||
263 | /** |
||
264 | * Checks an expected success record response. |
||
265 | * |
||
266 | * @param Tester $I |
||
267 | * @param Example $example |
||
268 | */ |
||
269 | protected function checkSuccessViewResponse(Tester $I, Example $example) |
||
274 | |||
275 | /** |
||
276 | * Checks an expected success record creation response. |
||
277 | * |
||
278 | * @param Tester $I |
||
279 | * @param Example $example |
||
280 | */ |
||
281 | protected function checkSuccessCreateResponse(Tester $I, Example $example) |
||
287 | |||
288 | /** |
||
289 | * Checks an expected response containing validation errors. |
||
290 | * |
||
291 | * @param Tester $I |
||
292 | * @param Example $example data used for the testing. It uses the keys |
||
293 | * - validationErrors: string[] (optional) pairs of field => message |
||
294 | * validation errors. |
||
295 | */ |
||
296 | protected function checkValidationResponse(Tester $I, Example $example) |
||
315 | |||
316 | /** |
||
317 | * Checks an expected error response from user input. |
||
318 | * |
||
319 | * @param Tester $I |
||
320 | * @param Example $example data used for the testing. It uses the keys |
||
321 | * - error: (optional) mixed expected json being contained in the response. |
||
322 | */ |
||
323 | protected function checkErrorResponse(Tester $I, Example $example) |
||
334 | |||
335 | /** |
||
336 | * @param Tester $I |
||
337 | * @param string $expected the expected self link. |
||
338 | */ |
||
339 | protected function checkSelfLink(Tester $I, string $expected) |
||
347 | |||
348 | /** |
||
349 | * @return string the path which will be used to obtain the self link of a |
||
350 | * record. Return empty string to skip that check. |
||
351 | */ |
||
352 | protected function selfLinkPath(): string |
||
356 | |||
357 | /** |
||
358 | * Expected Json Type for each resource. |
||
359 | * |
||
360 | * @return array |
||
361 | * @see http://codeception.com/docs/modules/REST#seeResponseMatchesJsonType |
||
362 | */ |
||
363 | abstract protected function recordJsonType(): array; |
||
364 | |||
365 | /** |
||
366 | * @return string route pattern to create the |
||
367 | */ |
||
368 | abstract protected function getRoutePattern(): string; |
||
369 | } |
||
370 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.