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 GenericRoute 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 GenericRoute, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class GenericRoute extends TemplateRoute |
||
| 43 | { |
||
| 44 | use TranslatorAwareTrait; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The URI path. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $path; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The object route. |
||
| 55 | * |
||
| 56 | * @var ObjectRouteInterface |
||
| 57 | */ |
||
| 58 | private $objectRoute; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The target object of the {@see GenericRoute Chainable::$objectRoute}. |
||
| 62 | * |
||
| 63 | * @var ModelInterface|RoutableInterface |
||
| 64 | */ |
||
| 65 | private $contextObject; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Store the factory instance for the current class. |
||
| 69 | * |
||
| 70 | * @var FactoryInterface |
||
| 71 | */ |
||
| 72 | private $modelFactory; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Store the collection loader for the current class. |
||
| 76 | * |
||
| 77 | * @var CollectionLoader |
||
| 78 | */ |
||
| 79 | private $collectionLoader; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Track the state of required dependencies. |
||
| 83 | * |
||
| 84 | * @var boolean |
||
| 85 | */ |
||
| 86 | protected $hasDependencies = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The class name of the object route model. |
||
| 90 | * |
||
| 91 | * Must be a fully-qualified PHP namespace and an implementation of |
||
| 92 | * {@see ObjectRouteInterface}. Used by the model factory. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected $objectRouteClass = ObjectRoute::class; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Store the available templates. |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | protected $availableTemplates = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Returns new template route object. |
||
| 107 | * |
||
| 108 | * @param array $data Class depdendencies. |
||
| 109 | */ |
||
| 110 | public function __construct(array $data) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Determine if the URI path resolves to an object. |
||
| 119 | * |
||
| 120 | * @param Container $container A DI (Pimple) container. |
||
| 121 | * @return boolean |
||
| 122 | */ |
||
| 123 | public function pathResolvable(Container $container) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Resolve the dynamic route. |
||
| 144 | * |
||
| 145 | * @param Container $container A DI (Pimple) container. |
||
| 146 | * @param RequestInterface $request A PSR-7 compatible Request instance. |
||
| 147 | * @param ResponseInterface $response A PSR-7 compatible Response instance. |
||
| 148 | * @return ResponseInterface |
||
| 149 | */ |
||
| 150 | public function __invoke( |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Create a route object. |
||
| 199 | * |
||
| 200 | * @return ObjectRouteInterface |
||
| 201 | */ |
||
| 202 | public function createRouteObject() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Retrieve the class name of the object route model. |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function objectRouteClass() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Inject dependencies from a DI Container. |
||
| 221 | * |
||
| 222 | * @param Container $container A dependencies container instance. |
||
| 223 | * @return void |
||
| 224 | */ |
||
| 225 | protected function setDependencies(Container $container) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Determine if the current object route is the latest object route. |
||
| 240 | * |
||
| 241 | * This method loads the latest object route from the datasource and compares |
||
| 242 | * their creation dates. Both instances could be the same object (ID). |
||
| 243 | * |
||
| 244 | * @param RequestInterface $request A PSR-7 compatible Request instance. |
||
| 245 | * @param ResponseInterface $response A PSR-7 compatible Response instance. |
||
| 246 | * @return ResponseInterface |
||
| 247 | */ |
||
| 248 | protected function resolveLatestObjectRoute( |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return self |
||
| 266 | */ |
||
| 267 | protected function resolveTemplateContextObject() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param Container $container A DI (Pimple) container. |
||
| 355 | * @param RequestInterface $request The request to intialize the template with. |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | protected function createTemplate(Container $container, RequestInterface $request) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Set the class name of the object route model. |
||
| 370 | * |
||
| 371 | * @param string $className The class name of the object route model. |
||
| 372 | * @throws InvalidArgumentException If the class name is not a string. |
||
| 373 | * @return self |
||
| 374 | */ |
||
| 375 | protected function setObjectRouteClass($className) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Asserts that the object route is valid, throws an Exception if not. |
||
| 390 | * |
||
| 391 | * @param RoutableInterface $contextObject A context object to test. |
||
| 392 | * @throws InvalidArgumentException If the context object is invalid. |
||
| 393 | * @return void |
||
| 394 | */ |
||
| 395 | protected function assertValidContextObject(RoutableInterface $contextObject) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Determine if the object route is valid. |
||
| 404 | * |
||
| 405 | * @param RoutableInterface $contextObject A context object to test. |
||
| 406 | * @return boolean |
||
| 407 | */ |
||
| 408 | protected function isValidContextObject(RoutableInterface $contextObject) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Get the object associated with the matching object route. |
||
| 427 | * |
||
| 428 | * @return RoutableInterface |
||
| 429 | */ |
||
| 430 | protected function getContextObject() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Load the object associated with the matching object route. |
||
| 441 | * |
||
| 442 | * Validating if the object ID exists is delegated to the |
||
| 443 | * {@see GenericRoute Chainable::pathResolvable()} method. |
||
| 444 | * |
||
| 445 | * @throws RuntimeException If the object route is incomplete. |
||
| 446 | * @return RoutableInterface |
||
| 447 | */ |
||
| 448 | protected function loadContextObject() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Asserts that the object route is valid, throws an Exception if not. |
||
| 462 | * |
||
| 463 | * @param ObjectRouteInterface $route An object route to test. |
||
| 464 | * @throws InvalidArgumentException If the object route is incomplete. |
||
| 465 | * @return void |
||
| 466 | */ |
||
| 467 | protected function assertValidObjectRoute(ObjectRouteInterface $route) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Determine if the object route is valid. |
||
| 476 | * |
||
| 477 | * @param ObjectRouteInterface $route An object route to test. |
||
| 478 | * @return boolean |
||
| 479 | */ |
||
| 480 | protected function isValidObjectRoute(ObjectRouteInterface $route) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Get the object route matching the URI path. |
||
| 487 | * |
||
| 488 | * @return ObjectRouteInterface |
||
| 489 | */ |
||
| 490 | protected function getObjectRouteFromPath() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Load the object route matching the URI path. |
||
| 501 | * |
||
| 502 | * @return ObjectRouteInterface |
||
| 503 | */ |
||
| 504 | protected function loadObjectRouteFromPath() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Retrieve the latest object route from the given object route's |
||
| 526 | * associated object. |
||
| 527 | * |
||
| 528 | * The object routes are ordered by descending creation date (latest first). |
||
| 529 | * Should never MISS, the given object route should exist. |
||
| 530 | * |
||
| 531 | * @param ObjectRouteInterface $route Routable Object. |
||
| 532 | * @return ObjectRouteInterface|null |
||
| 533 | */ |
||
| 534 | public function getLatestObjectPathHistory(ObjectRouteInterface $route) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * SETTERS |
||
| 562 | */ |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Set the specified URI path. |
||
| 566 | * |
||
| 567 | * @param string $path The path to use for route resolution. |
||
| 568 | * @return self |
||
| 569 | */ |
||
| 570 | protected function setPath($path) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Set an object model factory. |
||
| 579 | * |
||
| 580 | * @param FactoryInterface $factory The model factory, to create objects. |
||
| 581 | * @return self |
||
| 582 | */ |
||
| 583 | protected function setModelFactory(FactoryInterface $factory) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Set a model collection loader. |
||
| 592 | * |
||
| 593 | * @param CollectionLoader $loader The collection loader. |
||
| 594 | * @return self |
||
| 595 | */ |
||
| 596 | public function setCollectionLoader(CollectionLoader $loader) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Sets the environment's current locale. |
||
| 605 | * |
||
| 606 | * @param string $langCode The locale's language code. |
||
| 607 | * @return void |
||
| 608 | */ |
||
| 609 | protected function setLocale($langCode) |
||
| 639 | |||
| 640 | /** |
||
| 641 | * GETTERS |
||
| 642 | */ |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Retrieve the URI path. |
||
| 646 | * |
||
| 647 | * @return string |
||
| 648 | */ |
||
| 649 | protected function path() |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Retrieve the object model factory. |
||
| 656 | * |
||
| 657 | * @throws RuntimeException If the model factory was not previously set. |
||
| 658 | * @return FactoryInterface |
||
| 659 | */ |
||
| 660 | View Code Duplication | public function modelFactory() |
|
| 670 | |||
| 671 | /** |
||
| 672 | * Retrieve the model collection loader. |
||
| 673 | * |
||
| 674 | * @throws RuntimeException If the collection loader was not previously set. |
||
| 675 | * @return CollectionLoader |
||
| 676 | */ |
||
| 677 | View Code Duplication | protected function collectionLoader() |
|
| 687 | |||
| 688 | /** |
||
| 689 | * @return boolean |
||
| 690 | */ |
||
| 691 | protected function cacheEnabled() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @return integer |
||
| 699 | */ |
||
| 700 | protected function cacheTtl() |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @return string |
||
| 708 | */ |
||
| 709 | protected function cacheIdent() |
||
| 714 | } |
||
| 715 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.