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 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 |
||
| 25 | class Handler implements UrlAliasHandlerInterface |
||
| 26 | { |
||
| 27 | const ROOT_LOCATION_ID = 1; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * This is intentionally hardcoded for now as: |
||
| 31 | * 1. We don't implement this configuration option. |
||
| 32 | * 2. Such option should not be in this layer, should be handled higher up. |
||
| 33 | * |
||
| 34 | * @deprecated |
||
| 35 | */ |
||
| 36 | const CONTENT_REPOSITORY_ROOT_LOCATION_ID = 2; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The maximum level of alias depth. |
||
| 40 | */ |
||
| 41 | const MAX_URL_ALIAS_DEPTH_LEVEL = 60; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * UrlAlias Gateway. |
||
| 45 | * |
||
| 46 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway |
||
| 47 | */ |
||
| 48 | protected $gateway; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Gateway for handling location data. |
||
| 52 | * |
||
| 53 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway |
||
| 54 | */ |
||
| 55 | protected $locationGateway; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * UrlAlias Mapper. |
||
| 59 | * |
||
| 60 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper |
||
| 61 | */ |
||
| 62 | protected $mapper; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Caching language handler. |
||
| 66 | * |
||
| 67 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler |
||
| 68 | */ |
||
| 69 | protected $languageHandler; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * URL slug converter. |
||
| 73 | * |
||
| 74 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter |
||
| 75 | */ |
||
| 76 | protected $slugConverter; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Creates a new UrlAlias Handler. |
||
| 80 | * |
||
| 81 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $gateway |
||
| 82 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper $mapper |
||
| 83 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway |
||
| 84 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 85 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter $slugConverter |
||
| 86 | */ |
||
| 87 | public function __construct( |
||
| 100 | |||
| 101 | public function publishUrlAliasForLocation( |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Internal publish method, accepting language ID instead of language code and optionally |
||
| 123 | * new alias ID (used when swapping Locations). |
||
| 124 | * |
||
| 125 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
||
| 126 | * |
||
| 127 | * @param int $locationId |
||
| 128 | * @param int $parentLocationId |
||
| 129 | * @param string $name |
||
| 130 | * @param int $languageId |
||
| 131 | * @param bool $alwaysAvailable |
||
| 132 | * @param bool $updatePathIdentificationString legacy storage specific for updating ezcontentobject_tree.path_identification_string |
||
| 133 | * @param int $newId |
||
| 134 | */ |
||
| 135 | private function internalPublishUrlAliasForLocation( |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Create a user chosen $alias pointing to $locationId in $languageCode. |
||
| 271 | * |
||
| 272 | * If $languageCode is null the $alias is created in the system's default |
||
| 273 | * language. $alwaysAvailable makes the alias available in all languages. |
||
| 274 | * |
||
| 275 | * @param mixed $locationId |
||
| 276 | * @param string $path |
||
| 277 | * @param bool $forwarding |
||
| 278 | * @param string $languageCode |
||
| 279 | * @param bool $alwaysAvailable |
||
| 280 | * |
||
| 281 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 282 | */ |
||
| 283 | public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Create a user chosen $alias pointing to a resource in $languageCode. |
||
| 296 | * This method does not handle location resources - if a user enters a location target |
||
| 297 | * the createCustomUrlAlias method has to be used. |
||
| 298 | * |
||
| 299 | * If $languageCode is null the $alias is created in the system's default |
||
| 300 | * language. $alwaysAvailable makes the alias available in all languages. |
||
| 301 | * |
||
| 302 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given language |
||
| 303 | * |
||
| 304 | * @param string $resource |
||
| 305 | * @param string $path |
||
| 306 | * @param bool $forwarding |
||
| 307 | * @param string $languageCode |
||
| 308 | * @param bool $alwaysAvailable |
||
| 309 | * |
||
| 310 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 311 | */ |
||
| 312 | public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Internal method for creating global or custom URL alias (these are handled in the same way). |
||
| 325 | * |
||
| 326 | * @throws \eZ\Publish\Core\Base\Exceptions\ForbiddenException if the path already exists for the given language |
||
| 327 | * |
||
| 328 | * @param string $action |
||
| 329 | * @param string $path |
||
| 330 | * @param bool $forward |
||
| 331 | * @param string|null $languageCode |
||
| 332 | * @param bool $alwaysAvailable |
||
| 333 | * |
||
| 334 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 335 | */ |
||
| 336 | protected function createUrlAlias($action, $path, $forward, $languageCode, $alwaysAvailable) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Convenience method for inserting nop type row. |
||
| 414 | * |
||
| 415 | * @param mixed $parentId |
||
| 416 | * @param string $text |
||
| 417 | * @param string $textMD5 |
||
| 418 | * |
||
| 419 | * @return mixed |
||
| 420 | */ |
||
| 421 | protected function insertNopEntry($parentId, $text, $textMD5) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * List of user generated or autogenerated url entries, pointing to $locationId. |
||
| 436 | * |
||
| 437 | * @param mixed $locationId |
||
| 438 | * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated |
||
| 439 | * |
||
| 440 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
| 441 | */ |
||
| 442 | View Code Duplication | public function listURLAliasesForLocation($locationId, $custom = false) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * List global aliases. |
||
| 454 | * |
||
| 455 | * @param string|null $languageCode |
||
| 456 | * @param int $offset |
||
| 457 | * @param int $limit |
||
| 458 | * |
||
| 459 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
| 460 | */ |
||
| 461 | View Code Duplication | public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Removes url aliases. |
||
| 473 | * |
||
| 474 | * Autogenerated aliases are not removed by this method. |
||
| 475 | * |
||
| 476 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias[] $urlAliases |
||
| 477 | * |
||
| 478 | * @return bool |
||
| 479 | */ |
||
| 480 | public function removeURLAliases(array $urlAliases) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Looks up a url alias for the given url. |
||
| 496 | * |
||
| 497 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 498 | * @throws \RuntimeException |
||
| 499 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
| 500 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
| 501 | * |
||
| 502 | * @param string $url |
||
| 503 | * |
||
| 504 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 505 | */ |
||
| 506 | public function lookup($url) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Loads URL alias by given $id. |
||
| 545 | * |
||
| 546 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 547 | * |
||
| 548 | * @param string $id |
||
| 549 | * |
||
| 550 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 551 | */ |
||
| 552 | public function loadUrlAlias($id) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Notifies the underlying engine that a location has moved. |
||
| 568 | * |
||
| 569 | * This method triggers the change of the autogenerated aliases. |
||
| 570 | * |
||
| 571 | * @param mixed $locationId |
||
| 572 | * @param mixed $oldParentId |
||
| 573 | * @param mixed $newParentId |
||
| 574 | */ |
||
| 575 | public function locationMoved($locationId, $oldParentId, $newParentId) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Notifies the underlying engine that a location was copied. |
||
| 598 | * |
||
| 599 | * This method triggers the creation of the autogenerated aliases for the copied locations |
||
| 600 | * |
||
| 601 | * @param mixed $locationId |
||
| 602 | * @param mixed $newLocationId |
||
| 603 | * @param mixed $newParentId |
||
| 604 | */ |
||
| 605 | public function locationCopied($locationId, $newLocationId, $newParentId) |
||
| 618 | |||
| 619 | public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Historizes given existing active entries for two swapped Locations. |
||
| 673 | * |
||
| 674 | * This should be done before republishing URL aliases, in order to avoid unnecessary |
||
| 675 | * conflicts when swapped Locations are siblings. |
||
| 676 | * |
||
| 677 | * We need to historize everything separately per language (mask), in case the entries |
||
| 678 | * remain history future publishing reusages need to be able to take them over cleanly. |
||
| 679 | * |
||
| 680 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
||
| 681 | * |
||
| 682 | * @param array $location1Entries |
||
| 683 | * @param array $location2Entries |
||
| 684 | */ |
||
| 685 | private function historizeBeforeSwap($location1Entries, $location2Entries) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Extracts every language Ids contained in $languageMask. |
||
| 698 | * |
||
| 699 | * @param int $languageMask |
||
| 700 | * |
||
| 701 | * @return int[] An array of language IDs |
||
| 702 | */ |
||
| 703 | private function extractLanguageIdsFromMask($languageMask) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Returns possibly corrected alias id for given $locationId !! For use as parent id in logic. |
||
| 722 | * |
||
| 723 | * First level entries must have parent id set to 0 instead of their parent location alias id. |
||
| 724 | * There are two cases when alias id needs to be corrected: |
||
| 725 | * 1) location is special location without URL alias (location with id=1 in standard installation) |
||
| 726 | * 2) location is site root location, having special root entry in the ezurlalias_ml table (location with id=2 |
||
| 727 | * in standard installation) |
||
| 728 | * |
||
| 729 | * @param mixed $locationId |
||
| 730 | * |
||
| 731 | * @return mixed |
||
| 732 | */ |
||
| 733 | protected function getRealAliasId($locationId) |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Recursively copies aliases from old parent under new parent. |
||
| 754 | * |
||
| 755 | * @param array $actionMap |
||
| 756 | * @param mixed $oldParentAliasId |
||
| 757 | * @param mixed $newParentAliasId |
||
| 758 | */ |
||
| 759 | protected function copySubtree($actionMap, $oldParentAliasId, $newParentAliasId) |
||
| 783 | |||
| 784 | /** |
||
| 785 | * @param mixed $oldParentId |
||
| 786 | * @param mixed $newParentId |
||
| 787 | * |
||
| 788 | * @return array |
||
| 789 | */ |
||
| 790 | protected function getCopiedLocationsMap($oldParentId, $newParentId) |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Notifies the underlying engine that a location was deleted or moved to trash. |
||
| 805 | * |
||
| 806 | * @param mixed $locationId |
||
| 807 | */ |
||
| 808 | public function locationDeleted($locationId) |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Notifies the underlying engine that Locations Content Translation was removed. |
||
| 818 | * |
||
| 819 | * @param int[] $locationIds all Locations of the Content that got Translation removed |
||
| 820 | * @param string $languageCode language code of the removed Translation |
||
| 821 | */ |
||
| 822 | public function translationRemoved(array $locationIds, $languageCode) |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Recursively removes aliases by given $id and $action. |
||
| 835 | * |
||
| 836 | * $original parameter is used to limit removal of moved Location aliases to history entries only. |
||
| 837 | * |
||
| 838 | * @param mixed $id |
||
| 839 | * @param string $action |
||
| 840 | * @param mixed $original |
||
| 841 | */ |
||
| 842 | protected function removeSubtree($id, $action, $original) |
||
| 862 | |||
| 863 | /** |
||
| 864 | * @param string $text |
||
| 865 | * |
||
| 866 | * @return string |
||
| 867 | */ |
||
| 868 | protected function getHash($text) |
||
| 872 | } |
||
| 873 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.