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  | 
            ||
| 22 | class Repository implements APIRepository  | 
            ||
| 23 | { | 
            ||
| 24 | /**  | 
            ||
| 25 | * @var \eZ\Publish\Core\REST\Client\SectionService  | 
            ||
| 26 | */  | 
            ||
| 27 | private $sectionService;  | 
            ||
| 28 | |||
| 29 | /**  | 
            ||
| 30 | * @var \eZ\Publish\Core\REST\Client\LanguageService  | 
            ||
| 31 | */  | 
            ||
| 32 | private $languageService;  | 
            ||
| 33 | |||
| 34 | /**  | 
            ||
| 35 | * @var \eZ\Publish\Core\REST\Client\UserService  | 
            ||
| 36 | */  | 
            ||
| 37 | private $userService;  | 
            ||
| 38 | |||
| 39 | /**  | 
            ||
| 40 | * @var \eZ\Publish\Core\REST\Client\RoleService  | 
            ||
| 41 | */  | 
            ||
| 42 | private $roleService;  | 
            ||
| 43 | |||
| 44 | /**  | 
            ||
| 45 | * @var \eZ\Publish\Core\REST\Client\URLAliasService  | 
            ||
| 46 | */  | 
            ||
| 47 | private $urlAliasService;  | 
            ||
| 48 | |||
| 49 | /**  | 
            ||
| 50 | * @var \eZ\Publish\Core\REST\Client\ContentService  | 
            ||
| 51 | */  | 
            ||
| 52 | private $contentService;  | 
            ||
| 53 | |||
| 54 | /**  | 
            ||
| 55 | * @var \eZ\Publish\Core\REST\Client\ContentTypeService  | 
            ||
| 56 | */  | 
            ||
| 57 | private $contentTypeService;  | 
            ||
| 58 | |||
| 59 | /**  | 
            ||
| 60 | * @var \eZ\Publish\Core\REST\Client\TrashService  | 
            ||
| 61 | */  | 
            ||
| 62 | private $trashService;  | 
            ||
| 63 | |||
| 64 | /**  | 
            ||
| 65 | * @var \eZ\Publish\Core\REST\Client\LocationService  | 
            ||
| 66 | */  | 
            ||
| 67 | private $locationService;  | 
            ||
| 68 | |||
| 69 | /**  | 
            ||
| 70 | * @var \eZ\Publish\Core\REST\Client\ObjectStateService  | 
            ||
| 71 | */  | 
            ||
| 72 | private $objectStateService;  | 
            ||
| 73 | |||
| 74 | /**  | 
            ||
| 75 | * @var \eZ\Publish\Core\REST\Client\IOService  | 
            ||
| 76 | */  | 
            ||
| 77 | private $ioService;  | 
            ||
| 78 | |||
| 79 | /**  | 
            ||
| 80 | * @var \eZ\Publish\Core\REST\Client\FieldTypeService  | 
            ||
| 81 | */  | 
            ||
| 82 | private $fieldTypeService;  | 
            ||
| 83 | |||
| 84 | /**  | 
            ||
| 85 | * Client.  | 
            ||
| 86 | *  | 
            ||
| 87 | * @var \eZ\Publish\Core\REST\Client\HttpClient  | 
            ||
| 88 | */  | 
            ||
| 89 | private $client;  | 
            ||
| 90 | |||
| 91 | /**  | 
            ||
| 92 | * Input parsing dispatcher.  | 
            ||
| 93 | *  | 
            ||
| 94 | * @var \eZ\Publish\Core\REST\Common\Input\Dispatcher  | 
            ||
| 95 | */  | 
            ||
| 96 | private $inputDispatcher;  | 
            ||
| 97 | |||
| 98 | /**  | 
            ||
| 99 | * @var \eZ\Publish\Core\REST\Common\Output\Visitor  | 
            ||
| 100 | */  | 
            ||
| 101 | private $outputVisitor;  | 
            ||
| 102 | |||
| 103 | /**  | 
            ||
| 104 | * @var \eZ\Publish\Core\REST\Common\RequestParser  | 
            ||
| 105 | */  | 
            ||
| 106 | private $requestParser;  | 
            ||
| 107 | |||
| 108 | /**  | 
            ||
| 109 | * @var \eZ\Publish\SPI\FieldType\FieldType[]  | 
            ||
| 110 | */  | 
            ||
| 111 | private $fieldTypes;  | 
            ||
| 112 | |||
| 113 | /**  | 
            ||
| 114 | * Instantiates the REST Client repository.  | 
            ||
| 115 | *  | 
            ||
| 116 | * @param \eZ\Publish\Core\REST\Client\HttpClient $client  | 
            ||
| 117 | * @param \eZ\Publish\Core\REST\Common\Input\Dispatcher $inputDispatcher  | 
            ||
| 118 | * @param \eZ\Publish\Core\REST\Common\Output\Visitor $outputVisitor  | 
            ||
| 119 | * @param \eZ\Publish\Core\REST\Common\RequestParser $requestParser  | 
            ||
| 120 | * @param \eZ\Publish\SPI\FieldType\FieldType[] $fieldTypes  | 
            ||
| 121 | */  | 
            ||
| 122 | View Code Duplication | public function __construct(HttpClient $client, Common\Input\Dispatcher $inputDispatcher, Common\Output\Visitor $outputVisitor, Common\RequestParser $requestParser, array $fieldTypes)  | 
            |
| 130 | |||
| 131 | /**  | 
            ||
| 132 | * @deprecated since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead.  | 
            ||
| 133 | *  | 
            ||
| 134 | * Get current user.  | 
            ||
| 135 | *  | 
            ||
| 136 | * @return \eZ\Publish\API\Repository\Values\User\User  | 
            ||
| 137 | */  | 
            ||
| 138 | public function getCurrentUser()  | 
            ||
| 142 | |||
| 143 | /**  | 
            ||
| 144 | * @deprecated since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead.  | 
            ||
| 145 | *  | 
            ||
| 146 | * Get current user.  | 
            ||
| 147 | *  | 
            ||
| 148 | * @return \eZ\Publish\API\Repository\Values\User\UserReference  | 
            ||
| 149 | */  | 
            ||
| 150 | public function getCurrentUserReference()  | 
            ||
| 154 | |||
| 155 | /**  | 
            ||
| 156 | * @deprecated since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead.  | 
            ||
| 157 | *  | 
            ||
| 158 | * Sets the current user to the given $user.  | 
            ||
| 159 | *  | 
            ||
| 160 | * @param \eZ\Publish\API\Repository\Values\User\UserReference $user  | 
            ||
| 161 | *  | 
            ||
| 162 | * @return void  | 
            ||
| 163 | */  | 
            ||
| 164 | public function setCurrentUser(UserReference $user)  | 
            ||
| 170 | |||
| 171 | /**  | 
            ||
| 172 | * @deprecated since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.  | 
            ||
| 173 | *  | 
            ||
| 174 | * @param string $module  | 
            ||
| 175 | * @param string $function  | 
            ||
| 176 | * @param \eZ\Publish\API\Repository\Values\User\UserReference $user  | 
            ||
| 177 | *  | 
            ||
| 178 | * @return bool|\eZ\Publish\API\Repository\Values\User\Limitation[] if limitations are on this function an array of limitations is returned  | 
            ||
| 179 | */  | 
            ||
| 180 | public function hasAccess($module, $function, UserReference $user = null)  | 
            ||
| 184 | |||
| 185 | /**  | 
            ||
| 186 | * @deprecated since 6.6, to be removed. Use PermissionResolver::canUser() instead.  | 
            ||
| 187 | *  | 
            ||
| 188 | * Indicates if the current user is allowed to perform an action given by the function on the given  | 
            ||
| 189 | * objects.  | 
            ||
| 190 | *  | 
            ||
| 191 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If any of the arguments are invalid  | 
            ||
| 192 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If value of the LimitationValue is unsupported  | 
            ||
| 193 | *  | 
            ||
| 194 | * @param string $module The module, aka controller identifier to check permissions on  | 
            ||
| 195 | * @param string $function The function, aka the controller action to check permissions on  | 
            ||
| 196 | * @param \eZ\Publish\API\Repository\Values\ValueObject $object The object to check if the user has access to  | 
            ||
| 197 | * @param mixed $targets The location, parent or "assignment" value object, or an array of the same  | 
            ||
| 198 | *  | 
            ||
| 199 | * @return bool  | 
            ||
| 200 | */  | 
            ||
| 201 | public function canUser($module, $function, ValueObject $object, $targets = null)  | 
            ||
| 205 | |||
| 206 | /**  | 
            ||
| 207 | * Get Content Service.  | 
            ||
| 208 | *  | 
            ||
| 209 | * Get service object to perform operations on Content objects and it's aggregate members.  | 
            ||
| 210 | *  | 
            ||
| 211 | * @return \eZ\Publish\API\Repository\ContentService  | 
            ||
| 212 | */  | 
            ||
| 213 | public function getContentService()  | 
            ||
| 227 | |||
| 228 | /**  | 
            ||
| 229 | * Get Content Language Service.  | 
            ||
| 230 | *  | 
            ||
| 231 | * Get service object to perform operations on Content language objects  | 
            ||
| 232 | *  | 
            ||
| 233 | * @return \eZ\Publish\API\Repository\LanguageService  | 
            ||
| 234 | */  | 
            ||
| 235 | public function getContentLanguageService()  | 
            ||
| 250 | |||
| 251 | /**  | 
            ||
| 252 | * Get Content Type Service.  | 
            ||
| 253 | *  | 
            ||
| 254 | * Get service object to perform operations on Content Type objects and it's aggregate members.  | 
            ||
| 255 | * ( Group, Field & FieldCategory )  | 
            ||
| 256 | *  | 
            ||
| 257 | * @return \eZ\Publish\API\Repository\ContentTypeService  | 
            ||
| 258 | */  | 
            ||
| 259 | View Code Duplication | public function getContentTypeService()  | 
            |
| 272 | |||
| 273 | /**  | 
            ||
| 274 | * Get Content Location Service.  | 
            ||
| 275 | *  | 
            ||
| 276 | * Get service object to perform operations on Location objects and subtrees  | 
            ||
| 277 | *  | 
            ||
| 278 | * @return \eZ\Publish\API\Repository\LocationService  | 
            ||
| 279 | */  | 
            ||
| 280 | View Code Duplication | public function getLocationService()  | 
            |
| 293 | |||
| 294 | /**  | 
            ||
| 295 | * Get Content Trash service.  | 
            ||
| 296 | *  | 
            ||
| 297 | * Trash service allows to perform operations related to location trash  | 
            ||
| 298 | * (trash/untrash, load/list from trash...)  | 
            ||
| 299 | *  | 
            ||
| 300 | * @return \eZ\Publish\API\Repository\TrashService  | 
            ||
| 301 | */  | 
            ||
| 302 | public function getTrashService()  | 
            ||
| 316 | |||
| 317 | /**  | 
            ||
| 318 | * Get Content Section Service.  | 
            ||
| 319 | *  | 
            ||
| 320 | * Get Section service that lets you manipulate section objects  | 
            ||
| 321 | *  | 
            ||
| 322 | * @return \eZ\Publish\API\Repository\SectionService  | 
            ||
| 323 | */  | 
            ||
| 324 | View Code Duplication | public function getSectionService()  | 
            |
| 337 | |||
| 338 | /**  | 
            ||
| 339 | * Get Search Service.  | 
            ||
| 340 | *  | 
            ||
| 341 | * Get search service that lets you find content objects  | 
            ||
| 342 | *  | 
            ||
| 343 | * @return \eZ\Publish\API\Repository\SearchService  | 
            ||
| 344 | */  | 
            ||
| 345 | public function getSearchService()  | 
            ||
| 349 | |||
| 350 | /**  | 
            ||
| 351 | * Get User Service.  | 
            ||
| 352 | *  | 
            ||
| 353 | * Get service object to perform operations on Users and UserGroup  | 
            ||
| 354 | *  | 
            ||
| 355 | * @return \eZ\Publish\API\Repository\UserService  | 
            ||
| 356 | */  | 
            ||
| 357 | View Code Duplication | public function getUserService()  | 
            |
| 370 | |||
| 371 | /**  | 
            ||
| 372 | * Get IO Service.  | 
            ||
| 373 | *  | 
            ||
| 374 | * Get service object to perform operations on binary files  | 
            ||
| 375 | *  | 
            ||
| 376 | * @return \eZ\Publish\API\Repository\IOService  | 
            ||
| 377 | */  | 
            ||
| 378 | View Code Duplication | public function getIOService()  | 
            |
| 391 | |||
| 392 | /**  | 
            ||
| 393 | * Get RoleService.  | 
            ||
| 394 | *  | 
            ||
| 395 | * @return \eZ\Publish\API\Repository\RoleService  | 
            ||
| 396 | */  | 
            ||
| 397 | View Code Duplication | public function getRoleService()  | 
            |
| 411 | |||
| 412 | /**  | 
            ||
| 413 | * Get URLAliasService.  | 
            ||
| 414 | *  | 
            ||
| 415 | * @return \eZ\Publish\API\Repository\URLAliasService  | 
            ||
| 416 | */  | 
            ||
| 417 | View Code Duplication | public function getURLAliasService()  | 
            |
| 430 | |||
| 431 | /**  | 
            ||
| 432 | * Get URLWildcardService.  | 
            ||
| 433 | *  | 
            ||
| 434 | * @return \eZ\Publish\API\Repository\URLWildcardService  | 
            ||
| 435 | */  | 
            ||
| 436 | public function getURLWildcardService()  | 
            ||
| 440 | |||
| 441 | /**  | 
            ||
| 442 | * Get ObjectStateService.  | 
            ||
| 443 | *  | 
            ||
| 444 | * @return \eZ\Publish\API\Repository\ObjectStateService  | 
            ||
| 445 | */  | 
            ||
| 446 | View Code Duplication | public function getObjectStateService()  | 
            |
| 459 | |||
| 460 | /**  | 
            ||
| 461 | * Get FieldTypeService.  | 
            ||
| 462 | *  | 
            ||
| 463 | * @return \eZ\Publish\API\Repository\FieldTypeService  | 
            ||
| 464 | */  | 
            ||
| 465 | public function getFieldTypeService()  | 
            ||
| 473 | |||
| 474 | /**  | 
            ||
| 475 | * Get PermissionResolver.  | 
            ||
| 476 | *  | 
            ||
| 477 | * @return \eZ\Publish\API\Repository\PermissionResolver  | 
            ||
| 478 | */  | 
            ||
| 479 | public function getPermissionResolver()  | 
            ||
| 483 | |||
| 484 | /**  | 
            ||
| 485 | * Get URLService.  | 
            ||
| 486 | *  | 
            ||
| 487 | * @return \eZ\Publish\API\Repository\URLService  | 
            ||
| 488 | */  | 
            ||
| 489 | public function getURLService()  | 
            ||
| 493 | |||
| 494 | /**  | 
            ||
| 495 | * Get BookmarkService.  | 
            ||
| 496 | *  | 
            ||
| 497 | * @return \eZ\Publish\API\Repository\BookmarkService  | 
            ||
| 498 | */  | 
            ||
| 499 | public function getBookmarkService()  | 
            ||
| 503 | |||
| 504 | /**  | 
            ||
| 505 | * Get UserPreferenceService.  | 
            ||
| 506 | *  | 
            ||
| 507 | * @return \eZ\Publish\API\Repository\UserPreferenceService  | 
            ||
| 508 | */  | 
            ||
| 509 | public function getUserPreferenceService()  | 
            ||
| 513 | |||
| 514 | /**  | 
            ||
| 515 | * Get NotificationService  | 
            ||
| 516 | *  | 
            ||
| 517 | * @return \eZ\Publish\API\Repository\NotificationService  | 
            ||
| 518 | */  | 
            ||
| 519 | public function getNotificationService()  | 
            ||
| 523 | |||
| 524 | /**  | 
            ||
| 525 | * Begin transaction.  | 
            ||
| 526 | *  | 
            ||
| 527 | * Begins an transaction, make sure you'll call commit or rollback when done,  | 
            ||
| 528 | * otherwise work will be lost.  | 
            ||
| 529 | */  | 
            ||
| 530 | public function beginTransaction()  | 
            ||
| 534 | |||
| 535 | /**  | 
            ||
| 536 | * Commit transaction.  | 
            ||
| 537 | *  | 
            ||
| 538 | * Commit transaction, or throw exceptions if no transactions has been started.  | 
            ||
| 539 | *  | 
            ||
| 540 | * @throws \RuntimeException If no transaction has been started  | 
            ||
| 541 | */  | 
            ||
| 542 | public function commit()  | 
            ||
| 546 | |||
| 547 | /**  | 
            ||
| 548 | * Rollback transaction.  | 
            ||
| 549 | *  | 
            ||
| 550 | * Rollback transaction, or throw exceptions if no transactions has been started.  | 
            ||
| 551 | *  | 
            ||
| 552 | * @throws \RuntimeException If no transaction has been started  | 
            ||
| 553 | */  | 
            ||
| 554 | public function rollback()  | 
            ||
| 558 | |||
| 559 | /**  | 
            ||
| 560 |      * {@inheritdoc} | 
            ||
| 561 | */  | 
            ||
| 562 | public function sudo(callable $callback, APIRepository $outerRepository = null)  | 
            ||
| 566 | }  | 
            ||
| 567 | 
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.