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