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 |
||
222 | * @return boolean |
||
223 | */ |
||
224 | View Code Duplication | public function removeClosure($identifier) |
|
225 | { |
||
226 | $identifier = $this->processIdentifier($identifier); |
||
227 | if ($this->reservoir->has($identifier)) { |
||
228 | $this->reservoir->remove($this->reservoir->get($identifier)); |
||
229 | if ( ! $this->reservoir->has($identifier)) { |
||
230 | return true; |
||
231 | } |
||
232 | } |
||
233 | return false; |
||
234 | } |
||
235 | |||
236 | |||
237 | |||
238 | /** |
||
239 | * @param string $identifier Identifier for the entity class that the service applies to |
||
240 | * Typically a Fully Qualified Class Name |
||
241 | * @param mixed $service |
||
242 | * @return bool |
||
243 | */ |
||
244 | public function addService($identifier, $service) |
||
250 | |||
251 | |||
252 | |||
253 | /** |
||
254 | * @param string $identifier |
||
255 | * @return boolean |
||
256 | */ |
||
257 | View Code Duplication | public function removeService($identifier) |
|
258 | { |
||
259 | $identifier = $this->processIdentifier($identifier); |
||
260 | if ($this->carafe->has($identifier)) { |
||
261 | $this->carafe->remove($this->carafe->get($identifier)); |
||
262 | if ( ! $this->carafe->has($identifier)) { |
||
263 | return true; |
||
264 | } |
||
265 | } |
||
266 | return false; |
||
267 | } |
||
268 | |||
269 | |||
270 | |||
271 | /** |
||
272 | * Adds instructions on how to brew objects |
||
273 | * |
||
274 | * @param RecipeInterface $recipe |
||
275 | * @return mixed |
||
276 | */ |
||
277 | public function addRecipe(RecipeInterface $recipe) |
||
283 | |||
284 | |||
285 | |||
286 | /** |
||
287 | * @param string $identifier The Recipe's identifier |
||
288 | * @return boolean |
||
289 | */ |
||
290 | View Code Duplication | public function removeRecipe($identifier) |
|
291 | { |
||
292 | $identifier = $this->processIdentifier($identifier); |
||
293 | if ($this->recipes->has($identifier)) { |
||
294 | $this->recipes->remove( |
||
295 | $this->recipes->get($identifier) |
||
296 | ); |
||
297 | if ( ! $this->recipes->has($identifier)) { |
||
298 | return true; |
||
299 | } |
||
300 | } |
||
301 | return false; |
||
302 | } |
||
303 | |||
304 | |||
305 | |||
306 | /** |
||
307 | * Get instructions on how to brew objects |
||
308 | * |
||
309 | * @param string $identifier Identifier for the entity class that the recipe applies to |
||
310 | * Typically a Fully Qualified Class Name |
||
311 | * @param string $type |
||
312 | * @return RecipeInterface |
||
313 | */ |
||
314 | public function getRecipe($identifier, $type = '') |
||
348 | |||
349 | |||
350 | |||
351 | /** |
||
352 | * adds class name aliases to list of filters |
||
353 | * |
||
354 | * @param string $identifier Identifier for the entity class that the alias applies to |
||
355 | * Typically a Fully Qualified Class Name |
||
356 | * @param array $aliases |
||
357 | * @return void |
||
358 | * @throws InvalidIdentifierException |
||
359 | */ |
||
360 | public function addAliases($identifier, $aliases) |
||
370 | |||
371 | |||
372 | |||
373 | /** |
||
374 | * Adds a service to one of the internal collections |
||
375 | * |
||
376 | * @param $identifier |
||
377 | * @param array $arguments |
||
378 | * @param string $type |
||
379 | * @return mixed |
||
380 | * @throws ServiceExistsException |
||
381 | */ |
||
382 | private function makeCoffee($identifier, $arguments = array(), $type = '') |
||
393 | |||
394 | |||
395 | |||
396 | /** |
||
397 | * filters alias identifiers to find the real class name |
||
398 | * |
||
399 | * @param string $identifier Identifier for the entity class that the filter applies to |
||
400 | * Typically a Fully Qualified Class Name |
||
401 | * @return string |
||
402 | * @throws InvalidIdentifierException |
||
403 | */ |
||
404 | private function filterIdentifier($identifier) |
||
411 | |||
412 | |||
413 | |||
414 | /** |
||
415 | * verifies and standardizes identifiers |
||
416 | * |
||
417 | * @param string $identifier Identifier for the entity class |
||
418 | * Typically a Fully Qualified Class Name |
||
419 | * @return string |
||
420 | * @throws InvalidIdentifierException |
||
421 | */ |
||
422 | View Code Duplication | private function processIdentifier($identifier) |
|
432 | |||
433 | |||
434 | |||
435 | /** |
||
436 | * @param string $type |
||
437 | * @return CoffeeMakerInterface |
||
438 | * @throws InvalidDataTypeException |
||
439 | * @throws InvalidClassException |
||
440 | */ |
||
441 | private function getCoffeeMaker($type) |
||
450 | |||
451 | |||
452 | |||
453 | /** |
||
454 | * Retrieves all recipes that use a wildcard "*" in their identifier |
||
455 | * This allows recipes to be set up for handling |
||
456 | * legacy classes that do not support PSR-4 autoloading. |
||
457 | * for example: |
||
458 | * using "EEM_*" for a recipe identifier would target all legacy models like EEM_Attendee |
||
459 | * |
||
460 | * @return array |
||
461 | */ |
||
462 | private function getDefaultRecipes() |
||
477 | |||
478 | |||
479 | |||
480 | /** |
||
481 | * clones a default recipe and then copies details |
||
482 | * from the incoming request to it so that it can be used |
||
483 | * |
||
484 | * @param RecipeInterface $default_recipe |
||
485 | * @param string $identifier |
||
486 | * @param string $type |
||
487 | * @return RecipeInterface |
||
488 | */ |
||
489 | private function copyDefaultRecipe(RecipeInterface $default_recipe, $identifier, $type = '') |
||
511 | |||
512 | |||
513 | |||
514 | /** |
||
515 | * @param string $identifier Identifier for the entity class that the service applies to |
||
516 | * Typically a Fully Qualified Class Name |
||
517 | * @param mixed $service |
||
518 | * @return object |
||
519 | * @throws InvalidServiceException |
||
520 | */ |
||
521 | private function validateService($identifier, $service) |
||
531 | |||
532 | } |
||
533 | // End of file CoffeeShop.php |
||
534 | // 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..