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 |
||
| 31 | class Handler implements UrlAliasHandlerInterface |
||
| 32 | { |
||
| 33 | const ROOT_LOCATION_ID = 1; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * This is intentionally hardcoded for now as: |
||
| 37 | * 1. We don't implement this configuration option. |
||
| 38 | * 2. Such option should not be in this layer, should be handled higher up. |
||
| 39 | * |
||
| 40 | * @deprecated |
||
| 41 | */ |
||
| 42 | const CONTENT_REPOSITORY_ROOT_LOCATION_ID = 2; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The maximum level of alias depth. |
||
| 46 | */ |
||
| 47 | const MAX_URL_ALIAS_DEPTH_LEVEL = 60; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * UrlAlias Gateway. |
||
| 51 | * |
||
| 52 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway |
||
| 53 | */ |
||
| 54 | protected $gateway; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Gateway for handling location data. |
||
| 58 | * |
||
| 59 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway |
||
| 60 | */ |
||
| 61 | protected $locationGateway; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * UrlAlias Mapper. |
||
| 65 | * |
||
| 66 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper |
||
| 67 | */ |
||
| 68 | protected $mapper; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Caching language handler. |
||
| 72 | * |
||
| 73 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler |
||
| 74 | */ |
||
| 75 | protected $languageHandler; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * URL slug converter. |
||
| 79 | * |
||
| 80 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter |
||
| 81 | */ |
||
| 82 | protected $slugConverter; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Gateway for handling content data. |
||
| 86 | * |
||
| 87 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Gateway |
||
| 88 | */ |
||
| 89 | protected $contentGateway; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Language mask generator. |
||
| 93 | * |
||
| 94 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator |
||
| 95 | */ |
||
| 96 | protected $maskGenerator; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Creates a new UrlAlias Handler. |
||
| 100 | * |
||
| 101 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $gateway |
||
| 102 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper $mapper |
||
| 103 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway |
||
| 104 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 105 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter $slugConverter |
||
| 106 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Gateway $contentGateway |
||
| 107 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator $maskGenerator |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function __construct( |
|
| 126 | |||
| 127 | public function publishUrlAliasForLocation( |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Internal publish method, accepting language ID instead of language code and optionally |
||
| 149 | * new alias ID (used when swapping Locations). |
||
| 150 | * |
||
| 151 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
||
| 152 | * |
||
| 153 | * @param int $locationId |
||
| 154 | * @param int $parentLocationId |
||
| 155 | * @param string $name |
||
| 156 | * @param int $languageId |
||
| 157 | * @param bool $alwaysAvailable |
||
| 158 | * @param bool $updatePathIdentificationString legacy storage specific for updating ezcontentobject_tree.path_identification_string |
||
| 159 | * @param int $newId |
||
| 160 | */ |
||
| 161 | private function internalPublishUrlAliasForLocation( |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Create a user chosen $alias pointing to $locationId in $languageCode. |
||
| 297 | * |
||
| 298 | * If $languageCode is null the $alias is created in the system's default |
||
| 299 | * language. $alwaysAvailable makes the alias available in all languages. |
||
| 300 | * |
||
| 301 | * @param mixed $locationId |
||
| 302 | * @param string $path |
||
| 303 | * @param bool $forwarding |
||
| 304 | * @param string $languageCode |
||
| 305 | * @param bool $alwaysAvailable |
||
| 306 | * |
||
| 307 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 308 | */ |
||
| 309 | public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Create a user chosen $alias pointing to a resource in $languageCode. |
||
| 322 | * This method does not handle location resources - if a user enters a location target |
||
| 323 | * the createCustomUrlAlias method has to be used. |
||
| 324 | * |
||
| 325 | * If $languageCode is null the $alias is created in the system's default |
||
| 326 | * language. $alwaysAvailable makes the alias available in all languages. |
||
| 327 | * |
||
| 328 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given language |
||
| 329 | * |
||
| 330 | * @param string $resource |
||
| 331 | * @param string $path |
||
| 332 | * @param bool $forwarding |
||
| 333 | * @param string $languageCode |
||
| 334 | * @param bool $alwaysAvailable |
||
| 335 | * |
||
| 336 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 337 | */ |
||
| 338 | public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Internal method for creating global or custom URL alias (these are handled in the same way). |
||
| 351 | * |
||
| 352 | * @throws \eZ\Publish\Core\Base\Exceptions\ForbiddenException if the path already exists for the given language |
||
| 353 | * |
||
| 354 | * @param string $action |
||
| 355 | * @param string $path |
||
| 356 | * @param bool $forward |
||
| 357 | * @param string|null $languageCode |
||
| 358 | * @param bool $alwaysAvailable |
||
| 359 | * |
||
| 360 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 361 | */ |
||
| 362 | protected function createUrlAlias($action, $path, $forward, $languageCode, $alwaysAvailable) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Convenience method for inserting nop type row. |
||
| 440 | * |
||
| 441 | * @param mixed $parentId |
||
| 442 | * @param string $text |
||
| 443 | * @param string $textMD5 |
||
| 444 | * |
||
| 445 | * @return mixed |
||
| 446 | */ |
||
| 447 | protected function insertNopEntry($parentId, $text, $textMD5) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * List of user generated or autogenerated url entries, pointing to $locationId. |
||
| 462 | * |
||
| 463 | * @param mixed $locationId |
||
| 464 | * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated |
||
| 465 | * |
||
| 466 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
| 467 | */ |
||
| 468 | View Code Duplication | public function listURLAliasesForLocation($locationId, $custom = false) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * List global aliases. |
||
| 480 | * |
||
| 481 | * @param string|null $languageCode |
||
| 482 | * @param int $offset |
||
| 483 | * @param int $limit |
||
| 484 | * |
||
| 485 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
| 486 | */ |
||
| 487 | View Code Duplication | public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Removes url aliases. |
||
| 499 | * |
||
| 500 | * Autogenerated aliases are not removed by this method. |
||
| 501 | * |
||
| 502 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias[] $urlAliases |
||
| 503 | * |
||
| 504 | * @return bool |
||
| 505 | */ |
||
| 506 | public function removeURLAliases(array $urlAliases) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Looks up a url alias for the given url. |
||
| 522 | * |
||
| 523 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 524 | * @throws \RuntimeException |
||
| 525 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
| 526 | * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
||
| 527 | * |
||
| 528 | * @param string $url |
||
| 529 | * |
||
| 530 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 531 | */ |
||
| 532 | public function lookup($url) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Loads URL alias by given $id. |
||
| 571 | * |
||
| 572 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 573 | * |
||
| 574 | * @param string $id |
||
| 575 | * |
||
| 576 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 577 | */ |
||
| 578 | public function loadUrlAlias($id) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Notifies the underlying engine that a location has moved. |
||
| 594 | * |
||
| 595 | * This method triggers the change of the autogenerated aliases. |
||
| 596 | * |
||
| 597 | * @param mixed $locationId |
||
| 598 | * @param mixed $oldParentId |
||
| 599 | * @param mixed $newParentId |
||
| 600 | */ |
||
| 601 | public function locationMoved($locationId, $oldParentId, $newParentId) |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Notifies the underlying engine that a location was copied. |
||
| 624 | * |
||
| 625 | * This method triggers the creation of the autogenerated aliases for the copied locations |
||
| 626 | * |
||
| 627 | * @param mixed $locationId |
||
| 628 | * @param mixed $newLocationId |
||
| 629 | * @param mixed $newParentId |
||
| 630 | */ |
||
| 631 | public function locationCopied($locationId, $newLocationId, $newParentId) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Notify the underlying engine that a Location has been swapped. |
||
| 647 | * |
||
| 648 | * This method triggers the change of the autogenerated aliases. |
||
| 649 | * |
||
| 650 | * @param int $location1Id |
||
| 651 | * @param int $location1ParentId |
||
| 652 | * @param int $location2Id |
||
| 653 | * @param int $location2ParentId |
||
| 654 | */ |
||
| 655 | public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId) |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @param array $contentInfo |
||
| 708 | * |
||
| 709 | * @return array |
||
| 710 | */ |
||
| 711 | private function getNamesForAllLanguages(array $contentInfo) |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Historizes given existing active entries for two swapped Locations. |
||
| 731 | * |
||
| 732 | * This should be done before republishing URL aliases, in order to avoid unnecessary |
||
| 733 | * conflicts when swapped Locations are siblings. |
||
| 734 | * |
||
| 735 | * We need to historize everything separately per language (mask), in case the entries |
||
| 736 | * remain history future publishing reusages need to be able to take them over cleanly. |
||
| 737 | * |
||
| 738 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
||
| 739 | * |
||
| 740 | * @param array $location1Entries |
||
| 741 | * @param array $location2Entries |
||
| 742 | */ |
||
| 743 | private function historizeBeforeSwap($location1Entries, $location2Entries) |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Decides if UrlAlias for $location2 should be published first. |
||
| 756 | * |
||
| 757 | * The order in which Locations are published only matters if swapped Locations are siblings and they have the same |
||
| 758 | * name in a given language. In this case, the UrlAlias for Location which previously had lower number at the end of |
||
| 759 | * its UrlAlias text (or no number at all) should be published first. This ensures that the number still stays lower |
||
| 760 | * for this Location after the swap. If it wouldn't stay lower, then swapping Locations in conjunction with swapping |
||
| 761 | * UrlAliases would effectively cancel each other. |
||
| 762 | * |
||
| 763 | * @param array $location1Entries |
||
| 764 | * @param int $location1ParentId |
||
| 765 | * @param string $name1 |
||
| 766 | * @param array $location2Entries |
||
| 767 | * @param int $location2ParentId |
||
| 768 | * @param string $name2 |
||
| 769 | * @param int $languageId |
||
| 770 | * |
||
| 771 | * @return bool |
||
| 772 | */ |
||
| 773 | private function shouldUrlAliasForSecondLocationBePublishedFirst( |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Get in a proper order - to be published - a list of URL aliases for swapped Locations. |
||
| 800 | * |
||
| 801 | * @see shouldUrlAliasForSecondLocationBePublishedFirst |
||
| 802 | * |
||
| 803 | * @param \eZ\Publish\SPI\Persistence\Content\Language $language |
||
| 804 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\DTO\SwappedLocationProperties $location1 |
||
| 805 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\DTO\SwappedLocationProperties $location2 |
||
| 806 | * |
||
| 807 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\DTO\UrlAliasForSwappedLocation[] |
||
| 808 | */ |
||
| 809 | private function getUrlAliasesForSwappedLocations( |
||
| 853 | |||
| 854 | /** |
||
| 855 | * @param array $locationEntries |
||
| 856 | * @param int $languageId |
||
| 857 | * |
||
| 858 | * @return array|null |
||
| 859 | */ |
||
| 860 | private function getLocationEntryInLanguage(array $locationEntries, $languageId) |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Returns possibly corrected alias id for given $locationId !! For use as parent id in logic. |
||
| 874 | * |
||
| 875 | * First level entries must have parent id set to 0 instead of their parent location alias id. |
||
| 876 | * There are two cases when alias id needs to be corrected: |
||
| 877 | * 1) location is special location without URL alias (location with id=1 in standard installation) |
||
| 878 | * 2) location is site root location, having special root entry in the ezurlalias_ml table (location with id=2 |
||
| 879 | * in standard installation) |
||
| 880 | * |
||
| 881 | * @param mixed $locationId |
||
| 882 | * |
||
| 883 | * @return mixed |
||
| 884 | */ |
||
| 885 | protected function getRealAliasId($locationId) |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Recursively copies aliases from old parent under new parent. |
||
| 906 | * |
||
| 907 | * @param array $actionMap |
||
| 908 | * @param mixed $oldParentAliasId |
||
| 909 | * @param mixed $newParentAliasId |
||
| 910 | */ |
||
| 911 | protected function copySubtree($actionMap, $oldParentAliasId, $newParentAliasId) |
||
| 935 | |||
| 936 | /** |
||
| 937 | * @param mixed $oldParentId |
||
| 938 | * @param mixed $newParentId |
||
| 939 | * |
||
| 940 | * @return array |
||
| 941 | */ |
||
| 942 | protected function getCopiedLocationsMap($oldParentId, $newParentId) |
||
| 954 | |||
| 955 | /** |
||
| 956 | * Notifies the underlying engine that a location was deleted or moved to trash. |
||
| 957 | * |
||
| 958 | * @param mixed $locationId |
||
| 959 | */ |
||
| 960 | public function locationDeleted($locationId) |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Notifies the underlying engine that Locations Content Translation was removed. |
||
| 970 | * |
||
| 971 | * @param int[] $locationIds all Locations of the Content that got Translation removed |
||
| 972 | * @param string $languageCode language code of the removed Translation |
||
| 973 | */ |
||
| 974 | public function translationRemoved(array $locationIds, $languageCode) |
||
| 984 | |||
| 985 | /** |
||
| 986 | * Recursively removes aliases by given $id and $action. |
||
| 987 | * |
||
| 988 | * $original parameter is used to limit removal of moved Location aliases to history entries only. |
||
| 989 | * |
||
| 990 | * @param mixed $id |
||
| 991 | * @param string $action |
||
| 992 | * @param mixed $original |
||
| 993 | */ |
||
| 994 | protected function removeSubtree($id, $action, $original) |
||
| 1014 | |||
| 1015 | /** |
||
| 1016 | * @param string $text |
||
| 1017 | * |
||
| 1018 | * @return string |
||
| 1019 | */ |
||
| 1020 | protected function getHash($text) |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * {@inheritdoc} |
||
| 1027 | */ |
||
| 1028 | public function archiveUrlAliasesForDeletedTranslations($locationId, $parentLocationId, array $languageCodes) |
||
| 1061 | } |
||
| 1062 |
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.