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 | * UrlAlias Gateway. |
||
40 | * |
||
41 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway |
||
42 | */ |
||
43 | protected $gateway; |
||
44 | |||
45 | /** |
||
46 | * Gateway for handling location data. |
||
47 | * |
||
48 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway |
||
49 | */ |
||
50 | protected $locationGateway; |
||
51 | |||
52 | /** |
||
53 | * UrlAlias Mapper. |
||
54 | * |
||
55 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper |
||
56 | */ |
||
57 | protected $mapper; |
||
58 | |||
59 | /** |
||
60 | * Caching language handler. |
||
61 | * |
||
62 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler |
||
63 | */ |
||
64 | protected $languageHandler; |
||
65 | |||
66 | /** |
||
67 | * URL slug converter. |
||
68 | * |
||
69 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter |
||
70 | */ |
||
71 | protected $slugConverter; |
||
72 | |||
73 | /** |
||
74 | * Creates a new UrlAlias Handler. |
||
75 | * |
||
76 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $gateway |
||
77 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper $mapper |
||
78 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway |
||
79 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
80 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter $slugConverter |
||
81 | */ |
||
82 | public function __construct( |
||
95 | |||
96 | public function publishUrlAliasForLocation( |
||
115 | |||
116 | /** |
||
117 | * Internal publish method, accepting language ID instead of language code and optionally |
||
118 | * new alias ID (used when swapping Locations). |
||
119 | * |
||
120 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
||
121 | * |
||
122 | * @param int $locationId |
||
123 | * @param int $parentLocationId |
||
124 | * @param string $name |
||
125 | * @param int $languageId |
||
126 | * @param bool $alwaysAvailable |
||
127 | * @param bool $updatePathIdentificationString legacy storage specific for updating ezcontentobject_tree.path_identification_string |
||
128 | * @param int $newId |
||
129 | */ |
||
130 | private function internalPublishUrlAliasForLocation( |
||
249 | |||
250 | /** |
||
251 | * Create a user chosen $alias pointing to $locationId in $languageCode. |
||
252 | * |
||
253 | * If $languageCode is null the $alias is created in the system's default |
||
254 | * language. $alwaysAvailable makes the alias available in all languages. |
||
255 | * |
||
256 | * @param mixed $locationId |
||
257 | * @param string $path |
||
258 | * @param bool $forwarding |
||
259 | * @param string $languageCode |
||
260 | * @param bool $alwaysAvailable |
||
261 | * |
||
262 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
263 | */ |
||
264 | public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
274 | |||
275 | /** |
||
276 | * Create a user chosen $alias pointing to a resource in $languageCode. |
||
277 | * This method does not handle location resources - if a user enters a location target |
||
278 | * the createCustomUrlAlias method has to be used. |
||
279 | * |
||
280 | * If $languageCode is null the $alias is created in the system's default |
||
281 | * language. $alwaysAvailable makes the alias available in all languages. |
||
282 | * |
||
283 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given language |
||
284 | * |
||
285 | * @param string $resource |
||
286 | * @param string $path |
||
287 | * @param bool $forwarding |
||
288 | * @param string $languageCode |
||
289 | * @param bool $alwaysAvailable |
||
290 | * |
||
291 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
292 | */ |
||
293 | public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
303 | |||
304 | /** |
||
305 | * Internal method for creating global or custom URL alias (these are handled in the same way). |
||
306 | * |
||
307 | * @throws \eZ\Publish\Core\Base\Exceptions\ForbiddenException if the path already exists for the given language |
||
308 | * |
||
309 | * @param string $action |
||
310 | * @param string $path |
||
311 | * @param bool $forward |
||
312 | * @param string|null $languageCode |
||
313 | * @param bool $alwaysAvailable |
||
314 | * |
||
315 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
316 | */ |
||
317 | protected function createUrlAlias($action, $path, $forward, $languageCode, $alwaysAvailable) |
||
392 | |||
393 | /** |
||
394 | * Convenience method for inserting nop type row. |
||
395 | * |
||
396 | * @param mixed $parentId |
||
397 | * @param string $text |
||
398 | * @param string $textMD5 |
||
399 | * |
||
400 | * @return mixed |
||
401 | */ |
||
402 | protected function insertNopEntry($parentId, $text, $textMD5) |
||
414 | |||
415 | /** |
||
416 | * List of user generated or autogenerated url entries, pointing to $locationId. |
||
417 | * |
||
418 | * @param mixed $locationId |
||
419 | * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated |
||
420 | * |
||
421 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
422 | */ |
||
423 | View Code Duplication | public function listURLAliasesForLocation($locationId, $custom = false) |
|
432 | |||
433 | /** |
||
434 | * List global aliases. |
||
435 | * |
||
436 | * @param string|null $languageCode |
||
437 | * @param int $offset |
||
438 | * @param int $limit |
||
439 | * |
||
440 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
441 | */ |
||
442 | View Code Duplication | public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
|
451 | |||
452 | /** |
||
453 | * Removes url aliases. |
||
454 | * |
||
455 | * Autogenerated aliases are not removed by this method. |
||
456 | * |
||
457 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias[] $urlAliases |
||
458 | * |
||
459 | * @return bool |
||
460 | */ |
||
461 | public function removeURLAliases(array $urlAliases) |
||
474 | |||
475 | /** |
||
476 | * Looks up a url alias for the given url. |
||
477 | * |
||
478 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
479 | * @throws \RuntimeException |
||
480 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
481 | * |
||
482 | * @param string $url |
||
483 | * |
||
484 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
485 | */ |
||
486 | public function lookup($url) |
||
518 | |||
519 | /** |
||
520 | * Loads URL alias by given $id. |
||
521 | * |
||
522 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
523 | * |
||
524 | * @param string $id |
||
525 | * |
||
526 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
527 | */ |
||
528 | public function loadUrlAlias($id) |
||
541 | |||
542 | /** |
||
543 | * Notifies the underlying engine that a location has moved. |
||
544 | * |
||
545 | * This method triggers the change of the autogenerated aliases. |
||
546 | * |
||
547 | * @param mixed $locationId |
||
548 | * @param mixed $oldParentId |
||
549 | * @param mixed $newParentId |
||
550 | */ |
||
551 | public function locationMoved($locationId, $oldParentId, $newParentId) |
||
571 | |||
572 | /** |
||
573 | * Notifies the underlying engine that a location was copied. |
||
574 | * |
||
575 | * This method triggers the creation of the autogenerated aliases for the copied locations |
||
576 | * |
||
577 | * @param mixed $locationId |
||
578 | * @param mixed $newLocationId |
||
579 | * @param mixed $newParentId |
||
580 | */ |
||
581 | public function locationCopied($locationId, $newLocationId, $newParentId) |
||
594 | |||
595 | public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId) |
||
646 | |||
647 | /** |
||
648 | * Historizes given existing active entries for two swapped Locations. |
||
649 | * |
||
650 | * This should be done before republishing URL aliases, in order to avoid unnecessary |
||
651 | * conflicts when swapped Locations are siblings. |
||
652 | * |
||
653 | * We need to historize everything separately per language (mask), in case the entries |
||
654 | * remain history future publishing reusages need to be able to take them over cleanly. |
||
655 | * |
||
656 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
||
657 | * |
||
658 | * @param array $location1Entries |
||
659 | * @param array $location2Entries |
||
660 | */ |
||
661 | private function historizeBeforeSwap($location1Entries, $location2Entries) |
||
671 | |||
672 | /** |
||
673 | * Extracts every language Ids contained in $languageMask. |
||
674 | * |
||
675 | * @param int $languageMask |
||
676 | * |
||
677 | * @return int[] An array of language IDs |
||
678 | */ |
||
679 | private function extractLanguageIdsFromMask($languageMask) |
||
695 | |||
696 | /** |
||
697 | * Returns possibly corrected alias id for given $locationId !! For use as parent id in logic. |
||
698 | * |
||
699 | * First level entries must have parent id set to 0 instead of their parent location alias id. |
||
700 | * There are two cases when alias id needs to be corrected: |
||
701 | * 1) location is special location without URL alias (location with id=1 in standard installation) |
||
702 | * 2) location is site root location, having special root entry in the ezurlalias_ml table (location with id=2 |
||
703 | * in standard installation) |
||
704 | * |
||
705 | * @param mixed $locationId |
||
706 | * |
||
707 | * @return mixed |
||
708 | */ |
||
709 | protected function getRealAliasId($locationId) |
||
727 | |||
728 | /** |
||
729 | * Recursively copies aliases from old parent under new parent. |
||
730 | * |
||
731 | * @param array $actionMap |
||
732 | * @param mixed $oldParentAliasId |
||
733 | * @param mixed $newParentAliasId |
||
734 | */ |
||
735 | protected function copySubtree($actionMap, $oldParentAliasId, $newParentAliasId) |
||
759 | |||
760 | /** |
||
761 | * @param mixed $oldParentId |
||
762 | * @param mixed $newParentId |
||
763 | * |
||
764 | * @return array |
||
765 | */ |
||
766 | protected function getCopiedLocationsMap($oldParentId, $newParentId) |
||
778 | |||
779 | /** |
||
780 | * Notifies the underlying engine that a location was deleted or moved to trash. |
||
781 | * |
||
782 | * @param mixed $locationId |
||
783 | */ |
||
784 | public function locationDeleted($locationId) |
||
791 | |||
792 | /** |
||
793 | * Recursively removes aliases by given $id and $action. |
||
794 | * |
||
795 | * $original parameter is used to limit removal of moved Location aliases to history entries only. |
||
796 | * |
||
797 | * @param mixed $id |
||
798 | * @param string $action |
||
799 | * @param mixed $original |
||
800 | */ |
||
801 | protected function removeSubtree($id, $action, $original) |
||
821 | |||
822 | /** |
||
823 | * @param string $text |
||
824 | * |
||
825 | * @return string |
||
826 | */ |
||
827 | protected function getHash($text) |
||
831 | } |
||
832 |
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.