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 |
||
28 | abstract class AbstractFactory implements FactoryInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var array $resolved |
||
32 | */ |
||
33 | static protected $resolved = []; |
||
34 | |||
35 | /** |
||
36 | * If a base class is set, then it must be ensured that the |
||
37 | * @var string $baseClass |
||
38 | */ |
||
39 | private $baseClass = ''; |
||
40 | /** |
||
41 | * |
||
42 | * @var string $defaultClass |
||
43 | */ |
||
44 | private $defaultClass = ''; |
||
45 | |||
46 | /** |
||
47 | * @var array $arguments |
||
48 | */ |
||
49 | private $arguments; |
||
50 | |||
51 | /** |
||
52 | * @var callable $callback |
||
53 | */ |
||
54 | private $callback; |
||
55 | |||
56 | /** |
||
57 | * Keeps loaded instances in memory, in `[$type => $instance]` format. |
||
58 | * Used with the `get()` method only. |
||
59 | * @var array $instances |
||
60 | */ |
||
61 | private $instances = []; |
||
62 | |||
63 | /** |
||
64 | * @var Callable $resolver |
||
65 | */ |
||
66 | private $resolver; |
||
67 | |||
68 | /** |
||
69 | * The class map array holds available types, in `[$type => $className]` format. |
||
70 | * @var string[] $map |
||
71 | */ |
||
72 | private $map = []; |
||
73 | |||
74 | /** |
||
75 | * @param array $data Constructor dependencies. |
||
76 | */ |
||
77 | 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 | * |
||
113 | * @param string $type The type (class ident). |
||
114 | * @param array $args Optional. Constructor arguments (will override the arguments set on the class from constructor). |
||
115 | * @param callable $cb Optional. Object callback, called at creation. Leave blank to use `$callback` member. |
||
116 | * @throws Exception If the base class is set and the resulting instance is not of the base class. |
||
117 | * @throws InvalidArgumentException If type argument is not a string or is not an available type. |
||
118 | * @return mixed The instance / object |
||
119 | */ |
||
120 | final public function create($type, array $args = null, callable $cb = null) |
||
189 | |||
190 | /** |
||
191 | * Create a class instance with given arguments. |
||
192 | * |
||
193 | * How the constructor arguments are passed depends on its type: |
||
194 | * |
||
195 | * - if null, no arguments are passed at all. |
||
196 | * - if it's not an array, it's passed as a single argument. |
||
197 | * - if it's an associative array, it's passed as a sing argument. |
||
198 | * - if it's a sequential (numeric keys) array, it's |
||
199 | * |
||
200 | * @param string $classname The FQN of the class to instanciate. |
||
201 | * @param mixed $args The constructor arguments. |
||
202 | * @return mixed The created object. |
||
203 | */ |
||
204 | protected function createClass($classname, $args) |
||
220 | |||
221 | /** |
||
222 | * Get (load or create) an instance of a class, by type. |
||
223 | * |
||
224 | * Unlike `create()` (which always call a `new` instance), this function first tries to load / reuse |
||
225 | * an already created object of this type, from memory. |
||
226 | * |
||
227 | * @param string $type The type (class ident). |
||
228 | * @param array $args The constructor arguments (optional). |
||
229 | * @throws InvalidArgumentException If type argument is not a string. |
||
230 | * @return mixed The instance / object |
||
231 | */ |
||
232 | final public function get($type, array $args = null) |
||
244 | |||
245 | /** |
||
246 | * @param callable $resolver The class resolver instance to use. |
||
247 | * @return FactoryInterface Chainable |
||
248 | */ |
||
249 | private function setResolver(callable $resolver) |
||
254 | |||
255 | /** |
||
256 | * @return callable |
||
257 | */ |
||
258 | protected function resolver() |
||
262 | |||
263 | /** |
||
264 | * Add multiple types, in a an array of `type` => `className`. |
||
265 | * |
||
266 | * @param string[] $map The map (key=>classname) to use. |
||
267 | * @return FactoryInterface Chainable |
||
268 | */ |
||
269 | private function setMap(array $map) |
||
278 | |||
279 | /** |
||
280 | * Get the map of all types in `[$type => $class]` format. |
||
281 | * |
||
282 | * @return string[] |
||
283 | */ |
||
284 | protected function map() |
||
288 | |||
289 | /** |
||
290 | * Add a class name to the available types _map_. |
||
291 | * |
||
292 | * @param string $type The type (class ident). |
||
293 | * @param string $className The FQN of the class. |
||
294 | * @throws InvalidArgumentException If the $type parameter is not a striing or the $className class does not exist. |
||
295 | * @return FactoryInterface Chainable |
||
296 | */ |
||
297 | protected function addClassToMap($type, $className) |
||
308 | |||
309 | /** |
||
310 | * If a base class is set, then it must be ensured that the created objects |
||
311 | * are `instanceof` this base class. |
||
312 | * |
||
313 | * @param string $type The FQN of the class, or "type" of object, to set as base class. |
||
314 | * @throws InvalidArgumentException If the class is not a string or is not an existing class / interface. |
||
315 | * @return FactoryInterface Chainable |
||
316 | */ |
||
317 | public function setBaseClass($type) |
||
343 | |||
344 | /** |
||
345 | * @return string The FQN of the base class |
||
346 | */ |
||
347 | public function baseClass() |
||
351 | |||
352 | /** |
||
353 | * If a default class is set, then calling `get()` or `create()` an invalid type |
||
354 | * should return an object of this class instead of throwing an error. |
||
355 | * |
||
356 | * @param string $type The FQN of the class, or "type" of object, to set as default class. |
||
357 | * @throws InvalidArgumentException If the class name is not a string or not a valid class. |
||
358 | * @return FactoryInterface Chainable |
||
359 | */ |
||
360 | public function setDefaultClass($type) |
||
384 | |||
385 | /** |
||
386 | * @return string The FQN of the default class |
||
387 | */ |
||
388 | public function defaultClass() |
||
392 | |||
393 | /** |
||
394 | * @param array $arguments The constructor arguments to be passed to the created object's initialization. |
||
395 | * @return FactoryInterface Chainable |
||
396 | */ |
||
397 | public function setArguments(array $arguments) |
||
402 | |||
403 | /** |
||
404 | * @return array |
||
405 | */ |
||
406 | public function arguments() |
||
410 | |||
411 | /** |
||
412 | * @param callable $callback The object callback. |
||
413 | * @return FactoryInterface Chainable |
||
414 | */ |
||
415 | public function setCallback(callable $callback) |
||
420 | |||
421 | /** |
||
422 | * @return callable|null |
||
423 | */ |
||
424 | public function callback() |
||
428 | |||
429 | /** |
||
430 | * The Generic factory resolves the class name from an exact FQN. |
||
431 | * |
||
432 | * @param string $type The "type" of object to resolve (the object ident). |
||
433 | * @throws InvalidArgumentException If the type parameter is not a string. |
||
434 | * @return string The resolved class name (FQN). |
||
435 | */ |
||
436 | public function resolve($type) |
||
457 | |||
458 | /** |
||
459 | * Wether a `type` is resolvable. The Generic Factory simply checks if the _FQN_ `type` class exists. |
||
460 | * |
||
461 | * @param string $type The "type" of object to resolve (the object ident). |
||
462 | * @throws InvalidArgumentException If the type parameter is not a string. |
||
463 | * @return boolean |
||
464 | */ |
||
465 | public function isResolvable($type) |
||
490 | } |
||
491 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.