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 LocationService 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 LocationService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class LocationService implements LocationServiceInterface |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var \eZ\Publish\Core\Repository\Repository |
||
| 45 | */ |
||
| 46 | protected $repository; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
| 50 | */ |
||
| 51 | protected $persistenceHandler; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $settings; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \eZ\Publish\Core\Repository\Helper\DomainMapper |
||
| 60 | */ |
||
| 61 | protected $domainMapper; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 65 | */ |
||
| 66 | protected $nameSchemaService; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var \eZ\Publish\Core\Repository\PermissionsCriterionHandler |
||
| 70 | */ |
||
| 71 | protected $permissionsCriterionHandler; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 75 | * |
||
| 76 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 77 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
| 78 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
| 79 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
| 80 | * @param \eZ\Publish\Core\Repository\PermissionsCriterionHandler $permissionsCriterionHandler |
||
| 81 | * @param array $settings |
||
| 82 | */ |
||
| 83 | View Code Duplication | public function __construct( |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Copies the subtree starting from $subtree as a new subtree of $targetLocation. |
||
| 104 | * |
||
| 105 | * Only the items on which the user has read access are copied. |
||
| 106 | * |
||
| 107 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed copy the subtree to the given parent location |
||
| 108 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree |
||
| 109 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the target location is a sub location of the given location |
||
| 110 | * |
||
| 111 | * @param \eZ\Publish\API\Repository\Values\Content\Location $subtree - the subtree denoted by the location to copy |
||
| 112 | * @param \eZ\Publish\API\Repository\Values\Content\Location $targetParentLocation - the target parent location for the copy operation |
||
| 113 | * |
||
| 114 | * @return \eZ\Publish\API\Repository\Values\Content\Location The newly created location of the copied subtree |
||
| 115 | */ |
||
| 116 | public function copySubtree(APILocation $subtree, APILocation $targetParentLocation) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Loads a location object from its $locationId. |
||
| 191 | * |
||
| 192 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
||
| 193 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
||
| 194 | * |
||
| 195 | * @param mixed $locationId |
||
| 196 | * |
||
| 197 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
| 198 | */ |
||
| 199 | public function loadLocation($locationId) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Loads a location object from its $remoteId. |
||
| 212 | * |
||
| 213 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
||
| 214 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If more than one location with same remote ID was found |
||
| 215 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
||
| 216 | * |
||
| 217 | * @param string $remoteId |
||
| 218 | * |
||
| 219 | * @return \eZ\Publish\API\Repository\Values\Content\Location |
||
| 220 | */ |
||
| 221 | public function loadLocationByRemoteId($remoteId) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Loads the locations for the given content object. |
||
| 238 | * |
||
| 239 | * If a $rootLocation is given, only locations that belong to this location are returned. |
||
| 240 | * The location list is also filtered by permissions on reading locations. |
||
| 241 | * |
||
| 242 | * @todo permissions check is missing |
||
| 243 | * |
||
| 244 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if there is no published version yet |
||
| 245 | * |
||
| 246 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 247 | * @param \eZ\Publish\API\Repository\Values\Content\Location $rootLocation |
||
| 248 | * |
||
| 249 | * @return \eZ\Publish\API\Repository\Values\Content\Location[] An array of {@link Location} |
||
| 250 | */ |
||
| 251 | public function loadLocations(ContentInfo $contentInfo, APILocation $rootLocation = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Loads children which are readable by the current user of a location object sorted by sortField and sortOrder. |
||
| 272 | * |
||
| 273 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 274 | * @param int $offset the start offset for paging |
||
| 275 | * @param int $limit the number of locations returned |
||
| 276 | * |
||
| 277 | * @return \eZ\Publish\API\Repository\Values\Content\LocationList |
||
| 278 | */ |
||
| 279 | public function loadLocationChildren(APILocation $location, $offset = 0, $limit = 25) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns the number of children which are readable by the current user of a location object. |
||
| 319 | * |
||
| 320 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 321 | * |
||
| 322 | * @return int |
||
| 323 | */ |
||
| 324 | public function getLocationChildCount(APILocation $location) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Searches children locations of the provided parent location id. |
||
| 339 | * |
||
| 340 | * @param mixed $parentLocationId |
||
| 341 | * @param int $sortField |
||
| 342 | * @param int $sortOrder |
||
| 343 | * @param int $offset |
||
| 344 | * @param int $limit |
||
| 345 | * |
||
| 346 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 347 | */ |
||
| 348 | protected function searchChildrenLocations( |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Creates the new $location in the content repository for the given content. |
||
| 372 | * |
||
| 373 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create this location |
||
| 374 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the content is already below the specified parent |
||
| 375 | * or the parent is a sub location of the location of the content |
||
| 376 | * or if set the remoteId exists already |
||
| 377 | * |
||
| 378 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 379 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct |
||
| 380 | * |
||
| 381 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created Location |
||
| 382 | */ |
||
| 383 | public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Updates $location in the content repository. |
||
| 449 | * |
||
| 450 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to update this location |
||
| 451 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if if set the remoteId exists already |
||
| 452 | * |
||
| 453 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 454 | * @param \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct $locationUpdateStruct |
||
| 455 | * |
||
| 456 | * @return \eZ\Publish\API\Repository\Values\Content\Location the updated Location |
||
| 457 | */ |
||
| 458 | public function updateLocation(APILocation $location, LocationUpdateStruct $locationUpdateStruct) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Swaps the contents held by $location1 and $location2. |
||
| 512 | * |
||
| 513 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to swap content |
||
| 514 | * |
||
| 515 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location1 |
||
| 516 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location2 |
||
| 517 | */ |
||
| 518 | public function swapLocation(APILocation $location1, APILocation $location2) |
||
| 519 | { |
||
| 520 | $loadedLocation1 = $this->loadLocation($location1->id); |
||
| 521 | $loadedLocation2 = $this->loadLocation($location2->id); |
||
| 522 | |||
| 523 | if (!$this->repository->canUser('content', 'edit', $loadedLocation1->getContentInfo(), $loadedLocation1)) { |
||
| 524 | throw new UnauthorizedException('content', 'edit'); |
||
| 525 | } |
||
| 526 | if (!$this->repository->canUser('content', 'edit', $loadedLocation2->getContentInfo(), $loadedLocation2)) { |
||
| 527 | throw new UnauthorizedException('content', 'edit'); |
||
| 528 | } |
||
| 529 | |||
| 530 | $this->repository->beginTransaction(); |
||
| 531 | try { |
||
| 532 | $this->persistenceHandler->locationHandler()->swap($loadedLocation1->id, $loadedLocation2->id); |
||
| 533 | $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
||
| 534 | $loadedLocation1->id, |
||
| 535 | $loadedLocation1->parentLocationId, |
||
| 536 | $loadedLocation1->contentInfo->name, |
||
| 537 | $loadedLocation1->contentInfo->mainLanguageCode |
||
| 538 | ); |
||
| 539 | $this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
||
| 540 | $loadedLocation2->id, |
||
| 541 | $loadedLocation2->parentLocationId, |
||
| 542 | $loadedLocation2->contentInfo->name, |
||
| 543 | $loadedLocation2->contentInfo->mainLanguageCode |
||
| 544 | ); |
||
| 545 | $this->repository->commit(); |
||
| 546 | } catch (Exception $e) { |
||
| 547 | $this->repository->rollback(); |
||
| 548 | throw $e; |
||
| 549 | } |
||
| 550 | } |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Hides the $location and marks invisible all descendants of $location. |
||
| 554 | * |
||
| 555 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to hide this location |
||
| 556 | * |
||
| 557 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 558 | * |
||
| 559 | * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
||
| 560 | */ |
||
| 561 | View Code Duplication | public function hideLocation(APILocation $location) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Unhides the $location. |
||
| 581 | * |
||
| 582 | * This method and marks visible all descendants of $locations |
||
| 583 | * until a hidden location is found. |
||
| 584 | * |
||
| 585 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to unhide this location |
||
| 586 | * |
||
| 587 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 588 | * |
||
| 589 | * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
||
| 590 | */ |
||
| 591 | View Code Duplication | public function unhideLocation(APILocation $location) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Moves the subtree to $newParentLocation. |
||
| 611 | * |
||
| 612 | * If a user has the permission to move the location to a target location |
||
| 613 | * he can do it regardless of an existing descendant on which the user has no permission. |
||
| 614 | * |
||
| 615 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to move this location to the target |
||
| 616 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree |
||
| 617 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the new parent is in a subtree of the location |
||
| 618 | * |
||
| 619 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 620 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
| 621 | */ |
||
| 622 | public function moveSubtree(APILocation $location, APILocation $newParentLocation) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Deletes $location and all its descendants. |
||
| 695 | * |
||
| 696 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete this location or a descendant |
||
| 697 | * |
||
| 698 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 699 | */ |
||
| 700 | public function deleteLocation(APILocation $location) |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Instantiates a new location create class. |
||
| 749 | * |
||
| 750 | * @param mixed $parentLocationId the parent under which the new location should be created |
||
| 751 | * |
||
| 752 | * @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct |
||
| 753 | */ |
||
| 754 | public function newLocationCreateStruct($parentLocationId) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Instantiates a new location update class. |
||
| 765 | * |
||
| 766 | * @return \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct |
||
| 767 | */ |
||
| 768 | public function newLocationUpdateStruct() |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Instantiates a correct sort clause object based on provided location sort field and sort order. |
||
| 775 | * |
||
| 776 | * @param int $sortField |
||
| 777 | * @param int $sortOrder |
||
| 778 | * |
||
| 779 | * @return \eZ\Publish\API\Repository\Values\Content\Query\SortClause |
||
| 780 | */ |
||
| 781 | protected function getSortClauseBySortField($sortField, $sortOrder = APILocation::SORT_ORDER_ASC) |
||
| 825 | } |
||
| 826 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.