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