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  | 
            ||
| 23 | class Handler implements UrlAliasHandlerInterface  | 
            ||
| 24 | { | 
            ||
| 25 | const ROOT_LOCATION_ID = 1;  | 
            ||
| 26 | |||
| 27 | /**  | 
            ||
| 28 | * This is intentionally hardcoded for now as:  | 
            ||
| 29 | * 1. We don't implement this configuration option.  | 
            ||
| 30 | * 2. Such option should not be in this layer, should be handled higher up.  | 
            ||
| 31 | *  | 
            ||
| 32 | * @deprecated  | 
            ||
| 33 | */  | 
            ||
| 34 | const CONTENT_REPOSITORY_ROOT_LOCATION_ID = 2;  | 
            ||
| 35 | |||
| 36 | /**  | 
            ||
| 37 | * UrlAlias Gateway.  | 
            ||
| 38 | *  | 
            ||
| 39 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway  | 
            ||
| 40 | */  | 
            ||
| 41 | protected $gateway;  | 
            ||
| 42 | |||
| 43 | /**  | 
            ||
| 44 | * Gateway for handling location data.  | 
            ||
| 45 | *  | 
            ||
| 46 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway  | 
            ||
| 47 | */  | 
            ||
| 48 | protected $locationGateway;  | 
            ||
| 49 | |||
| 50 | /**  | 
            ||
| 51 | * UrlAlias Mapper.  | 
            ||
| 52 | *  | 
            ||
| 53 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper  | 
            ||
| 54 | */  | 
            ||
| 55 | protected $mapper;  | 
            ||
| 56 | |||
| 57 | /**  | 
            ||
| 58 | * Caching language handler.  | 
            ||
| 59 | *  | 
            ||
| 60 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler  | 
            ||
| 61 | */  | 
            ||
| 62 | protected $languageHandler;  | 
            ||
| 63 | |||
| 64 | /**  | 
            ||
| 65 | * URL slug converter.  | 
            ||
| 66 | *  | 
            ||
| 67 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter  | 
            ||
| 68 | */  | 
            ||
| 69 | protected $slugConverter;  | 
            ||
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * Creates a new UrlAlias Handler.  | 
            ||
| 73 | *  | 
            ||
| 74 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $gateway  | 
            ||
| 75 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper $mapper  | 
            ||
| 76 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway  | 
            ||
| 77 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler  | 
            ||
| 78 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter $slugConverter  | 
            ||
| 79 | */  | 
            ||
| 80 | public function __construct(  | 
            ||
| 93 | |||
| 94 | public function publishUrlAliasForLocation(  | 
            ||
| 113 | |||
| 114 | /**  | 
            ||
| 115 | * Internal publish method, accepting language ID instead of language code and optionally  | 
            ||
| 116 | * new alias ID (used when swapping Locations).  | 
            ||
| 117 | *  | 
            ||
| 118 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped()  | 
            ||
| 119 | *  | 
            ||
| 120 | * @param int $locationId  | 
            ||
| 121 | * @param int $parentLocationId  | 
            ||
| 122 | * @param string $name  | 
            ||
| 123 | * @param int $languageId  | 
            ||
| 124 | * @param bool $alwaysAvailable  | 
            ||
| 125 | * @param bool $updatePathIdentificationString legacy storage specific for updating ezcontentobject_tree.path_identification_string  | 
            ||
| 126 | * @param int $newId  | 
            ||
| 127 | */  | 
            ||
| 128 | private function internalPublishUrlAliasForLocation(  | 
            ||
| 261 | |||
| 262 | /**  | 
            ||
| 263 | * Create a user chosen $alias pointing to $locationId in $languageCode.  | 
            ||
| 264 | *  | 
            ||
| 265 | * If $languageCode is null the $alias is created in the system's default  | 
            ||
| 266 | * language. $alwaysAvailable makes the alias available in all languages.  | 
            ||
| 267 | *  | 
            ||
| 268 | * @param mixed $locationId  | 
            ||
| 269 | * @param string $path  | 
            ||
| 270 | * @param bool $forwarding  | 
            ||
| 271 | * @param string $languageCode  | 
            ||
| 272 | * @param bool $alwaysAvailable  | 
            ||
| 273 | *  | 
            ||
| 274 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias  | 
            ||
| 275 | */  | 
            ||
| 276 | public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false)  | 
            ||
| 286 | |||
| 287 | /**  | 
            ||
| 288 | * Create a user chosen $alias pointing to a resource in $languageCode.  | 
            ||
| 289 | * This method does not handle location resources - if a user enters a location target  | 
            ||
| 290 | * the createCustomUrlAlias method has to be used.  | 
            ||
| 291 | *  | 
            ||
| 292 | * If $languageCode is null the $alias is created in the system's default  | 
            ||
| 293 | * language. $alwaysAvailable makes the alias available in all languages.  | 
            ||
| 294 | *  | 
            ||
| 295 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given language  | 
            ||
| 296 | *  | 
            ||
| 297 | * @param string $resource  | 
            ||
| 298 | * @param string $path  | 
            ||
| 299 | * @param bool $forwarding  | 
            ||
| 300 | * @param string $languageCode  | 
            ||
| 301 | * @param bool $alwaysAvailable  | 
            ||
| 302 | *  | 
            ||
| 303 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias  | 
            ||
| 304 | */  | 
            ||
| 305 | public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false)  | 
            ||
| 315 | |||
| 316 | /**  | 
            ||
| 317 | * Internal method for creating global or custom URL alias (these are handled in the same way).  | 
            ||
| 318 | *  | 
            ||
| 319 | * @throws \eZ\Publish\Core\Base\Exceptions\ForbiddenException if the path already exists for the given language  | 
            ||
| 320 | *  | 
            ||
| 321 | * @param string $action  | 
            ||
| 322 | * @param string $path  | 
            ||
| 323 | * @param bool $forward  | 
            ||
| 324 | * @param string|null $languageCode  | 
            ||
| 325 | * @param bool $alwaysAvailable  | 
            ||
| 326 | *  | 
            ||
| 327 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias  | 
            ||
| 328 | */  | 
            ||
| 329 | protected function createUrlAlias($action, $path, $forward, $languageCode, $alwaysAvailable)  | 
            ||
| 404 | |||
| 405 | /**  | 
            ||
| 406 | * Convenience method for inserting nop type row.  | 
            ||
| 407 | *  | 
            ||
| 408 | * @param mixed $parentId  | 
            ||
| 409 | * @param string $text  | 
            ||
| 410 | * @param string $textMD5  | 
            ||
| 411 | *  | 
            ||
| 412 | * @return mixed  | 
            ||
| 413 | */  | 
            ||
| 414 | protected function insertNopEntry($parentId, $text, $textMD5)  | 
            ||
| 426 | |||
| 427 | /**  | 
            ||
| 428 | * List of user generated or autogenerated url entries, pointing to $locationId.  | 
            ||
| 429 | *  | 
            ||
| 430 | * @param mixed $locationId  | 
            ||
| 431 | * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated  | 
            ||
| 432 | *  | 
            ||
| 433 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[]  | 
            ||
| 434 | */  | 
            ||
| 435 | View Code Duplication | public function listURLAliasesForLocation($locationId, $custom = false)  | 
            |
| 444 | |||
| 445 | /**  | 
            ||
| 446 | * List global aliases.  | 
            ||
| 447 | *  | 
            ||
| 448 | * @param string|null $languageCode  | 
            ||
| 449 | * @param int $offset  | 
            ||
| 450 | * @param int $limit  | 
            ||
| 451 | *  | 
            ||
| 452 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[]  | 
            ||
| 453 | */  | 
            ||
| 454 | View Code Duplication | public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1)  | 
            |
| 463 | |||
| 464 | /**  | 
            ||
| 465 | * Removes url aliases.  | 
            ||
| 466 | *  | 
            ||
| 467 | * Autogenerated aliases are not removed by this method.  | 
            ||
| 468 | *  | 
            ||
| 469 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias[] $urlAliases  | 
            ||
| 470 | *  | 
            ||
| 471 | * @return bool  | 
            ||
| 472 | */  | 
            ||
| 473 | public function removeURLAliases(array $urlAliases)  | 
            ||
| 486 | |||
| 487 | /**  | 
            ||
| 488 | * Looks up a url alias for the given url.  | 
            ||
| 489 | *  | 
            ||
| 490 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException  | 
            ||
| 491 | * @throws \RuntimeException  | 
            ||
| 492 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException  | 
            ||
| 493 | *  | 
            ||
| 494 | * @param string $url  | 
            ||
| 495 | *  | 
            ||
| 496 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias  | 
            ||
| 497 | */  | 
            ||
| 498 | public function lookup($url)  | 
            ||
| 530 | |||
| 531 | /**  | 
            ||
| 532 | * Loads URL alias by given $id.  | 
            ||
| 533 | *  | 
            ||
| 534 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException  | 
            ||
| 535 | *  | 
            ||
| 536 | * @param string $id  | 
            ||
| 537 | *  | 
            ||
| 538 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias  | 
            ||
| 539 | */  | 
            ||
| 540 | public function loadUrlAlias($id)  | 
            ||
| 553 | |||
| 554 | /**  | 
            ||
| 555 | * Notifies the underlying engine that a location has moved.  | 
            ||
| 556 | *  | 
            ||
| 557 | * This method triggers the change of the autogenerated aliases.  | 
            ||
| 558 | *  | 
            ||
| 559 | * @param mixed $locationId  | 
            ||
| 560 | * @param mixed $oldParentId  | 
            ||
| 561 | * @param mixed $newParentId  | 
            ||
| 562 | */  | 
            ||
| 563 | public function locationMoved($locationId, $oldParentId, $newParentId)  | 
            ||
| 583 | |||
| 584 | /**  | 
            ||
| 585 | * Notifies the underlying engine that a location was copied.  | 
            ||
| 586 | *  | 
            ||
| 587 | * This method triggers the creation of the autogenerated aliases for the copied locations  | 
            ||
| 588 | *  | 
            ||
| 589 | * @param mixed $locationId  | 
            ||
| 590 | * @param mixed $newLocationId  | 
            ||
| 591 | * @param mixed $newParentId  | 
            ||
| 592 | */  | 
            ||
| 593 | public function locationCopied($locationId, $newLocationId, $newParentId)  | 
            ||
| 606 | |||
| 607 | public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId)  | 
            ||
