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 CoffeeShop 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 CoffeeShop, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class CoffeeShop implements CoffeePotInterface |
||
| 33 | { |
||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * This was the best coffee related name I could think of to represent class name "aliases" |
||
| 38 | * So classes can be found via an alias identifier, |
||
| 39 | * that is revealed when it is run through... the filters... eh? get it? |
||
| 40 | * |
||
| 41 | * @var array $filters |
||
| 42 | */ |
||
| 43 | private $filters; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * These are the classes that will actually build the objects (to order of course) |
||
| 47 | * |
||
| 48 | * @var array $coffee_makers |
||
| 49 | */ |
||
| 50 | private $coffee_makers; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * where the instantiated "singleton" objects are stored |
||
| 54 | * |
||
| 55 | * @var CollectionInterface $carafe |
||
| 56 | */ |
||
| 57 | private $carafe; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * collection of Recipes that instruct us how to brew objects |
||
| 61 | * |
||
| 62 | * @var CollectionInterface $recipes |
||
| 63 | */ |
||
| 64 | private $recipes; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * collection of closures for brewing objects |
||
| 68 | * |
||
| 69 | * @var CollectionInterface $reservoir |
||
| 70 | */ |
||
| 71 | private $reservoir; |
||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * CoffeeShop constructor |
||
| 77 | * |
||
| 78 | * @throws InvalidInterfaceException |
||
| 79 | */ |
||
| 80 | public function __construct() |
||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns true if the container can return an entry for the given identifier. |
||
| 98 | * Returns false otherwise. |
||
| 99 | * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
||
| 100 | * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
||
| 101 | * |
||
| 102 | * @param string $identifier Identifier of the entry to look for. |
||
| 103 | * Typically a Fully Qualified Class Name |
||
| 104 | * @return boolean |
||
| 105 | * @throws InvalidIdentifierException |
||
| 106 | */ |
||
| 107 | public function has($identifier) |
||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * finds a previously brewed (SHARED) service and returns it |
||
| 117 | * |
||
| 118 | * @param string $identifier Identifier for the entity class to be constructed. |
||
| 119 | * Typically a Fully Qualified Class Name |
||
| 120 | * @return mixed |
||
| 121 | * @throws InvalidIdentifierException |
||
| 122 | * @throws ServiceNotFoundException No service was found for this identifier. |
||
| 123 | */ |
||
| 124 | public function get($identifier) |
||
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * returns an instance of the requested entity type using the supplied arguments. |
||
| 137 | * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
||
| 138 | * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
||
| 139 | * If the request is for a new entity and a closure exists in the reservoir for creating it, |
||
| 140 | * then a new entity will be instantiated from the closure and returned. |
||
| 141 | * If a closure does not exist, then one will be built and added to the reservoir |
||
| 142 | * before instantiating the requested entity. |
||
| 143 | * |
||
| 144 | * @param string $identifier Identifier for the entity class to be constructed. |
||
| 145 | * Typically a Fully Qualified Class Name |
||
| 146 | * @param array $arguments an array of arguments to be passed to the entity constructor |
||
| 147 | * @param string $type |
||
| 148 | * @return mixed |
||
| 149 | * @throws OutOfBoundsException |
||
| 150 | * @throws InstantiationException |
||
| 151 | * @throws InvalidDataTypeException |
||
| 152 | * @throws InvalidClassException |
||
| 153 | * @throws InvalidIdentifierException |
||
| 154 | * @throws ServiceExistsException |
||
| 155 | * @throws ServiceNotFoundException No service was found for this identifier. |
||
| 156 | */ |
||
| 157 | public function brew($identifier, $arguments = array(), $type = '') |
||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $identifier |
||
| 197 | * @param string $type |
||
| 198 | * @return bool|mixed |
||
| 199 | * @throws InvalidIdentifierException |
||
| 200 | */ |
||
| 201 | protected function getShared($identifier, $type) |
||
| 214 | |||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * @param mixed $brewed |
||
| 219 | * @param string $identifier |
||
| 220 | * @param string $type |
||
| 221 | * @return bool |
||
| 222 | * @throws InvalidClassException |
||
| 223 | * @throws InvalidDataTypeException |
||
| 224 | * @throws InvalidIdentifierException |
||
| 225 | * @throws OutOfBoundsException |
||
| 226 | * @throws ServiceExistsException |
||
| 227 | * @throws ServiceNotFoundException |
||
| 228 | */ |
||
| 229 | protected function brewedLoadOnly($brewed, $identifier, $type) |
||
| 247 | |||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $identifier |
||
| 252 | * @param array $arguments |
||
| 253 | * @return mixed |
||
| 254 | * @throws InstantiationException |
||
| 255 | */ |
||
| 256 | protected function brewedClosure($identifier, array $arguments) |
||
| 272 | |||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * @param CoffeeMakerInterface $coffee_maker |
||
| 277 | * @param string $type |
||
| 278 | * @return bool |
||
| 279 | * @throws InvalidIdentifierException |
||
| 280 | * @throws InvalidEntityException |
||
| 281 | */ |
||
| 282 | public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $identifier |
||
| 292 | * @param callable $closure |
||
| 293 | * @return callable|null |
||
| 294 | * @throws InvalidIdentifierException |
||
| 295 | * @throws InvalidDataTypeException |
||
| 296 | */ |
||
| 297 | public function addClosure($identifier, $closure) |
||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $identifier |
||
| 313 | * @return boolean |
||
| 314 | * @throws InvalidIdentifierException |
||
| 315 | */ |
||
| 316 | View Code Duplication | public function removeClosure($identifier) |
|
| 324 | |||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $identifier Identifier for the entity class that the service applies to |
||
| 329 | * Typically a Fully Qualified Class Name |
||
| 330 | * @param mixed $service |
||
| 331 | * @return bool |
||
| 332 | * @throws \EventEspresso\core\services\container\exceptions\InvalidServiceException |
||
| 333 | * @throws InvalidIdentifierException |
||
| 334 | */ |
||
| 335 | public function addService($identifier, $service) |
||
| 341 | |||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * @param string $identifier |
||
| 346 | * @return boolean |
||
| 347 | * @throws InvalidIdentifierException |
||
| 348 | */ |
||
| 349 | View Code Duplication | public function removeService($identifier) |
|
| 357 | |||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * Adds instructions on how to brew objects |
||
| 362 | * |
||
| 363 | * @param RecipeInterface $recipe |
||
| 364 | * @return mixed |
||
| 365 | * @throws InvalidIdentifierException |
||
| 366 | */ |
||
| 367 | public function addRecipe(RecipeInterface $recipe) |
||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $identifier The Recipe's identifier |
||
| 378 | * @return boolean |
||
| 379 | * @throws InvalidIdentifierException |
||
| 380 | */ |
||
| 381 | View Code Duplication | public function removeRecipe($identifier) |
|
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * Get instructions on how to brew objects |
||
| 394 | * |
||
| 395 | * @param string $identifier Identifier for the entity class that the recipe applies to |
||
| 396 | * Typically a Fully Qualified Class Name |
||
| 397 | * @param string $type |
||
| 398 | * @return RecipeInterface |
||
| 399 | * @throws OutOfBoundsException |
||
| 400 | * @throws InvalidIdentifierException |
||
| 401 | */ |
||
| 402 | public function getRecipe($identifier, $type = '') |
||
| 436 | |||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * adds class name aliases to list of filters |
||
| 441 | * |
||
| 442 | * @param string $identifier Identifier for the entity class that the alias applies to |
||
| 443 | * Typically a Fully Qualified Class Name |
||
| 444 | * @param array|string $aliases |
||
| 445 | * @return void |
||
| 446 | * @throws InvalidIdentifierException |
||
| 447 | */ |
||
| 448 | public function addAliases($identifier, $aliases) |
||
| 458 | |||
| 459 | |||
| 460 | |||
| 461 | /** |
||
| 462 | * Adds a service to one of the internal collections |
||
| 463 | * |
||
| 464 | * @param $identifier |
||
| 465 | * @param array $arguments |
||
| 466 | * @param string $type |
||
| 467 | * @return mixed |
||
| 468 | * @throws InvalidDataTypeException |
||
| 469 | * @throws InvalidClassException |
||
| 470 | * @throws OutOfBoundsException |
||
| 471 | * @throws InvalidIdentifierException |
||
| 472 | * @throws ServiceExistsException |
||
| 473 | */ |
||
| 474 | private function makeCoffee($identifier, $arguments = array(), $type = '') |
||
| 485 | |||
| 486 | |||
| 487 | |||
| 488 | /** |
||
| 489 | * filters alias identifiers to find the real class name |
||
| 490 | * |
||
| 491 | * @param string $identifier Identifier for the entity class that the filter applies to |
||
| 492 | * Typically a Fully Qualified Class Name |
||
| 493 | * @return string |
||
| 494 | * @throws InvalidIdentifierException |
||
| 495 | */ |
||
| 496 | private function filterIdentifier($identifier) |
||
| 503 | |||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * verifies and standardizes identifiers |
||
| 508 | * |
||
| 509 | * @param string $identifier Identifier for the entity class |
||
| 510 | * Typically a Fully Qualified Class Name |
||
| 511 | * @return string |
||
| 512 | * @throws InvalidIdentifierException |
||
| 513 | */ |
||
| 514 | View Code Duplication | private function processIdentifier($identifier) |
|
| 524 | |||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * @param string $type |
||
| 529 | * @return CoffeeMakerInterface |
||
| 530 | * @throws OutOfBoundsException |
||
| 531 | * @throws InvalidDataTypeException |
||
| 532 | * @throws InvalidClassException |
||
| 533 | */ |
||
| 534 | private function getCoffeeMaker($type) |
||
| 543 | |||
| 544 | |||
| 545 | |||
| 546 | /** |
||
| 547 | * Retrieves all recipes that use a wildcard "*" in their identifier |
||
| 548 | * This allows recipes to be set up for handling |
||
| 549 | * legacy classes that do not support PSR-4 autoloading. |
||
| 550 | * for example: |
||
| 551 | * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
||
| 552 | * |
||
| 553 | * @return array |
||
| 554 | */ |
||
| 555 | private function getDefaultRecipes() |
||
| 570 | |||
| 571 | |||
| 572 | |||
| 573 | /** |
||
| 574 | * clones a default recipe and then copies details |
||
| 575 | * from the incoming request to it so that it can be used |
||
| 576 | * |
||
| 577 | * @param RecipeInterface $default_recipe |
||
| 578 | * @param string $identifier |
||
| 579 | * @param string $type |
||
| 580 | * @return RecipeInterface |
||
| 581 | */ |
||
| 582 | private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
||
| 604 | |||
| 605 | |||
| 606 | |||
| 607 | /** |
||
| 608 | * @param string $identifier Identifier for the entity class that the service applies to |
||
| 609 | * Typically a Fully Qualified Class Name |
||
| 610 | * @param mixed $service |
||
| 611 | * @return mixed |
||
| 612 | * @throws InvalidServiceException |
||
| 613 | */ |
||
| 614 | private function validateService($identifier, $service) |
||
| 624 | |||
| 625 | } |
||
| 626 | // End of file CoffeeShop.php |
||
| 627 | // Location: /CoffeeShop.php |