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 |
||
| 28 | trait RoutableTrait |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * The object's route. |
||
| 32 | * |
||
| 33 | * @var \Charcoal\Translator\Translation|null |
||
| 34 | */ |
||
| 35 | protected $slug; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Whether the slug is editable. |
||
| 39 | * |
||
| 40 | * If FALSE, the slug is always auto-generated from its pattern. |
||
| 41 | * If TRUE, the slug is auto-generated only if the slug is empty. |
||
| 42 | * |
||
| 43 | * @var boolean|null |
||
| 44 | */ |
||
| 45 | private $isSlugEditable; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The object's route pattern. |
||
| 49 | * |
||
| 50 | * @var \Charcoal\Translator\Translation|null |
||
| 51 | */ |
||
| 52 | private $slugPattern = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * A prefix for the object's route. |
||
| 56 | * |
||
| 57 | * @var \Charcoal\Translator\Translation|null |
||
| 58 | */ |
||
| 59 | private $slugPrefix = ''; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * A suffix for the object's route. |
||
| 63 | * |
||
| 64 | * @var \Charcoal\Translator\Translation|null |
||
| 65 | */ |
||
| 66 | private $slugSuffix = ''; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Latest ObjectRoute object concerning the current object. |
||
| 70 | * |
||
| 71 | * @var ObjectRouteInterface |
||
| 72 | */ |
||
| 73 | private $latestObjectRoute; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The class name of the object route model. |
||
| 77 | * |
||
| 78 | * Must be a fully-qualified PHP namespace and an implementation of |
||
| 79 | * {@see \Charcoal\Object\ObjectRouteInterface}. Used by the model factory. |
||
| 80 | * |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | private $objectRouteClass = ObjectRoute::class; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The object's route options. |
||
| 87 | * |
||
| 88 | * @var array|null |
||
| 89 | */ |
||
| 90 | protected $routeOptions; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Retrieve the foreign object's routes options ident. |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | protected $routeOptionsIdent; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Set the object's URL slug pattern. |
||
| 101 | * |
||
| 102 | * @param mixed $pattern The slug pattern. |
||
| 103 | * @return RoutableInterface Chainable |
||
| 104 | */ |
||
| 105 | public function setSlugPattern($pattern) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Retrieve the object's URL slug pattern. |
||
| 114 | * |
||
| 115 | * @throws Exception If a slug pattern is not defined. |
||
| 116 | * @return \Charcoal\Translator\Translation|null |
||
| 117 | */ |
||
| 118 | public function slugPattern() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Retrieve route prefix for the object's URL slug pattern. |
||
| 140 | * |
||
| 141 | * @return \Charcoal\Translator\Translation|null |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function slugPrefix() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Retrieve route suffix for the object's URL slug pattern. |
||
| 158 | * |
||
| 159 | * @return \Charcoal\Translator\Translation|null |
||
| 160 | */ |
||
| 161 | View Code Duplication | public function slugSuffix() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Determine if the slug is editable. |
||
| 176 | * |
||
| 177 | * @return boolean |
||
| 178 | */ |
||
| 179 | public function isSlugEditable() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set the object's URL slug. |
||
| 196 | * |
||
| 197 | * @param mixed $slug The slug. |
||
| 198 | * @return RoutableInterface Chainable |
||
| 199 | */ |
||
| 200 | public function setSlug($slug) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Retrieve the object's URL slug. |
||
| 224 | * |
||
| 225 | * @return \Charcoal\Translator\Translation|null |
||
| 226 | */ |
||
| 227 | public function slug() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Generate a URL slug from the object's URL slug pattern. |
||
| 234 | * |
||
| 235 | * @throws UnexpectedValueException If the slug is empty. |
||
| 236 | * @return \Charcoal\Translator\Translation |
||
| 237 | */ |
||
| 238 | public function generateSlug() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Generate a route from the given pattern. |
||
| 283 | * |
||
| 284 | * @uses self::parseRouteToken() If a view renderer is unavailable. |
||
| 285 | * @param string $pattern The slug pattern. |
||
| 286 | * @return string Returns the generated route. |
||
| 287 | */ |
||
| 288 | protected function generateRoutePattern($pattern) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Parse the given slug (URI token) for the current object. |
||
| 301 | * |
||
| 302 | * @used-by self::generateRoutePattern() If a view renderer is unavailable. |
||
| 303 | * @uses self::filterRouteToken() For customize the route value filtering, |
||
| 304 | * @param string|array $token The token to parse relative to the model entry. |
||
| 305 | * @throws InvalidArgumentException If a route token is not a string. |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | protected function parseRouteToken($token) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Filter the given value for a URI. |
||
| 342 | * |
||
| 343 | * @used-by self::parseRouteToken() To resolve the token's value. |
||
| 344 | * @param mixed $value A value to filter. |
||
| 345 | * @param string $token The parsed token. |
||
| 346 | * @return string The filtered $value. |
||
| 347 | */ |
||
| 348 | protected function filterRouteToken($value, $token = null) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Route generation. |
||
| 369 | * |
||
| 370 | * Saves all routes to {@see \Charcoal\Object\ObjectRoute}. |
||
| 371 | * |
||
| 372 | * @param mixed $slug Slug by langs. |
||
| 373 | * @param array $data Object route custom data. |
||
| 374 | * @throws InvalidArgumentException If the slug is invalid. |
||
| 375 | * @return void |
||
| 376 | */ |
||
| 377 | protected function generateObjectRoute($slug = null, array $data = []) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Retrieve the latest object route. |
||
| 453 | * |
||
| 454 | * @param string|null $lang If object is multilingual, return the object route for the specified locale. |
||
| 455 | * @throws InvalidArgumentException If the given language is invalid. |
||
| 456 | * @return ObjectRouteInterface Latest object route. |
||
| 457 | */ |
||
| 458 | protected function getLatestObjectRoute($lang = null) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Retrieve the object's URI. |
||
| 514 | * |
||
| 515 | * @param string|null $lang If object is multilingual, return the object route for the specified locale. |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public function url($lang = null) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Convert a string into a slug. |
||
| 536 | * |
||
| 537 | * @param string $str The string to slugify. |
||
| 538 | * @return string The slugified string. |
||
| 539 | */ |
||
| 540 | public function slugify($str) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Finalize slug. |
||
| 603 | * |
||
| 604 | * Adds any prefix and suffix defined in the routable configuration set. |
||
| 605 | * |
||
| 606 | * @param string $slug A slug. |
||
| 607 | * @throws UnexpectedValueException If the slug affixes are invalid. |
||
| 608 | * @return string |
||
| 609 | */ |
||
| 610 | protected function finalizeSlug($slug) |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Delete all object routes. |
||
| 635 | * |
||
| 636 | * Should be called on object deletion {@see \Charcoal\Model\AbstractModel::preDelete()}. |
||
| 637 | * |
||
| 638 | * @return boolean Success or failure. |
||
| 639 | */ |
||
| 640 | protected function deleteObjectRoutes() |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Create a route object. |
||
| 671 | * |
||
| 672 | * @return ObjectRouteInterface |
||
| 673 | */ |
||
| 674 | public function createRouteObject() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Set the class name of the object route model. |
||
| 683 | * |
||
| 684 | * @param string $className The class name of the object route model. |
||
| 685 | * @throws InvalidArgumentException If the class name is not a string. |
||
| 686 | * @return AbstractPropertyDisplay Chainable |
||
| 687 | */ |
||
| 688 | protected function setObjectRouteClass($className) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Retrieve the class name of the object route model. |
||
| 703 | * |
||
| 704 | * @return string |
||
| 705 | */ |
||
| 706 | public function objectRouteClass() |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Set the object's route options |
||
| 713 | * |
||
| 714 | * @param mixed $options The object routes's options. |
||
| 715 | * @return self |
||
| 716 | */ |
||
| 717 | View Code Duplication | public function setRouteOptions($options) |
|
| 727 | |||
| 728 | /** |
||
| 729 | * Retrieve the object's route options |
||
| 730 | * |
||
| 731 | * @return array|null |
||
| 732 | */ |
||
| 733 | public function routeOptions() |
||
| 737 | |||
| 738 | /** |
||
| 739 | * @param string $routeOptionsIdent Template options ident. |
||
| 740 | * @return self |
||
| 741 | */ |
||
| 742 | public function setRouteOptionsIdent($routeOptionsIdent) |
||
| 748 | |||
| 749 | /** |
||
| 750 | * @return string |
||
| 751 | */ |
||
| 752 | public function routeOptionsIdent() |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Determine if the routable object is active. |
||
| 759 | * |
||
| 760 | * The route controller will validate the object via this method. If the routable object |
||
| 761 | * is NOT active, the route controller will usually default to _404 Not Found_. |
||
| 762 | * |
||
| 763 | * By default — if the object has an "active" property, that value is checked, else — |
||
| 764 | * the route is always _active_. |
||
| 765 | * |
||
| 766 | * @return boolean |
||
| 767 | */ |
||
| 768 | public function isActiveRoute() |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Retrieve the object model factory. |
||
| 779 | * |
||
| 780 | * @return \Charcoal\Factory\FactoryInterface |
||
| 781 | */ |
||
| 782 | abstract public function modelFactory(); |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Retrieve the routable object's template identifier. |
||
| 786 | * |
||
| 787 | * @return mixed |
||
| 788 | */ |
||
| 789 | abstract public function templateIdent(); |
||
| 790 | |||
| 791 | /** |
||
| 792 | * @return \Charcoal\Translator\Translator |
||
| 793 | */ |
||
| 794 | abstract protected function translator(); |
||
| 795 | } |
||
| 796 |
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.