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 |
||
26 | class Handler implements BaseLocationHandler |
||
27 | { |
||
28 | /** |
||
29 | * Gateway for handling location data. |
||
30 | * |
||
31 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway |
||
32 | */ |
||
33 | protected $locationGateway; |
||
34 | |||
35 | /** |
||
36 | * Location locationMapper. |
||
37 | * |
||
38 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper |
||
39 | */ |
||
40 | protected $locationMapper; |
||
41 | |||
42 | /** |
||
43 | * Content handler. |
||
44 | * |
||
45 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Handler |
||
46 | */ |
||
47 | protected $contentHandler; |
||
48 | |||
49 | /** |
||
50 | * Object state handler. |
||
51 | * |
||
52 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Handler |
||
53 | */ |
||
54 | protected $objectStateHandler; |
||
55 | |||
56 | /** |
||
57 | * Tree handler. |
||
58 | * |
||
59 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler |
||
60 | */ |
||
61 | protected $treeHandler; |
||
62 | |||
63 | /** |
||
64 | * Construct from userGateway. |
||
65 | * |
||
66 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway |
||
67 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper $locationMapper |
||
68 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Handler $contentHandler |
||
69 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\ObjectState\Handler $objectStateHandler |
||
70 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\TreeHandler $treeHandler |
||
71 | * |
||
72 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\Location\Handler |
||
|
|||
73 | */ |
||
74 | public function __construct( |
||
87 | |||
88 | /** |
||
89 | * Returns parent path string for a path string. |
||
90 | * |
||
91 | * @param string $pathString |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | protected function getParentPathString($pathString) |
||
99 | |||
100 | /** |
||
101 | * Loads the data for the location identified by $locationId. |
||
102 | * |
||
103 | * @param int $locationId |
||
104 | * |
||
105 | * @return \eZ\Publish\SPI\Persistence\Content\Location |
||
106 | */ |
||
107 | public function load($locationId) |
||
111 | |||
112 | /** |
||
113 | * Loads the subtree ids of the location identified by $locationId. |
||
114 | * |
||
115 | * @param int $locationId |
||
116 | * |
||
117 | * @return array Location ids are in the index, Content ids in the value. |
||
118 | */ |
||
119 | public function loadSubtreeIds($locationId) |
||
123 | |||
124 | /** |
||
125 | * Loads the data for the location identified by $remoteId. |
||
126 | * |
||
127 | * @param string $remoteId |
||
128 | * |
||
129 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
130 | * |
||
131 | * @return \eZ\Publish\SPI\Persistence\Content\Location |
||
132 | */ |
||
133 | public function loadByRemoteId($remoteId) |
||
139 | |||
140 | /** |
||
141 | * Loads all locations for $contentId, optionally limited to a sub tree |
||
142 | * identified by $rootLocationId. |
||
143 | * |
||
144 | * @param int $contentId |
||
145 | * @param int $rootLocationId |
||
146 | * |
||
147 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] |
||
148 | */ |
||
149 | public function loadLocationsByContent($contentId, $rootLocationId = null) |
||
155 | |||
156 | /** |
||
157 | * @see \eZ\Publish\SPI\Persistence\Content\Location\Handler::loadParentLocationsForDraftContent |
||
158 | */ |
||
159 | public function loadParentLocationsForDraftContent($contentId) |
||
165 | |||
166 | /** |
||
167 | * Returns an array of default content states with content state group id as key. |
||
168 | * |
||
169 | * @return \eZ\Publish\SPI\Persistence\Content\ObjectState[] |
||
170 | */ |
||
171 | protected function getDefaultContentStates() |
||
185 | |||
186 | /** |
||
187 | * @param Content $content |
||
188 | * @param \eZ\Publish\SPI\Persistence\Content\ObjectState[] $contentStates |
||
189 | */ |
||
190 | protected function setContentStates(Content $content, array $contentStates) |
||
200 | |||
201 | /** |
||
202 | * Copy location object identified by $sourceId, into destination identified by $destinationParentId. |
||
203 | * |
||
204 | * Performs a deep copy of the location identified by $sourceId and all of |
||
205 | * its child locations, copying the most recent published content object |
||
206 | * for each location to a new content object without any additional version |
||
207 | * information. Relations are not copied. URLs are not touched at all. |
||
208 | * |
||
209 | * @todo Either move to async/batch or find ways toward optimizing away operations per object. |
||
210 | * @todo Optionally retain dates and set creator |
||
211 | * |
||
212 | * @param mixed $sourceId |
||
213 | * @param mixed $destinationParentId |
||
214 | * |
||
215 | * @return Location the newly created Location. |
||
216 | */ |
||
217 | public function copySubtree($sourceId, $destinationParentId) |
||
318 | |||
319 | /** |
||
320 | * Retrieves section ID of the location's content. |
||
321 | * |
||
322 | * @param int $locationId |
||
323 | * |
||
324 | * @return int |
||
325 | */ |
||
326 | private function getSectionId($locationId) |
||
333 | |||
334 | /** |
||
335 | * If the location is the main location for its content, updates subtree section. |
||
336 | * |
||
337 | * @param Location $location |
||
338 | * @param int $sectionId |
||
339 | */ |
||
340 | private function updateSubtreeSectionIfNecessary(Location $location, $sectionId) |
||
346 | |||
347 | /** |
||
348 | * Checks if the location is the main location for its content. |
||
349 | * |
||
350 | * @param Location $location |
||
351 | * |
||
352 | * @return bool |
||
353 | */ |
||
354 | private function isMainLocation(Location $location) |
||
360 | |||
361 | /** |
||
362 | * Moves location identified by $sourceId into new parent identified by $destinationParentId. |
||
363 | * |
||
364 | * Performs a full move of the location identified by $sourceId to a new |
||
365 | * destination, identified by $destinationParentId. Relations do not need |
||
366 | * to be updated, since they refer to Content. URLs are not touched. |
||
367 | * |
||
368 | * @param mixed $sourceId |
||
369 | * @param mixed $destinationParentId |
||
370 | * |
||
371 | * @return bool |
||
372 | */ |
||
373 | public function move($sourceId, $destinationParentId) |
||
394 | |||
395 | /** |
||
396 | * Marks the given nodes and all ancestors as modified. |
||
397 | * |
||
398 | * Optionally a time stamp with the modification date may be specified, |
||
399 | * otherwise the current time is used. |
||
400 | * |
||
401 | * @param int|string $locationId |
||
402 | * @param int $timestamp |
||
403 | */ |
||
404 | public function markSubtreeModified($locationId, $timestamp = null) |
||
410 | |||
411 | /** |
||
412 | * Sets a location to be hidden, and it self + all children to invisible. |
||
413 | * |
||
414 | * @param mixed $id Location ID |
||
415 | */ |
||
416 | public function hide($id) |
||
422 | |||
423 | /** |
||
424 | * Sets a location to be unhidden, and self + children to visible unless a parent is hiding the tree. |
||
425 | * If not make sure only children down to first hidden node is marked visible. |
||
426 | * |
||
427 | * @param mixed $id |
||
428 | */ |
||
429 | public function unHide($id) |
||
435 | |||
436 | /** |
||
437 | * Swaps the content object being pointed to by a location object. |
||
438 | * |
||
439 | * Make the location identified by $locationId1 refer to the Content |
||
440 | * referred to by $locationId2 and vice versa. |
||
441 | * |
||
442 | * @param mixed $locationId1 |
||
443 | * @param mixed $locationId2 |
||
444 | * |
||
445 | * @return bool |
||
446 | */ |
||
447 | public function swap($locationId1, $locationId2) |
||
451 | |||
452 | /** |
||
453 | * Updates an existing location. |
||
454 | * |
||
455 | * @param \eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct $location |
||
456 | * @param int $locationId |
||
457 | */ |
||
458 | public function update(UpdateStruct $location, $locationId) |
||
462 | |||
463 | /** |
||
464 | * Creates a new location rooted at $location->parentId. |
||
465 | * |
||
466 | * @param \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct $createStruct |
||
467 | * |
||
468 | * @return \eZ\Publish\SPI\Persistence\Content\Location |
||
469 | */ |
||
470 | public function create(CreateStruct $createStruct) |
||
482 | |||
483 | /** |
||
484 | * Removes all Locations under and including $locationId. |
||
485 | * |
||
486 | * Performs a recursive delete on the location identified by $locationId, |
||
487 | * including all of its child locations. Content which is not referred to |
||
488 | * by any other location is automatically removed. Content which looses its |
||
489 | * main Location will get the first of its other Locations assigned as the |
||
490 | * new main Location. |
||
491 | * |
||
492 | * @param mixed $locationId |
||
493 | * |
||
494 | * @return bool |
||
495 | */ |
||
496 | public function removeSubtree($locationId) |
||
500 | |||
501 | /** |
||
502 | * Set section on all content objects in the subtree. |
||
503 | * |
||
504 | * @param mixed $locationId |
||
505 | * @param mixed $sectionId |
||
506 | */ |
||
507 | public function setSectionForSubtree($locationId, $sectionId) |
||
511 | |||
512 | /** |
||
513 | * Changes main location of content identified by given $contentId to location identified by given $locationId. |
||
514 | * |
||
515 | * Updates ezcontentobject_tree and eznode_assignment tables (eznode_assignment for content current version number). |
||
516 | * |
||
517 | * @param mixed $contentId |
||
518 | * @param mixed $locationId |
||
519 | */ |
||
520 | public function changeMainLocation($contentId, $locationId) |
||
524 | } |
||
525 |
Adding a
@return
annotation 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.