Complex classes like ExceptionConversion 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 ExceptionConversion, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class ExceptionConversion extends Gateway |
||
24 | { |
||
25 | /** |
||
26 | * The wrapped gateway. |
||
27 | * |
||
28 | * @var Gateway |
||
29 | */ |
||
30 | protected $innerGateway; |
||
31 | |||
32 | /** |
||
33 | * Creates a new exception conversion gateway around $innerGateway. |
||
34 | * |
||
35 | * @param Gateway $innerGateway |
||
36 | */ |
||
37 | public function __construct(Gateway $innerGateway) |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public function getBasicNodeData($nodeId, array $translations = null, bool $useAlwaysAvailable = true) |
||
46 | { |
||
47 | try { |
||
48 | return $this->innerGateway->getBasicNodeData($nodeId, $translations, $useAlwaysAvailable); |
||
|
|||
49 | } catch (DBALException $e) { |
||
50 | throw new RuntimeException('Database error', 0, $e); |
||
51 | } catch (PDOException $e) { |
||
52 | throw new RuntimeException('Database error', 0, $e); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public function getNodeDataList(array $locationIds, array $translations = null, bool $useAlwaysAvailable = true): iterable |
||
60 | { |
||
61 | try { |
||
62 | return $this->innerGateway->getNodeDataList($locationIds, $translations, $useAlwaysAvailable); |
||
63 | } catch (DBALException | PDOException $e) { |
||
64 | throw new RuntimeException('Database error', 0, $e); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function getBasicNodeDataByRemoteId($remoteId, array $translations = null, bool $useAlwaysAvailable = true) |
||
72 | { |
||
73 | try { |
||
74 | return $this->innerGateway->getBasicNodeDataByRemoteId($remoteId, $translations, $useAlwaysAvailable); |
||
75 | } catch (DBALException $e) { |
||
76 | throw new RuntimeException('Database error', 0, $e); |
||
77 | } catch (PDOException $e) { |
||
78 | throw new RuntimeException('Database error', 0, $e); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Returns total count and data for all Locations satisfying the parameters. |
||
84 | * |
||
85 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
86 | * @param int $offset |
||
87 | * @param int|null $limit |
||
88 | * @param \eZ\Publish\API\Repository\Values\Content\Query\SortClause[] $sortClauses |
||
89 | * |
||
90 | * @return mixed[][] |
||
91 | */ |
||
92 | public function find(Criterion $criterion, $offset = 0, $limit = null, array $sortClauses = null) |
||
102 | |||
103 | /** |
||
104 | * Loads data for all Locations for $contentId, optionally only in the |
||
105 | * subtree starting at $rootLocationId. |
||
106 | * |
||
107 | * @param int $contentId |
||
108 | * @param int $rootLocationId |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | public function loadLocationDataByContent($contentId, $rootLocationId = null) |
||
122 | |||
123 | /** |
||
124 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway::loadParentLocationsDataForDraftContent |
||
125 | */ |
||
126 | public function loadParentLocationsDataForDraftContent($contentId) |
||
136 | |||
137 | /** |
||
138 | * Find all content in the given subtree. |
||
139 | * |
||
140 | * @param mixed $sourceId |
||
141 | * @param bool $onlyIds |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | public function getSubtreeContent($sourceId, $onlyIds = false) |
||
155 | |||
156 | /** |
||
157 | * Returns data for the first level children of the location identified by given $locationId. |
||
158 | * |
||
159 | * @param mixed $locationId |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | public function getChildren($locationId) |
||
173 | |||
174 | /** |
||
175 | * Update path strings to move nodes in the ezcontentobject_tree table. |
||
176 | * |
||
177 | * This query can likely be optimized to use some more advanced string |
||
178 | * operations, which then depend on the respective database. |
||
179 | * |
||
180 | * @todo optimize |
||
181 | * |
||
182 | * @param array $fromPathString |
||
183 | * @param array $toPathString |
||
184 | */ |
||
185 | public function moveSubtreeNodes(array $fromPathString, array $toPathString) |
||
195 | |||
196 | /** |
||
197 | * Updated subtree modification time for all nodes on path. |
||
198 | * |
||
199 | * @param string $pathString |
||
200 | * @param int|null $timestamp |
||
201 | */ |
||
202 | public function updateSubtreeModificationTime($pathString, $timestamp = null) |
||
212 | |||
213 | /** |
||
214 | * Update node assignment table. |
||
215 | * |
||
216 | * @param int $contentObjectId |
||
217 | * @param int $oldParent |
||
218 | * @param int $newParent |
||
219 | * @param int $opcode |
||
220 | */ |
||
221 | public function updateNodeAssignment($contentObjectId, $oldParent, $newParent, $opcode) |
||
231 | |||
232 | /** |
||
233 | * Create locations from node assignments. |
||
234 | * |
||
235 | * Convert existing node assignments into real locations. |
||
236 | * |
||
237 | * @param mixed $contentId |
||
238 | * @param mixed $versionNo |
||
239 | */ |
||
240 | public function createLocationsFromNodeAssignments($contentId, $versionNo) |
||
250 | |||
251 | /** |
||
252 | * Updates all Locations of content identified with $contentId with $versionNo. |
||
253 | * |
||
254 | * @param mixed $contentId |
||
255 | * @param mixed $versionNo |
||
256 | */ |
||
257 | public function updateLocationsContentVersionNo($contentId, $versionNo) |
||
267 | |||
268 | /** |
||
269 | * Sets a location to be hidden, and it self + all children to invisible. |
||
270 | * |
||
271 | * @param string $pathString |
||
272 | */ |
||
273 | public function hideSubtree($pathString) |
||
283 | |||
284 | /** |
||
285 | * Sets a location to be unhidden, and self + children to visible unless a parent is hiding the tree. |
||
286 | * If not make sure only children down to first hidden node is marked visible. |
||
287 | * |
||
288 | * @param string $pathString |
||
289 | */ |
||
290 | public function unHideSubtree($pathString) |
||
300 | |||
301 | /** |
||
302 | * Swaps the content object being pointed to by a location object. |
||
303 | * |
||
304 | * Make the location identified by $locationId1 refer to the Content |
||
305 | * referred to by $locationId2 and vice versa. |
||
306 | * |
||
307 | * @param mixed $locationId1 |
||
308 | * @param mixed $locationId2 |
||
309 | * |
||
310 | * @return bool |
||
311 | */ |
||
312 | public function swap($locationId1, $locationId2) |
||
322 | |||
323 | /** |
||
324 | * Creates a new location in given $parentNode. |
||
325 | * |
||
326 | * @param \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct $createStruct |
||
327 | * @param array $parentNode |
||
328 | * |
||
329 | * @return \eZ\Publish\SPI\Persistence\Content\Location |
||
330 | */ |
||
331 | public function create(CreateStruct $createStruct, array $parentNode) |
||
341 | |||
342 | /** |
||
343 | * Create an entry in the node assignment table. |
||
344 | * |
||
345 | * @param \eZ\Publish\SPI\Persistence\Content\Location\CreateStruct $createStruct |
||
346 | * @param mixed $parentNodeId |
||
347 | * @param int $type |
||
348 | */ |
||
349 | public function createNodeAssignment(CreateStruct $createStruct, $parentNodeId, $type = self::NODE_ASSIGNMENT_OP_CODE_CREATE_NOP) |
||
359 | |||
360 | /** |
||
361 | * Deletes node assignment for given $contentId and $versionNo. |
||
362 | * |
||
363 | * @param int $contentId |
||
364 | * @param int $versionNo |
||
365 | */ |
||
366 | public function deleteNodeAssignment($contentId, $versionNo = null) |
||
376 | |||
377 | /** |
||
378 | * Updates an existing location. |
||
379 | * |
||
380 | * Will not throw anything if location id is invalid or no entries are affected. |
||
381 | * |
||
382 | * @param \eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct $location |
||
383 | * @param int $locationId |
||
384 | */ |
||
385 | public function update(UpdateStruct $location, $locationId) |
||
395 | |||
396 | /** |
||
397 | * Updates path identification string for given $locationId. |
||
398 | * |
||
399 | * @param mixed $locationId |
||
400 | * @param mixed $parentLocationId |
||
401 | * @param string $text |
||
402 | */ |
||
403 | public function updatePathIdentificationString($locationId, $parentLocationId, $text) |
||
413 | |||
414 | /** |
||
415 | * Deletes ezcontentobject_tree row for given $locationId (node_id). |
||
416 | * |
||
417 | * @param mixed $locationId |
||
418 | */ |
||
419 | public function removeLocation($locationId) |
||
429 | |||
430 | /** |
||
431 | * Returns id of the next in line node to be set as a new main node. |
||
432 | * |
||
433 | * This returns lowest node id for content identified by $contentId, and not of |
||
434 | * the node identified by given $locationId (current main node). |
||
435 | * Assumes that content has more than one location. |
||
436 | * |
||
437 | * @param mixed $contentId |
||
438 | * @param mixed $locationId |
||
439 | * |
||
440 | * @return array |
||
441 | */ |
||
442 | public function getFallbackMainNodeData($contentId, $locationId) |
||
452 | |||
453 | /** |
||
454 | * Sends a single location identified by given $locationId to the trash. |
||
455 | * |
||
456 | * The associated content object is left untouched. |
||
457 | * |
||
458 | * @param mixed $locationId |
||
459 | * |
||
460 | * @return bool |
||
461 | */ |
||
462 | public function trashLocation($locationId) |
||
472 | |||
473 | /** |
||
474 | * Returns a trashed location to normal state. |
||
475 | * |
||
476 | * Recreates the originally trashed location in the new position. If no new |
||
477 | * position has been specified, it will be tried to re-create the location |
||
478 | * at the old position. If this is not possible ( because the old location |
||
479 | * does not exist any more) and exception is thrown. |
||
480 | * |
||
481 | * @param mixed $locationId |
||
482 | * @param mixed $newParentId |
||
483 | * |
||
484 | * @return \eZ\Publish\SPI\Persistence\Content\Location |
||
485 | */ |
||
486 | public function untrashLocation($locationId, $newParentId = null) |
||
496 | |||
497 | /** |
||
498 | * Loads trash data specified by location ID. |
||
499 | * |
||
500 | * @param mixed $locationId |
||
501 | * |
||
502 | * @return array |
||
503 | */ |
||
504 | public function loadTrashByLocation($locationId) |
||
514 | |||
515 | /** |
||
516 | * Removes every entries in the trash. |
||
517 | * Will NOT remove associated content objects nor attributes. |
||
518 | * |
||
519 | * Basically truncates ezcontentobject_trash table. |
||
520 | */ |
||
521 | public function cleanupTrash() |
||
531 | |||
532 | /** |
||
533 | * Lists trashed items. |
||
534 | * Returns entries from ezcontentobject_trash. |
||
535 | * |
||
536 | * @param int $offset |
||
537 | * @param int $limit |
||
538 | * @param array $sort |
||
539 | * |
||
540 | * @return array |
||
541 | */ |
||
542 | public function listTrashed($offset, $limit, array $sort = null) |
||
552 | |||
553 | /** |
||
554 | * Removes trashed element identified by $id from trash. |
||
555 | * Will NOT remove associated content object nor attributes. |
||
556 | * |
||
557 | * @param int $id The trashed location Id |
||
558 | */ |
||
559 | public function removeElementFromTrash($id) |
||
569 | |||
570 | /** |
||
571 | * Set section on all content objects in the subtree. |
||
572 | * |
||
573 | * @param mixed $pathString |
||
574 | * @param mixed $sectionId |
||
575 | * |
||
576 | * @return bool |
||
577 | */ |
||
578 | public function setSectionForSubtree($pathString, $sectionId) |
||
588 | |||
589 | /** |
||
590 | * Returns how many locations given content object identified by $contentId has. |
||
591 | * |
||
592 | * @param int $contentId |
||
593 | * |
||
594 | * @return int |
||
595 | */ |
||
596 | public function countLocationsByContentId($contentId) |
||
606 | |||
607 | /** |
||
608 | * Changes main location of content identified by given $contentId to location identified by given $locationId. |
||
609 | * |
||
610 | * Updates ezcontentobject_tree table for the given $contentId and eznode_assignment table for the given |
||
611 | * $contentId, $parentLocationId and $versionNo |
||
612 | * |
||
613 | * @param mixed $contentId |
||
614 | * @param mixed $locationId |
||
615 | * @param mixed $versionNo version number, needed to update eznode_assignment table |
||
616 | * @param mixed $parentLocationId parent location of location identified by $locationId, needed to update |
||
617 | * eznode_assignment table |
||
618 | */ |
||
619 | public function changeMainLocation($contentId, $locationId, $versionNo, $parentLocationId) |
||
629 | |||
630 | /** |
||
631 | * Get the total number of all Locations, except the Root node. |
||
632 | * |
||
633 | * @see loadAllLocationsData |
||
634 | * |
||
635 | * @return int |
||
636 | */ |
||
637 | public function countAllLocations() |
||
647 | |||
648 | /** |
||
649 | * Load data of every Location, except the Root node. |
||
650 | * |
||
651 | * @param int $offset Paginator offset |
||
652 | * @param int $limit Paginator limit |
||
653 | * |
||
654 | * @return array |
||
655 | */ |
||
656 | public function loadAllLocationsData($offset, $limit) |
||
666 | } |
||
667 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.