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 Repository 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 Repository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Repository implements RepositoryInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Repository Handler object. |
||
| 34 | * |
||
| 35 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
| 36 | */ |
||
| 37 | protected $persistenceHandler; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Instance of main Search Handler. |
||
| 41 | * |
||
| 42 | * @var \eZ\Publish\SPI\Search\Handler |
||
| 43 | */ |
||
| 44 | protected $searchHandler; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Currently logged in user object if already loaded. |
||
| 48 | * |
||
| 49 | * @var \eZ\Publish\API\Repository\Values\User\User|null |
||
| 50 | */ |
||
| 51 | protected $currentUser; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Currently logged in user reference for permission purposes. |
||
| 55 | * |
||
| 56 | * @var \eZ\Publish\API\Repository\Values\User\UserReference |
||
| 57 | */ |
||
| 58 | protected $currentUserRef; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Counter for the current sudo nesting level {@see sudo()}. |
||
| 62 | * |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | private $sudoNestingLevel = 0; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Instance of content service. |
||
| 69 | * |
||
| 70 | * @var \eZ\Publish\API\Repository\ContentService |
||
| 71 | */ |
||
| 72 | protected $contentService; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Instance of section service. |
||
| 76 | * |
||
| 77 | * @var \eZ\Publish\API\Repository\SectionService |
||
| 78 | */ |
||
| 79 | protected $sectionService; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Instance of role service. |
||
| 83 | * |
||
| 84 | * @var \eZ\Publish\API\Repository\RoleService |
||
| 85 | */ |
||
| 86 | protected $roleService; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Instance of search service. |
||
| 90 | * |
||
| 91 | * @var \eZ\Publish\API\Repository\SearchService |
||
| 92 | */ |
||
| 93 | protected $searchService; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Instance of user service. |
||
| 97 | * |
||
| 98 | * @var \eZ\Publish\API\Repository\UserService |
||
| 99 | */ |
||
| 100 | protected $userService; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Instance of language service. |
||
| 104 | * |
||
| 105 | * @var \eZ\Publish\API\Repository\LanguageService |
||
| 106 | */ |
||
| 107 | protected $languageService; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Instance of location service. |
||
| 111 | * |
||
| 112 | * @var \eZ\Publish\API\Repository\LocationService |
||
| 113 | */ |
||
| 114 | protected $locationService; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Instance of Trash service. |
||
| 118 | * |
||
| 119 | * @var \eZ\Publish\API\Repository\TrashService |
||
| 120 | */ |
||
| 121 | protected $trashService; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Instance of content type service. |
||
| 125 | * |
||
| 126 | * @var \eZ\Publish\API\Repository\ContentTypeService |
||
| 127 | */ |
||
| 128 | protected $contentTypeService; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Instance of object state service. |
||
| 132 | * |
||
| 133 | * @var \eZ\Publish\API\Repository\ObjectStateService |
||
| 134 | */ |
||
| 135 | protected $objectStateService; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Instance of field type service. |
||
| 139 | * |
||
| 140 | * @var \eZ\Publish\API\Repository\FieldTypeService |
||
| 141 | */ |
||
| 142 | protected $fieldTypeService; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Instance of FieldTypeRegistry. |
||
| 146 | * |
||
| 147 | * @var \eZ\Publish\Core\Repository\Helper\FieldTypeRegistry |
||
| 148 | */ |
||
| 149 | private $fieldTypeRegistry; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Instance of name schema resolver service. |
||
| 153 | * |
||
| 154 | * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 155 | */ |
||
| 156 | protected $nameSchemaService; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Instance of relation processor service. |
||
| 160 | * |
||
| 161 | * @var \eZ\Publish\Core\Repository\Helper\RelationProcessor |
||
| 162 | */ |
||
| 163 | protected $relationProcessor; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Instance of URL alias service. |
||
| 167 | * |
||
| 168 | * @var \eZ\Publish\Core\Repository\URLAliasService |
||
| 169 | */ |
||
| 170 | protected $urlAliasService; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Instance of URL wildcard service. |
||
| 174 | * |
||
| 175 | * @var \eZ\Publish\Core\Repository\URLWildcardService |
||
| 176 | */ |
||
| 177 | protected $urlWildcardService; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Service settings, first level key is service name. |
||
| 181 | * |
||
| 182 | * @var array |
||
| 183 | */ |
||
| 184 | protected $serviceSettings; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Instance of role service. |
||
| 188 | * |
||
| 189 | * @var \eZ\Publish\Core\Repository\Helper\LimitationService |
||
| 190 | */ |
||
| 191 | protected $limitationService; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var \eZ\Publish\Core\Repository\Helper\RoleDomainMapper |
||
| 195 | */ |
||
| 196 | protected $roleDomainMapper; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Instance of domain mapper. |
||
| 200 | * |
||
| 201 | * @var \eZ\Publish\Core\Repository\Helper\DomainMapper |
||
| 202 | */ |
||
| 203 | protected $domainMapper; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Instance of permissions criterion handler. |
||
| 207 | * |
||
| 208 | * @var \eZ\Publish\Core\Repository\PermissionsCriterionHandler |
||
| 209 | */ |
||
| 210 | protected $permissionsCriterionHandler; |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Array of arrays of commit events indexed by the transaction count. |
||
| 214 | * |
||
| 215 | * @var array |
||
| 216 | */ |
||
| 217 | protected $commitEventsQueue = array(); |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @var int |
||
| 221 | */ |
||
| 222 | protected $transactionDepth = 0; |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @var int |
||
| 226 | */ |
||
| 227 | private $transactionCount = 0; |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Constructor. |
||
| 231 | * |
||
| 232 | * Construct repository object with provided storage engine |
||
| 233 | * |
||
| 234 | * @param \eZ\Publish\SPI\Persistence\Handler $persistenceHandler |
||
| 235 | * @param \eZ\Publish\SPI\Search\Handler $searchHandler |
||
| 236 | * @param array $serviceSettings |
||
| 237 | * @param \eZ\Publish\API\Repository\Values\User\UserReference|null $user |
||
| 238 | */ |
||
| 239 | public function __construct( |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get current user. |
||
| 284 | * |
||
| 285 | * Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()} |
||
| 286 | * |
||
| 287 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 288 | */ |
||
| 289 | public function getCurrentUser() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get current user reference. |
||
| 302 | * |
||
| 303 | * @since 5.4.5 |
||
| 304 | * @return \eZ\Publish\API\Repository\Values\User\UserReference |
||
| 305 | */ |
||
| 306 | public function getCurrentUserReference() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Sets the current user to the given $user. |
||
| 313 | * |
||
| 314 | * @param \eZ\Publish\API\Repository\Values\User\UserReference $user |
||
| 315 | * |
||
| 316 | * @throws InvalidArgumentValue If UserReference does not contain a id |
||
| 317 | */ |
||
| 318 | public function setCurrentUser(APIUserReference $user) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Allows API execution to be performed with full access sand-boxed. |
||
| 336 | * |
||
| 337 | * The closure sandbox will do a catch all on exceptions and rethrow after |
||
| 338 | * re-setting the sudo flag. |
||
| 339 | * |
||
| 340 | * Example use: |
||
| 341 | * $location = $repository->sudo( |
||
| 342 | * function ( Repository $repo ) use ( $locationId ) |
||
| 343 | * { |
||
| 344 | * return $repo->getLocationService()->loadLocation( $locationId ) |
||
| 345 | * } |
||
| 346 | * ); |
||
| 347 | * |
||
| 348 | * |
||
| 349 | * @param \Closure $callback |
||
| 350 | * @param \eZ\Publish\API\Repository\Repository $outerRepository |
||
| 351 | * |
||
| 352 | * @throws \RuntimeException Thrown on recursive sudo() use. |
||
| 353 | * @throws \Exception Re throws exceptions thrown inside $callback |
||
| 354 | * |
||
| 355 | * @return mixed |
||
| 356 | */ |
||
| 357 | public function sudo(\Closure $callback, RepositoryInterface $outerRepository = null) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Check if user has access to a given module / function. |
||
| 374 | * |
||
| 375 | * Low level function, use canUser instead if you have objects to check against. |
||
| 376 | * |
||
| 377 | * @param string $module |
||
| 378 | * @param string $function |
||
| 379 | * @param \eZ\Publish\API\Repository\Values\User\UserReference $user |
||
| 380 | * |
||
| 381 | * @return bool|array Bool if user has full or no access, array if limitations if not |
||
| 382 | */ |
||
| 383 | public function hasAccess($module, $function, APIUserReference $user = null) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Check if user has access to a given action on a given value object. |
||
| 447 | * |
||
| 448 | * Indicates if the current user is allowed to perform an action given by the function on the given |
||
| 449 | * objects. |
||
| 450 | * |
||
| 451 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If any of the arguments are invalid |
||
| 452 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If value of the LimitationValue is unsupported |
||
| 453 | * |
||
| 454 | * @param string $module The module, aka controller identifier to check permissions on |
||
| 455 | * @param string $function The function, aka the controller action to check permissions on |
||
| 456 | * @param \eZ\Publish\API\Repository\Values\ValueObject $object The object to check if the user has access to |
||
| 457 | * @param mixed $targets The location, parent or "assignment" value object, or an array of the same |
||
| 458 | * |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function canUser($module, $function, ValueObject $object, $targets = null) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Get Content Service. |
||
| 549 | * |
||
| 550 | * Get service object to perform operations on Content objects and it's aggregate members. |
||
| 551 | * |
||
| 552 | * @return \eZ\Publish\API\Repository\ContentService |
||
| 553 | */ |
||
| 554 | public function getContentService() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Get Content Language Service. |
||
| 575 | * |
||
| 576 | * Get service object to perform operations on Content language objects |
||
| 577 | * |
||
| 578 | * @return \eZ\Publish\API\Repository\LanguageService |
||
| 579 | */ |
||
| 580 | public function getContentLanguageService() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Get Content Type Service. |
||
| 597 | * |
||
| 598 | * Get service object to perform operations on Content Type objects and it's aggregate members. |
||
| 599 | * ( Group, Field & FieldCategory ) |
||
| 600 | * |
||
| 601 | * @return \eZ\Publish\API\Repository\ContentTypeService |
||
| 602 | */ |
||
| 603 | View Code Duplication | public function getContentTypeService() |
|
| 619 | |||
| 620 | /** |
||
| 621 | * Get Content Location Service. |
||
| 622 | * |
||
| 623 | * Get service object to perform operations on Location objects and subtrees |
||
| 624 | * |
||
| 625 | * @return \eZ\Publish\API\Repository\LocationService |
||
| 626 | */ |
||
| 627 | public function getLocationService() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Get Content Trash service. |
||
| 647 | * |
||
| 648 | * Trash service allows to perform operations related to location trash |
||
| 649 | * (trash/untrash, load/list from trash...) |
||
| 650 | * |
||
| 651 | * @return \eZ\Publish\API\Repository\TrashService |
||
| 652 | */ |
||
| 653 | public function getTrashService() |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Get Content Section Service. |
||
| 671 | * |
||
| 672 | * Get Section service that lets you manipulate section objects |
||
| 673 | * |
||
| 674 | * @return \eZ\Publish\API\Repository\SectionService |
||
| 675 | */ |
||
| 676 | public function getSectionService() |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Get User Service. |
||
| 693 | * |
||
| 694 | * Get service object to perform operations on Users and UserGroup |
||
| 695 | * |
||
| 696 | * @return \eZ\Publish\API\Repository\UserService |
||
| 697 | */ |
||
| 698 | public function getUserService() |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Get URLAliasService. |
||
| 715 | * |
||
| 716 | * @return \eZ\Publish\API\Repository\URLAliasService |
||
| 717 | */ |
||
| 718 | public function getURLAliasService() |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Get URLWildcardService. |
||
| 735 | * |
||
| 736 | * @return \eZ\Publish\API\Repository\URLWildcardService |
||
| 737 | */ |
||
| 738 | public function getURLWildcardService() |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Get ObjectStateService. |
||
| 755 | * |
||
| 756 | * @return \eZ\Publish\API\Repository\ObjectStateService |
||
| 757 | */ |
||
| 758 | public function getObjectStateService() |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Get RoleService. |
||
| 775 | * |
||
| 776 | * @return \eZ\Publish\API\Repository\RoleService |
||
| 777 | */ |
||
| 778 | public function getRoleService() |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Get LimitationService. |
||
| 797 | * |
||
| 798 | * @return \eZ\Publish\Core\Repository\Helper\LimitationService |
||
| 799 | */ |
||
| 800 | protected function getLimitationService() |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Get RoleDomainMapper. |
||
| 813 | * |
||
| 814 | * @return \eZ\Publish\Core\Repository\Helper\RoleDomainMapper |
||
| 815 | */ |
||
| 816 | protected function getRoleDomainMapper() |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Get SearchService. |
||
| 829 | * |
||
| 830 | * @return \eZ\Publish\API\Repository\SearchService |
||
| 831 | */ |
||
| 832 | public function getSearchService() |
||
| 848 | |||
| 849 | /** |
||
| 850 | * Get FieldTypeService. |
||
| 851 | * |
||
| 852 | * @return \eZ\Publish\API\Repository\FieldTypeService |
||
| 853 | */ |
||
| 854 | public function getFieldTypeService() |
||
| 864 | |||
| 865 | /** |
||
| 866 | * @return Helper\FieldTypeRegistry |
||
| 867 | */ |
||
| 868 | protected function getFieldTypeRegistry() |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Get NameSchemaResolverService. |
||
| 881 | * |
||
| 882 | * |
||
| 883 | * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory. |
||
| 884 | * |
||
| 885 | * @internal |
||
| 886 | * @private |
||
| 887 | * |
||
| 888 | * @return \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 889 | */ |
||
| 890 | View Code Duplication | public function getNameSchemaService() |
|
| 904 | |||
| 905 | /** |
||
| 906 | * Get RelationProcessor. |
||
| 907 | * |
||
| 908 | * |
||
| 909 | * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory. |
||
| 910 | * |
||
| 911 | * @return \eZ\Publish\Core\Repository\Helper\RelationProcessor |
||
| 912 | */ |
||
| 913 | protected function getRelationProcessor() |
||
| 923 | |||
| 924 | /** |
||
| 925 | * Get RelationProcessor. |
||
| 926 | * |
||
| 927 | * |
||
| 928 | * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory. |
||
| 929 | * |
||
| 930 | * @return \eZ\Publish\Core\Repository\Helper\DomainMapper |
||
| 931 | */ |
||
| 932 | protected function getDomainMapper() |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Get PermissionsCriterionHandler. |
||
| 951 | * |
||
| 952 | * |
||
| 953 | * @todo Move out from this & other repo instances when services becomes proper services in DIC terms using factory. |
||
| 954 | * |
||
| 955 | * @return \eZ\Publish\Core\Repository\PermissionsCriterionHandler |
||
| 956 | */ |
||
| 957 | protected function getPermissionsCriterionHandler() |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Begin transaction. |
||
| 966 | * |
||
| 967 | * Begins an transaction, make sure you'll call commit or rollback when done, |
||
| 968 | * otherwise work will be lost. |
||
| 969 | */ |
||
| 970 | public function beginTransaction() |
||
| 977 | |||
| 978 | /** |
||
| 979 | * Commit transaction. |
||
| 980 | * |
||
| 981 | * Commit transaction, or throw exceptions if no transactions has been started. |
||
| 982 | * |
||
| 983 | * @throws RuntimeException If no transaction has been started |
||
| 984 | */ |
||
| 985 | public function commit() |
||
| 1014 | |||
| 1015 | /** |
||
| 1016 | * Rollback transaction. |
||
| 1017 | * |
||
| 1018 | * Rollback transaction, or throw exceptions if no transactions has been started. |
||
| 1019 | * |
||
| 1020 | * @throws RuntimeException If no transaction has been started |
||
| 1021 | */ |
||
| 1022 | public function rollback() |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Enqueue an event to be triggered at commit or directly if no transaction has started. |
||
| 1036 | * |
||
| 1037 | * @param Callable $event |
||
| 1038 | */ |
||
| 1039 | public function commitEvent($event) |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * Only for internal use. |
||
| 1051 | * |
||
| 1052 | * Creates a \DateTime object for $timestamp in the current time zone |
||
| 1053 | * |
||
| 1054 | * @param int $timestamp |
||
| 1055 | * |
||
| 1056 | * @return \DateTime |
||
| 1057 | */ |
||
| 1058 | View Code Duplication | public function createDateTime($timestamp = null) |
|
| 1067 | } |
||
| 1068 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.