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 RoutableTrait 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 RoutableTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | trait RoutableTrait |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * The object's route. |
||
| 28 | * |
||
| 29 | * @var TranslationString|string|null |
||
| 30 | */ |
||
| 31 | protected $slug; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Whether the slug is editable. |
||
| 35 | * |
||
| 36 | * If FALSE, the slug is always auto-generated from its pattern. |
||
| 37 | * If TRUE, the slug is auto-generated only if the slug is empty. |
||
| 38 | * |
||
| 39 | * @var boolean|null |
||
| 40 | */ |
||
| 41 | private $isSlugEditable; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The object's route pattern. |
||
| 45 | * |
||
| 46 | * @var TranslationString|string|null |
||
| 47 | */ |
||
| 48 | private $slugPattern = ''; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * A prefix for the object's route. |
||
| 52 | * |
||
| 53 | * @var TranslationString|string|null |
||
| 54 | */ |
||
| 55 | private $slugPrefix = ''; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * A suffix for the object's route. |
||
| 59 | * |
||
| 60 | * @var TranslationString|string|null |
||
| 61 | */ |
||
| 62 | private $slugSuffix = ''; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Latest ObjectRoute object concerning the current object. |
||
| 66 | * |
||
| 67 | * @var ObjectRouteInterface |
||
| 68 | */ |
||
| 69 | private $latestObjectRoute; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The class name of the object route model. |
||
| 73 | * |
||
| 74 | * Must be a fully-qualified PHP namespace and an implementation of |
||
| 75 | * {@see \Charcoal\Object\ObjectRouteInterface}. Used by the model factory. |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | private $objectRouteClass = ObjectRoute::class; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set the object's URL slug pattern. |
||
| 83 | * |
||
| 84 | * @param mixed $pattern The slug pattern. |
||
| 85 | * @return RoutableInterface Chainable |
||
| 86 | */ |
||
| 87 | public function setSlugPattern($pattern) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Retrieve the object's URL slug pattern. |
||
| 100 | * |
||
| 101 | * @throws InvalidArgumentException If a slug pattern is not defined. |
||
| 102 | * @return TranslationString|null |
||
| 103 | */ |
||
| 104 | public function slugPattern() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Retrieve route prefix for the object's URL slug pattern. |
||
| 125 | * |
||
| 126 | * @return TranslationString|null |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function slugPrefix() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Retrieve route suffix for the object's URL slug pattern. |
||
| 147 | * |
||
| 148 | * @return TranslationString|null |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function slugSuffix() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Determine if the slug is editable. |
||
| 169 | * |
||
| 170 | * @return boolean |
||
| 171 | */ |
||
| 172 | public function isSlugEditable() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set the object's URL slug. |
||
| 189 | * |
||
| 190 | * @param mixed $slug The slug. |
||
| 191 | * @return RoutableInterface Chainable |
||
| 192 | */ |
||
| 193 | public function setSlug($slug) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Retrieve the object's URL slug. |
||
| 216 | * |
||
| 217 | * @return TranslationString|null |
||
| 218 | */ |
||
| 219 | public function slug() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Generate a URL slug from the object's URL slug pattern. |
||
| 226 | * |
||
| 227 | * @return TranslationString |
||
| 228 | */ |
||
| 229 | public function generateSlug() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Generate a route from the given pattern. |
||
| 281 | * |
||
| 282 | * @uses self::parseRouteToken() If a view renderer is unavailable. |
||
| 283 | * @param string $pattern The slug pattern. |
||
| 284 | * @return string Returns the generated route. |
||
| 285 | */ |
||
| 286 | protected function generateRoutePattern($pattern) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Parse the given slug (URI token) for the current object. |
||
| 299 | * |
||
| 300 | * @used-by self::generateRoutePattern() If a view renderer is unavailable. |
||
| 301 | * @uses self::filterRouteToken() For customize the route value filtering, |
||
| 302 | * @param string|array $token The token to parse relative to the model entry. |
||
| 303 | * @throws InvalidArgumentException If a route token is not a string. |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | protected function parseRouteToken($token) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Filter the given value for a URI. |
||
| 341 | * |
||
| 342 | * @used-by self::parseRouteToken() To resolve the token's value. |
||
| 343 | * @param mixed $value A value to filter. |
||
| 344 | * @param string $token The parsed token. |
||
| 345 | * @return string The filtered $value. |
||
| 346 | */ |
||
| 347 | protected function filterRouteToken($value, $token = null) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Route generation. |
||
| 364 | * |
||
| 365 | * Saves all routes to {@see \Charcoal\Object\ObjectRoute}. |
||
| 366 | * |
||
| 367 | * @param mixed $slug Slug by langs. |
||
| 368 | * @return void |
||
| 369 | */ |
||
| 370 | protected function generateObjectRoute($slug = null) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Retrieve the latest object route. |
||
| 429 | * |
||
| 430 | * @param string|null $lang If object is multilingual, return the object route for the specified locale. |
||
| 431 | * @throws InvalidArgumentException If the given language is invalid. |
||
| 432 | * @return ObjectRouteInterface Latest object route. |
||
| 433 | */ |
||
| 434 | protected function getLatestObjectRoute($lang = null) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Sync the object routes with the object's data. |
||
| 497 | * @return void |
||
| 498 | */ |
||
| 499 | public function syncObjectRoutes() |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Retrieve the object's URI. |
||
| 529 | * |
||
| 530 | * @param string|null $lang If object is multilingual, return the object route for the specified locale. |
||
| 531 | * @return TranslationString|string |
||
| 532 | */ |
||
| 533 | public function url($lang = null) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Convert a string into a slug. |
||
| 551 | * |
||
| 552 | * @param string $str The string to slugify. |
||
| 553 | * @return string The slugified string. |
||
| 554 | */ |
||
| 555 | public function slugify($str) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Finalize slug. |
||
| 618 | * |
||
| 619 | * Adds any prefix and suffix defined in the routable configuration set. |
||
| 620 | * |
||
| 621 | * @param string $slug A slug. |
||
| 622 | * @return string |
||
| 623 | */ |
||
| 624 | protected function finalizeSlug($slug) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Delete all object routes. |
||
| 649 | * |
||
| 650 | * Should be called on object deletion {@see \Charcoal\Model\AbstractModel::preDelete()}. |
||
| 651 | * |
||
| 652 | * @return boolean Success or failure. |
||
| 653 | */ |
||
| 654 | protected function deleteObjectRoutes() |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Create a route object. |
||
| 685 | * |
||
| 686 | * @return ObjectRouteInterface |
||
| 687 | */ |
||
| 688 | public function createRouteObject() |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Set the class name of the object route model. |
||
| 697 | * |
||
| 698 | * @param string $className The class name of the object route model. |
||
| 699 | * @throws InvalidArgumentException If the class name is not a string. |
||
| 700 | * @return AbstractPropertyDisplay Chainable |
||
| 701 | */ |
||
| 702 | protected function setObjectRouteClass($className) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Retrieve the class name of the object route model. |
||
| 717 | * |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | public function objectRouteClass() |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Retrieve the object model factory. |
||
| 727 | * |
||
| 728 | * @return \Charcoal\Factory\FactoryInterface |
||
| 729 | */ |
||
| 730 | abstract public function modelFactory(); |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Retrieve the routable object's template identifier. |
||
| 734 | * |
||
| 735 | * @return mixed |
||
| 736 | */ |
||
| 737 | abstract public function templateIdent(); |
||
| 738 | } |
||
| 739 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.