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 Container 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 Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Container implements ContainerInterface |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * Holds all resolved or resolvable instances into the container. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | |||
34 | protected $collection; |
||
35 | |||
36 | /** |
||
37 | * Class specific defined dependencies. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | |||
42 | protected $dependencies; |
||
43 | |||
44 | /** |
||
45 | * Cache of classes inspector and resolver. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | |||
50 | protected $resolving; |
||
51 | |||
52 | /** |
||
53 | * Cache of classes dependencies in callbacks ready for resolution. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | |||
58 | protected $resolved; |
||
59 | |||
60 | /** |
||
61 | * Call a user function injecting the dependencies. |
||
62 | * |
||
63 | * @param string|Closure $function The function or the user function name. |
||
64 | * @param array $parameters The predefined dependencies. |
||
65 | * @param bool $force Specify if a new element must be given and the dependencies must have be recalculated. |
||
66 | * |
||
67 | * @return mixed |
||
68 | */ |
||
69 | |||
70 | 3 | public function call($function, array $parameters = [], bool $force = false) |
|
84 | |||
85 | /** |
||
86 | * Makes an element or class injecting automatically all the dependencies. |
||
87 | * |
||
88 | * @param string $abstract The class name or container element name to make. |
||
89 | * @param array $parameters Specific parameters definition. |
||
90 | * @param bool $force Specify if a new element must be given and the dependencies must have be recalculated. |
||
91 | * |
||
92 | * @throws ContainerException |
||
93 | * @return object|null |
||
94 | */ |
||
95 | |||
96 | 18 | public function make(string $abstract, array $parameters = [], bool $force = false) |
|
112 | |||
113 | /** |
||
114 | * Construct a class and all the dependencies using the reflection library of PHP. |
||
115 | * |
||
116 | * @param string $abstract The class name or container element name to make. |
||
117 | * @param bool $force Specify if a new element must be given and the dependencies must have be recalculated. |
||
118 | * |
||
119 | * @throws ReflectionException |
||
120 | * @return Closure |
||
121 | */ |
||
122 | |||
123 | 18 | protected function construct(string $abstract, bool $force) : Closure |
|
145 | |||
146 | /** |
||
147 | * Resolve all the given class reflected dependencies. |
||
148 | * |
||
149 | * @param string $abstract The class name or container element name to resolve dependencies. |
||
150 | * @param ReflectionParameter $dependency The class dependency to be resolved. |
||
151 | * @param bool $force Specify if the dependencies must be recalculated. |
||
152 | * |
||
153 | * @return Object |
||
154 | */ |
||
155 | |||
156 | 7 | protected function resolve(string $abstract, ReflectionParameter $dependency, bool $force) |
|
166 | |||
167 | /** |
||
168 | * Generate the dependencies callbacks to jump some conditions in every dependency creation. |
||
169 | * |
||
170 | * @param string $abstract The class name or container element name to resolve dependencies. |
||
171 | * @param ReflectionParameter $dependency The class dependency to be resolved. |
||
172 | * |
||
173 | * @throws ContainerException When a dependency cannot be solved. |
||
174 | * @return Closure |
||
175 | */ |
||
176 | |||
177 | 7 | protected function generate(string $abstract, ReflectionParameter $dependency) : Closure |
|
202 | |||
203 | /** |
||
204 | * Reset the container, removing all the elements, cache and options. |
||
205 | * |
||
206 | * @return self |
||
207 | */ |
||
208 | |||
209 | 1 | public function flush() : self |
|
218 | |||
219 | /** |
||
220 | * Finds an entry of the container by its identifier and returns it. |
||
221 | * |
||
222 | * @param string $abstract Identifier of the entry to look for. |
||
223 | * |
||
224 | * @throws NotFoundException No entry was found for this identifier. |
||
225 | * @throws ContainerException Error while retrieving the entry. |
||
226 | * |
||
227 | * @return mixed Entry. |
||
228 | */ |
||
229 | 6 | public function get($abstract) |
|
245 | |||
246 | /** |
||
247 | * Returns true if the container can return an entry for the given identifier. |
||
248 | * Returns false otherwise. |
||
249 | * |
||
250 | * `has($abstract)` returning true does not mean that `get($abstract)` will not throw an exception. |
||
251 | * It does however mean that `get($abstract)` will not throw a `NotFoundException`. |
||
252 | * |
||
253 | * @param string $abstract Identifier of the entry to look for. |
||
254 | * |
||
255 | * @return boolean |
||
256 | */ |
||
257 | |||
258 | 2 | public function has($abstract) |
|
262 | |||
263 | /** |
||
264 | * Verify if an element has a singleton instance. |
||
265 | * |
||
266 | * @param string The class name or container element name to resolve dependencies. |
||
267 | * @return bool |
||
268 | */ |
||
269 | |||
270 | 5 | public function isSingleton(string $abstract) : bool |
|
274 | |||
275 | /** |
||
276 | * Verify if an element is a instance of something. |
||
277 | * |
||
278 | * @param string The class name or container element name to resolve dependencies. |
||
279 | * @return bool |
||
280 | */ |
||
281 | public function isInstance(string $abstract) : bool |
||
285 | |||
286 | /** |
||
287 | * Bind a new element to the container. |
||
288 | * |
||
289 | * @param string $abstract The alias name that will be used to call the element. |
||
290 | * @param string|closure|object $concrete The element class name, or an closure that makes the element, or the object itself. |
||
291 | * @param bool $shared Define if the element will be a singleton instance. |
||
292 | * |
||
293 | * @return self |
||
294 | */ |
||
295 | |||
296 | 12 | public function set(string $abstract, $concrete, bool $shared = false) : self |
|
314 | |||
315 | /** |
||
316 | * Bind a new element to the container IF the element name not exists in the container. |
||
317 | * |
||
318 | * @param string $abstract The alias name that will be used to call the element. |
||
319 | * @param string|closure $concrete The element class name, or an closure that makes the element. |
||
320 | * @param bool $shared Define if the element will be a singleton instance. |
||
321 | * |
||
322 | * @return self |
||
323 | */ |
||
324 | |||
325 | 1 | public function setIf(string $abstract, $concrete, bool $shared = false) : self |
|
333 | |||
334 | /** |
||
335 | * Bind an specific instance to a class dependency. |
||
336 | * |
||
337 | * @param string $class The class full name. |
||
338 | * @param string $dependencyName The dependency full name. |
||
339 | * @param string|closure $dependency The specific object class name or a classure that makes the element. |
||
340 | * |
||
341 | * @return self |
||
342 | */ |
||
343 | |||
344 | 3 | public function setTo(string $class, string $dependencyName, $dependency) : self |
|
362 | |||
363 | /** |
||
364 | * Bind an element that will be construct only one time, and every call for the element, |
||
365 | * the same instance will be given. |
||
366 | * |
||
367 | * @param string $abstract The alias name that will be used to call the element. |
||
368 | * @param string|closure $concrete The element class name, or an closure that makes the element. |
||
369 | * |
||
370 | * @return self |
||
371 | */ |
||
372 | |||
373 | 3 | public function singleton(string $abstract, $concrete) : self |
|
379 | |||
380 | /** |
||
381 | * Bind an object to the container. |
||
382 | * |
||
383 | * @param string $abstract The alias name that will be used to call the object. |
||
384 | * @param object $instance The object that will be inserted. |
||
385 | * |
||
386 | * @throws ContainerException When $instance is not an object. |
||
387 | * @return self |
||
388 | */ |
||
389 | |||
390 | 2 | public function instance(string $abstract, $instance) : self |
|
400 | |||
401 | /** |
||
402 | * Modify an element with a given function that receive the old element as argument. |
||
403 | * |
||
404 | * @param string $abstract The alias name that will be used to call the element. |
||
405 | * @param closure $extension The function that receives the old element and return a new or modified one. |
||
406 | * |
||
407 | * @throws NotFoundException When no element was found with $abstract key. |
||
408 | * @return self |
||
409 | */ |
||
410 | |||
411 | 2 | public function extend(string $abstract, closure $extension) : self |
|
429 | |||
430 | /** |
||
431 | * Makes an resolvable element an singleton. |
||
432 | * |
||
433 | * @param string $abstract The alias name that will be used to call the element. |
||
434 | * |
||
435 | * @throws NotFoundException When no element was found with $abstract key. |
||
436 | * @throws ContainerException When the element on $abstract key is not resolvable. |
||
437 | * |
||
438 | * @return self |
||
439 | */ |
||
440 | |||
441 | 1 | public function share(string $abstract) : self |
|
455 | |||
456 | } |
||
457 |
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.