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 |
||
16 | class RoleTest extends RESTFunctionalTestCase |
||
17 | { |
||
18 | /** |
||
19 | * @covers POST /user/roles |
||
20 | * |
||
21 | * BC compatible mode, will return a role |
||
22 | * |
||
23 | * @return string The created role href |
||
24 | */ |
||
25 | View Code Duplication | public function testCreateRole() |
|
26 | { |
||
27 | $xml = <<< XML |
||
28 | <?xml version="1.0" encoding="UTF-8"?> |
||
29 | <RoleInput> |
||
30 | <identifier>testCreateRole</identifier> |
||
31 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
32 | <names> |
||
33 | <value languageCode="eng-GB">testCreateRole</value> |
||
34 | </names> |
||
35 | <descriptions> |
||
36 | <value languageCode="eng-GB">testCreateRole description</value> |
||
37 | </descriptions> |
||
38 | </RoleInput> |
||
39 | XML; |
||
40 | $request = $this->createHttpRequest('POST', '/api/ezp/v2/user/roles', 'RoleInput+xml', 'Role+json'); |
||
41 | $request->setContent($xml); |
||
42 | $response = $this->sendHttpRequest($request); |
||
43 | |||
44 | self::assertHttpResponseCodeEquals($response, 201); |
||
45 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
46 | |||
47 | $href = $response->getHeader('Location'); |
||
48 | $this->addCreatedElement($href); |
||
49 | |||
50 | return $href; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @covers POST /user/roles |
||
55 | * |
||
56 | * BC incompatible mode, will return a role draft |
||
57 | * |
||
58 | * @return string The created role draft href |
||
59 | */ |
||
60 | View Code Duplication | public function testCreateRoleWithDraft() |
|
61 | { |
||
62 | $xml = <<< XML |
||
63 | <?xml version="1.0" encoding="UTF-8"?> |
||
64 | <RoleInput> |
||
65 | <identifier>testCreateRoleDraft</identifier> |
||
66 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
67 | <names> |
||
68 | <value languageCode="eng-GB">testCreateRoleDraft</value> |
||
69 | </names> |
||
70 | <descriptions> |
||
71 | <value languageCode="eng-GB">testCreateRoleDraft description</value> |
||
72 | </descriptions> |
||
73 | </RoleInput> |
||
74 | XML; |
||
75 | $request = $this->createHttpRequest( |
||
76 | 'POST', |
||
77 | '/api/ezp/v2/user/roles?publish=false', |
||
78 | 'RoleInput+xml', |
||
79 | 'RoleDraft+json' |
||
80 | ); |
||
81 | $request->setContent($xml); |
||
82 | $response = $this->sendHttpRequest($request); |
||
83 | |||
84 | self::assertHttpResponseCodeEquals($response, 201); |
||
85 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
86 | |||
87 | $href = $response->getHeader('Location'); |
||
88 | $this->addCreatedElement($href); |
||
89 | |||
90 | return $href . '/draft'; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @covers GET /user/roles |
||
95 | */ |
||
96 | public function testListRoles() |
||
104 | |||
105 | /** |
||
106 | * @depends testCreateRole |
||
107 | * @covers GET /user/roles/{roleId} |
||
108 | */ |
||
109 | public function testLoadRole($roleHref) |
||
117 | |||
118 | /** |
||
119 | * @depends testCreateRole |
||
120 | * @covers POST /user/roles/{roleId} |
||
121 | * |
||
122 | * @return string The created role draft href |
||
123 | */ |
||
124 | View Code Duplication | public function testCreateRoleDraft($roleHref) |
|
125 | { |
||
126 | $xml = <<< XML |
||
127 | <?xml version="1.0" encoding="UTF-8"?> |
||
128 | <RoleInput> |
||
129 | <identifier>testCreateRoleDraft</identifier> |
||
130 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
131 | <names> |
||
132 | <value languageCode="eng-GB">testCreateRoleDraft</value> |
||
133 | </names> |
||
134 | <descriptions> |
||
135 | <value languageCode="eng-GB">testCreateRoleDraft description</value> |
||
136 | </descriptions> |
||
137 | </RoleInput> |
||
138 | XML; |
||
139 | $request = $this->createHttpRequest( |
||
140 | 'POST', |
||
141 | $roleHref, |
||
142 | 'RoleInput+xml', |
||
143 | 'RoleDraft+json' |
||
144 | ); |
||
145 | $request->setContent($xml); |
||
146 | $response = $this->sendHttpRequest($request); |
||
147 | |||
148 | self::assertHttpResponseCodeEquals($response, 201); |
||
149 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
150 | |||
151 | $href = $response->getHeader('Location'); |
||
152 | $this->addCreatedElement($href); |
||
153 | |||
154 | return $href . '/draft'; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @depends testCreateRoleDraft |
||
159 | * @covers GET /user/roles/{roleId}/draft |
||
160 | */ |
||
161 | public function testLoadRoleDraft($roleDraftHref) |
||
162 | { |
||
163 | $response = $this->sendHttpRequest( |
||
164 | $this->createHttpRequest('GET', $roleDraftHref) |
||
165 | ); |
||
166 | |||
167 | self::assertHttpResponseCodeEquals($response, 200); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @depends testCreateRole |
||
172 | * @covers PATCH /user/roles/{roleId} |
||
173 | */ |
||
174 | View Code Duplication | public function testUpdateRole($roleHref) |
|
175 | { |
||
176 | $xml = <<< XML |
||
177 | <?xml version="1.0" encoding="UTF-8"?> |
||
178 | <RoleInput> |
||
179 | <identifier>testUpdateRole</identifier> |
||
180 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
181 | <names> |
||
182 | <value languageCode="eng-GB">testUpdateRole</value> |
||
183 | </names> |
||
184 | <descriptions> |
||
185 | <value languageCode="eng-GB">testUpdateRole description</value> |
||
186 | </descriptions> |
||
187 | </RoleInput> |
||
188 | XML; |
||
189 | |||
190 | $request = $this->createHttpRequest('PATCH', $roleHref, 'RoleInput+xml', 'Role+json'); |
||
191 | $request->setContent($xml); |
||
192 | $response = $this->sendHttpRequest($request); |
||
193 | |||
194 | // @todo Fix failure Notice: Trying to get property of non-object in \/home\/bertrand\/www\/ezpublish-kernel\/eZ\/Publish\/Core\/Persistence\/Cache\/UserHandler.php line 174 |
||
195 | self::assertHttpResponseCodeEquals($response, 200); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @depends testCreateRoleDraft |
||
200 | * @covers PATCH /user/roles/{roleId}/draft |
||
201 | */ |
||
202 | View Code Duplication | public function testUpdateRoleDraft($roleDraftHref) |
|
203 | { |
||
204 | $xml = <<< XML |
||
205 | <?xml version="1.0" encoding="UTF-8"?> |
||
206 | <RoleInput> |
||
207 | <identifier>testUpdateRoleDraft</identifier> |
||
208 | <mainLanguageCode>eng-GB</mainLanguageCode> |
||
209 | <names> |
||
210 | <value languageCode="eng-GB">testUpdateRoleDraft</value> |
||
211 | </names> |
||
212 | <descriptions> |
||
213 | <value languageCode="eng-GB">testUpdateRoleDraft description</value> |
||
214 | </descriptions> |
||
215 | </RoleInput> |
||
216 | XML; |
||
217 | |||
218 | $request = $this->createHttpRequest('PATCH', $roleDraftHref, 'RoleInput+xml', 'RoleDraft+json'); |
||
219 | $request->setContent($xml); |
||
220 | $response = $this->sendHttpRequest($request); |
||
221 | |||
222 | self::assertHttpResponseCodeEquals($response, 200); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @covers POST /user/roles/{roleId}/policies |
||
227 | * @depends testCreateRole |
||
228 | * |
||
229 | * @return string The created policy href |
||
230 | */ |
||
231 | View Code Duplication | public function testAddPolicy($roleHref) |
|
260 | |||
261 | /** |
||
262 | * @covers POST /user/roles/{roleId}/policies |
||
263 | * @depends testCreateRoleDraft |
||
264 | * |
||
265 | * @return string The created policy href |
||
266 | */ |
||
267 | View Code Duplication | public function testAddPolicyByRoleDraft($roleDraftHref) |
|
268 | { |
||
269 | $xml = <<< XML |
||
270 | <?xml version="1.0" encoding="UTF-8"?> |
||
271 | <PolicyCreate> |
||
272 | <module>content</module> |
||
273 | <function>create</function> |
||
274 | <limitations> |
||
275 | <limitation identifier="Class"> |
||
276 | <values> |
||
277 | <ref href="1"/> |
||
278 | </values> |
||
279 | </limitation> |
||
280 | </limitations> |
||
281 | </PolicyCreate> |
||
282 | XML; |
||
283 | $request = $this->createHttpRequest( |
||
284 | 'POST', |
||
285 | $this->roleDraftHrefToRoleHref($roleDraftHref) . '/policies', |
||
286 | 'PolicyCreate+xml', |
||
287 | 'Policy+json' |
||
288 | ); |
||
289 | $request->setContent($xml); |
||
290 | |||
291 | $response = $this->sendHttpRequest($request); |
||
292 | self::assertHttpResponseCodeEquals($response, 201); |
||
293 | self::assertHttpResponseHasHeader($response, 'Location'); |
||
294 | |||
295 | $href = $response->getHeader('Location'); |
||
296 | $this->addCreatedElement($href); |
||
297 | |||
298 | return $href; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @covers GET /user/roles/{roleId}/policies/{policyId} |
||
303 | * @depends testAddPolicy |
||
304 | */ |
||
305 | public function testLoadPolicy($policyHref) |
||
313 | |||
314 | /** |
||
315 | * @covers GET /user/roles/{roleId}/policies |
||
316 | * @depends testCreateRole |
||
317 | */ |
||
318 | public function testLoadPolicies($roleHref) |
||
326 | |||
327 | /** |
||
328 | * @covers PATCH /user/roles/{roleId}/policies/{policyId} |
||
329 | * @depends testAddPolicy |
||
330 | */ |
||
331 | View Code Duplication | public function testUpdatePolicy($policyHref) |
|
332 | { |
||
333 | $xml = <<< XML |
||
334 | <?xml version="1.0" encoding="UTF-8"?> |
||
335 | <PolicyUpdate> |
||
336 | <limitations> |
||
337 | <limitation identifier="Class"> |
||
338 | <values> |
||
339 | <ref href="1"/> |
||
340 | </values> |
||
341 | </limitation> |
||
342 | </limitations> |
||
343 | </PolicyUpdate> |
||
344 | XML; |
||
345 | |||
346 | $request = $this->createHttpRequest('PATCH', $policyHref, 'PolicyUpdate+xml', 'Policy+json'); |
||
347 | $request->setContent($xml); |
||
348 | $response = $this->sendHttpRequest($request); |
||
349 | |||
350 | self::assertHttpResponseCodeEquals($response, 200); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * @covers PATCH /user/roles/{roleId}/policies/{policyId} |
||
355 | * @depends testAddPolicyByRoleDraft |
||
356 | */ |
||
357 | View Code Duplication | public function testUpdatePolicyByRoleDraft($policyHref) |
|
358 | { |
||
359 | $xml = <<< XML |
||
360 | <?xml version="1.0" encoding="UTF-8"?> |
||
361 | <PolicyUpdate> |
||
362 | <limitations> |
||
363 | <limitation identifier="Class"> |
||
364 | <values> |
||
365 | <ref href="1"/> |
||
366 | </values> |
||
367 | </limitation> |
||
368 | </limitations> |
||
369 | </PolicyUpdate> |
||
370 | XML; |
||
371 | |||
372 | $request = $this->createHttpRequest('PATCH', $policyHref, 'PolicyUpdate+xml', 'Policy+json'); |
||
373 | $request->setContent($xml); |
||
374 | $response = $this->sendHttpRequest($request); |
||
375 | |||
376 | self::assertHttpResponseCodeEquals($response, 200); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @depends testCreateRole |
||
381 | * @covers POST /user/users/{userId}/roles |
||
382 | * |
||
383 | * @return string assigned role href |
||
384 | * |
||
385 | * @todo stop using the anonymous user, this is dangerous... |
||
386 | */ |
||
387 | View Code Duplication | public function testAssignRoleToUser($roleHref) |
|
388 | { |
||
389 | $xml = <<< XML |
||
390 | <?xml version="1.0" encoding="UTF-8"?> |
||
391 | <RoleAssignInput> |
||
392 | <Role href="{$roleHref}" media-type="application/vnd.ez.api.RoleAssignInput+xml"/> |
||
393 | </RoleAssignInput> |
||
394 | XML; |
||
395 | |||
396 | $request = $this->createHttpRequest( |
||
397 | 'POST', |
||
398 | '/api/ezp/v2/user/users/10/roles', |
||
399 | 'RoleAssignInput+xml', |
||
400 | 'RoleAssignmentList+json' |
||
401 | ); |
||
402 | $request->setContent($xml); |
||
403 | |||
404 | $response = $this->sendHttpRequest($request); |
||
405 | $roleAssignmentArray = json_decode($response->getContent(), true); |
||
406 | |||
407 | self::assertHttpResponseCodeEquals($response, 200); |
||
408 | |||
409 | return $roleAssignmentArray['RoleAssignmentList']['RoleAssignment'][0]['_href']; |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * @covers POST /user/users/{userId}/roles |
||
414 | * |
||
415 | * @param string $roleHref |
||
|
|||
416 | * @param array $limitation |
||
417 | * |
||
418 | * @return string assigned role href |
||
419 | * @dataProvider provideLimitations |
||
420 | */ |
||
421 | public function testAssignRoleToUserWithLimitation(array $limitation) |
||
422 | { |
||
423 | $roleHref = $this->createAndPublishRole(__METHOD__ . '_' . $limitation['identifier']); |
||
424 | |||
425 | $xml = <<< XML |
||
426 | <?xml version="1.0" encoding="UTF-8"?> |
||
427 | <RoleAssignInput> |
||
428 | <Role href="{$roleHref}" media-type="application/vnd.ez.api.RoleAssignInput+xml"/> |
||
429 | <limitation identifier="{$limitation['identifier']}"> |
||
430 | <values> |
||
431 | <ref href="{$limitation['href']}" media-type="application/vnd.ez.api.{$limitation['identifier']}+xml" /> |
||
432 | </values> |
||
433 | </limitation> |
||
434 | </RoleAssignInput> |
||
435 | XML; |
||
436 | |||
437 | $request = $this->createHttpRequest( |
||
438 | 'POST', |
||
439 | '/api/ezp/v2/user/users/10/roles', |
||
440 | 'RoleAssignInput+xml', |
||
441 | 'RoleAssignmentList+json' |
||
442 | ); |
||
443 | $request->setContent($xml); |
||
444 | |||
445 | $response = $this->sendHttpRequest($request); |
||
446 | $roleAssignmentArray = json_decode($response->getContent(), true); |
||
447 | |||
448 | self::assertHttpResponseCodeEquals($response, 200); |
||
449 | |||
450 | return $roleAssignmentArray['RoleAssignmentList']['RoleAssignment'][0]['_href']; |
||
451 | } |
||
452 | |||
453 | public function provideLimitations() |
||
454 | { |
||
455 | return [ |
||
456 | [['identifier' => 'Section', 'href' => '/api/ezp/v2/content/sections/1']], |
||
457 | [['identifier' => 'Subtree', 'href' => '/api/ezp/v2/content/locations/1/2/']], |
||
458 | ]; |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * @covers GET /user/users/{userId}/roles/{roleId} |
||
463 | * @depends testAssignRoleToUser |
||
464 | */ |
||
465 | public function testLoadRoleAssignmentForUser($roleAssignmentHref) |
||
473 | |||
474 | /** |
||
475 | * @covers DELETE /user/users/{userId}/roles/{roleId} |
||
476 | * @depends testAssignRoleToUser |
||
477 | */ |
||
478 | public function testUnassignRoleFromUser($roleAssignmentHref) |
||
486 | |||
487 | /** |
||
488 | * @depends testCreateRole |
||
489 | * @covers POST /user/groups/{groupId}/roles |
||
490 | * |
||
491 | * @return string role assignment href |
||
492 | */ |
||
493 | View Code Duplication | public function testAssignRoleToUserGroup($roleHref) |
|
494 | { |
||
495 | self::markTestSkipped('Breaks roles, thus preventing login'); |
||
496 | |||
497 | return; |
||
498 | |||
499 | $xml = <<< XML |
||
500 | <?xml version="1.0" encoding="UTF-8"?> |
||
501 | <RoleAssignInput> |
||
502 | <Role href="{$roleHref}" media-type="application/vnd.ez.api.RoleAssignInput+xml"/> |
||
503 | <limitation identifier="Section"> |
||
504 | <values> |
||
505 | <ref href="/api/ezp/v2/content/sections/1" media-type="application/vnd.ez.api.Section+xml" /> |
||
506 | </values> |
||
507 | </limitation> |
||
508 | </RoleAssignInput> |
||
509 | XML; |
||
510 | |||
511 | $request = $this->createHttpRequest( |
||
512 | 'POST', |
||
513 | '/api/ezp/v2/user/groups/1/5/44/roles', |
||
514 | 'RoleAssignInput+xml', |
||
515 | 'RoleAssignmentList+json' |
||
516 | ); |
||
517 | $request->setContent($xml); |
||
518 | |||
519 | $response = $this->sendHttpRequest($request); |
||
520 | $roleAssignmentArray = json_decode($response->getContent(), true); |
||
521 | |||
522 | self::assertHttpResponseCodeEquals($response, 200); |
||
523 | |||
524 | return $roleAssignmentArray['RoleAssignmentList']['RoleAssignment'][0]['_href']; |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * @covers GET /user/groups/{groupId}/roles/{roleId} |
||
529 | * @depends testAssignRoleToUserGroup |
||
530 | */ |
||
531 | View Code Duplication | public function testLoadRoleAssignmentForUserGroup($roleAssignmentHref) |
|
540 | |||
541 | /** |
||
542 | * @covers DELETE /user/groups/{groupId}/roles/{roleId} |
||
543 | * @depends testAssignRoleToUserGroup |
||
544 | */ |
||
545 | View Code Duplication | public function testUnassignRoleFromUserGroup($roleAssignmentHref) |
|
554 | |||
555 | /** |
||
556 | * @covers GET /user/users/{userId}/roles |
||
557 | */ |
||
558 | public function testLoadRoleAssignmentsForUser() |
||
566 | |||
567 | /** |
||
568 | * @covers GET /user/groups/{groupPath}/roles |
||
569 | */ |
||
570 | public function testLoadRoleAssignmentsForUserGroup() |
||
578 | |||
579 | /** |
||
580 | * @covers GET /user/policies?userId={userId} |
||
581 | */ |
||
582 | public function testListPoliciesForUser() |
||
590 | |||
591 | /** |
||
592 | * @covers DELETE /user/roles/{roleId}/policies/{policyId} |
||
593 | * @depends testAddPolicy |
||
594 | */ |
||
595 | public function testDeletePolicy($policyHref) |
||
603 | |||
604 | /** |
||
605 | * @covers DELETE /user/roles/{roleId}/policies/{policyId} |
||
606 | * @depends testAddPolicyByRoleDraft |
||
607 | */ |
||
608 | public function testRemovePolicyByRoleDraft($policyHref) |
||
616 | |||
617 | /** |
||
618 | * @covers DELETE /user/roles/{roleId}/policies |
||
619 | * @depends testCreateRole |
||
620 | */ |
||
621 | public function testDeletePolicies($roleHref) |
||
629 | |||
630 | /** |
||
631 | * @covers DELETE /user/roles/{roleId} |
||
632 | * @depends testCreateRole |
||
633 | */ |
||
634 | public function testDeleteRole($roleHref) |
||
642 | |||
643 | /** |
||
644 | * @covers PUBLISH /user/roles/{roleId}/draft |
||
645 | * @depends testCreateRoleDraft |
||
646 | */ |
||
647 | View Code Duplication | public function testPublishRoleDraft($roleDraftHref) |
|
660 | |||
661 | /** |
||
662 | * @covers DELETE /user/roles/{roleId}/draft |
||
663 | * @depends testCreateRoleDraft |
||
664 | */ |
||
665 | public function testDeleteRoleDraft($roleDraftHref) |
||
676 | |||
677 | /** |
||
678 | * Helper method for changing a roledraft href to a role href. |
||
679 | * |
||
680 | * @param string $roleDraftHref Role draft href |
||
681 | * |
||
682 | * @return string Role href |
||
683 | */ |
||
684 | private function roleDraftHrefToRoleHref($roleDraftHref) |
||
688 | |||
689 | /** |
||
690 | * Creates and publishes a role with $identifier. |
||
691 | * |
||
692 | * @param string $identifier |
||
693 | * |
||
694 | * @return string The href of the published role |
||
695 | */ |
||
696 | private function createAndPublishRole($identifier) |
||
725 | } |
||
726 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.