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 | * The class name of the object route model. |
||
| 70 | * |
||
| 71 | * Must be a fully-qualified PHP namespace and an implementation of |
||
| 72 | * {@see \Charcoal\Object\ObjectRouteInterface}. Used by the model factory. |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $objectRouteClass = ObjectRoute::class; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * The object's route options. |
||
| 80 | * |
||
| 81 | * @var array|null |
||
| 82 | */ |
||
| 83 | protected $routeOptions; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Retrieve the foreign object's routes options ident. |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $routeOptionsIdent; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Set the object's URL slug pattern. |
||
| 94 | * |
||
| 95 | * @param mixed $pattern The slug pattern. |
||
| 96 | * @return RoutableInterface Chainable |
||
| 97 | */ |
||
| 98 | public function setSlugPattern($pattern) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Retrieve the object's URL slug pattern. |
||
| 107 | * |
||
| 108 | * @throws Exception If a slug pattern is not defined. |
||
| 109 | * @return \Charcoal\Translator\Translation|null |
||
| 110 | */ |
||
| 111 | public function slugPattern() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Retrieve route prefix for the object's URL slug pattern. |
||
| 133 | * |
||
| 134 | * @return \Charcoal\Translator\Translation|null |
||
| 135 | */ |
||
| 136 | View Code Duplication | public function slugPrefix() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Retrieve route suffix for the object's URL slug pattern. |
||
| 151 | * |
||
| 152 | * @return \Charcoal\Translator\Translation|null |
||
| 153 | */ |
||
| 154 | 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) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Retrieve the object's URL slug. |
||
| 217 | * |
||
| 218 | * @return \Charcoal\Translator\Translation|null |
||
| 219 | */ |
||
| 220 | public function getSlug() |
||
| 221 | { |
||
| 222 | return $this->slug; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Generate a URL slug from the object's URL slug pattern. |
||
| 227 | * |
||
| 228 | * @throws UnexpectedValueException If the slug is empty. |
||
| 229 | * @return \Charcoal\Translator\Translation |
||
| 230 | */ |
||
| 231 | public function generateSlug() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Generate a route from the given pattern. |
||
| 276 | * |
||
| 277 | * @uses self::parseRouteToken() If a view renderer is unavailable. |
||
| 278 | * @param string $pattern The slug pattern. |
||
| 279 | * @return string Returns the generated route. |
||
| 280 | */ |
||
| 281 | protected function generateRoutePattern($pattern) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Parse the given slug (URI token) for the current object. |
||
| 294 | * |
||
| 295 | * @used-by self::generateRoutePattern() If a view renderer is unavailable. |
||
| 296 | * @uses self::filterRouteToken() For customize the route value filtering, |
||
| 297 | * @param string|array $token The token to parse relative to the model entry. |
||
| 298 | * @throws InvalidArgumentException If a route token is not a string. |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | protected function parseRouteToken($token) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Filter the given value for a URI. |
||
| 335 | * |
||
| 336 | * @used-by self::parseRouteToken() To resolve the token's value. |
||
| 337 | * @param mixed $value A value to filter. |
||
| 338 | * @param string $token The parsed token. |
||
| 339 | * @return string The filtered $value. |
||
| 340 | */ |
||
| 341 | protected function filterRouteToken($value, $token = null) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Route generation. |
||
| 362 | * |
||
| 363 | * Saves all routes to {@see \Charcoal\Object\ObjectRoute}. |
||
| 364 | * |
||
| 365 | * @param mixed $slug Slug by langs. |
||
| 366 | * @param array $data Object route custom data. |
||
| 367 | * @throws InvalidArgumentException If the slug is invalid. |
||
| 368 | * @return void |
||
| 369 | */ |
||
| 370 | protected function generateObjectRoute($slug = null, array $data = []) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Retrieve the latest object route. |
||
| 448 | * |
||
| 449 | * @param string|null $lang If object is multilingual, return the object route for the specified locale. |
||
| 450 | * @throws InvalidArgumentException If the given language is invalid. |
||
| 451 | * @return ObjectRouteInterface Latest object route. |
||
| 452 | */ |
||
| 453 | protected function getLatestObjectRoute($lang = null) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Retrieve the object's URI. |
||
| 507 | * |
||
| 508 | * @param string|null $lang If object is multilingual, return the object route for the specified locale. |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public function url($lang = null) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Convert a string into a slug. |
||
| 529 | * |
||
| 530 | * @param string $str The string to slugify. |
||
| 531 | * @return string The slugified string. |
||
| 532 | */ |
||
| 533 | public function slugify($str) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Finalize slug. |
||
| 596 | * |
||
| 597 | * Adds any prefix and suffix defined in the routable configuration set. |
||
| 598 | * |
||
| 599 | * @param string $slug A slug. |
||
| 600 | * @throws UnexpectedValueException If the slug affixes are invalid. |
||
| 601 | * @return string |
||
| 602 | */ |
||
| 603 | protected function finalizeSlug($slug) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Delete all object routes. |
||
| 630 | * |
||
| 631 | * Should be called on object deletion {@see \Charcoal\Model\AbstractModel::preDelete()}. |
||
| 632 | * |
||
| 633 | * @return boolean Success or failure. |
||
| 634 | */ |
||
| 635 | protected function deleteObjectRoutes() |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Create a route collection loader. |
||
| 668 | * |
||
| 669 | * @return CollectionLoader |
||
| 670 | */ |
||
| 671 | public function createRouteObjectCollectionLoader() |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Create a route object. |
||
| 684 | * |
||
| 685 | * @return ObjectRouteInterface |
||
| 686 | */ |
||
| 687 | public function createRouteObject() |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Retrieve the route object prototype. |
||
| 696 | * |
||
| 697 | * @return ObjectRouteInterface |
||
| 698 | */ |
||
| 699 | public function getRouteObjectPrototype() |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Set the class name of the object route model. |
||
| 708 | * |
||
| 709 | * @param string $className The class name of the object route model. |
||
| 710 | * @throws InvalidArgumentException If the class name is not a string. |
||
| 711 | * @return AbstractPropertyDisplay Chainable |
||
| 712 | */ |
||
| 713 | protected function setObjectRouteClass($className) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Retrieve the class name of the object route model. |
||
| 728 | * |
||
| 729 | * @return string |
||
| 730 | */ |
||
| 731 | public function getObjectRouteClass() |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Alias of {@see self::getObjectRouteClass()}. |
||
| 738 | * |
||
| 739 | * @return string |
||
| 740 | */ |
||
| 741 | public function objectRouteClass() |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Set the object's route options |
||
| 748 | * |
||
| 749 | * @param mixed $options The object routes's options. |
||
| 750 | * @return self |
||
| 751 | */ |
||
| 752 | View Code Duplication | public function setRouteOptions($options) |
|
| 762 | |||
| 763 | /** |
||
| 764 | * Retrieve the object's route options |
||
| 765 | * |
||
| 766 | * @return array|null |
||
| 767 | */ |
||
| 768 | public function routeOptions() |
||
| 772 | |||
| 773 | /** |
||
| 774 | * @param string $routeOptionsIdent Template options ident. |
||
| 775 | * @return self |
||
| 776 | */ |
||
| 777 | public function setRouteOptionsIdent($routeOptionsIdent) |
||
| 783 | |||
| 784 | /** |
||
| 785 | * @return string |
||
| 786 | */ |
||
| 787 | public function routeOptionsIdent() |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Determine if the routable object is active. |
||
| 794 | * |
||
| 795 | * The route controller will validate the object via this method. If the routable object |
||
| 796 | * is NOT active, the route controller will usually default to _404 Not Found_. |
||
| 797 | * |
||
| 798 | * By default — if the object has an "active" property, that value is checked, else — |
||
| 799 | * the route is always _active_. |
||
| 800 | * |
||
| 801 | * @return boolean |
||
| 802 | */ |
||
| 803 | public function isActiveRoute() |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Retrieve the object model factory. |
||
| 814 | * |
||
| 815 | * @return \Charcoal\Factory\FactoryInterface |
||
| 816 | */ |
||
| 817 | abstract public function modelFactory(); |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Retrieve the routable object's template identifier. |
||
| 821 | * |
||
| 822 | * @return mixed |
||
| 823 | */ |
||
| 824 | abstract public function templateIdent(); |
||
| 825 | |||
| 826 | /** |
||
| 827 | * @return \Charcoal\Translator\Translator |
||
| 828 | */ |
||
| 829 | abstract protected function translator(); |
||
| 830 | } |
||
| 831 |
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.