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 |
||
| 31 | class CoffeeShop implements CoffeePotInterface |
||
| 32 | { |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * This was the best coffee related name I could think of to represent class name "aliases" |
||
| 37 | * So classes can be found via an alias identifier, |
||
| 38 | * that is revealed when it is run through... the filters... eh? get it? |
||
| 39 | * |
||
| 40 | * @var array $filters |
||
| 41 | */ |
||
| 42 | private $filters = array(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * These are the classes that will actually build the objects (to order of course) |
||
| 46 | * |
||
| 47 | * @var array $coffee_makers |
||
| 48 | */ |
||
| 49 | private $coffee_makers = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * where the instantiated "singleton" objects are stored |
||
| 53 | * |
||
| 54 | * @var CollectionInterface $carafe |
||
| 55 | */ |
||
| 56 | private $carafe; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * collection of Recipes that instruct us how to brew objects |
||
| 60 | * |
||
| 61 | * @var CollectionInterface $recipes |
||
| 62 | */ |
||
| 63 | private $recipes; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * collection of closures for brewing objects |
||
| 67 | * |
||
| 68 | * @var CollectionInterface $reservoir |
||
| 69 | */ |
||
| 70 | private $reservoir; |
||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * CoffeeShop constructor |
||
| 76 | */ |
||
| 77 | public function __construct() |
||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns true if the container can return an entry for the given identifier. |
||
| 95 | * Returns false otherwise. |
||
| 96 | * `has($identifier)` returning true does not mean that `get($identifier)` will not throw an exception. |
||
| 97 | * It does however mean that `get($identifier)` will not throw a `ServiceNotFoundException`. |
||
| 98 | * |
||
| 99 | * @param string $identifier Identifier of the entry to look for. |
||
| 100 | * Typically a Fully Qualified Class Name |
||
| 101 | * @return boolean |
||
| 102 | */ |
||
| 103 | public function has($identifier) |
||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * finds a previously brewed (SHARED) service and returns it |
||
| 113 | * |
||
| 114 | * @param string $identifier Identifier for the entity class to be constructed. |
||
| 115 | * Typically a Fully Qualified Class Name |
||
| 116 | * @return mixed |
||
| 117 | * @throws ServiceNotFoundException No service was found for this identifier. |
||
| 118 | */ |
||
| 119 | public function get($identifier) |
||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * returns an instance of the requested entity type using the supplied arguments. |
||
| 132 | * If a shared service is requested and an instance is already in the carafe, then it will be returned. |
||
| 133 | * If it is not already in the carafe, then the service will be constructed, added to the carafe, and returned |
||
| 134 | * If the request is for a new entity and a closure exists in the reservoir for creating it, |
||
| 135 | * then a new entity will be instantiated from the closure and returned. |
||
| 136 | * If a closure does not exist, then one will be built and added to the reservoir |
||
| 137 | * before instantiating the requested entity. |
||
| 138 | * |
||
| 139 | * @param string $identifier Identifier for the entity class to be constructed. |
||
| 140 | * Typically a Fully Qualified Class Name |
||
| 141 | * @param array $arguments an array of arguments to be passed to the entity constructor |
||
| 142 | * @param string $type |
||
| 143 | * @return mixed |
||
| 144 | * @throws ServiceNotFoundException No service was found for this identifier. |
||
| 145 | */ |
||
| 146 | public function brew($identifier, $arguments = array(), $type = '') |
||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * @param CoffeeMakerInterface $coffee_maker |
||
| 190 | * @param string $type |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function addCoffeeMaker(CoffeeMakerInterface $coffee_maker, $type) |
||
| 198 | |||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string $identifier |
||
| 203 | * @param callable $closure |
||
| 204 | * @return callable|null |
||
| 205 | */ |
||
| 206 | public function addClosure($identifier, $closure) |
||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $identifier Identifier for the entity class that the service applies to |
||
| 222 | * Typically a Fully Qualified Class Name |
||
| 223 | * @param mixed $service |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | public function addService($identifier, $service) |
||
| 232 | |||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * Adds instructions on how to brew objects |
||
| 237 | * |
||
| 238 | * @param RecipeInterface $recipe |
||
| 239 | * @return mixed |
||
| 240 | */ |
||
| 241 | public function addRecipe(RecipeInterface $recipe) |
||
| 247 | |||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * Get instructions on how to brew objects |
||
| 252 | * |
||
| 253 | * @param string $identifier Identifier for the entity class that the recipe applies to |
||
| 254 | * Typically a Fully Qualified Class Name |
||
| 255 | * @param string $type |
||
| 256 | * @return RecipeInterface |
||
| 257 | */ |
||
| 258 | public function getRecipe($identifier, $type = '') |
||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * adds class name aliases to list of filters |
||
| 292 | * |
||
| 293 | * @param string $identifier Identifier for the entity class that the alias applies to |
||
| 294 | * Typically a Fully Qualified Class Name |
||
| 295 | * @param array $aliases |
||
| 296 | * @return void |
||
| 297 | * @throws InvalidIdentifierException |
||
| 298 | */ |
||
| 299 | public function addAliases($identifier, $aliases) |
||
| 309 | |||
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Adds a service to one of the internal collections |
||
| 314 | * |
||
| 315 | * @param $identifier |
||
| 316 | * @param array $arguments |
||
| 317 | * @param string $type |
||
| 318 | * @return mixed |
||
| 319 | * @throws ServiceExistsException |
||
| 320 | */ |
||
| 321 | private function makeCoffee($identifier, $arguments = array(), $type = '') |
||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * filters alias identifiers to find the real class name |
||
| 337 | * |
||
| 338 | * @param string $identifier Identifier for the entity class that the filter applies to |
||
| 339 | * Typically a Fully Qualified Class Name |
||
| 340 | * @return string |
||
| 341 | * @throws InvalidIdentifierException |
||
| 342 | */ |
||
| 343 | private function filterIdentifier($identifier) |
||
| 350 | |||
| 351 | |||
| 352 | |||
| 353 | /** |
||
| 354 | * verifies and standardizes identifiers |
||
| 355 | * |
||
| 356 | * @param string $identifier Identifier for the entity class |
||
| 357 | * Typically a Fully Qualified Class Name |
||
| 358 | * @return string |
||
| 359 | * @throws InvalidIdentifierException |
||
| 360 | */ |
||
| 361 | View Code Duplication | private function processIdentifier($identifier) |
|
| 371 | |||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * @param string $type |
||
| 376 | * @return CoffeeMakerInterface |
||
| 377 | * @throws InvalidDataTypeException |
||
| 378 | * @throws InvalidClassException |
||
| 379 | */ |
||
| 380 | private function getCoffeeMaker($type) |
||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * Retrieves all recipes that use a wildcard "*" in their identifier |
||
| 394 | * This allows recipes to be set up for handling |
||
| 395 | * legacy classes that do not support PSR-4 autoloading. |
||
| 396 | * for example: |
||
| 397 | * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
||
| 398 | * |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | private function getDefaultRecipes() |
||
| 416 | |||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * clones a default recipe and then copies details |
||
| 421 | * from the incoming request to it so that it can be used |
||
| 422 | * |
||
| 423 | * @param RecipeInterface $default_recipe |
||
| 424 | * @param string $identifier |
||
| 425 | * @param string $type |
||
| 426 | * @return RecipeInterface |
||
| 427 | */ |
||
| 428 | private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
||
| 450 | |||
| 451 | |||
| 452 | |||
| 453 | /** |
||
| 454 | * @param string $identifier Identifier for the entity class that the service applies to |
||
| 455 | * Typically a Fully Qualified Class Name |
||
| 456 | * @param mixed $service |
||
| 457 | * @return object |
||
| 458 | * @throws InvalidServiceException |
||
| 459 | */ |
||
| 460 | private function validateService($identifier, $service) |
||
| 470 | |||
| 471 | } |
||
| 472 | // End of file CoffeeShop.php |
||
| 473 | // Location: /CoffeeShop.php |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..