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 declare(strict_types=1); |
||
13 | class Container implements ContainerInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $dicValues = []; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $aliases = []; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $dicObjects = []; |
||
29 | |||
30 | /** |
||
31 | * Set a DIC value. |
||
32 | * |
||
33 | * Wrap objects provided in a closure for lazy loading. |
||
34 | * |
||
35 | * @param string $key |
||
36 | * @param mixed $value |
||
37 | * @return ContainerInterface |
||
38 | */ |
||
39 | 25 | public function set(string $key, $value): ContainerInterface |
|
50 | |||
51 | 1 | public function unset(string $key): void |
|
58 | |||
59 | 23 | public function alias(string $alias, string $key): ContainerInterface |
|
65 | |||
66 | 1 | public function unalias(string $alias): void |
|
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | 4 | public function has($key): bool |
|
82 | |||
83 | 4 | View Code Duplication | public function hasInstance(string $key): bool |
91 | |||
92 | /** |
||
93 | * @inheritdoc |
||
94 | */ |
||
95 | 36 | public function get($key) |
|
120 | |||
121 | /** |
||
122 | * Get a new instance of a DIC object |
||
123 | * |
||
124 | * @param string $key |
||
125 | * @return mixed |
||
126 | */ |
||
127 | 7 | public function getNew(string $key) |
|
142 | |||
143 | 25 | private function getValueInstance(string $key) |
|
155 | |||
156 | /** |
||
157 | * Destroy a DIC object instance. |
||
158 | * |
||
159 | * Will force a new object to be created on next call. |
||
160 | * |
||
161 | * @param string $key |
||
162 | */ |
||
163 | 3 | View Code Duplication | public function destroyInstance(string $key): void |
171 | |||
172 | 2 | public function destroyAllInstances(): void |
|
180 | |||
181 | /** |
||
182 | * Magic method to get or set DIC values. |
||
183 | * |
||
184 | * @param string $name |
||
185 | * @param array $arguments |
||
186 | * @return mixed |
||
187 | */ |
||
188 | 8 | public function __call(string $name, array $arguments) |
|
212 | |||
213 | /** |
||
214 | * Instantiate an object of named class, recursively resolving dependencies |
||
215 | * |
||
216 | * @param string $className Fully qualified class name |
||
217 | * @return mixed |
||
218 | * @throws \ReflectionException |
||
219 | */ |
||
220 | 16 | private function resolveInstance(string $className) |
|
237 | |||
238 | /** |
||
239 | * Recursively resolve function parameters using type hints |
||
240 | * |
||
241 | * @param \ReflectionParameter[] $parameters |
||
242 | * @param array $predefinedValues |
||
243 | * @return array |
||
244 | * @throws \ReflectionException |
||
245 | */ |
||
246 | 12 | public function resolveParameters(array $parameters, array $predefinedValues = []): array |
|
279 | } |
||
280 |
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.