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 | * The class name of the object route model. |
||
| 83 | * |
||
| 84 | * Must be a fully-qualified PHP namespace and an implementation of |
||
| 85 | * {@see \Charcoal\Object\ObjectRouteInterface}. Used by the model factory. |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $objectRouteClass = ObjectRoute::class; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Store the available templates. |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $availableTemplates = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns new template route object. |
||
| 100 | * |
||
| 101 | * @param array $data Class depdendencies. |
||
| 102 | */ |
||
| 103 | public function __construct(array $data) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Inject dependencies from a DI Container. |
||
| 112 | * |
||
| 113 | * @param Container $container A dependencies container instance. |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | public function setDependencies(Container $container) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Determine if the URI path resolves to an object. |
||
| 129 | * |
||
| 130 | * @param Container $container A DI (Pimple) container. |
||
| 131 | * @return boolean |
||
| 132 | */ |
||
| 133 | public function pathResolvable(Container $container) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Resolve the dynamic route. |
||
| 153 | * |
||
| 154 | * @param Container $container A DI (Pimple) container. |
||
| 155 | * @param RequestInterface $request A PSR-7 compatible Request instance. |
||
| 156 | * @param ResponseInterface $response A PSR-7 compatible Response instance. |
||
| 157 | * @return ResponseInterface |
||
| 158 | */ |
||
| 159 | public function __invoke( |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param RequestInterface $request A PSR-7 compatible Request instance. |
||
| 179 | * @param ResponseInterface $response A PSR-7 compatible Response instance. |
||
| 180 | * @return ResponseInterface |
||
| 181 | */ |
||
| 182 | protected function resolveLatestObjectRoute( |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return self |
||
| 203 | */ |
||
| 204 | protected function resolveTemplateContextObject() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param Container $container A DI (Pimple) container. |
||
| 295 | * @param RequestInterface $request The request to intialize the template with. |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | protected function createTemplate(Container $container, RequestInterface $request) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Create a route object. |
||
| 310 | * |
||
| 311 | * @return ObjectRouteInterface |
||
| 312 | */ |
||
| 313 | public function createRouteObject() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Set the class name of the object route model. |
||
| 322 | * |
||
| 323 | * @param string $className The class name of the object route model. |
||
| 324 | * @throws InvalidArgumentException If the class name is not a string. |
||
| 325 | * @return self |
||
| 326 | */ |
||
| 327 | protected function setObjectRouteClass($className) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Retrieve the class name of the object route model. |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | public function objectRouteClass() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Load the object associated with the matching object route. |
||
| 352 | * |
||
| 353 | * Validating if the object ID exists is delegated to the |
||
| 354 | * {@see GenericRoute Chainable::pathResolvable()} method. |
||
| 355 | * |
||
| 356 | * @return RoutableInterface |
||
| 357 | */ |
||
| 358 | protected function loadContextObject() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Load the object route matching the URI path. |
||
| 376 | * |
||
| 377 | * @return \Charcoal\Object\ObjectRouteInterface |
||
| 378 | */ |
||
| 379 | protected function loadObjectRouteFromPath() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Retrieve the latest object route from the given object route's |
||
| 403 | * associated object. |
||
| 404 | * |
||
| 405 | * The object routes are ordered by descending creation date (latest first). |
||
| 406 | * Should never MISS, the given object route should exist. |
||
| 407 | * |
||
| 408 | * @param ObjectRouteInterface $route Routable Object. |
||
| 409 | * @return ObjectRouteInterface |
||
| 410 | */ |
||
| 411 | public function getLatestObjectPathHistory(ObjectRouteInterface $route) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * SETTERS |
||
| 435 | */ |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Set the specified URI path. |
||
| 439 | * |
||
| 440 | * @param string $path The path to use for route resolution. |
||
| 441 | * @return self |
||
| 442 | */ |
||
| 443 | protected function setPath($path) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Set an object model factory. |
||
| 452 | * |
||
| 453 | * @param FactoryInterface $factory The model factory, to create objects. |
||
| 454 | * @return self |
||
| 455 | */ |
||
| 456 | protected function setModelFactory(FactoryInterface $factory) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Set a model collection loader. |
||
| 465 | * |
||
| 466 | * @param CollectionLoader $loader The collection loader. |
||
| 467 | * @return self |
||
| 468 | */ |
||
| 469 | public function setCollectionLoader(CollectionLoader $loader) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Sets the environment's current locale. |
||
| 478 | * |
||
| 479 | * @param string $langCode The locale's language code. |
||
| 480 | * @return void |
||
| 481 | */ |
||
| 482 | protected function setLocale($langCode) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * GETTERS |
||
| 508 | */ |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Retrieve the URI path. |
||
| 512 | * |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | protected function path() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Retrieve the object model factory. |
||
| 522 | * |
||
| 523 | * @throws RuntimeException If the model factory was not previously set. |
||
| 524 | * @return FactoryInterface |
||
| 525 | */ |
||
| 526 | View Code Duplication | public function modelFactory() |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Retrieve the model collection loader. |
||
| 539 | * |
||
| 540 | * @throws RuntimeException If the collection loader was not previously set. |
||
| 541 | * @return CollectionLoader |
||
| 542 | */ |
||
| 543 | View Code Duplication | protected function collectionLoader() |
|
| 553 | |||
| 554 | /** |
||
| 555 | * @return boolean |
||
| 556 | */ |
||
| 557 | protected function cacheEnabled() |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @return integer |
||
| 565 | */ |
||
| 566 | protected function cacheTtl() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @return string |
||
| 574 | */ |
||
| 575 | protected function cacheIdent() |
||
| 580 | } |
||
| 581 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.