Complex classes like Handler 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 Handler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Handler implements BaseLocationHandler |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Gateway for handling location data. |
||
| 29 | * |
||
| 30 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway |
||
| 31 | */ |
||
| 32 | protected $locationGateway; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Location locationMapper. |
||
| 36 | * |
||
| 37 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper |
||
| 38 | */ |
||
| 39 | protected $locationMapper; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Content handler. |
||
| 43 | * |
||
| 44 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Handler |
||
| 45 | */ |
||
| 46 | protected $contentHandler; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Object state handler. |
||
| 50 | * |
||
| 51 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Handler |
||
| 52 | */ |
||
| 53 | protected $objectStateHandler; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Tree handler. |
||
| 57 | * |
||
| 58 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler |
||
| 59 | */ |
||
| 60 | protected $treeHandler; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Construct from userGateway. |
||
| 64 | * |
||
| 65 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway |
||
| 66 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper $locationMapper |
||
| 67 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Handler $contentHandler |
||
| 68 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Handler $objectStateHandler |
||
| 69 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler $treeHandler |
||
| 70 | * |
||
| 71 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler |
||
|
|
|||
| 72 | */ |
||
| 73 | public function __construct( |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns parent path string for a path string. |
||
| 89 | * |
||
| 90 | * @param string $pathString |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | protected function getParentPathString($pathString) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | public function load($locationId, array $translations = null, bool $useAlwaysAvailable = true) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | public function loadList(array $locationIds, array $translations = null, bool $useAlwaysAvailable = true): iterable |
||
| 111 | { |
||
| 112 | $list = $this->locationGateway->getNodeDataList($locationIds, $translations, $useAlwaysAvailable); |
||
| 113 | |||
| 114 | $locations = []; |
||
| 115 | foreach ($list as $row) { |
||
| 116 | $id = (int)$row['node_id']; |
||
| 117 | if (!isset($locations[$id])) { |
||
| 118 | $locations[$id] = $this->locationMapper->createLocationFromRow($row); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | return $locations; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Loads the subtree ids of the location identified by $locationId. |
||
| 127 | * |
||
| 128 | * @param int $locationId |
||
| 129 | * |
||
| 130 | * @return array Location ids are in the index, Content ids in the value. |
||
| 131 | */ |
||
| 132 | public function loadSubtreeIds($locationId) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function loadByRemoteId($remoteId, array $translations = null, bool $useAlwaysAvailable = true) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Loads all locations for $contentId, optionally limited to a sub tree |
||
| 149 | * identified by $rootLocationId. |
||
| 150 | * |
||
| 151 | * @param int $contentId |
||
| 152 | * @param int $rootLocationId |
||
| 153 | * |
||
| 154 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] |
||
| 155 | */ |
||
| 156 | public function loadLocationsByContent($contentId, $rootLocationId = null) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | public function loadLocationsByTrashContent(int $contentId, ?int $rootLocationId = null): array |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @see \eZ\Publish\SPI\Persistence\Content\Location\Handler::loadParentLocationsForDraftContent |
||
| 175 | */ |
||
| 176 | public function loadParentLocationsForDraftContent($contentId) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Returns an array of default content states with content state group id as key. |
||
| 185 | * |
||
| 186 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[] |
||
| 187 | */ |
||
| 188 | protected function getDefaultContentStates() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param Content $content |
||
| 205 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState[] $contentStates |
||
| 206 | */ |
||
| 207 | protected function setContentStates(Content $content, array $contentStates) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Copy location object identified by $sourceId, into destination identified by $destinationParentId. |
||
| 220 | * |
||
| 221 | * Performs a deep copy of the location identified by $sourceId and all of |
||
| 222 | * its child locations, copying the most recent published content object |
||
| 223 | * for each location to a new content object without any additional version |
||
| 224 | * information. Relations are not copied. URLs are not touched at all. |
||
| 225 | * |
||
| 226 | * @todo Either move to async/batch or find ways toward optimizing away operations per object. |
||
| 227 | * @todo Optionally retain dates and set creator |
||
| 228 | * |
||
| 229 | * @param mixed $sourceId |
||
| 230 | * @param mixed $destinationParentId |
||
| 231 | * @param int|null $newOwnerId |
||
| 232 | * |
||
| 233 | * @return Location the newly created Location. |
||
| 234 | * |
||
| 235 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 236 | */ |
||
| 237 | public function copySubtree($sourceId, $destinationParentId, $newOwnerId = null) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Retrieves section ID of the location's content. |
||
| 342 | * |
||
| 343 | * @param int $locationId |
||
| 344 | * |
||
| 345 | * @return int |
||
| 346 | */ |
||
| 347 | private function getSectionId($locationId) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * If the location is the main location for its content, updates subtree section. |
||
| 357 | * |
||
| 358 | * @param Location $location |
||
| 359 | * @param int $sectionId |
||
| 360 | */ |
||
| 361 | private function updateSubtreeSectionIfNecessary(Location $location, $sectionId) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Checks if the location is the main location for its content. |
||
| 370 | * |
||
| 371 | * @param Location $location |
||
| 372 | * |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | private function isMainLocation(Location $location) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Moves location identified by $sourceId into new parent identified by $destinationParentId. |
||
| 384 | * |
||
| 385 | * Performs a full move of the location identified by $sourceId to a new |
||
| 386 | * destination, identified by $destinationParentId. Relations do not need |
||
| 387 | * to be updated, since they refer to Content. URLs are not touched. |
||
| 388 | * |
||
| 389 | * @param mixed $sourceId |
||
| 390 | * @param mixed $destinationParentId |
||
| 391 | * |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | public function move($sourceId, $destinationParentId) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Marks the given nodes and all ancestors as modified. |
||
| 418 | * |
||
| 419 | * Optionally a time stamp with the modification date may be specified, |
||
| 420 | * otherwise the current time is used. |
||
| 421 | * |
||
| 422 | * @param int|string $locationId |
||
| 423 | * @param int $timestamp |
||
| 424 | */ |
||
| 425 | public function markSubtreeModified($locationId, $timestamp = null) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Sets a location to be hidden, and it self + all children to invisible. |
||
| 434 | * |
||
| 435 | * @param mixed $id Location ID |
||
| 436 | */ |
||
| 437 | public function hide($id) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Sets a location to be unhidden, and self + children to visible unless a parent is hiding the tree. |
||
| 446 | * If not make sure only children down to first hidden node is marked visible. |
||
| 447 | * |
||
| 448 | * @param mixed $id |
||
| 449 | */ |
||
| 450 | public function unHide($id) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Sets a location + all children to invisible. |
||
| 459 | * |
||
| 460 | * @param int $id Location ID |
||
| 461 | */ |
||
| 462 | public function setInvisible(int $id): void |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Sets a location + all children to visible. |
||
| 471 | * |
||
| 472 | * @param int $id Location ID |
||
| 473 | */ |
||
| 474 | public function setVisible(int $id): void |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Swaps the content object being pointed to by a location object. |
||
| 483 | * |
||
| 484 | * Make the location identified by $locationId1 refer to the Content |
||
| 485 | * referred to by $locationId2 and vice versa. |
||
| 486 | * |
||
| 487 | * @param mixed $locationId1 |
||
| 488 | * @param mixed $locationId2 |
||
| 489 | * |
||
| 490 | * @return bool |
||
| 491 | */ |
||
| 492 | public function swap($locationId1, $locationId2) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Updates an existing location. |
||
| 499 | * |
||
| 500 | * @param \eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct $location |
||
| 501 | * @param int $locationId |
||
| 502 | */ |
||
| 503 | public function update(UpdateStruct $location, $locationId) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Creates a new location rooted at $location->parentId. |
||
| 510 | * |
||
| 511 | * @param \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct $createStruct |
||
| 512 | * |
||
| 513 | * @return \eZ\Publish\SPI\Persistence\Content\Location |
||
| 514 | */ |
||
| 515 | public function create(CreateStruct $createStruct) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Removes all Locations under and including $locationId. |
||
| 530 | * |
||
| 531 | * Performs a recursive delete on the location identified by $locationId, |
||
| 532 | * including all of its child locations. Content which is not referred to |
||
| 533 | * by any other location is automatically removed. Content which looses its |
||
| 534 | * main Location will get the first of its other Locations assigned as the |
||
| 535 | * new main Location. |
||
| 536 | * |
||
| 537 | * @param mixed $locationId |
||
| 538 | * |
||
| 539 | * @return bool |
||
| 540 | */ |
||
| 541 | public function removeSubtree($locationId) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Set section on all content objects in the subtree. |
||
| 548 | * |
||
| 549 | * @param mixed $locationId |
||
| 550 | * @param mixed $sectionId |
||
| 551 | */ |
||
| 552 | public function setSectionForSubtree($locationId, $sectionId) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Changes main location of content identified by given $contentId to location identified by given $locationId. |
||
| 559 | * |
||
| 560 | * Updates ezcontentobject_tree and eznode_assignment tables (eznode_assignment for content current version number). |
||
| 561 | * |
||
| 562 | * @param mixed $contentId |
||
| 563 | * @param mixed $locationId |
||
| 564 | */ |
||
| 565 | public function changeMainLocation($contentId, $locationId) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Get the total number of all existing Locations. Can be combined with loadAllLocations. |
||
| 572 | * |
||
| 573 | * @return int |
||
| 574 | */ |
||
| 575 | public function countAllLocations() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Bulk-load all existing Locations, constrained by $limit and $offset to paginate results. |
||
| 582 | * |
||
| 583 | * @param int $offset |
||
| 584 | * @param int $limit |
||
| 585 | * |
||
| 586 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] |
||
| 587 | */ |
||
| 588 | public function loadAllLocations($offset, $limit) |
||
| 594 | } |
||
| 595 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.