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 | /** |
||
| 97 | * This method creates or updates an urlalias from a new or changed content name in a language |
||
| 98 | * (if published). It also can be used to create an alias for a new location of content. |
||
| 99 | * On update the old alias is linked to the new one (i.e. a history alias is generated). |
||
| 100 | * |
||
| 101 | * $alwaysAvailable controls whether the url alias is accessible in all |
||
| 102 | * languages. |
||
| 103 | * |
||
| 104 | * @param mixed $locationId |
||
| 105 | * @param mixed $parentLocationId |
||
| 106 | * @param string $name the new name computed by the name schema or url alias schema |
||
| 107 | * @param string $languageCode |
||
| 108 | * @param bool $alwaysAvailable |
||
| 109 | * @param bool $updatePathIdentificationString legacy storage specific for updating ezcontentobject_tree.path_identification_string |
||
| 110 | */ |
||
| 111 | public function publishUrlAliasForLocation( |
||
| 112 | $locationId, |
||
| 113 | $parentLocationId, |
||
| 114 | $name, |
||
| 115 | $languageCode, |
||
| 116 | $alwaysAvailable = false, |
||
| 117 | $updatePathIdentificationString = false |
||
| 118 | ) { |
||
| 119 | $parentId = $this->getRealAliasId($parentLocationId); |
||
| 120 | $uniqueCounter = $this->slugConverter->getUniqueCounterValue($name, $parentId == 0); |
||
| 121 | $name = $this->slugConverter->convert($name, 'location_' . $locationId); |
||
| 122 | $languageId = $this->languageHandler->loadByLanguageCode($languageCode)->id; |
||
| 123 | $languageMask = $languageId | (int)$alwaysAvailable; |
||
| 124 | $action = 'eznode:' . $locationId; |
||
| 125 | $cleanup = false; |
||
| 126 | |||
| 127 | // Exiting the loop with break; |
||
| 128 | while (true) { |
||
| 129 | $newText = ''; |
||
| 130 | if ($locationId != self::CONTENT_REPOSITORY_ROOT_LOCATION_ID) { |
||
| 131 | $newText = $name . ($uniqueCounter > 1 ? $uniqueCounter : ''); |
||
| 132 | } |
||
| 133 | $newTextMD5 = $this->getHash($newText); |
||
| 134 | |||
| 135 | // Try to load existing entry |
||
| 136 | $row = $this->gateway->loadRow($parentId, $newTextMD5); |
||
| 137 | |||
| 138 | // If nothing was returned insert new entry |
||
| 139 | if (empty($row)) { |
||
| 140 | // Check for existing active location entry on this level and reuse it's id |
||
| 141 | $existingLocationEntry = $this->gateway->loadAutogeneratedEntry($action, $parentId); |
||
| 142 | if (!empty($existingLocationEntry)) { |
||
| 143 | $cleanup = true; |
||
| 144 | $newId = $existingLocationEntry['id']; |
||
| 145 | } else { |
||
| 146 | $newId = null; |
||
| 147 | } |
||
| 148 | |||
| 149 | $newId = $this->gateway->insertRow( |
||
| 150 | array( |
||
| 151 | 'id' => $newId, |
||
| 152 | 'link' => $newId, |
||
| 153 | 'parent' => $parentId, |
||
| 154 | 'action' => $action, |
||
| 155 | 'lang_mask' => $languageMask, |
||
| 156 | 'text' => $newText, |
||
| 157 | 'text_md5' => $newTextMD5, |
||
| 158 | ) |
||
| 159 | ); |
||
| 160 | |||
| 161 | break; |
||
| 162 | } |
||
| 163 | |||
| 164 | // Row exists, check if it is reusable. There are 3 cases when this is possible: |
||
| 165 | // 1. NOP entry |
||
| 166 | // 2. existing location or custom alias entry |
||
| 167 | // 3. history entry |
||
| 168 | if ($row['action'] == 'nop:' || $row['action'] == $action || $row['is_original'] == 0) { |
||
| 169 | // Check for existing location entry on this level, if it exists and it's id differs from reusable |
||
| 170 | // entry id then reusable entry should be updated with the existing location entry id. |
||
| 171 | // Note: existing location entry may be downgraded and relinked later, depending on its language. |
||
| 172 | $existingLocationEntry = $this->gateway->loadAutogeneratedEntry($action, $parentId); |
||
| 173 | $newId = $row['id']; |
||
| 174 | if (!empty($existingLocationEntry)) { |
||
| 175 | if ($existingLocationEntry['id'] != $row['id']) { |
||
| 176 | $cleanup = true; |
||
| 177 | $newId = $existingLocationEntry['id']; |
||
| 178 | } else { |
||
| 179 | // If we are reusing existing location entry merge existing language mask |
||
| 180 | $languageMask |= ($row['lang_mask'] & ~1); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | $this->gateway->updateRow( |
||
| 184 | $parentId, |
||
| 185 | $newTextMD5, |
||
| 186 | array( |
||
| 187 | 'action' => $action, |
||
| 188 | // In case when NOP row was reused |
||
| 189 | 'action_type' => 'eznode', |
||
| 190 | 'lang_mask' => $languageMask, |
||
| 191 | // Updating text ensures that letter case changes are stored |
||
| 192 | 'text' => $newText, |
||
| 193 | // Set "id" and "link" for case when reusable entry is history |
||
| 194 | 'id' => $newId, |
||
| 195 | 'link' => $newId, |
||
| 196 | // Entry should be active location entry (original and not alias). |
||
| 197 | // Note: this takes care of taking over custom alias entry for the location on the same level |
||
| 198 | // and with same name and action. |
||
| 199 | 'alias_redirects' => 1, |
||
| 200 | 'is_original' => 1, |
||
| 201 | 'is_alias' => 0, |
||
| 202 | ) |
||
| 203 | ); |
||
| 204 | |||
| 205 | break; |
||
| 206 | } |
||
| 207 | |||
| 208 | // If existing row is not reusable, increment $uniqueCounter and try again |
||
| 209 | $uniqueCounter += 1; |
||
| 210 | } |
||
| 211 | |||
| 212 | if ($updatePathIdentificationString) { |
||
| 213 | $this->locationGateway->updatePathIdentificationString( |
||
| 214 | $locationId, |
||
| 215 | $parentLocationId, |
||
| 216 | $this->slugConverter->convert($newText, 'node_' . $locationId, 'urlalias_compat') |
||
| 217 | ); |
||
| 218 | } |
||
| 219 | |||
| 220 | /* @var $newId */ |
||
| 221 | /* @var $newTextMD5 */ |
||
| 222 | // Note: cleanup does not touch custom and global entries |
||
| 223 | if ($cleanup) { |
||
| 224 | $this->gateway->cleanupAfterPublish($action, $languageId, $newId, $parentId, $newTextMD5); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Create a user chosen $alias pointing to $locationId in $languageCode. |
||
| 230 | * |
||
| 231 | * If $languageCode is null the $alias is created in the system's default |
||
| 232 | * language. $alwaysAvailable makes the alias available in all languages. |
||
| 233 | * |
||
| 234 | * @param mixed $locationId |
||
| 235 | * @param string $path |
||
| 236 | * @param bool $forwarding |
||
| 237 | * @param string $languageCode |
||
| 238 | * @param bool $alwaysAvailable |
||
| 239 | * |
||
| 240 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 241 | */ |
||
| 242 | public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Create a user chosen $alias pointing to a resource in $languageCode. |
||
| 255 | * This method does not handle location resources - if a user enters a location target |
||
| 256 | * the createCustomUrlAlias method has to be used. |
||
| 257 | * |
||
| 258 | * If $languageCode is null the $alias is created in the system's default |
||
| 259 | * language. $alwaysAvailable makes the alias available in all languages. |
||
| 260 | * |
||
| 261 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given language |
||
| 262 | * |
||
| 263 | * @param string $resource |
||
| 264 | * @param string $path |
||
| 265 | * @param bool $forwarding |
||
| 266 | * @param string $languageCode |
||
| 267 | * @param bool $alwaysAvailable |
||
| 268 | * |
||
| 269 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 270 | */ |
||
| 271 | public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Internal method for creating global or custom URL alias (these are handled in the same way). |
||
| 284 | * |
||
| 285 | * @throws \eZ\Publish\Core\Base\Exceptions\ForbiddenException if the path already exists for the given language |
||
| 286 | * |
||
| 287 | * @param string $action |
||
| 288 | * @param string $path |
||
| 289 | * @param bool $forward |
||
| 290 | * @param string|null $languageCode |
||
| 291 | * @param bool $alwaysAvailable |
||
| 292 | * |
||
| 293 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 294 | */ |
||
| 295 | protected function createUrlAlias($action, $path, $forward, $languageCode, $alwaysAvailable) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Convenience method for inserting nop type row. |
||
| 373 | * |
||
| 374 | * @param mixed $parentId |
||
| 375 | * @param string $text |
||
| 376 | * @param string $textMD5 |
||
| 377 | * |
||
| 378 | * @return mixed |
||
| 379 | */ |
||
| 380 | protected function insertNopEntry($parentId, $text, $textMD5) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * List of user generated or autogenerated url entries, pointing to $locationId. |
||
| 395 | * |
||
| 396 | * @param mixed $locationId |
||
| 397 | * @param bool $custom if true the user generated aliases are listed otherwise the autogenerated |
||
| 398 | * |
||
| 399 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
| 400 | */ |
||
| 401 | View Code Duplication | public function listURLAliasesForLocation($locationId, $custom = false) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * List global aliases. |
||
| 413 | * |
||
| 414 | * @param string|null $languageCode |
||
| 415 | * @param int $offset |
||
| 416 | * @param int $limit |
||
| 417 | * |
||
| 418 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
||
| 419 | */ |
||
| 420 | View Code Duplication | public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Removes url aliases. |
||
| 432 | * |
||
| 433 | * Autogenerated aliases are not removed by this method. |
||
| 434 | * |
||
| 435 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias[] $urlAliases |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | public function removeURLAliases(array $urlAliases) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Looks up a url alias for the given url. |
||
| 455 | * |
||
| 456 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 457 | * @throws \RuntimeException |
||
| 458 | * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
||
| 459 | * |
||
| 460 | * @param string $url |
||
| 461 | * |
||
| 462 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 463 | */ |
||
| 464 | public function lookup($url) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Loads URL alias by given $id. |
||
| 499 | * |
||
| 500 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 501 | * |
||
| 502 | * @param string $id |
||
| 503 | * |
||
| 504 | * @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
||
| 505 | */ |
||
| 506 | public function loadUrlAlias($id) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Notifies the underlying engine that a location has moved. |
||
| 522 | * |
||
| 523 | * This method triggers the change of the autogenerated aliases. |
||
| 524 | * |
||
| 525 | * @param mixed $locationId |
||
| 526 | * @param mixed $oldParentId |
||
| 527 | * @param mixed $newParentId |
||
| 528 | */ |
||
| 529 | public function locationMoved($locationId, $oldParentId, $newParentId) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Notifies the underlying engine that a location has moved. |
||
| 552 | * |
||
| 553 | * This method triggers the creation of the autogenerated aliases for the copied locations |
||
| 554 | * |
||
| 555 | * @param mixed $locationId |
||
| 556 | * @param mixed $oldParentId |
||
| 557 | * @param mixed $newParentId |
||
| 558 | */ |
||
| 559 | public function locationCopied($locationId, $newLocationId, $newParentId) |
||
| 560 | { |
||
| 561 | $newParentAliasId = $this->getRealAliasId($newLocationId); |
||
| 562 | $oldParentAliasId = $this->getRealAliasId($locationId); |
||
| 563 | |||
| 564 | $actionMap = $this->getCopiedLocationsMap($locationId, $newLocationId); |
||
| 565 | |||
| 566 | $this->copySubtree( |
||
| 567 | $actionMap, |
||
| 568 | $oldParentAliasId, |
||
| 569 | $newParentAliasId, |
||
| 570 | $locationId |
||
| 571 | ); |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Returns possibly corrected alias id for given $locationId !! For use as parent id in logic. |
||
| 576 | * |
||
| 577 | * First level entries must have parent id set to 0 instead of their parent location alias id. |
||
| 578 | * There are two cases when alias id needs to be corrected: |
||
| 579 | * 1) location is special location without URL alias (location with id=1 in standard installation) |
||
| 580 | * 2) location is site root location, having special root entry in the ezurlalias_ml table (location with id=2 |
||
| 581 | * in standard installation) |
||
| 582 | * |
||
| 583 | * @param mixed $locationId |
||
| 584 | * |
||
| 585 | * @return mixed |
||
| 586 | */ |
||
| 587 | protected function getRealAliasId($locationId) |
||
| 588 | { |
||
| 589 | // Absolute root location does have a url alias entry so we can skip lookup |
||
| 590 | if ($locationId == self::ROOT_LOCATION_ID) { |
||
| 591 | return 0; |
||
| 592 | } |
||
| 593 | |||
| 594 | $data = $this->gateway->loadAutogeneratedEntry('eznode:' . $locationId); |
||
| 595 | |||
| 596 | // Root entries (URL wise) can return 0 as the returned value is used as parent (parent is 0 for root entries) |
||
| 597 | if (empty($data) || $data['id'] != 0 && $data['parent'] == 0 && strlen($data['text']) == 0) { |
||
| 598 | $id = 0; |
||
| 599 | } else { |
||
| 600 | $id = $data['id']; |
||
| 601 | } |
||
| 602 | |||
| 603 | return $id; |
||
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Recursively copies aliases from old parent under new parent. |
||
| 608 | * |
||
| 609 | * @param array $actionMap |
||
| 610 | * @param mixed $oldParentAliasId |
||
| 611 | * @param mixed $newParentAliasId |
||
| 612 | */ |
||
| 613 | protected function copySubtree($actionMap, $oldParentAliasId, $newParentAliasId) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @param mixed $oldParentId |
||
| 640 | * @param mixed $newParentId |
||
| 641 | * |
||
| 642 | * @return array |
||
| 643 | */ |
||
| 644 | protected function getCopiedLocationsMap($oldParentId, $newParentId) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Notifies the underlying engine that a location was deleted or moved to trash. |
||
| 659 | * |
||
| 660 | * @param mixed $locationId |
||
| 661 | */ |
||
| 662 | public function locationDeleted($locationId) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Recursively removes aliases by given $id and $action. |
||
| 672 | * |
||
| 673 | * $original parameter is used to limit removal of moved Location aliases to history entries only. |
||
| 674 | * |
||
| 675 | * @param mixed $id |
||
| 676 | * @param string $action |
||
| 677 | * @param mixed $original |
||
| 678 | */ |
||
| 679 | protected function removeSubtree($id, $action, $original) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * @param string $text |
||
| 702 | * |
||
| 703 | * @return string |
||
| 704 | */ |
||
| 705 | protected function getHash($text) |
||
| 709 | } |
||
| 710 |
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.