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 |
||
| 29 | class ExceptionConversion extends Gateway |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * The wrapped gateway. |
||
| 33 | * |
||
| 34 | * @var Gateway |
||
| 35 | */ |
||
| 36 | protected $innerGateway; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Creates a new exception conversion gateway around $innerGateway. |
||
| 40 | * |
||
| 41 | * @param Gateway $innerGateway |
||
| 42 | */ |
||
| 43 | public function __construct(Gateway $innerGateway) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Get context definition for external storage layers. |
||
| 50 | * |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | public function getContext() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Inserts a new content object. |
||
| 66 | * |
||
| 67 | * @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct |
||
| 68 | * @param mixed $currentVersionNo |
||
| 69 | * |
||
| 70 | * @return int ID |
||
| 71 | */ |
||
| 72 | public function insertContentObject(CreateStruct $struct, $currentVersionNo = 1) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Inserts a new version. |
||
| 85 | * |
||
| 86 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo |
||
| 87 | * @param \eZ\Publish\SPI\Persistence\Content\Field[] $fields |
||
| 88 | * |
||
| 89 | * @return int ID |
||
| 90 | */ |
||
| 91 | public function insertVersion(VersionInfo $versionInfo, array $fields) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Updates an existing content identified by $contentId in respect to $struct. |
||
| 104 | * |
||
| 105 | * @param int $contentId |
||
| 106 | * @param \eZ\Publish\SPI\Persistence\Content\MetadataUpdateStruct $struct |
||
| 107 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $prePublishVersionInfo Provided on publish |
||
| 108 | */ |
||
| 109 | public function updateContent($contentId, MetadataUpdateStruct $struct, VersionInfo $prePublishVersionInfo = null) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Updates version $versionNo for content identified by $contentId, in respect to $struct. |
||
| 122 | * |
||
| 123 | * @param int $contentId |
||
| 124 | * @param int $versionNo |
||
| 125 | * @param \eZ\Publish\SPI\Persistence\Content\UpdateStruct $struct |
||
| 126 | */ |
||
| 127 | public function updateVersion($contentId, $versionNo, UpdateStruct $struct) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Updates "always available" flag for content identified by $contentId, in respect to $alwaysAvailable. |
||
| 140 | * |
||
| 141 | * @param int $contentId |
||
| 142 | * @param bool $newAlwaysAvailable New "always available" value |
||
| 143 | */ |
||
| 144 | public function updateAlwaysAvailableFlag($contentId, $newAlwaysAvailable) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Sets the state of object identified by $contentId and $version to $state. |
||
| 157 | * |
||
| 158 | * The $status can be one of STATUS_DRAFT, STATUS_PUBLISHED, STATUS_ARCHIVED |
||
| 159 | * |
||
| 160 | * @param int $contentId |
||
| 161 | * @param int $version |
||
| 162 | * @param int $status |
||
| 163 | * |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | public function setStatus($contentId, $version, $status) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Inserts a new field. |
||
| 179 | * |
||
| 180 | * Only used when a new field is created (i.e. a new object or a field in a |
||
| 181 | * new language!). After that, field IDs need to stay the same, only the |
||
| 182 | * version number changes. |
||
| 183 | * |
||
| 184 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
| 185 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
| 186 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
||
| 187 | * |
||
| 188 | * @return int ID |
||
| 189 | */ |
||
| 190 | View Code Duplication | public function insertNewField(Content $content, Field $field, StorageFieldValue $value) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Inserts an existing field. |
||
| 203 | * |
||
| 204 | * Used to insert a field with an exsting ID but a new version number. |
||
| 205 | * |
||
| 206 | * @param Content $content |
||
| 207 | * @param Field $field |
||
| 208 | * @param StorageFieldValue $value |
||
| 209 | */ |
||
| 210 | View Code Duplication | public function insertExistingField(Content $content, Field $field, StorageFieldValue $value) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Updates an existing field. |
||
| 223 | * |
||
| 224 | * @param Field $field |
||
| 225 | * @param StorageFieldValue $value |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function updateField(Field $field, StorageFieldValue $value) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Updates an existing, non-translatable field. |
||
| 240 | * |
||
| 241 | * @param \eZ\Publish\SPI\Persistence\Content\Field $field |
||
| 242 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue $value |
||
| 243 | * @param int $contentId |
||
| 244 | */ |
||
| 245 | View Code Duplication | public function updateNonTranslatableField( |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Loads data for a content object. |
||
| 261 | * |
||
| 262 | * Returns an array with the relevant data. |
||
| 263 | * |
||
| 264 | * @param mixed $contentId |
||
| 265 | * @param mixed $version |
||
| 266 | * @param string[] $translations |
||
| 267 | * |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | public function load($contentId, $version, array $translations = null) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Loads data for a content object identified by its remote ID. |
||
| 283 | * |
||
| 284 | * Returns an array with the relevant data. |
||
| 285 | * |
||
| 286 | * @param mixed $remoteId |
||
| 287 | * |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public function loadContentInfoByRemoteId($remoteId) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Loads info for content identified by $contentId. |
||
| 303 | * Will basically return a hash containing all field values for ezcontentobject table plus following keys: |
||
| 304 | * - always_available => Boolean indicating if content's language mask contains alwaysAvailable bit field |
||
| 305 | * - main_language_code => Language code for main (initial) language. E.g. "eng-GB". |
||
| 306 | * |
||
| 307 | * @param int $contentId |
||
| 308 | * |
||
| 309 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
| 310 | * |
||
| 311 | * @return array |
||
| 312 | */ |
||
| 313 | public function loadContentInfo($contentId) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Loads version info for content identified by $contentId and $versionNo. |
||
| 326 | * Will basically return a hash containing all field values from ezcontentobject_version table plus following keys: |
||
| 327 | * - names => Hash of content object names. Key is the language code, value is the name. |
||
| 328 | * - 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. |
||
| 329 | * - initial_language_code => Language code for initial language in this version. |
||
| 330 | * |
||
| 331 | * @param int $contentId |
||
| 332 | * @param int $versionNo |
||
| 333 | * |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | public function loadVersionInfo($contentId, $versionNo) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns data for all versions with given status created by the given $userId. |
||
| 349 | * |
||
| 350 | * @param int $userId |
||
| 351 | * @param int $status |
||
| 352 | * |
||
| 353 | * @return string[][] |
||
| 354 | */ |
||
| 355 | public function listVersionsForUser($userId, $status = VersionInfo::STATUS_DRAFT) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Returns all version data for the given $contentId. |
||
| 368 | * |
||
| 369 | * @param mixed $contentId |
||
| 370 | * |
||
| 371 | * @return string[][] |
||
| 372 | */ |
||
| 373 | public function listVersions($contentId) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Returns all version numbers for the given $contentId. |
||
| 386 | * |
||
| 387 | * @param mixed $contentId |
||
| 388 | * |
||
| 389 | * @return int[] |
||
| 390 | */ |
||
| 391 | public function listVersionNumbers($contentId) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Returns last version number for content identified by $contentId. |
||
| 404 | * |
||
| 405 | * @param int $contentId |
||
| 406 | * |
||
| 407 | * @return int |
||
| 408 | */ |
||
| 409 | public function getLastVersionNumber($contentId) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Returns all IDs for locations that refer to $contentId. |
||
| 422 | * |
||
| 423 | * @param int $contentId |
||
| 424 | * |
||
| 425 | * @return int[] |
||
| 426 | */ |
||
| 427 | public function getAllLocationIds($contentId) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Returns all field IDs of $contentId grouped by their type. |
||
| 440 | * If $versionNo is set only field IDs for that version are returned. |
||
| 441 | * |
||
| 442 | * @param int $contentId |
||
| 443 | * @param int|null $versionNo |
||
| 444 | * |
||
| 445 | * @return int[][] |
||
| 446 | */ |
||
| 447 | public function getFieldIdsByType($contentId, $versionNo = null) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Deletes relations to and from $contentId. |
||
| 460 | * If $versionNo is set only relations for that version are deleted. |
||
| 461 | * |
||
| 462 | * @param int $contentId |
||
| 463 | * @param int|null $versionNo |
||
| 464 | */ |
||
| 465 | public function deleteRelations($contentId, $versionNo = null) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Removes relations to Content with $contentId from Relation and RelationList field type fields. |
||
| 478 | * |
||
| 479 | * @param int $contentId |
||
| 480 | */ |
||
| 481 | public function removeReverseFieldRelations($contentId) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Deletes the field with the given $fieldId. |
||
| 494 | * |
||
| 495 | * @param int $fieldId |
||
| 496 | */ |
||
| 497 | public function deleteField($fieldId) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Deletes all fields of $contentId in all versions. |
||
| 510 | * If $versionNo is set only fields for that version are deleted. |
||
| 511 | * |
||
| 512 | * @param int $contentId |
||
| 513 | * @param int|null $versionNo |
||
| 514 | */ |
||
| 515 | public function deleteFields($contentId, $versionNo = null) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Deletes all versions of $contentId. |
||
| 528 | * If $versionNo is set only that version is deleted. |
||
| 529 | * |
||
| 530 | * @param int $contentId |
||
| 531 | * @param int|null $versionNo |
||
| 532 | */ |
||
| 533 | public function deleteVersions($contentId, $versionNo = null) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Deletes all names of $contentId. |
||
| 546 | * If $versionNo is set only names for that version are deleted. |
||
| 547 | * |
||
| 548 | * @param int $contentId |
||
| 549 | * @param int|null $versionNo |
||
| 550 | */ |
||
| 551 | public function deleteNames($contentId, $versionNo = null) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Sets the content object name. |
||
| 564 | * |
||
| 565 | * @param int $contentId |
||
| 566 | * @param int $version |
||
| 567 | * @param string $name |
||
| 568 | * @param string $language |
||
| 569 | */ |
||
| 570 | public function setName($contentId, $version, $name, $language) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Deletes the actual content object referred to by $contentId. |
||
| 583 | * |
||
| 584 | * @param int $contentId |
||
| 585 | */ |
||
| 586 | public function deleteContent($contentId) |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Loads data of related to/from $contentId. |
||
| 599 | * |
||
| 600 | * @param int $contentId |
||
| 601 | * @param int $contentVersionNo |
||
| 602 | * @param int $relationType |
||
| 603 | * |
||
| 604 | * @return mixed[][] Content data, array structured like {@see \eZ\Publish\Core\Persistence\Legacy\Content\Gateway::load()} |
||
| 605 | */ |
||
| 606 | public function loadRelations($contentId, $contentVersionNo = null, $relationType = null) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Loads data of related to/from $contentId. |
||
| 619 | * |
||
| 620 | * @param int $contentId |
||
| 621 | * @param bool $reverse Reverse relation, default false |
||
|
|
|||
| 622 | * @param int $contentVersionNo |
||
| 623 | * @param int $relationType |
||
| 624 | * |
||
| 625 | * @return mixed[][] Content data, array structured like {@see \eZ\Publish\Core\Persistence\Legacy\Content\Gateway::load()} |
||
| 626 | */ |
||
| 627 | public function loadReverseRelations($contentId, $relationType = null) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Deletes the relation with the given $relationId. |
||
| 640 | * |
||
| 641 | * @param int $relationId |
||
| 642 | * @param int $type {@see \eZ\Publish\API\Repository\Values\Content\Relation::COMMON, |
||
| 643 | * \eZ\Publish\API\Repository\Values\Content\Relation::EMBED, |
||
| 644 | * \eZ\Publish\API\Repository\Values\Content\Relation::LINK, |
||
| 645 | * \eZ\Publish\API\Repository\Values\Content\Relation::FIELD} |
||
| 646 | */ |
||
| 647 | public function deleteRelation($relationId, $type) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Inserts a new relation database record. |
||
| 660 | * |
||
| 661 | * @param \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct $createStruct |
||
| 662 | * |
||
| 663 | * @return int ID the inserted ID |
||
| 664 | */ |
||
| 665 | public function insertRelation(RelationCreateStruct $struct) |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Returns all Content IDs for a given $contentTypeId. |
||
| 678 | * |
||
| 679 | * @param int $contentTypeId |
||
| 680 | * |
||
| 681 | * @return int[] |
||
| 682 | */ |
||
| 683 | public function getContentIdsByContentTypeId($contentTypeId) |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Load name data for set of content id's and corresponding version number. |
||
| 696 | * |
||
| 697 | * @param array[] $rows array of hashes with 'id' and 'version' to load names for |
||
| 698 | * |
||
| 699 | * @return array |
||
| 700 | */ |
||
| 701 | public function loadVersionedNameData($rows) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Batch method for copying all relation meta data for copied Content object. |
||
| 714 | * |
||
| 715 | * {@inheritdoc} |
||
| 716 | * |
||
| 717 | * @param int $originalContentId |
||
| 718 | * @param int $copiedContentId |
||
| 719 | * @param int|null $versionNo If specified only copy for a given version number, otherwise all. |
||
| 720 | */ |
||
| 721 | public function copyRelations($originalContentId, $copiedContentId, $versionNo = null) |
||
| 731 | } |
||
| 732 |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.