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 |
||
| 23 | class Handler implements UrlAliasHandlerInterface |
||
| 24 | { |
||
| 25 | const ROOT_LOCATION_ID = 1; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * This is intentionally hardcoded for now as: |
||
| 29 | * 1. We don't implement this configuration option. |
||
| 30 | * 2. Such option should not be in this layer, should be handled higher up. |
||
| 31 | * |
||
| 32 | * @deprecated |
||
| 33 | */ |
||
| 34 | const CONTENT_REPOSITORY_ROOT_LOCATION_ID = 2; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * UrlAlias Gateway. |
||
| 38 | * |
||
| 39 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway |
||
| 40 | */ |
||
| 41 | protected $gateway; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Gateway for handling location data. |
||
| 45 | * |
||
| 46 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway |
||
| 47 | */ |
||
| 48 | protected $locationGateway; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * UrlAlias Mapper. |
||
| 52 | * |
||
| 53 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper |
||
| 54 | */ |
||
| 55 | protected $mapper; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Caching language handler. |
||
| 59 | * |
||
| 60 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler |
||
| 61 | */ |
||
| 62 | protected $languageHandler; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * URL slug converter. |
||
| 66 | * |
||
| 67 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter |
||
| 68 | */ |
||
| 69 | protected $slugConverter; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Creates a new UrlAlias Handler. |
||
| 73 | * |
||
| 74 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $gateway |
||
| 75 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper $mapper |
||
| 76 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway |
||
| 77 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 78 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter $slugConverter |
||
| 79 | */ |
||
| 80 | public function __construct( |
||
| 81 | Gateway $gateway, |
||
| 82 | Mapper $mapper, |
||
| 83 | LocationGateway $locationGateway, |
||
| 84 | LanguageHandler $languageHandler, |
||
| 85 | SlugConverter $slugConverter |
||
| 86 | ) { |
||
| 87 | $this->gateway = $gateway; |
||
| 88 | $this->mapper = $mapper; |
||
| 89 | $this->locationGateway = $locationGateway; |
||
| 90 | $this->languageHandler = $languageHandler; |
||
|
|
|||
| 91 | $this->slugConverter = $slugConverter; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function publishUrlAliasForLocation( |
||
| 95 | $locationId, |
||
| 96 | $parentLocationId, |
||
| 97 | $name, |
||
| 98 | $languageCode, |
||
| 99 | $alwaysAvailable = false, |
||
| 100 | $updatePathIdentificationString = false |
||
| 101 | ) { |
||
| 102 | $languageId = $this->languageHandler->loadByLanguageCode($languageCode)->id; |
||
| 103 | |||
| 104 | $this->internalPublishUrlAliasForLocation( |
||
| 105 | $locationId, |
||
| 106 | $parentLocationId, |
||
| 107 | $name, |
||
| 108 | $languageId, |
||
| 109 | $alwaysAvailable, |
||
| 110 | $updatePathIdentificationString |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Internal publish method, accepting language ID instead of language code and optionally |
||
| 116 | * new alias ID (used when swapping Locations). |
||
| 117 | * |
||
| 118 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
||
| 119 | * |
||
| 120 | * @param int $locationId |
||
| 121 | * @param int $parentLocationId |
||
| 122 | * @param string $name |
||
| 123 | * @param int $languageId |
||
| 124 | * @param bool $alwaysAvailable |
||
| 125 | * @param bool $updatePathIdentificationString legacy storage specific for updating ezcontentobject_tree.path_identification_string |
||
| 126 | * @param int $newId |
||
| 127 | */ |
||
| 128 | private function internalPublishUrlAliasForLocation( |
||
| 129 | $locationId, |
||
| 130 | $parentLocationId, |
||
| 131 | $name, |
||
| 132 | $languageId, |
||
| 133 | $alwaysAvailable = false, |
||
| 134 | $updatePathIdentificationString = false, |
||
| 135 | $newId = null |
||
| 136 | ) { |
||
| 137 | $parentId = $this->getRealAliasId($parentLocationId); |
||
| 138 | $name = $this->slugConverter->convert($name, 'location_' . $locationId); |
||
| 139 | $uniqueCounter = $this->slugConverter->getUniqueCounterValue($name, $parentId == 0); |
||
| 140 | $languageMask = $languageId | (int)$alwaysAvailable; |
||
| 141 | $action = 'eznode:' . $locationId; |
||
| 142 | $cleanup = false; |
||
| 143 | |||
| 144 | // Exiting the loop with break; |
||
| 145 | while (true) { |
||
| 146 | $newText = ''; |
||
| 147 | if ($locationId != self::CONTENT_REPOSITORY_ROOT_LOCATION_ID) { |
||
| 148 | $newText = $name . ($uniqueCounter > 1 ? $uniqueCounter : ''); |
||
| 149 | } |
||
| 150 | $newTextMD5 = $this->getHash($newText); |
||
| 151 | |||
| 152 | // Try to load existing entry |
||
| 153 | $row = $this->gateway->loadRow($parentId, $newTextMD5); |
||
| 154 | |||
| 155 | // If nothing was returned insert new entry |
||
| 156 | if (empty($row)) { |
||
| 157 | // Check for existing active location entry on this level and reuse it's id |
||
| 158 | $existingLocationEntry = $this->gateway->loadAutogeneratedEntry($action, $parentId); |
||
| 159 | if (!empty($existingLocationEntry)) { |
||
| 160 | $cleanup = true; |
||
| 161 | $newId = $existingLocationEntry['id']; |
||
| 162 | } |
||
| 163 | |||
| 164 | $newId = $this->gateway->insertRow( |
||
| 165 | array( |
||
| 166 | 'id' => $newId, |
||
| 167 | 'link' => $newId, |
||
| 168 | 'parent' => $parentId, |
||
| 169 | 'action' => $action, |
||
| 170 | 'lang_mask' => $languageMask, |
||
| 171 | 'text' => $newText, |
||
| 172 | 'text_md5' => $newTextMD5, |
||
| 173 | ) |
||
| 174 | ); |
||
| 175 | |||
| 176 | break; |
||
| 177 | } |
||
| 178 | |||
| 179 | // Row exists, check if it is reusable. There are 3 cases when this is possible: |
||
| 180 | // 1. NOP entry |
||
| 181 | // 2. existing location or custom alias entry |
||
| 182 | // 3. history entry |
||
| 183 | if ($row['action'] == 'nop:' || $row['action'] == $action || $row['is_original'] == 0) { |
||
| 184 | // Check for existing location entry on this level, if it exists and it's id differs from reusable |
||
| 185 | // entry id then reusable entry should be updated with the existing location entry id. |
||
| 186 | // Note: existing location entry may be downgraded and relinked later, depending on its language. |
||
| 187 | $existingLocationEntry = $this->gateway->loadAutogeneratedEntry($action, $parentId); |
||
| 188 | |||
| 189 | if (!empty($existingLocationEntry)) { |
||
| 190 | // Always cleanup when active autogenerated entry exists on the same level |
||
| 191 | $cleanup = true; |
||
| 192 | $newId = $existingLocationEntry['id']; |
||
| 193 | if ($existingLocationEntry['id'] == $row['id']) { |
||
| 194 | // If we are reusing existing location entry merge existing language mask |
||
| 195 | $languageMask |= ($row['lang_mask'] & ~1); |
||
| 196 | } |
||
| 197 | } elseif ($newId === null) { |
||
| 198 | // Use reused row ID only if publishing normally, else use given $newId |
||
| 199 | $newId = $row['id']; |
||
| 200 | } |
||
| 201 | |||
| 202 | $this->gateway->updateRow( |
||
| 203 | $parentId, |
||
| 204 | $newTextMD5, |
||
| 205 | array( |
||
| 206 | 'action' => $action, |
||
| 207 | // In case when NOP row was reused |
||
| 208 | 'action_type' => 'eznode', |
||
| 209 | 'lang_mask' => $languageMask, |
||
| 210 | // Updating text ensures that letter case changes are stored |
||
| 211 | 'text' => $newText, |
||
| 212 | // Set "id" and "link" for case when reusable entry is history |
||
| 213 | 'id' => $newId, |
||
| 214 | 'link' => $newId, |
||
| 215 | // Entry should be active location entry (original and not alias). |
||
| 216 | // Note: this takes care of taking over custom alias entry for the location on the same level |
||
| 217 | // and with same name and action. |
||
| 218 | 'alias_redirects' => 1, |
||
| 219 | 'is_original' => 1, |
||
| 220 | 'is_alias' => 0, |
||
| 221 | ) |
||
| 222 | ); |
||
| 223 | |||
| 224 | break; |
||
| 225 | } |
||
| 226 | |||
| 227 | // If existing row is not reusable, increment $uniqueCounter and try again |
||
| 228 | $uniqueCounter += 1; |
||
| 229 | } |
||
| 230 | |||
| 231 | /* @var $newText */ |
||
| 232 | if ($updatePathIdentificationString) { |
||
| 233 | $this->locationGateway->updatePathIdentificationString( |
||
| 234 | $locationId, |
||
| 235 | $parentLocationId, |
||
| 236 | $this->slugConverter->convert($newText, 'node_' . $locationId, 'urlalias_compat') |
||
| 237 | ); |
||
| 238 | } |
||
| 239 | |||
| 240 | /* @var $newId */ |
||
| 241 | /* @var $newTextMD5 */ |
||
| 242 | // Note: cleanup does not touch custom and global entries |
||
| 243 | if ($cleanup) { |
||
| 244 | $this->gateway->cleanupAfterPublish($action, $languageId, $newId, $parentId, $newTextMD5); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Create a user chosen $alias pointing to $locationId in $languageCode. |
||
| 250 | * |
||
| 251 | * If $languageCode is null the $alias is created in the system's default |
||
| 252 | * language. $alwaysAvailable makes the alias available in all languages. |
||
| 253 | * |
||
| 254 | * @param mixed $locationId |
||
| 255 | * @param string $path |
||
| 256 | * @param bool $forwarding |
||
| 257 | * @param string $languageCode |
||
| 258 | * @param bool $alwaysAvailable |
||
| 259 | * |
||
| 260 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 261 | */ |
||
| 262 | public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Create a user chosen $alias pointing to a resource in $languageCode. |
||
| 275 | * This method does not handle location resources - if a user enters a location target |
||
| 276 | * the createCustomUrlAlias method has to be used. |
||
| 277 | * |
||
| 278 | * If $languageCode is null the $alias is created in the system's default |
||
| 279 | * language. $alwaysAvailable makes the alias available in all languages. |
||
| 280 | * |
||
| 281 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given language |
||
| 282 | * |
||
| 283 | * @param string $resource |
||
| 284 | * @param string $path |
||
| 285 | * @param bool $forwarding |
||
| 286 | * @param string $languageCode |
||
| 287 | * @param bool $alwaysAvailable |
||
| 288 | * |
||
| 289 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 290 | */ |
||
| 291 | public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Internal method for creating global or custom URL alias (these are handled in the same way). |
||
| 304 | * |
||
| 305 | * @throws \eZ\Publish\Core\Base\Exceptions\ForbiddenException if the path already exists for the given language |
||
| 306 | * |
||
| 307 | * @param string $action |
||
| 308 | * @param string $path |
||
| 309 | * @param bool $forward |
||
| 310 | * @param string|null $languageCode |
||
| 311 | * @param bool $alwaysAvailable |
||
| 312 | * |
||
| 313 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 314 | */ |
||
| 315 | protected function createUrlAlias($action, $path, $forward, $languageCode, $alwaysAvailable) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Convenience method for inserting nop type row. |
||
| 393 | * |
||
| 394 | * @param mixed $parentId |
||
| 395 | * @param string $text |
||
| 396 | * @param string $textMD5 |
||
| 397 | * |
||
| 398 | * @return mixed |
||
| 399 | */ |
||
| 400 | protected function insertNopEntry($parentId, $text, $textMD5) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * List of user generated or autogenerated url entries, pointing to $locationId. |
||
| 415 | * |
||
| 416 | * @param mixed $locationId |
||
| 417 | * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated |
||
| 418 | * |
||
| 419 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
| 420 | */ |
||
| 421 | View Code Duplication | public function listURLAliasesForLocation($locationId, $custom = false) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * List global aliases. |
||
| 433 | * |
||
| 434 | * @param string|null $languageCode |
||
| 435 | * @param int $offset |
||
| 436 | * @param int $limit |
||
| 437 | * |
||
| 438 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
| 439 | */ |
||
| 440 | View Code Duplication | public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Removes url aliases. |
||
| 452 | * |
||
| 453 | * Autogenerated aliases are not removed by this method. |
||
| 454 | * |
||
| 455 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias[] $urlAliases |
||
| 456 | * |
||
| 457 | * @return bool |
||
| 458 | */ |
||
| 459 | public function removeURLAliases(array $urlAliases) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Looks up a url alias for the given url. |
||
| 475 | * |
||
| 476 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 477 | * @throws \RuntimeException |
||
| 478 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
| 479 | * |
||
| 480 | * @param string $url |
||
| 481 | * |
||
| 482 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 483 | */ |
||
| 484 | public function lookup($url) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Loads URL alias by given $id. |
||
| 519 | * |
||
| 520 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 521 | * |
||
| 522 | * @param string $id |
||
| 523 | * |
||
| 524 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 525 | */ |
||
| 526 | public function loadUrlAlias($id) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Notifies the underlying engine that a location has moved. |
||
| 542 | * |
||
| 543 | * This method triggers the change of the autogenerated aliases. |
||
| 544 | * |
||
| 545 | * @param mixed $locationId |
||
| 546 | * @param mixed $oldParentId |
||
| 547 | * @param mixed $newParentId |
||
| 548 | */ |
||
| 549 | public function locationMoved($locationId, $oldParentId, $newParentId) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Notifies the underlying engine that a location was copied. |
||
| 572 | * |
||
| 573 | * This method triggers the creation of the autogenerated aliases for the copied locations |
||
| 574 | * |
||
| 575 | * @param mixed $locationId |
||
| 576 | * @param mixed $newLocationId |
||
| 577 | * @param mixed $newParentId |
||
| 578 | */ |
||
| 579 | public function locationCopied($locationId, $newLocationId, $newParentId) |
||
| 592 | |||
| 593 | public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId) |
||
| 594 | { |
||
| 595 | $location1Entries = $this->gateway->loadLocationEntries($location1Id); |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Historizes given existing active entries for two swapped Locations. |
||
| 647 | * |
||
| 648 | * This should be done before republishing URL aliases, in order to avoid unnecessary |
||
| 649 | * conflicts when swapped Locations are siblings. |
||
| 650 | * |
||
| 651 | * We need to historize everything separately per language (mask), in case the entries |
||
| 652 | * remain history future publishing reusages need to be able to take them over cleanly. |
||
| 653 | * |
||
| 654 | * @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
||
| 655 | * |
||
| 656 | * @param array $location1Entries |
||
| 657 | * @param array $location2Entries |
||
| 658 | */ |
||
| 659 | private function historizeBeforeSwap($location1Entries, $location2Entries) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Extracts every language Ids contained in $languageMask. |
||
| 672 | * |
||
| 673 | * @param int $languageMask |
||
| 674 | * |
||
| 675 | * @return int[] An array of language IDs |
||
| 676 | */ |
||
| 677 | private function extractLanguageIdsFromMask($languageMask) |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Returns possibly corrected alias id for given $locationId !! For use as parent id in logic. |
||
| 696 | * |
||
| 697 | * First level entries must have parent id set to 0 instead of their parent location alias id. |
||
| 698 | * There are two cases when alias id needs to be corrected: |
||
| 699 | * 1) location is special location without URL alias (location with id=1 in standard installation) |
||
| 700 | * 2) location is site root location, having special root entry in the ezurlalias_ml table (location with id=2 |
||
| 701 | * in standard installation) |
||
| 702 | * |
||
| 703 | * @param mixed $locationId |
||
| 704 | * |
||
| 705 | * @return mixed |
||
| 706 | */ |
||
| 707 | protected function getRealAliasId($locationId) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Recursively copies aliases from old parent under new parent. |
||
| 728 | * |
||
| 729 | * @param array $actionMap |
||
| 730 | * @param mixed $oldParentAliasId |
||
| 731 | * @param mixed $newParentAliasId |
||
| 732 | */ |
||
| 733 | protected function copySubtree($actionMap, $oldParentAliasId, $newParentAliasId) |
||
| 757 | |||
| 758 | /** |
||
| 759 | * @param mixed $oldParentId |
||
| 760 | * @param mixed $newParentId |
||
| 761 | * |
||
| 762 | * @return array |
||
| 763 | */ |
||
| 764 | protected function getCopiedLocationsMap($oldParentId, $newParentId) |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Notifies the underlying engine that a location was deleted or moved to trash. |
||
| 779 | * |
||
| 780 | * @param mixed $locationId |
||
| 781 | */ |
||
| 782 | public function locationDeleted($locationId) |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Recursively removes aliases by given $id and $action. |
||
| 792 | * |
||
| 793 | * $original parameter is used to limit removal of moved Location aliases to history entries only. |
||
| 794 | * |
||
| 795 | * @param mixed $id |
||
| 796 | * @param string $action |
||
| 797 | * @param mixed $original |
||
| 798 | */ |
||
| 799 | protected function removeSubtree($id, $action, $original) |
||
| 819 | |||
| 820 | /** |
||
| 821 | * @param string $text |
||
| 822 | * |
||
| 823 | * @return string |
||
| 824 | */ |
||
| 825 | protected function getHash($text) |
||
| 829 | } |
||
| 830 |
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.