| 658 | |||
| 659 | /**  | 
            ||
| 660 | * Historizes given existing active entries for two swapped Locations.  | 
            ||
| 661 | *  | 
            ||
| 662 | * This should be done before republishing URL aliases, in order to avoid unnecessary  | 
            ||
| 663 | * conflicts when swapped Locations are siblings.  | 
            ||
| 664 | *  | 
            ||
| 665 | * We need to historize everything separately per language (mask), in case the entries  | 
            ||
| 666 | * remain history future publishing reusages need to be able to take them over cleanly.  | 
            ||
| 667 | *  | 
            ||
| 668 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped()  | 
            ||
| 669 | *  | 
            ||
| 670 | * @param array $location1Entries  | 
            ||
| 671 | * @param array $location2Entries  | 
            ||
| 672 | */  | 
            ||
| 673 | private function historizeBeforeSwap($location1Entries, $location2Entries)  | 
            ||
| 683 | |||
| 684 | /**  | 
            ||
| 685 | * Extracts every language Ids contained in $languageMask.  | 
            ||
| 686 | *  | 
            ||
| 687 | * @param int $languageMask  | 
            ||
| 688 | *  | 
            ||
| 689 | * @return int[] An array of language IDs  | 
            ||
| 690 | */  | 
            ||
| 691 | private function extractLanguageIdsFromMask($languageMask)  | 
            ||
| 707 | |||
| 708 | /**  | 
            ||
| 709 | * Returns possibly corrected alias id for given $locationId !! For use as parent id in logic.  | 
            ||
| 710 | *  | 
            ||
| 711 | * First level entries must have parent id set to 0 instead of their parent location alias id.  | 
            ||
| 712 | * There are two cases when alias id needs to be corrected:  | 
            ||
| 713 | * 1) location is special location without URL alias (location with id=1 in standard installation)  | 
            ||
| 714 | * 2) location is site root location, having special root entry in the ezurlalias_ml table (location with id=2  | 
            ||
| 715 | * in standard installation)  | 
            ||
| 716 | *  | 
            ||
| 717 | * @param mixed $locationId  | 
            ||
| 718 | *  | 
            ||
| 719 | * @return mixed  | 
            ||
| 720 | */  | 
            ||
| 721 | protected function getRealAliasId($locationId)  | 
            ||
| 739 | |||
| 740 | /**  | 
            ||
| 741 | * Recursively copies aliases from old parent under new parent.  | 
            ||
| 742 | *  | 
            ||
| 743 | * @param array $actionMap  | 
            ||
| 744 | * @param mixed $oldParentAliasId  | 
            ||
| 745 | * @param mixed $newParentAliasId  | 
            ||
| 746 | */  | 
            ||
| 747 | protected function copySubtree($actionMap, $oldParentAliasId, $newParentAliasId)  | 
            ||
| 771 | |||
| 772 | /**  | 
            ||
| 773 | * @param mixed $oldParentId  | 
            ||
| 774 | * @param mixed $newParentId  | 
            ||
| 775 | *  | 
            ||
| 776 | * @return array  | 
            ||
| 777 | */  | 
            ||
| 778 | protected function getCopiedLocationsMap($oldParentId, $newParentId)  | 
            ||
| 790 | |||
| 791 | /**  | 
            ||
| 792 | * Notifies the underlying engine that a location was deleted or moved to trash.  | 
            ||
| 793 | *  | 
            ||
| 794 | * @param mixed $locationId  | 
            ||
| 795 | */  | 
            ||
| 796 | public function locationDeleted($locationId)  | 
            ||
| 803 | |||
| 804 | /**  | 
            ||
| 805 | * Recursively removes aliases by given $id and $action.  | 
            ||
| 806 | *  | 
            ||
| 807 | * $original parameter is used to limit removal of moved Location aliases to history entries only.  | 
            ||
| 808 | *  | 
            ||
| 809 | * @param mixed $id  | 
            ||
| 810 | * @param string $action  | 
            ||
| 811 | * @param mixed $original  | 
            ||
| 812 | */  | 
            ||
| 813 | protected function removeSubtree($id, $action, $original)  | 
            ||
| 833 | |||
| 834 | /**  | 
            ||
| 835 | * @param string $text  | 
            ||
| 836 | *  | 
            ||
| 837 | * @return string  | 
            ||
| 838 | */  | 
            ||
| 839 | protected function getHash($text)  | 
            ||
| 843 | }  | 
            ||
| 844 | 
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.