Complex classes like URLAliasService 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 URLAliasService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class URLAliasService implements URLAliasServiceInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var \eZ\Publish\API\Repository\Repository |
||
| 34 | */ |
||
| 35 | protected $repository; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler |
||
| 39 | */ |
||
| 40 | protected $urlAliasHandler; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $settings; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 49 | */ |
||
| 50 | protected $nameSchemaService; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 54 | * |
||
| 55 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 56 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler $urlAliasHandler |
||
| 57 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 58 | * @param array $settings |
||
| 59 | */ |
||
| 60 | public function __construct( |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Create a user chosen $alias pointing to $location in $languageCode. |
||
| 79 | * |
||
| 80 | * This method runs URL filters and transformers before storing them. |
||
| 81 | * Hence the path returned in the URLAlias Value may differ from the given. |
||
| 82 | * $alwaysAvailable makes the alias available in all languages. |
||
| 83 | * |
||
| 84 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 85 | * @param string $path |
||
| 86 | * @param bool $forwarding if true a redirect is performed |
||
| 87 | * @param string $languageCode the languageCode for which this alias is valid |
||
| 88 | * @param bool $alwaysAvailable |
||
| 89 | * |
||
| 90 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the path already exists for the given language |
||
| 91 | * |
||
| 92 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 93 | */ |
||
| 94 | public function createUrlAlias(Location $location, $path, $languageCode, $forwarding = false, $alwaysAvailable = false) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Create a user chosen $alias pointing to a resource in $languageCode. |
||
| 125 | * |
||
| 126 | * This method does not handle location resources - if a user enters a location target |
||
| 127 | * the createCustomUrlAlias method has to be used. |
||
| 128 | * This method runs URL filters and and transformers before storing them. |
||
| 129 | * Hence the path returned in the URLAlias Value may differ from the given. |
||
| 130 | * |
||
| 131 | * $alwaysAvailable makes the alias available in all languages. |
||
| 132 | * |
||
| 133 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the path already exists for the given |
||
| 134 | * language or if resource is not valid |
||
| 135 | * |
||
| 136 | * @param string $resource |
||
| 137 | * @param string $path |
||
| 138 | * @param string $languageCode |
||
| 139 | * @param bool $forwarding |
||
| 140 | * @param bool $alwaysAvailable |
||
| 141 | * |
||
| 142 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 143 | */ |
||
| 144 | public function createGlobalUrlAlias($resource, $path, $languageCode, $forwarding = false, $alwaysAvailable = false) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * List of url aliases pointing to $location, sorted by language priority. |
||
| 192 | * |
||
| 193 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 194 | * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated |
||
| 195 | * @param string $languageCode filters those which are valid for the given language |
||
| 196 | * @param null|bool $showAllTranslations |
||
| 197 | * @param null|string[] $prioritizedLanguageList |
||
| 198 | * |
||
| 199 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias[] |
||
| 200 | */ |
||
| 201 | public function listLocationAliases( |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Determines alias language code. |
||
| 264 | * |
||
| 265 | * Method will return false if language code can't be matched against alias language codes or language settings. |
||
| 266 | * |
||
| 267 | * @param \eZ\Publish\SPI\Persistence\Content\URLAlias $spiUrlAlias |
||
| 268 | * @param string|null $languageCode |
||
| 269 | * @param bool $showAllTranslations |
||
| 270 | * @param string[] $prioritizedLanguageList |
||
| 271 | * |
||
| 272 | * @return string|bool |
||
| 273 | */ |
||
| 274 | protected function selectAliasLanguageCode( |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Returns path extracted from normalized path data returned from persistence, using language settings. |
||
| 302 | * |
||
| 303 | * Will return false if path could not be determined. |
||
| 304 | * |
||
| 305 | * @param \eZ\Publish\SPI\Persistence\Content\URLAlias $spiUrlAlias |
||
| 306 | * @param string $languageCode |
||
| 307 | * @param bool $showAllTranslations |
||
| 308 | * @param string[] $prioritizedLanguageList |
||
| 309 | * |
||
| 310 | * @return string|bool |
||
| 311 | */ |
||
| 312 | protected function extractPath( |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Returns language code with highest priority. |
||
| 349 | * |
||
| 350 | * Will return false if language code could nto be matched with language settings in place. |
||
| 351 | * |
||
| 352 | * @param array $entries |
||
| 353 | * @param bool $showAllTranslations |
||
| 354 | * @param string[] $prioritizedLanguageList |
||
| 355 | * |
||
| 356 | * @return string|bool |
||
| 357 | */ |
||
| 358 | protected function choosePrioritizedLanguageCode(array $entries, $showAllTranslations, $prioritizedLanguageList) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Matches path string with normalized path data returned from persistence. |
||
| 377 | * |
||
| 378 | * Returns matched path string (possibly case corrected) and array of corresponding language codes or false |
||
| 379 | * if path could not be matched. |
||
| 380 | * |
||
| 381 | * @param \eZ\Publish\SPI\Persistence\Content\URLAlias $spiUrlAlias |
||
| 382 | * @param string $path |
||
| 383 | * @param string $languageCode |
||
| 384 | * |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | protected function matchPath(SPIURLAlias $spiUrlAlias, $path, $languageCode) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param array $pathElementData |
||
| 419 | * @param string $pathElement |
||
| 420 | * |
||
| 421 | * @return string|bool |
||
| 422 | */ |
||
| 423 | protected function matchLanguageCode(array $pathElementData, $pathElement) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Needed when translations for the part of the alias are the same for multiple languages. |
||
| 436 | * In that case we need to ensure that more prioritized language is chosen. |
||
| 437 | * |
||
| 438 | * @param array $translations |
||
| 439 | * |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | private function sortTranslationsByPrioritizedLanguages(array $translations) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Returns true or false depending if URL alias is loadable or not for language settings in place. |
||
| 467 | * |
||
| 468 | * @param \eZ\Publish\SPI\Persistence\Content\URLAlias $spiUrlAlias |
||
| 469 | * @param string|null $languageCode |
||
| 470 | * |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | protected function isUrlAliasLoadable( |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Returns true or false depending if URL alias is loadable or not for language settings in place. |
||
| 506 | * |
||
| 507 | * @param array $pathData |
||
| 508 | * @param array $languageCodes |
||
| 509 | * |
||
| 510 | * @return bool |
||
| 511 | */ |
||
| 512 | protected function isPathLoadable(array $pathData, array $languageCodes) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * List global aliases. |
||
| 535 | * |
||
| 536 | * @param string $languageCode filters those which are valid for the given language |
||
| 537 | * @param int $offset |
||
| 538 | * @param int $limit |
||
| 539 | * |
||
| 540 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias[] |
||
| 541 | */ |
||
| 542 | public function listGlobalAliases($languageCode = null, $offset = 0, $limit = -1) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Removes urls aliases. |
||
| 571 | * |
||
| 572 | * This method does not remove autogenerated aliases for locations. |
||
| 573 | * |
||
| 574 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if alias list contains |
||
| 575 | * autogenerated alias |
||
| 576 | * |
||
| 577 | * @param \eZ\Publish\API\Repository\Values\Content\URLAlias[] $aliasList |
||
| 578 | */ |
||
| 579 | public function removeAliases(array $aliasList) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Builds persistence domain object. |
||
| 604 | * |
||
| 605 | * @param \eZ\Publish\API\Repository\Values\Content\URLAlias $urlAlias |
||
| 606 | * |
||
| 607 | * @return \eZ\Publish\SPI\Persistence\Content\URLAlias |
||
| 608 | */ |
||
| 609 | protected function buildSPIUrlAlias(URLAlias $urlAlias) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * looks up the URLAlias for the given url. |
||
| 623 | * |
||
| 624 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the path does not exist or is not valid for the given language |
||
| 625 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the path exceeded maximum depth level |
||
| 626 | * |
||
| 627 | * @param string $url |
||
| 628 | * @param string $languageCode |
||
| 629 | * |
||
| 630 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 631 | */ |
||
| 632 | public function lookup($url, $languageCode = null) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Returns the URL alias for the given location in the given language. |
||
| 648 | * |
||
| 649 | * If $languageCode is null the method returns the url alias in the most prioritized language. |
||
| 650 | * |
||
| 651 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if no url alias exist for the given language |
||
| 652 | * |
||
| 653 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 654 | * @param string $languageCode |
||
| 655 | * @param null|bool $showAllTranslations |
||
| 656 | * @param null|string[] $prioritizedLanguageList |
||
| 657 | * |
||
| 658 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 659 | */ |
||
| 660 | public function reverseLookup( |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Loads URL alias by given $id. |
||
| 703 | * |
||
| 704 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 705 | * |
||
| 706 | * @param string $id |
||
| 707 | * |
||
| 708 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 709 | */ |
||
| 710 | public function load($id) |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Refresh all system URL aliases for the given Location (and historize outdated if needed). |
||
| 729 | * |
||
| 730 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 731 | * @param \eZ\Publish\API\Repository\Values\Content\Content $content |
||
| 732 | * |
||
| 733 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException |
||
| 734 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 735 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 736 | * @throws \Exception any unexpected exception that might come from custom Field Type implementation |
||
| 737 | */ |
||
| 738 | public function refreshSystemUrlAliasesForLocation(Location $location, Content $content = null) |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Delete global, system or custom URL alias pointing to non-existent Locations. |
||
| 792 | * |
||
| 793 | * @return int Number of removed URL aliases |
||
| 794 | * |
||
| 795 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 796 | */ |
||
| 797 | public function deleteCorruptedUrlAliases() |
||
| 805 | |||
| 806 | /** |
||
| 807 | * @param string $url |
||
| 808 | * |
||
| 809 | * @return string |
||
| 810 | */ |
||
| 811 | protected function cleanUrl($url) |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Builds API UrlAlias object from given SPI UrlAlias object. |
||
| 818 | * |
||
| 819 | * @param \eZ\Publish\SPI\Persistence\Content\URLAlias $spiUrlAlias |
||
| 820 | * @param string|null $path |
||
| 821 | * |
||
| 822 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 823 | */ |
||
| 824 | protected function buildUrlAliasDomainObject(SPIURLAlias $spiUrlAlias, $path) |
||
| 840 | } |
||
| 841 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: