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 | * todo document. |
||
118 | * |
||
119 | * @param int $locationId |
||
120 | * @param int $parentLocationId |
||
121 | * @param string $name |
||
122 | * @param int $languageId |
||
123 | * @param bool $alwaysAvailable |
||
124 | * @param bool $updatePathIdentificationString legacy storage specific for updating ezcontentobject_tree.path_identification_string |
||
125 | * @param int $newId |
||
126 | */ |
||
127 | private function internalPublishUrlAliasForLocation( |
||
246 | |||
247 | /** |
||
248 | * Create a user chosen $alias pointing to $locationId in $languageCode. |
||
249 | * |
||
250 | * If $languageCode is null the $alias is created in the system's default |
||
251 | * language. $alwaysAvailable makes the alias available in all languages. |
||
252 | * |
||
253 | * @param mixed $locationId |
||
254 | * @param string $path |
||
255 | * @param bool $forwarding |
||
256 | * @param string $languageCode |
||
257 | * @param bool $alwaysAvailable |
||
258 | * |
||
259 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
260 | */ |
||
261 | public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
271 | |||
272 | /** |
||
273 | * Create a user chosen $alias pointing to a resource in $languageCode. |
||
274 | * This method does not handle location resources - if a user enters a location target |
||
275 | * the createCustomUrlAlias method has to be used. |
||
276 | * |
||
277 | * If $languageCode is null the $alias is created in the system's default |
||
278 | * language. $alwaysAvailable makes the alias available in all languages. |
||
279 | * |
||
280 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given language |
||
281 | * |
||
282 | * @param string $resource |
||
283 | * @param string $path |
||
284 | * @param bool $forwarding |
||
285 | * @param string $languageCode |
||
286 | * @param bool $alwaysAvailable |
||
287 | * |
||
288 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
289 | */ |
||
290 | public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
300 | |||
301 | /** |
||
302 | * Internal method for creating global or custom URL alias (these are handled in the same way). |
||
303 | * |
||
304 | * @throws \eZ\Publish\Core\Base\Exceptions\ForbiddenException if the path already exists for the given language |
||
305 | * |
||
306 | * @param string $action |
||
307 | * @param string $path |
||
308 | * @param bool $forward |
||
309 | * @param string|null $languageCode |
||
310 | * @param bool $alwaysAvailable |
||
311 | * |
||
312 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
313 | */ |
||
314 | protected function createUrlAlias($action, $path, $forward, $languageCode, $alwaysAvailable) |
||
389 | |||
390 | /** |
||
391 | * Convenience method for inserting nop type row. |
||
392 | * |
||
393 | * @param mixed $parentId |
||
394 | * @param string $text |
||
395 | * @param string $textMD5 |
||
396 | * |
||
397 | * @return mixed |
||
398 | */ |
||
399 | protected function insertNopEntry($parentId, $text, $textMD5) |
||
411 | |||
412 | /** |
||
413 | * List of user generated or autogenerated url entries, pointing to $locationId. |
||
414 | * |
||
415 | * @param mixed $locationId |
||
416 | * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated |
||
417 | * |
||
418 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
419 | */ |
||
420 | View Code Duplication | public function listURLAliasesForLocation($locationId, $custom = false) |
|
429 | |||
430 | /** |
||
431 | * List global aliases. |
||
432 | * |
||
433 | * @param string|null $languageCode |
||
434 | * @param int $offset |
||
435 | * @param int $limit |
||
436 | * |
||
437 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
438 | */ |
||
439 | View Code Duplication | public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
|
448 | |||
449 | /** |
||
450 | * Removes url aliases. |
||
451 | * |
||
452 | * Autogenerated aliases are not removed by this method. |
||
453 | * |
||
454 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias[] $urlAliases |
||
455 | * |
||
456 | * @return bool |
||
457 | */ |
||
458 | public function removeURLAliases(array $urlAliases) |
||
471 | |||
472 | /** |
||
473 | * Looks up a url alias for the given url. |
||
474 | * |
||
475 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
476 | * @throws \RuntimeException |
||
477 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
478 | * |
||
479 | * @param string $url |
||
480 | * |
||
481 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
482 | */ |
||
483 | public function lookup($url) |
||
515 | |||
516 | /** |
||
517 | * Loads URL alias by given $id. |
||
518 | * |
||
519 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
520 | * |
||
521 | * @param string $id |
||
522 | * |
||
523 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
524 | */ |
||
525 | public function loadUrlAlias($id) |
||
538 | |||
539 | /** |
||
540 | * Notifies the underlying engine that a location has moved. |
||
541 | * |
||
542 | * This method triggers the change of the autogenerated aliases. |
||
543 | * |
||
544 | * @param mixed $locationId |
||
545 | * @param mixed $oldParentId |
||
546 | * @param mixed $newParentId |
||
547 | */ |
||
548 | public function locationMoved($locationId, $oldParentId, $newParentId) |
||
568 | |||
569 | /** |
||
570 | * Notifies the underlying engine that a location was copied. |
||
571 | * |
||
572 | * This method triggers the creation of the autogenerated aliases for the copied locations |
||
573 | * |
||
574 | * @param mixed $locationId |
||
575 | * @param mixed $newLocationId |
||
576 | * @param mixed $newParentId |
||
577 | */ |
||
578 | public function locationCopied($locationId, $newLocationId, $newParentId) |
||
591 | |||
592 | public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId) |
||
647 | |||
648 | /** |
||
649 | * Extracts every language Ids contained in $languageMask. |
||
650 | * |
||
651 | * @param int $languageMask |
||
652 | * |
||
653 | * @return int[] An array of language IDs |
||
654 | */ |
||
655 | private function extractLanguageIdsFromMask($languageMask) |
||
671 | |||
672 | /** |
||
673 | * Returns possibly corrected alias id for given $locationId !! For use as parent id in logic. |
||
674 | * |
||
675 | * First level entries must have parent id set to 0 instead of their parent location alias id. |
||
676 | * There are two cases when alias id needs to be corrected: |
||
677 | * 1) location is special location without URL alias (location with id=1 in standard installation) |
||
678 | * 2) location is site root location, having special root entry in the ezurlalias_ml table (location with id=2 |
||
679 | * in standard installation) |
||
680 | * |
||
681 | * @param mixed $locationId |
||
682 | * |
||
683 | * @return mixed |
||
684 | */ |
||
685 | protected function getRealAliasId($locationId) |
||
703 | |||
704 | /** |
||
705 | * Recursively copies aliases from old parent under new parent. |
||
706 | * |
||
707 | * @param array $actionMap |
||
708 | * @param mixed $oldParentAliasId |
||
709 | * @param mixed $newParentAliasId |
||
710 | */ |
||
711 | protected function copySubtree($actionMap, $oldParentAliasId, $newParentAliasId) |
||
735 | |||
736 | /** |
||
737 | * @param mixed $oldParentId |
||
738 | * @param mixed $newParentId |
||
739 | * |
||
740 | * @return array |
||
741 | */ |
||
742 | protected function getCopiedLocationsMap($oldParentId, $newParentId) |
||
754 | |||
755 | /** |
||
756 | * Notifies the underlying engine that a location was deleted or moved to trash. |
||
757 | * |
||
758 | * @param mixed $locationId |
||
759 | */ |
||
760 | public function locationDeleted($locationId) |
||
767 | |||
768 | /** |
||
769 | * Recursively removes aliases by given $id and $action. |
||
770 | * |
||
771 | * $original parameter is used to limit removal of moved Location aliases to history entries only. |
||
772 | * |
||
773 | * @param mixed $id |
||
774 | * @param string $action |
||
775 | * @param mixed $original |
||
776 | */ |
||
777 | protected function removeSubtree($id, $action, $original) |
||
797 | |||
798 | /** |
||
799 | * @param string $text |
||
800 | * |
||
801 | * @return string |
||
802 | */ |
||
803 | protected function getHash($text) |
||
807 | } |
||
808 |
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.