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:
Complex classes like RepositoryTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RepositoryTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class RepositoryTest extends BaseTest |
||
23 | { |
||
24 | /** |
||
25 | * Test for the getContentService() method. |
||
26 | * |
||
27 | * @group content |
||
28 | * @group user |
||
29 | * |
||
30 | * @see \eZ\Publish\API\Repository\Repository::getContentService() |
||
31 | */ |
||
32 | public function testGetContentService() |
||
40 | |||
41 | /** |
||
42 | * Test for the getContentLanguageService() method. |
||
43 | * |
||
44 | * @group language |
||
45 | * |
||
46 | * @see \eZ\Publish\API\Repository\Repository::getContentLanguageService() |
||
47 | */ |
||
48 | public function testGetContentLanguageService() |
||
56 | |||
57 | /** |
||
58 | * Test for the getContentTypeService() method. |
||
59 | * |
||
60 | * @group content-type |
||
61 | * @group field-type |
||
62 | * @group user |
||
63 | * |
||
64 | * @see \eZ\Publish\API\Repository\Repository::getContentTypeService() |
||
65 | */ |
||
66 | public function testGetContentTypeService() |
||
74 | |||
75 | /** |
||
76 | * Test for the getLocationService() method. |
||
77 | * |
||
78 | * @group location |
||
79 | * |
||
80 | * @see \eZ\Publish\API\Repository\Repository::getLocationService() |
||
81 | */ |
||
82 | public function testGetLocationService() |
||
90 | |||
91 | /** |
||
92 | * Test for the getSectionService() method. |
||
93 | * |
||
94 | * @group section |
||
95 | * |
||
96 | * @see \eZ\Publish\API\Repository\Repository::getSectionService() |
||
97 | */ |
||
98 | public function testGetSectionService() |
||
106 | |||
107 | /** |
||
108 | * Test for the getUserService() method. |
||
109 | * |
||
110 | * @group user |
||
111 | * |
||
112 | * @see \eZ\Publish\API\Repository\Repository::getUserService() |
||
113 | */ |
||
114 | public function testGetUserService() |
||
122 | |||
123 | /** |
||
124 | * Test for the getTrashService() method. |
||
125 | * |
||
126 | * @group trash |
||
127 | * |
||
128 | * @see \eZ\Publish\API\Repository\Repository::getTrashService() |
||
129 | */ |
||
130 | public function testGetTrashService() |
||
138 | |||
139 | /** |
||
140 | * Test for the getRoleService() method. |
||
141 | * |
||
142 | * @group role |
||
143 | * |
||
144 | * @see \eZ\Publish\API\Repository\Repository::getRoleService() |
||
145 | */ |
||
146 | public function testGetRoleService() |
||
154 | |||
155 | /** |
||
156 | * Test for the getURLAliasService() method. |
||
157 | * |
||
158 | * @group url-alias |
||
159 | * |
||
160 | * @see \eZ\Publish\API\Repository\Repository::getURLAliasService() |
||
161 | */ |
||
162 | public function testGetURLAliasService() |
||
170 | |||
171 | /** |
||
172 | * Test for the getUrlWildcardService() method. |
||
173 | * |
||
174 | * @group url-wildcard |
||
175 | * |
||
176 | * @see \eZ\Publish\API\Repository\Repository::getUrlWildcardService() |
||
177 | */ |
||
178 | public function testGetURLWildcardService() |
||
186 | |||
187 | /** |
||
188 | * Test for the getObjectStateService(). |
||
189 | * |
||
190 | * @group object-state |
||
191 | * |
||
192 | * @see \eZ\Publish\API\Repository\Repository::getObjectStateService() |
||
193 | */ |
||
194 | public function testGetObjectStateService() |
||
202 | |||
203 | /** |
||
204 | * Test for the getFieldTypeService(). |
||
205 | * |
||
206 | * @group object-state |
||
207 | * |
||
208 | * @see \eZ\Publish\API\Repository\Repository::getFieldTypeService() |
||
209 | */ |
||
210 | public function testGetFieldTypeService() |
||
218 | |||
219 | /** |
||
220 | * Test for the getSearchService() method. |
||
221 | * |
||
222 | * @group search |
||
223 | * |
||
224 | * @see \eZ\Publish\API\Repository\Repository::getSearchService() |
||
225 | */ |
||
226 | public function testGetSearchService() |
||
235 | |||
236 | /** |
||
237 | * Test for the commit() method. |
||
238 | * |
||
239 | * @see \eZ\Publish\API\Repository\Repository::commit() |
||
240 | */ |
||
241 | public function testCommit() |
||
254 | |||
255 | /** |
||
256 | * Test for the commit() method. |
||
257 | * |
||
258 | * @see \eZ\Publish\API\Repository\Repository::commit() |
||
259 | * @expectedException \RuntimeException |
||
260 | */ |
||
261 | public function testCommitThrowsRuntimeException() |
||
266 | |||
267 | /** |
||
268 | * Test for the rollback() method. |
||
269 | * |
||
270 | * @see \eZ\Publish\API\Repository\Repository::rollback() |
||
271 | */ |
||
272 | public function testRollback() |
||
278 | |||
279 | /** |
||
280 | * Test for the rollback() method. |
||
281 | * |
||
282 | * @see \eZ\Publish\API\Repository\Repository::rollback() |
||
283 | * @expectedException \RuntimeException |
||
284 | */ |
||
285 | public function testRollbackThrowsRuntimeException() |
||
290 | |||
291 | /** |
||
292 | * Test for the getCurrentUser() method. |
||
293 | * |
||
294 | * @group content |
||
295 | * @group user |
||
296 | * |
||
297 | * @see \eZ\Publish\API\Repository\Repository::getCurrentUser() |
||
298 | */ |
||
299 | public function testGetCurrentUserReturnsAnonymousUser() |
||
321 | |||
322 | /** |
||
323 | * Test for the setCurrentUser() method. |
||
324 | * |
||
325 | * @group content |
||
326 | * @group user |
||
327 | * |
||
328 | * @see \eZ\Publish\API\Repository\Repository::setCurrentUser() |
||
329 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
330 | */ |
||
331 | public function testSetCurrentUser() |
||
365 | |||
366 | /** |
||
367 | * Test for the hasAccess() method. |
||
368 | * |
||
369 | * @see \eZ\Publish\API\Repository\Repository::hasAccess() |
||
370 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
371 | */ |
||
372 | public function testHasAccessWithAnonymousUserNo() |
||
392 | |||
393 | /** |
||
394 | * Test for the hasAccess() method. |
||
395 | * |
||
396 | * @see \eZ\Publish\API\Repository\Repository::hasAccess() |
||
397 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
398 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessWithAnonymousUserNo |
||
399 | */ |
||
400 | public function testHasAccessForCurrentUserNo() |
||
423 | |||
424 | /** |
||
425 | * Test for the hasAccess() method. |
||
426 | * |
||
427 | * @see \eZ\Publish\API\Repository\Repository::hasAccess() |
||
428 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
429 | */ |
||
430 | public function testHasAccessWithAdministratorUser() |
||
450 | |||
451 | /** |
||
452 | * Test for the hasAccess() method. |
||
453 | * |
||
454 | * @see \eZ\Publish\API\Repository\Repository::hasAccess() |
||
455 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
456 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser |
||
457 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessWithAdministratorUser |
||
458 | */ |
||
459 | public function testHasAccessForCurrentUserYes() |
||
482 | |||
483 | /** |
||
484 | * Test for the hasAccess() method. |
||
485 | * |
||
486 | * @see \eZ\Publish\API\Repository\Repository::hasAccess() |
||
487 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
488 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser |
||
489 | */ |
||
490 | public function testHasAccessLimited() |
||
511 | |||
512 | /** |
||
513 | * Test for the canUser() method. |
||
514 | * |
||
515 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
516 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
517 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
518 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessForCurrentUserNo |
||
519 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
520 | */ |
||
521 | public function testCanUserForAnonymousUserNo() |
||
555 | |||
556 | /** |
||
557 | * Test for the canUser() method. |
||
558 | * |
||
559 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
560 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
561 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
562 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessForCurrentUserYes |
||
563 | * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
564 | */ |
||
565 | public function testCanUserForAdministratorUser() |
||
566 | { |
||
567 | $repository = $this->getRepository(); |
||
568 | |||
569 | $administratorUserId = $this->generateId('user', 14); |
||
570 | $homeId = $this->generateId('object', 57); |
||
571 | |||
572 | /* BEGIN: Use Case */ |
||
573 | // $administratorUserId contains the ID of the administrator user |
||
574 | // $homeId contains the ID of the "Home" frontpage |
||
575 | |||
576 | $contentService = $repository->getContentService(); |
||
577 | $userService = $repository->getUserService(); |
||
578 | |||
579 | // Load administrator user |
||
580 | $administratorUser = $userService->loadUser($administratorUserId); |
||
581 | |||
582 | // Set administrator user as current user |
||
583 | $repository->setCurrentUser($administratorUser); |
||
584 | |||
585 | // Load the ContentInfo for "Home" frontpage |
||
586 | $contentInfo = $contentService->loadContentInfo($homeId); |
||
587 | |||
588 | // This call will return true |
||
589 | $canUser = $repository->canUser('content', 'remove', $contentInfo); |
||
590 | |||
591 | // Performing an action having necessary permissions will succeed |
||
592 | $contentService->deleteContent($contentInfo); |
||
593 | /* END: Use Case */ |
||
594 | |||
595 | $this->assertTrue($canUser); |
||
596 | $contentService->loadContent($homeId); |
||
597 | } |
||
598 | |||
599 | /** |
||
600 | * Test for the canUser() method. |
||
601 | * |
||
602 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
603 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
604 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
605 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
||
606 | */ |
||
607 | View Code Duplication | public function testCanUserWithLimitationYes() |
|
608 | { |
||
609 | $repository = $this->getRepository(); |
||
610 | |||
611 | $imagesFolderId = $this->generateId('object', 49); |
||
612 | |||
613 | /* BEGIN: Use Case */ |
||
614 | // $imagesFolderId contains the ID of the "Images" folder |
||
615 | |||
616 | $user = $this->createUserVersion1(); |
||
617 | |||
618 | // Set created user as current user |
||
619 | $repository->setCurrentUser($user); |
||
620 | |||
621 | $contentService = $repository->getContentService(); |
||
622 | |||
623 | // Performing an action having necessary permissions will succeed |
||
624 | $imagesFolder = $contentService->loadContent($imagesFolderId); |
||
625 | |||
626 | // This call will return true |
||
627 | $canUser = $repository->canUser('content', 'read', $imagesFolder); |
||
628 | /* END: Use Case */ |
||
629 | |||
630 | $this->assertTrue($canUser); |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * Test for the canUser() method. |
||
635 | * |
||
636 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
637 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
638 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
639 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
||
640 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
641 | */ |
||
642 | public function testCanUserWithLimitationNo() |
||
673 | |||
674 | /** |
||
675 | * Test for the canUser() method. |
||
676 | * |
||
677 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
678 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
679 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
680 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser |
||
681 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
||
682 | * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
683 | */ |
||
684 | View Code Duplication | public function testCanUserThrowsInvalidArgumentException() |
|
685 | { |
||
686 | $repository = $this->getRepository(); |
||
687 | |||
688 | $userGroupContentTypeId = $this->generateId('type', 3); |
||
689 | |||
690 | /* BEGIN: Use Case */ |
||
691 | // $userGroupContentTypeId contains the ID of the "UserGroup" ContentType |
||
692 | |||
693 | $user = $this->createUserVersion1(); |
||
694 | |||
695 | // Set created user as current user |
||
696 | $repository->setCurrentUser($user); |
||
697 | |||
698 | $contentTypeService = $repository->getContentTypeService(); |
||
699 | |||
700 | // Load the "UserGroup" ContentType |
||
701 | $userGroupContentType = $contentTypeService->loadContentType($userGroupContentTypeId); |
||
702 | |||
703 | // This call will throw "InvalidArgumentException" because $userGroupContentType |
||
704 | // is an instance of \eZ\Publish\API\Repository\Values\ContentType\ContentType, |
||
705 | // which can not be checked for user access |
||
706 | $canUser = $repository->canUser('content', 'create', $userGroupContentType); |
||
707 | /* END: Use Case */ |
||
708 | } |
||
709 | |||
710 | /** |
||
711 | * Test for the canUser() method. |
||
712 | * |
||
713 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
714 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
715 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
716 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
717 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
||
718 | */ |
||
719 | public function testCanUserWithTargetYes() |
||
768 | |||
769 | /** |
||
770 | * Test for the canUser() method. |
||
771 | * |
||
772 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
773 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
774 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
775 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
776 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
||
777 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
778 | */ |
||
779 | public function testCanUserWithTargetNo() |
||
825 | |||
826 | /** |
||
827 | * Test for the canUser() method. |
||
828 | * |
||
829 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
830 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
831 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
832 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
833 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
||
834 | */ |
||
835 | public function testCanUserWithMultipleTargetsYes() |
||
885 | |||
886 | /** |
||
887 | * Test for the canUser() method. |
||
888 | * |
||
889 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
890 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
891 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
892 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
893 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
||
894 | * @expectedException \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
895 | */ |
||
896 | public function testCanUserWithMultipleTargetsNo() |
||
943 | |||
944 | /** |
||
945 | * Test for the canUser() method. |
||
946 | * |
||
947 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
948 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
949 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
950 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser |
||
951 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
||
952 | * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
953 | */ |
||
954 | View Code Duplication | public function testCanUserWithTargetThrowsInvalidArgumentException() |
|
955 | { |
||
956 | $repository = $this->getRepository(); |
||
957 | |||
958 | $homeId = $this->generateId('object', 57); |
||
959 | |||
960 | /* BEGIN: Use Case */ |
||
961 | // $homeId contains the ID of the "Home" frontpage |
||
962 | |||
963 | $user = $this->createUserVersion1(); |
||
964 | |||
965 | // Set created user as current user |
||
966 | $repository->setCurrentUser($user); |
||
967 | |||
968 | $contentService = $repository->getContentService(); |
||
969 | |||
970 | // Load the ContentInfo for "Home" frontpage |
||
971 | $contentInfo = $contentService->loadContentInfo($homeId); |
||
972 | |||
973 | // This call will throw "InvalidArgumentException" because $targets argument must be an |
||
974 | // instance of \eZ\Publish\API\Repository\Values\ValueObject class or an array of the same |
||
975 | $canUser = $repository->canUser( |
||
976 | 'content', |
||
977 | 'remove', |
||
978 | $contentInfo, |
||
979 | new \stdClass() |
||
980 | ); |
||
981 | /* END: Use Case */ |
||
982 | } |
||
983 | |||
984 | /** |
||
985 | * Test for the canUser() method. |
||
986 | * |
||
987 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
988 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetUserService |
||
989 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentService |
||
990 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetContentTypeService |
||
991 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testGetURLAliasService |
||
992 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testSetCurrentUser |
||
993 | * @depends eZ\Publish\API\Repository\Tests\RepositoryTest::testHasAccessLimited |
||
994 | * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
995 | */ |
||
996 | public function testCanUserWithTargetThrowsInvalidArgumentExceptionVariant() |
||
1029 | |||
1030 | /** |
||
1031 | * Test for the canUser() method. |
||
1032 | * |
||
1033 | * @see \eZ\Publish\API\Repository\Repository::canUser() |
||
1034 | * @expectedException \eZ\Publish\API\Repository\Exceptions\BadStateException |
||
1035 | */ |
||
1036 | public function testCanUserThrowsBadStateException() |
||
1042 | } |
||
1043 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.