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 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 | View Code Duplication | public function getContext() |
|
52 | { |
||
53 | try { |
||
54 | return $this->innerGateway->getContext(); |
||
55 | } catch (DBALException $e) { |
||
56 | throw new RuntimeException('Database error', 0, $e); |
||
57 | } catch (PDOException $e) { |
||
58 | throw new RuntimeException('Database error', 0, $e); |
||
59 | } |
||
60 | } |
||
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 | View Code Duplication | public function insertContentObject(CreateStruct $struct, $currentVersionNo = 1) |
|
71 | { |
||
72 | try { |
||
73 | return $this->innerGateway->insertContentObject($struct, $currentVersionNo); |
||
74 | } catch (DBALException $e) { |
||
75 | throw new RuntimeException('Database error', 0, $e); |
||
76 | } catch (PDOException $e) { |
||
77 | throw new RuntimeException('Database error', 0, $e); |
||
78 | } |
||
79 | } |
||
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 | View Code Duplication | public function insertVersion(VersionInfo $versionInfo, array $fields) |
|
90 | { |
||
91 | try { |
||
92 | return $this->innerGateway->insertVersion($versionInfo, $fields); |
||
93 | } catch (DBALException $e) { |
||
94 | throw new RuntimeException('Database error', 0, $e); |
||
95 | } catch (PDOException $e) { |
||
96 | throw new RuntimeException('Database error', 0, $e); |
||
97 | } |
||
98 | } |
||
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 | View Code Duplication | public function updateContent($contentId, MetadataUpdateStruct $struct, VersionInfo $prePublishVersionInfo = null) |
|
108 | { |
||
109 | try { |
||
110 | return $this->innerGateway->updateContent($contentId, $struct, $prePublishVersionInfo); |
||
111 | } catch (DBALException $e) { |
||
112 | throw new RuntimeException('Database error', 0, $e); |
||
113 | } catch (PDOException $e) { |
||
114 | throw new RuntimeException('Database error', 0, $e); |
||
115 | } |
||
116 | } |
||
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 | View Code Duplication | public function updateVersion($contentId, $versionNo, UpdateStruct $struct) |
|
126 | { |
||
127 | try { |
||
128 | return $this->innerGateway->updateVersion($contentId, $versionNo, $struct); |
||
129 | } catch (DBALException $e) { |
||
130 | throw new RuntimeException('Database error', 0, $e); |
||
131 | } catch (PDOException $e) { |
||
132 | throw new RuntimeException('Database error', 0, $e); |
||
133 | } |
||
134 | } |
||
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 | View Code Duplication | public function updateAlwaysAvailableFlag($contentId, $newAlwaysAvailable) |
|
143 | { |
||
144 | try { |
||
145 | return $this->innerGateway->updateAlwaysAvailableFlag($contentId, $newAlwaysAvailable); |
||
146 | } catch (DBALException $e) { |
||
147 | throw new RuntimeException('Database error', 0, $e); |
||
148 | } catch (PDOException $e) { |
||
149 | throw new RuntimeException('Database error', 0, $e); |
||
150 | } |
||
151 | } |
||
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 | View Code Duplication | public function setStatus($contentId, $version, $status) |
|
165 | { |
||
166 | try { |
||
167 | return $this->innerGateway->setStatus($contentId, $version, $status); |
||
168 | } catch (DBALException $e) { |
||
169 | throw new RuntimeException('Database error', 0, $e); |
||
170 | } catch (PDOException $e) { |
||
171 | throw new RuntimeException('Database error', 0, $e); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Inserts a new field. |
||
177 | * |
||
178 | * Only used when a new field is created (i.e. a new object or a field in a |
||
179 | * new language!). After that, field IDs need to stay the same, only the |
||
180 | * version number changes. |
||
181 | * |
||
182 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
183 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
184 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
||
185 | * |
||
186 | * @return int ID |
||
187 | */ |
||
188 | View Code Duplication | public function insertNewField(Content $content, Field $field, StorageFieldValue $value) |
|
198 | |||
199 | /** |
||
200 | * Inserts an existing field. |
||
201 | * |
||
202 | * Used to insert a field with an exsting ID but a new version number. |
||
203 | * |
||
204 | * @param Content $content |
||
205 | * @param Field $field |
||
206 | * @param StorageFieldValue $value |
||
207 | */ |
||
208 | View Code Duplication | public function insertExistingField(Content $content, Field $field, StorageFieldValue $value) |
|
218 | |||
219 | /** |
||
220 | * Updates an existing field. |
||
221 | * |
||
222 | * @param Field $field |
||
223 | * @param StorageFieldValue $value |
||
224 | */ |
||
225 | View Code Duplication | public function updateField(Field $field, StorageFieldValue $value) |
|
235 | |||
236 | /** |
||
237 | * Updates an existing, non-translatable field. |
||
238 | * |
||
239 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
240 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
||
241 | * @param int $contentId |
||
242 | */ |
||
243 | View Code Duplication | public function updateNonTranslatableField( |
|
256 | |||
257 | /** |
||
258 | * Loads data for a content object. |
||
259 | * |
||
260 | * Returns an array with the relevant data. |
||
261 | * |
||
262 | * @param mixed $contentId |
||
263 | * @param mixed $version |
||
264 | * @param string[] $translations |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | View Code Duplication | public function load($contentId, $version, array $translations = null) |
|
278 | |||
279 | /** |
||
280 | * Loads data for a content object identified by its remote ID. |
||
281 | * |
||
282 | * Returns an array with the relevant data. |
||
283 | * |
||
284 | * @param mixed $remoteId |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | View Code Duplication | public function loadContentInfoByRemoteId($remoteId) |
|
298 | |||
299 | /** |
||
300 | * Loads info for a content object identified by its location ID (node ID). |
||
301 | * |
||
302 | * Returns an array with the relevant data. |
||
303 | * |
||
304 | * @param int $locationId |
||
305 | * |
||
306 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | View Code Duplication | public function loadContentInfoByLocationId($locationId) |
|
320 | |||
321 | /** |
||
322 | * Loads info for content identified by $contentId. |
||
323 | * Will basically return a hash containing all field values for ezcontentobject table plus following keys: |
||
324 | * - always_available => Boolean indicating if content's language mask contains alwaysAvailable bit field |
||
325 | * - main_language_code => Language code for main (initial) language. E.g. "eng-GB". |
||
326 | * |
||
327 | * @param int $contentId |
||
328 | * |
||
329 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
330 | * |
||
331 | * @return array |
||
332 | */ |
||
333 | View Code Duplication | public function loadContentInfo($contentId) |
|
343 | |||
344 | View Code Duplication | public function loadContentInfoList(array $contentIds) |
|
354 | |||
355 | /** |
||
356 | * Loads version info for content identified by $contentId and $versionNo. |
||
357 | * Will basically return a hash containing all field values from ezcontentobject_version table plus following keys: |
||
358 | * - names => Hash of content object names. Key is the language code, value is the name. |
||
359 | * - 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. |
||
360 | * - initial_language_code => Language code for initial language in this version. |
||
361 | * |
||
362 | * @param int $contentId |
||
363 | * @param int $versionNo |
||
364 | * |
||
365 | * @return array |
||
366 | */ |
||
367 | View Code Duplication | public function loadVersionInfo($contentId, $versionNo) |
|
377 | |||
378 | /** |
||
379 | * Returns data for all versions with given status created by the given $userId. |
||
380 | * |
||
381 | * @param int $userId |
||
382 | * @param int $status |
||
383 | * |
||
384 | * @return string[][] |
||
385 | */ |
||
386 | View Code Duplication | public function listVersionsForUser($userId, $status = VersionInfo::STATUS_DRAFT) |
|
396 | |||
397 | /** |
||
398 | * Returns all version data for the given $contentId. |
||
399 | * |
||
400 | * Result is returned with oldest version first (using version id as it has index and is auto increment). |
||
401 | * |
||
402 | * @param mixed $contentId |
||
403 | * @param mixed|null $status Optional argument to filter versions by status, like {@see VersionInfo::STATUS_ARCHIVED}. |
||
404 | * @param int $limit Limit for items returned, -1 means none. |
||
405 | * |
||
406 | * @return string[][] |
||
407 | */ |
||
408 | View Code Duplication | public function listVersions($contentId, $status = null, $limit = -1) |
|
418 | |||
419 | /** |
||
420 | * Returns all version numbers for the given $contentId. |
||
421 | * |
||
422 | * @param mixed $contentId |
||
423 | * |
||
424 | * @return int[] |
||
425 | */ |
||
426 | View Code Duplication | public function listVersionNumbers($contentId) |
|
436 | |||
437 | /** |
||
438 | * Returns last version number for content identified by $contentId. |
||
439 | * |
||
440 | * @param int $contentId |
||
441 | * |
||
442 | * @return int |
||
443 | */ |
||
444 | View Code Duplication | public function getLastVersionNumber($contentId) |
|
454 | |||
455 | /** |
||
456 | * Returns all IDs for locations that refer to $contentId. |
||
457 | * |
||
458 | * @param int $contentId |
||
459 | * |
||
460 | * @return int[] |
||
461 | */ |
||
462 | View Code Duplication | public function getAllLocationIds($contentId) |
|
472 | |||
473 | /** |
||
474 | * Returns all field IDs of $contentId grouped by their type. |
||
475 | * If $versionNo is set only field IDs for that version are returned. |
||
476 | * If $languageCode is set, only field IDs for that language are returned. |
||
477 | * |
||
478 | * @param int $contentId |
||
479 | * @param int|null $versionNo |
||
480 | * @param string|null $languageCode |
||
481 | * |
||
482 | * @return int[][] |
||
483 | */ |
||
484 | View Code Duplication | public function getFieldIdsByType($contentId, $versionNo = null, $languageCode = null) |
|
494 | |||
495 | /** |
||
496 | * Deletes relations to and from $contentId. |
||
497 | * If $versionNo is set only relations for that version are deleted. |
||
498 | * |
||
499 | * @param int $contentId |
||
500 | * @param int|null $versionNo |
||
501 | */ |
||
502 | View Code Duplication | public function deleteRelations($contentId, $versionNo = null) |
|
512 | |||
513 | /** |
||
514 | * Removes relations to Content with $contentId from Relation and RelationList field type fields. |
||
515 | * |
||
516 | * @param int $contentId |
||
517 | */ |
||
518 | View Code Duplication | public function removeReverseFieldRelations($contentId) |
|
528 | |||
529 | /** |
||
530 | * Deletes the field with the given $fieldId. |
||
531 | * |
||
532 | * @param int $fieldId |
||
533 | */ |
||
534 | View Code Duplication | public function deleteField($fieldId) |
|
544 | |||
545 | /** |
||
546 | * Deletes all fields of $contentId in all versions. |
||
547 | * If $versionNo is set only fields for that version are deleted. |
||
548 | * |
||
549 | * @param int $contentId |
||
550 | * @param int|null $versionNo |
||
551 | */ |
||
552 | View Code Duplication | public function deleteFields($contentId, $versionNo = null) |
|
562 | |||
563 | /** |
||
564 | * Deletes all versions of $contentId. |
||
565 | * If $versionNo is set only that version is deleted. |
||
566 | * |
||
567 | * @param int $contentId |
||
568 | * @param int|null $versionNo |
||
569 | */ |
||
570 | View Code Duplication | public function deleteVersions($contentId, $versionNo = null) |
|
580 | |||
581 | /** |
||
582 | * Deletes all names of $contentId. |
||
583 | * If $versionNo is set only names for that version are deleted. |
||
584 | * |
||
585 | * @param int $contentId |
||
586 | * @param int|null $versionNo |
||
587 | */ |
||
588 | View Code Duplication | public function deleteNames($contentId, $versionNo = null) |
|
598 | |||
599 | /** |
||
600 | * Sets the content object name. |
||
601 | * |
||
602 | * @param int $contentId |
||
603 | * @param int $version |
||
604 | * @param string $name |
||
605 | * @param string $language |
||
606 | */ |
||
607 | View Code Duplication | public function setName($contentId, $version, $name, $language) |
|
617 | |||
618 | /** |
||
619 | * Deletes the actual content object referred to by $contentId. |
||
620 | * |
||
621 | * @param int $contentId |
||
622 | */ |
||
623 | View Code Duplication | public function deleteContent($contentId) |
|
633 | |||
634 | /** |
||
635 | * Loads data of related to/from $contentId. |
||
636 | * |
||
637 | * @param int $contentId |
||
638 | * @param int $contentVersionNo |
||
639 | * @param int $relationType |
||
640 | * |
||
641 | * @return mixed[][] Content data, array structured like {@see \eZ\Publish\Core\Persistence\Legacy\Content\Gateway::load()} |
||
642 | */ |
||
643 | View Code Duplication | public function loadRelations($contentId, $contentVersionNo = null, $relationType = null) |
|
653 | |||
654 | /** |
||
655 | * Loads data of related to/from $contentId. |
||
656 | * |
||
657 | * @param int $contentId |
||
658 | * @param bool $reverse Reverse relation, default false |
||
|
|||
659 | * @param int $contentVersionNo |
||
660 | * @param int $relationType |
||
661 | * |
||
662 | * @return mixed[][] Content data, array structured like {@see \eZ\Publish\Core\Persistence\Legacy\Content\Gateway::load()} |
||
663 | */ |
||
664 | View Code Duplication | public function loadReverseRelations($contentId, $relationType = null) |
|
674 | |||
675 | /** |
||
676 | * Deletes the relation with the given $relationId. |
||
677 | * |
||
678 | * @param int $relationId |
||
679 | * @param int $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON, |
||
680 | * \eZ\Publish\API\Repository\Values\Content\Relation::EMBED, |
||
681 | * \eZ\Publish\API\Repository\Values\Content\Relation::LINK, |
||
682 | * \eZ\Publish\API\Repository\Values\Content\Relation::FIELD} |
||
683 | */ |
||
684 | View Code Duplication | public function deleteRelation($relationId, $type) |
|
694 | |||
695 | /** |
||
696 | * Inserts a new relation database record. |
||
697 | * |
||
698 | * @param \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct $createStruct |
||
699 | * |
||
700 | * @return int ID the inserted ID |
||
701 | */ |
||
702 | View Code Duplication | public function insertRelation(RelationCreateStruct $struct) |
|
712 | |||
713 | /** |
||
714 | * Returns all Content IDs for a given $contentTypeId. |
||
715 | * |
||
716 | * @param int $contentTypeId |
||
717 | * |
||
718 | * @return int[] |
||
719 | */ |
||
720 | View Code Duplication | public function getContentIdsByContentTypeId($contentTypeId) |
|
730 | |||
731 | /** |
||
732 | * Load name data for set of content id's and corresponding version number. |
||
733 | * |
||
734 | * @param array[] $rows array of hashes with 'id' and 'version' to load names for |
||
735 | * |
||
736 | * @return array |
||
737 | */ |
||
738 | View Code Duplication | public function loadVersionedNameData($rows) |
|
748 | |||
749 | /** |
||
750 | * Batch method for copying all relation meta data for copied Content object. |
||
751 | * |
||
752 | * {@inheritdoc} |
||
753 | * |
||
754 | * @param int $originalContentId |
||
755 | * @param int $copiedContentId |
||
756 | * @param int|null $versionNo If specified only copy for a given version number, otherwise all. |
||
757 | */ |
||
758 | View Code Duplication | public function copyRelations($originalContentId, $copiedContentId, $versionNo = null) |
|
768 | |||
769 | /** |
||
770 | * Remove the specified translation from all the Versions of a Content Object. |
||
771 | * |
||
772 | * @param int $contentId |
||
773 | * @param string $languageCode language code of the translation |
||
774 | */ |
||
775 | View Code Duplication | public function deleteTranslationFromContent($contentId, $languageCode) |
|
785 | |||
786 | /** |
||
787 | * Delete Content fields (attributes) for the given Translation. |
||
788 | * If $versionNo is given, fields for that Version only will be deleted. |
||
789 | * |
||
790 | * @param string $languageCode |
||
791 | * @param int $contentId |
||
792 | * @param int $versionNo (optional) filter by versionNo |
||
793 | */ |
||
794 | View Code Duplication | public function deleteTranslatedFields($languageCode, $contentId, $versionNo = null) |
|
804 | |||
805 | /** |
||
806 | * Delete the specified Translation from the given Version. |
||
807 | * |
||
808 | * @param int $contentId |
||
809 | * @param int $versionNo |
||
810 | * @param string $languageCode |
||
811 | */ |
||
812 | View Code Duplication | public function deleteTranslationFromVersion($contentId, $versionNo, $languageCode) |
|
822 | } |
||
823 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.