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 ObjectRoute 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 ObjectRoute, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class ObjectRoute extends AbstractModel implements |
||
|
|
|||
| 41 | ObjectRouteInterface |
||
| 42 | { |
||
| 43 | use ModelFactoryTrait; |
||
| 44 | use CollectionLoaderAwareTrait; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * A route is active by default. |
||
| 48 | * |
||
| 49 | * @var boolean |
||
| 50 | */ |
||
| 51 | protected $active = true; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The route's URI. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $slug; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The route's locale. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $lang; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The creation timestamp. |
||
| 69 | * |
||
| 70 | * @var DateTime |
||
| 71 | */ |
||
| 72 | protected $creationDate; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The last modification timestamp. |
||
| 76 | * |
||
| 77 | * @var DateTime |
||
| 78 | */ |
||
| 79 | protected $lastModificationDate; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The foreign object type related to this route. |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $routeObjType; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The foreign object ID related to this route. |
||
| 90 | * |
||
| 91 | * @var mixed |
||
| 92 | */ |
||
| 93 | protected $routeObjId; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The foreign object's template identifier. |
||
| 97 | * |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | protected $routeTemplate; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Retrieve the foreign object's routes options. |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $routeOptions; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Retrieve the foreign object's routes options ident. |
||
| 111 | * |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected $routeOptionsIdent; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Store a copy of the original—_preferred_—slug before alterations are made. |
||
| 118 | * |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | private $originalSlug; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Store the increment used to create a unique slug. |
||
| 125 | * |
||
| 126 | * @var integer |
||
| 127 | */ |
||
| 128 | private $slugInc = 0; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Inject dependencies from a DI Container. |
||
| 132 | * |
||
| 133 | * @param Container $container A dependencies container instance. |
||
| 134 | * @return void |
||
| 135 | */ |
||
| 136 | protected function setDependencies(Container $container) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Event called before _creating_ the object. |
||
| 146 | * |
||
| 147 | * @see Charcoal\Source\StorableTrait::preSave() For the "create" Event. |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | protected function preSave() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Event called before _updating_ the object. |
||
| 161 | * |
||
| 162 | * @see Charcoal\Source\StorableTrait::preUpdate() For the "update" Event. |
||
| 163 | * @param array $properties Optional. The list of properties to update. |
||
| 164 | * @return boolean |
||
| 165 | */ |
||
| 166 | protected function preUpdate(array $properties = null) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Determine if the current slug is unique. |
||
| 176 | * |
||
| 177 | * @return boolean |
||
| 178 | */ |
||
| 179 | public function isSlugUnique() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Generate a unique URL slug for routable object. |
||
| 218 | * |
||
| 219 | * @return self |
||
| 220 | */ |
||
| 221 | public function generateUniqueSlug() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Set the object route URI. |
||
| 238 | * |
||
| 239 | * @param string|null $slug The route. |
||
| 240 | * @throws InvalidArgumentException If the slug argument is not a string. |
||
| 241 | * @return self |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function setSlug($slug) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Set the locale of the object route. |
||
| 262 | * |
||
| 263 | * @param string $lang The route's locale. |
||
| 264 | * @return self |
||
| 265 | */ |
||
| 266 | public function setLang($lang) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Set the route's last creation date. |
||
| 275 | * |
||
| 276 | * @param string|DateTimeInterface|null $time The date/time value. |
||
| 277 | * @throws InvalidArgumentException If the date/time value is invalid. |
||
| 278 | * @return self |
||
| 279 | */ |
||
| 280 | public function setCreationDate($time) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set the route's last modification date. |
||
| 312 | * |
||
| 313 | * @param string|DateTimeInterface|null $time The date/time value. |
||
| 314 | * @throws InvalidArgumentException If the date/time value is invalid. |
||
| 315 | * @return self |
||
| 316 | */ |
||
| 317 | public function setLastModificationDate($time) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Set the foreign object type related to this route. |
||
| 349 | * |
||
| 350 | * @param string $type The object type. |
||
| 351 | * @return self |
||
| 352 | */ |
||
| 353 | public function setRouteObjType($type) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Set the foreign object ID related to this route. |
||
| 362 | * |
||
| 363 | * @param string $id The object ID. |
||
| 364 | * @return self |
||
| 365 | */ |
||
| 366 | public function setRouteObjId($id) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Set the foreign object's template identifier. |
||
| 375 | * |
||
| 376 | * @param string $template The template identifier. |
||
| 377 | * @return self |
||
| 378 | */ |
||
| 379 | public function setRouteTemplate($template) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Customize the template's options. |
||
| 388 | * |
||
| 389 | * @param mixed $options Template options. |
||
| 390 | * @return self |
||
| 391 | */ |
||
| 392 | View Code Duplication | public function setRouteOptions($options) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $routeOptionsIdent Template options ident. |
||
| 405 | * @return self |
||
| 406 | */ |
||
| 407 | public function setRouteOptionsIdent($routeOptionsIdent) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Retrieve the object model factory. |
||
| 416 | * |
||
| 417 | * @throws RuntimeException If the model factory was not previously set. |
||
| 418 | * @return FactoryInterface |
||
| 419 | */ |
||
| 420 | View Code Duplication | public function modelFactory() |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Retrieve the model collection loader. |
||
| 434 | * |
||
| 435 | * @throws RuntimeException If the collection loader was not previously set. |
||
| 436 | * @return CollectionLoader |
||
| 437 | */ |
||
| 438 | public function collectionLoader() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Retrieve the object route. |
||
| 452 | * |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | public function getSlug() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Retrieve the locale of the object route. |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | public function getLang() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Retrieve the route's creation date. |
||
| 472 | * |
||
| 473 | * @return DateTimeInterface|null |
||
| 474 | */ |
||
| 475 | public function getCreationDate() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Retrieve the route's last modification date. |
||
| 482 | * |
||
| 483 | * @return DateTimeInterface|null |
||
| 484 | */ |
||
| 485 | public function getLastModificationDate() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Retrieve the foreign object type related to this route. |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | public function getRouteObjType() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Retrieve the foreign object ID related to this route. |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | public function getRouteObjId() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Retrieve the foreign object's template identifier. |
||
| 512 | * |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | public function getRouteTemplate() |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Retrieve the template's customized options. |
||
| 522 | * |
||
| 523 | * @return array |
||
| 524 | */ |
||
| 525 | public function getRouteOptions() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | public function getRouteOptionsIdent() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Alias of {@see self::slug()}. |
||
| 540 | * |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | public function __toString() |
||
| 547 | } |
||
| 548 |