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 |
||
27 | class ExceptionConversion extends Gateway |
||
28 | { |
||
29 | /** |
||
30 | * The wrapped gateway. |
||
31 | * |
||
32 | * @var Gateway |
||
33 | */ |
||
34 | protected $innerGateway; |
||
35 | |||
36 | /** |
||
37 | * Creates a new exception conversion gateway around $innerGateway. |
||
38 | * |
||
39 | * @param Gateway $innerGateway |
||
40 | */ |
||
41 | public function __construct(Gateway $innerGateway) |
||
45 | |||
46 | /** |
||
47 | * Get context definition for external storage layers. |
||
48 | * |
||
49 | * @return array |
||
50 | */ |
||
51 | public function getContext() |
||
61 | |||
62 | /** |
||
63 | * Inserts a new content object. |
||
64 | * |
||
65 | * @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct |
||
66 | * @param mixed $currentVersionNo |
||
67 | * |
||
68 | * @return int ID |
||
69 | */ |
||
70 | public function insertContentObject(CreateStruct $struct, $currentVersionNo = 1) |
||
80 | |||
81 | /** |
||
82 | * Inserts a new version. |
||
83 | * |
||
84 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo |
||
85 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $fields |
||
86 | * |
||
87 | * @return int ID |
||
88 | */ |
||
89 | public function insertVersion(VersionInfo $versionInfo, array $fields) |
||
99 | |||
100 | /** |
||
101 | * Updates an existing content identified by $contentId in respect to $struct. |
||
102 | * |
||
103 | * @param int $contentId |
||
104 | * @param \eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct $struct |
||
105 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $prePublishVersionInfo Provided on publish |
||
106 | */ |
||
107 | public function updateContent($contentId, MetadataUpdateStruct $struct, VersionInfo $prePublishVersionInfo = null) |
||
117 | |||
118 | /** |
||
119 | * Updates version $versionNo for content identified by $contentId, in respect to $struct. |
||
120 | * |
||
121 | * @param int $contentId |
||
122 | * @param int $versionNo |
||
123 | * @param \eZ\Publish\SPI\Persistence\Content\UpdateStruct $struct |
||
124 | */ |
||
125 | public function updateVersion($contentId, $versionNo, UpdateStruct $struct) |
||
135 | |||
136 | /** |
||
137 | * Updates "always available" flag for content identified by $contentId, in respect to $alwaysAvailable. |
||
138 | * |
||
139 | * @param int $contentId |
||
140 | * @param bool $newAlwaysAvailable New "always available" value |
||
141 | */ |
||
142 | public function updateAlwaysAvailableFlag($contentId, $newAlwaysAvailable) |
||
152 | |||
153 | /** |
||
154 | * Sets the state of object identified by $contentId and $version to $state. |
||
155 | * |
||
156 | * The $status can be one of STATUS_DRAFT, STATUS_PUBLISHED, STATUS_ARCHIVED |
||
157 | * |
||
158 | * @param int $contentId |
||
159 | * @param int $version |
||
160 | * @param int $status |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function setStatus($contentId, $version, $status) |
||
174 | |||
175 | public function setPublishedStatus(int $contentId, int $status): void |
||
183 | |||
184 | /** |
||
185 | * Inserts a new field. |
||
186 | * |
||
187 | * Only used when a new field is created (i.e. a new object or a field in a |
||
188 | * new language!). After that, field IDs need to stay the same, only the |
||
189 | * version number changes. |
||
190 | * |
||
191 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
192 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
193 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
||
194 | * |
||
195 | * @return int ID |
||
196 | */ |
||
197 | public function insertNewField(Content $content, Field $field, StorageFieldValue $value) |
||
207 | |||
208 | /** |
||
209 | * Inserts an existing field. |
||
210 | * |
||
211 | * Used to insert a field with an exsting ID but a new version number. |
||
212 | * |
||
213 | * @param Content $content |
||
214 | * @param Field $field |
||
215 | * @param StorageFieldValue $value |
||
216 | */ |
||
217 | public function insertExistingField(Content $content, Field $field, StorageFieldValue $value) |
||
227 | |||
228 | /** |
||
229 | * Updates an existing field. |
||
230 | * |
||
231 | * @param Field $field |
||
232 | * @param StorageFieldValue $value |
||
233 | */ |
||
234 | public function updateField(Field $field, StorageFieldValue $value) |
||
244 | |||
245 | /** |
||
246 | * Updates an existing, non-translatable field. |
||
247 | * |
||
248 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
249 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
||
250 | * @param int $contentId |
||
251 | */ |
||
252 | public function updateNonTranslatableField( |
||
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function load($contentId, $version = null, array $translations = null) |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function loadContentList(array $contentIds, array $translations = null): array |
||
291 | |||
292 | /** |
||
293 | * Loads data for a content object identified by its remote ID. |
||
294 | * |
||
295 | * Returns an array with the relevant data. |
||
296 | * |
||
297 | * @param mixed $remoteId |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | public function loadContentInfoByRemoteId($remoteId) |
||
311 | |||
312 | /** |
||
313 | * Loads info for a content object identified by its location ID (node ID). |
||
314 | * |
||
315 | * Returns an array with the relevant data. |
||
316 | * |
||
317 | * @param int $locationId |
||
318 | * |
||
319 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
320 | * |
||
321 | * @return array |
||
322 | */ |
||
323 | public function loadContentInfoByLocationId($locationId) |
||
333 | |||
334 | /** |
||
335 | * Loads info for content identified by $contentId. |
||
336 | * Will basically return a hash containing all field values for ezcontentobject table plus following keys: |
||
337 | * - always_available => Boolean indicating if content's language mask contains alwaysAvailable bit field |
||
338 | * - main_language_code => Language code for main (initial) language. E.g. "eng-GB". |
||
339 | * |
||
340 | * @param int $contentId |
||
341 | * |
||
342 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
343 | * |
||
344 | * @return array |
||
345 | */ |
||
346 | public function loadContentInfo($contentId) |
||
356 | |||
357 | public function loadContentInfoList(array $contentIds) |
||
367 | |||
368 | /** |
||
369 | * Loads version info for content identified by $contentId and $versionNo. |
||
370 | * Will basically return a hash containing all field values from ezcontentobject_version table plus following keys: |
||
371 | * - names => Hash of content object names. Key is the language code, value is the name. |
||
372 | * - languages => Hash of language ids. Key is the language code (e.g. "eng-GB"), value is the language numeric id without the always available bit. |
||
373 | * - initial_language_code => Language code for initial language in this version. |
||
374 | * |
||
375 | * @param int $contentId |
||
376 | * @param int|null $versionNo |
||
377 | * |
||
378 | * @return array |
||
379 | */ |
||
380 | public function loadVersionInfo($contentId, $versionNo = null) |
||
390 | |||
391 | /** |
||
392 | * Returns the number of all versions with given status created by the given $userId. |
||
393 | * |
||
394 | * @param int $userId |
||
395 | * @param int $status |
||
396 | * |
||
397 | * @return int |
||
398 | */ |
||
399 | public function countVersionsForUser(int $userId, int $status = VersionInfo::STATUS_DRAFT): int |
||
407 | |||
408 | /** |
||
409 | * Returns data for all versions with given status created by the given $userId. |
||
410 | * |
||
411 | * @param int $userId |
||
412 | * @param int $status |
||
413 | * |
||
414 | * @return string[][] |
||
415 | */ |
||
416 | public function listVersionsForUser($userId, $status = VersionInfo::STATUS_DRAFT, int $offset = 0, int $limit = -1) |
||
426 | |||
427 | /** |
||
428 | * {@inheritdoc} |
||
429 | */ |
||
430 | public function loadVersionsForUser($userId, $status = VersionInfo::STATUS_DRAFT, int $offset = 0, int $limit = -1): array |
||
438 | |||
439 | /** |
||
440 | * Returns all version data for the given $contentId. |
||
441 | * |
||
442 | * Result is returned with oldest version first (using version id as it has index and is auto increment). |
||
443 | * |
||
444 | * @param mixed $contentId |
||
445 | * @param mixed|null $status Optional argument to filter versions by status, like {@see VersionInfo::STATUS_ARCHIVED}. |
||
446 | * @param int $limit Limit for items returned, -1 means none. |
||
447 | * |
||
448 | * @return string[][] |
||
449 | */ |
||
450 | public function listVersions($contentId, $status = null, $limit = -1) |
||
460 | |||
461 | /** |
||
462 | * Returns all version numbers for the given $contentId. |
||
463 | * |
||
464 | * @param mixed $contentId |
||
465 | * |
||
466 | * @return int[] |
||
467 | */ |
||
468 | public function listVersionNumbers($contentId) |
||
478 | |||
479 | /** |
||
480 | * Returns last version number for content identified by $contentId. |
||
481 | * |
||
482 | * @param int $contentId |
||
483 | * |
||
484 | * @return int |
||
485 | */ |
||
486 | public function getLastVersionNumber($contentId) |
||
496 | |||
497 | /** |
||
498 | * Returns all IDs for locations that refer to $contentId. |
||
499 | * |
||
500 | * @param int $contentId |
||
501 | * |
||
502 | * @return int[] |
||
503 | */ |
||
504 | public function getAllLocationIds($contentId) |
||
514 | |||
515 | /** |
||
516 | * Returns all field IDs of $contentId grouped by their type. |
||
517 | * If $versionNo is set only field IDs for that version are returned. |
||
518 | * If $languageCode is set, only field IDs for that language are returned. |
||
519 | * |
||
520 | * @param int $contentId |
||
521 | * @param int|null $versionNo |
||
522 | * @param string|null $languageCode |
||
523 | * |
||
524 | * @return int[][] |
||
525 | */ |
||
526 | public function getFieldIdsByType($contentId, $versionNo = null, $languageCode = null) |
||
536 | |||
537 | /** |
||
538 | * Deletes relations to and from $contentId. |
||
539 | * If $versionNo is set only relations for that version are deleted. |
||
540 | * |
||
541 | * @param int $contentId |
||
542 | * @param int|null $versionNo |
||
543 | */ |
||
544 | public function deleteRelations($contentId, $versionNo = null) |
||
554 | |||
555 | /** |
||
556 | * Removes relations to Content with $contentId from Relation and RelationList field type fields. |
||
557 | * |
||
558 | * @param int $contentId |
||
559 | */ |
||
560 | public function removeReverseFieldRelations($contentId) |
||
570 | |||
571 | /** |
||
572 | * Deletes the field with the given $fieldId. |
||
573 | * |
||
574 | * @param int $fieldId |
||
575 | */ |
||
576 | public function deleteField($fieldId) |
||
586 | |||
587 | /** |
||
588 | * Deletes all fields of $contentId in all versions. |
||
589 | * If $versionNo is set only fields for that version are deleted. |
||
590 | * |
||
591 | * @param int $contentId |
||
592 | * @param int|null $versionNo |
||
593 | */ |
||
594 | public function deleteFields($contentId, $versionNo = null) |
||
604 | |||
605 | /** |
||
606 | * Deletes all versions of $contentId. |
||
607 | * If $versionNo is set only that version is deleted. |
||
608 | * |
||
609 | * @param int $contentId |
||
610 | * @param int|null $versionNo |
||
611 | */ |
||
612 | public function deleteVersions($contentId, $versionNo = null) |
||
622 | |||
623 | /** |
||
624 | * Deletes all names of $contentId. |
||
625 | * If $versionNo is set only names for that version are deleted. |
||
626 | * |
||
627 | * @param int $contentId |
||
628 | * @param int|null $versionNo |
||
629 | */ |
||
630 | public function deleteNames($contentId, $versionNo = null) |
||
640 | |||
641 | /** |
||
642 | * Sets the content object name. |
||
643 | * |
||
644 | * @param int $contentId |
||
645 | * @param int $version |
||
646 | * @param string $name |
||
647 | * @param string $language |
||
648 | */ |
||
649 | public function setName($contentId, $version, $name, $language) |
||
659 | |||
660 | /** |
||
661 | * Deletes the actual content object referred to by $contentId. |
||
662 | * |
||
663 | * @param int $contentId |
||
664 | */ |
||
665 | public function deleteContent($contentId) |
||
675 | |||
676 | /** |
||
677 | * Loads data of related to/from $contentId. |
||
678 | * |
||
679 | * @param int $contentId |
||
680 | * @param int $contentVersionNo |
||
681 | * @param int $relationType |
||
682 | * |
||
683 | * @return mixed[][] Content data, array structured like {@see \eZ\Publish\Core\Persistence\Legacy\Content\Gateway::load()} |
||
684 | */ |
||
685 | public function loadRelations($contentId, $contentVersionNo = null, $relationType = null) |
||
695 | |||
696 | /** |
||
697 | * {@inheritdoc} |
||
698 | */ |
||
699 | public function countReverseRelations(int $contentId, ?int $relationType = null): int |
||
700 | { |
||
701 | try { |
||
702 | return $this->innerGateway->countReverseRelations($contentId, $relationType); |
||
703 | } catch (DBALException | PDOException $e) { |
||
704 | throw new RuntimeException('Database error', 0, $e); |
||
705 | } |
||
706 | } |
||
707 | |||
708 | /** |
||
709 | * Loads data of related to/from $contentId. |
||
710 | * |
||
711 | * @param int $contentId |
||
712 | * @param bool $reverse Reverse relation, default false |
||
713 | * @param int $contentVersionNo |
||
714 | * @param int $relationType |
||
715 | * |
||
716 | * @return mixed[][] Content data, array structured like {@see \eZ\Publish\Core\Persistence\Legacy\Content\Gateway::load()} |
||
717 | */ |
||
718 | public function loadReverseRelations($contentId, $relationType = null) |
||
719 | { |
||
720 | try { |
||
721 | return $this->innerGateway->loadReverseRelations($contentId, $relationType); |
||
722 | } catch (DBALException $e) { |
||
723 | throw new RuntimeException('Database error', 0, $e); |
||
724 | } catch (PDOException $e) { |
||
725 | throw new RuntimeException('Database error', 0, $e); |
||
726 | } |
||
727 | } |
||
728 | |||
729 | /** |
||
730 | * Deletes the relation with the given $relationId. |
||
731 | * |
||
732 | * @param int $relationId |
||
733 | * @param int $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON, |
||
734 | * \eZ\Publish\API\Repository\Values\Content\Relation::EMBED, |
||
735 | * \eZ\Publish\API\Repository\Values\Content\Relation::LINK, |
||
736 | * \eZ\Publish\API\Repository\Values\Content\Relation::FIELD} |
||
737 | */ |
||
738 | public function deleteRelation($relationId, $type) |
||
748 | |||
749 | /** |
||
750 | * Inserts a new relation database record. |
||
751 | * |
||
752 | * @param \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct $createStruct |
||
753 | * |
||
754 | * @return int ID the inserted ID |
||
755 | */ |
||
756 | public function insertRelation(RelationCreateStruct $struct) |
||
766 | |||
767 | /** |
||
768 | * Returns all Content IDs for a given $contentTypeId. |
||
769 | * |
||
770 | * @param int $contentTypeId |
||
771 | * |
||
772 | * @return int[] |
||
773 | */ |
||
774 | public function getContentIdsByContentTypeId($contentTypeId) |
||
784 | |||
785 | /** |
||
786 | * Load name data for set of content id's and corresponding version number. |
||
787 | * |
||
788 | * @param array[] $rows array of hashes with 'id' and 'version' to load names for |
||
789 | * |
||
790 | * @return array |
||
791 | */ |
||
792 | public function loadVersionedNameData($rows) |
||
802 | |||
803 | /** |
||
804 | * Batch method for copying all relation meta data for copied Content object. |
||
805 | * |
||
806 | * {@inheritdoc} |
||
807 | * |
||
808 | * @param int $originalContentId |
||
809 | * @param int $copiedContentId |
||
810 | * @param int|null $versionNo If specified only copy for a given version number, otherwise all. |
||
811 | */ |
||
812 | public function copyRelations($originalContentId, $copiedContentId, $versionNo = null) |
||
822 | |||
823 | /** |
||
824 | * Remove the specified translation from all the Versions of a Content Object. |
||
825 | * |
||
826 | * @param int $contentId |
||
827 | * @param string $languageCode language code of the translation |
||
828 | */ |
||
829 | public function deleteTranslationFromContent($contentId, $languageCode) |
||
839 | |||
840 | /** |
||
841 | * Delete Content fields (attributes) for the given Translation. |
||
842 | * If $versionNo is given, fields for that Version only will be deleted. |
||
843 | * |
||
844 | * @param string $languageCode |
||
845 | * @param int $contentId |
||
846 | * @param int $versionNo (optional) filter by versionNo |
||
847 | */ |
||
848 | public function deleteTranslatedFields($languageCode, $contentId, $versionNo = null) |
||
858 | |||
859 | /** |
||
860 | * Delete the specified Translation from the given Version. |
||
861 | * |
||
862 | * @param int $contentId |
||
863 | * @param int $versionNo |
||
864 | * @param string $languageCode |
||
865 | */ |
||
866 | public function deleteTranslationFromVersion($contentId, $versionNo, $languageCode) |
||
876 | } |
||
877 |
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.