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 |
||
| 27 | class URLAliasService implements URLAliasServiceInterface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var \eZ\Publish\API\Repository\Repository |
||
| 31 | */ |
||
| 32 | protected $repository; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var \eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler |
||
| 36 | */ |
||
| 37 | protected $urlAliasHandler; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $settings; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 46 | * |
||
| 47 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 48 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler $urlAliasHandler |
||
| 49 | * @param array $settings |
||
| 50 | */ |
||
| 51 | public function __construct(RepositoryInterface $repository, Handler $urlAliasHandler, array $settings = array()) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Create a user chosen $alias pointing to $location in $languageCode. |
||
| 65 | * |
||
| 66 | * This method runs URL filters and transformers before storing them. |
||
| 67 | * Hence the path returned in the URLAlias Value may differ from the given. |
||
| 68 | * $alwaysAvailable makes the alias available in all languages. |
||
| 69 | * |
||
| 70 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 71 | * @param string $path |
||
| 72 | * @param bool $forwarding if true a redirect is performed |
||
| 73 | * @param string $languageCode the languageCode for which this alias is valid |
||
| 74 | * @param bool $alwaysAvailable |
||
| 75 | * |
||
| 76 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the path already exists for the given language |
||
| 77 | * |
||
| 78 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 79 | */ |
||
| 80 | public function createUrlAlias(Location $location, $path, $languageCode, $forwarding = false, $alwaysAvailable = false) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Create a user chosen $alias pointing to a resource in $languageCode. |
||
| 111 | * |
||
| 112 | * This method does not handle location resources - if a user enters a location target |
||
| 113 | * the createCustomUrlAlias method has to be used. |
||
| 114 | * This method runs URL filters and and transformers before storing them. |
||
| 115 | * Hence the path returned in the URLAlias Value may differ from the given. |
||
| 116 | * |
||
| 117 | * $alwaysAvailable makes the alias available in all languages. |
||
| 118 | * |
||
| 119 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the path already exists for the given |
||
| 120 | * language or if resource is not valid |
||
| 121 | * |
||
| 122 | * @param string $resource |
||
| 123 | * @param string $path |
||
| 124 | * @param string $languageCode |
||
| 125 | * @param bool $forwarding |
||
| 126 | * @param bool $alwaysAvailable |
||
| 127 | * |
||
| 128 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 129 | */ |
||
| 130 | public function createGlobalUrlAlias($resource, $path, $languageCode, $forwarding = false, $alwaysAvailable = false) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * List of url aliases pointing to $location, sorted by language priority. |
||
| 178 | * |
||
| 179 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 180 | * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated |
||
| 181 | * @param string $languageCode filters those which are valid for the given language |
||
| 182 | * @param null|bool $showAllTranslations |
||
| 183 | * @param null|string[] $prioritizedLanguageList |
||
| 184 | * |
||
| 185 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias[] |
||
| 186 | */ |
||
| 187 | public function listLocationAliases( |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param \eZ\Publish\API\Repository\Values\Content\URLAlias $firstAlias |
||
| 253 | * @param \eZ\Publish\API\Repository\Values\Content\URLAlias $secondAlias |
||
| 254 | * |
||
| 255 | * @return int |
||
| 256 | */ |
||
| 257 | private function compareUrlAliases(URLAlias $firstAlias, URLAlias $secondAlias) |
||
| 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 | * @param string $url |
||
| 729 | * |
||
| 730 | * @return string |
||
| 731 | */ |
||
| 732 | protected function cleanUrl($url) |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Builds API UrlAlias object from given SPI UrlAlias object. |
||
| 739 | * |
||
| 740 | * @param \eZ\Publish\SPI\Persistence\Content\URLAlias $spiUrlAlias |
||
| 741 | * @param string|null $path |
||
| 742 | * |
||
| 743 | * @return \eZ\Publish\API\Repository\Values\Content\URLAlias |
||
| 744 | */ |
||
| 745 | protected function buildUrlAliasDomainObject(SPIURLAlias $spiUrlAlias, $path) |
||
| 761 | } |
||
| 762 |
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: