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 AbstractFactory 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 AbstractFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | abstract class AbstractFactory implements FactoryInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var array $resolved |
||
28 | */ |
||
29 | protected $resolved = []; |
||
30 | |||
31 | /** |
||
32 | * If a base class is set, then it must be ensured that the |
||
33 | * @var string $baseClass |
||
34 | */ |
||
35 | private $baseClass = ''; |
||
36 | /** |
||
37 | * |
||
38 | * @var string $defaultClass |
||
39 | */ |
||
40 | private $defaultClass = ''; |
||
41 | |||
42 | /** |
||
43 | * @var array $arguments |
||
44 | */ |
||
45 | private $arguments; |
||
46 | |||
47 | /** |
||
48 | * @var callable $callback |
||
49 | */ |
||
50 | private $callback; |
||
51 | |||
52 | /** |
||
53 | * Keeps loaded instances in memory, in `[$type => $instance]` format. |
||
54 | * Used with the `get()` method only. |
||
55 | * @var array $instances |
||
56 | */ |
||
57 | private $instances = []; |
||
58 | |||
59 | /** |
||
60 | * @var callable $resolver |
||
61 | */ |
||
62 | private $resolver; |
||
63 | |||
64 | /** |
||
65 | * The class map array holds available types, in `[$type => $className]` format. |
||
66 | * @var string[] $map |
||
67 | */ |
||
68 | private $map = []; |
||
69 | |||
70 | /** |
||
71 | * @param array $data Constructor dependencies. |
||
72 | */ |
||
73 | public function __construct(array $data = null) |
||
102 | |||
103 | /** |
||
104 | * Create a new instance of a class, by type. |
||
105 | * |
||
106 | * Unlike `get()`, this method *always* return a new instance of the requested class. |
||
107 | * |
||
108 | * ## Object callback |
||
109 | * It is possible to pass a callback method that will be executed upon object instanciation. |
||
110 | * The callable should have a signature: `function($obj);` where $obj is the newly created object. |
||
111 | * |
||
112 | * @param string $type The type (class ident). |
||
113 | * @param array $args Optional. Constructor arguments |
||
114 | * (will override the arguments set on the class from constructor). |
||
115 | * @param callable $cb Optional. Object callback, called at creation. |
||
116 | * Will run in addition to the default callback, if any. |
||
117 | * @throws Exception If the base class is set and the resulting instance is not of the base class. |
||
118 | * @throws InvalidArgumentException If type argument is not a string or is not an available type. |
||
119 | * @return mixed The instance / object |
||
120 | */ |
||
121 | final public function create($type, array $args = null, callable $cb = null) |
||
182 | |||
183 | /** |
||
184 | * Get (load or create) an instance of a class, by type. |
||
185 | * |
||
186 | * Unlike `create()` (which always call a `new` instance), |
||
187 | * this function first tries to load / reuse |
||
188 | * an already created object of this type, from memory. |
||
189 | * |
||
190 | * @param string $type The type (class ident). |
||
191 | * @param array $args The constructor arguments (optional). |
||
192 | * @throws InvalidArgumentException If type argument is not a string. |
||
193 | * @return mixed The instance / object |
||
194 | */ |
||
195 | final public function get($type, array $args = null) |
||
207 | |||
208 | |||
209 | /** |
||
210 | * If a base class is set, then it must be ensured that the created objects |
||
211 | * are `instanceof` this base class. |
||
212 | * |
||
213 | * @param string $type The FQN of the class, or "type" of object, to set as base class. |
||
214 | * @throws InvalidArgumentException If the class is not a string or is not an existing class / interface. |
||
215 | * @return self |
||
216 | */ |
||
217 | public function setBaseClass($type) |
||
243 | |||
244 | /** |
||
245 | * @return string The FQN of the base class |
||
246 | */ |
||
247 | public function baseClass() |
||
251 | |||
252 | /** |
||
253 | * If a default class is set, then calling `get()` or `create()` an invalid type |
||
254 | * should return an object of this class instead of throwing an error. |
||
255 | * |
||
256 | * @param string $type The FQN of the class, or "type" of object, to set as default class. |
||
257 | * @throws InvalidArgumentException If the class name is not a string or not a valid class. |
||
258 | * @return self |
||
259 | */ |
||
260 | public function setDefaultClass($type) |
||
284 | |||
285 | /** |
||
286 | * @return string The FQN of the default class |
||
287 | */ |
||
288 | public function defaultClass() |
||
292 | |||
293 | /** |
||
294 | * @param array $arguments The constructor arguments to be passed to the created object's initialization. |
||
295 | * @return self |
||
296 | */ |
||
297 | public function setArguments(array $arguments) |
||
302 | |||
303 | /** |
||
304 | * @return array |
||
305 | */ |
||
306 | public function arguments() |
||
310 | |||
311 | /** |
||
312 | * @param callable $callback The object callback. |
||
313 | * @return self |
||
314 | */ |
||
315 | public function setCallback(callable $callback) |
||
320 | |||
321 | /** |
||
322 | * @return callable|null |
||
323 | */ |
||
324 | public function callback() |
||
328 | |||
329 | /** |
||
330 | * The Generic factory resolves the class name from an exact FQN. |
||
331 | * |
||
332 | * @param string $type The "type" of object to resolve (the object ident). |
||
333 | * @throws InvalidArgumentException If the type parameter is not a string. |
||
334 | * @return string The resolved class name (FQN). |
||
335 | */ |
||
336 | public function resolve($type) |
||
357 | |||
358 | /** |
||
359 | * Whether a `type` is resolvable. The Generic Factory simply checks if the _FQN_ `type` class exists. |
||
360 | * |
||
361 | * @param string $type The "type" of object to resolve (the object ident). |
||
362 | * @throws InvalidArgumentException If the type parameter is not a string. |
||
363 | * @return boolean |
||
364 | */ |
||
365 | public function isResolvable($type) |
||
390 | |||
391 | |||
392 | /** |
||
393 | * Create a class instance with given arguments. |
||
394 | * |
||
395 | * How the constructor arguments are passed depends on its type: |
||
396 | * |
||
397 | * - if null, no arguments are passed at all. |
||
398 | * - if it's not an array, it's passed as a single argument. |
||
399 | * - if it's an associative array, it's passed as a sing argument. |
||
400 | * - if it's a sequential (numeric keys) array, it's |
||
401 | * |
||
402 | * @param string $className The FQN of the class to instanciate. |
||
403 | * @param mixed $args The constructor arguments. |
||
404 | * @return mixed The created object. |
||
405 | */ |
||
406 | protected function createClass($className, $args) |
||
425 | |||
426 | /** |
||
427 | * @return callable |
||
428 | */ |
||
429 | protected function resolver() |
||
433 | |||
434 | /** |
||
435 | * Get the map of all types in `[$type => $class]` format. |
||
436 | * |
||
437 | * @return string[] |
||
438 | */ |
||
439 | protected function map() |
||
443 | |||
444 | /** |
||
445 | * Add a class name to the available types _map_. |
||
446 | * |
||
447 | * @param string $type The type (class ident). |
||
448 | * @param string $className The FQN of the class. |
||
449 | * @throws InvalidArgumentException If the $type parameter is not a striing or the $className class does not exist. |
||
450 | * @return self |
||
451 | */ |
||
452 | protected function addClassToMap($type, $className) |
||
463 | |||
464 | /** |
||
465 | * @param callable $resolver The class resolver instance to use. |
||
466 | * @return self |
||
467 | */ |
||
468 | private function setResolver(callable $resolver) |
||
473 | |||
474 | /** |
||
475 | * Add multiple types, in a an array of `type` => `className`. |
||
476 | * |
||
477 | * @param string[] $map The map (key=>classname) to use. |
||
478 | * @return self |
||
479 | */ |
||
480 | private function setMap(array $map) |
||
489 | |||
490 | /** |
||
491 | * Run the callback(s) on the object, if applicable. |
||
492 | * |
||
493 | * @param mixed $obj The object to pass to callback(s). |
||
494 | * @param callable $customCallback An optional additional custom callback. |
||
495 | * @return void |
||
496 | */ |
||
497 | private function runCallbacks(&$obj, callable $customCallback = null) |
||
507 | } |
||
508 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.