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 DependencyInjection 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 DependencyInjection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class DependencyInjection |
||
14 | { |
||
15 | /** |
||
16 | * @var ContainerInterface |
||
17 | */ |
||
18 | protected $containerInterface; |
||
19 | |||
20 | protected $class; |
||
21 | |||
22 | protected $args = []; |
||
23 | |||
24 | protected $instance; |
||
25 | |||
26 | protected $singleton = false; |
||
27 | |||
28 | protected $factory = null; |
||
29 | |||
30 | protected $methodCall = []; |
||
31 | |||
32 | /** |
||
33 | * @param $containerInterface ContainerInterface |
||
34 | * @return DependencyInjection |
||
35 | */ |
||
36 | public function injectContainer($containerInterface) |
||
41 | |||
42 | /** |
||
43 | * @return mixed |
||
44 | */ |
||
45 | protected function getClass() |
||
49 | |||
50 | /** |
||
51 | * @param mixed $class |
||
52 | * @throws DependencyInjectionException |
||
53 | */ |
||
54 | protected function setClass($class) |
||
61 | |||
62 | /** |
||
63 | * @return mixed |
||
64 | */ |
||
65 | protected function getArgs() |
||
78 | |||
79 | /** |
||
80 | * @param mixed $args |
||
81 | * @return DependencyInjection |
||
82 | * @throws DependencyInjectionException |
||
83 | */ |
||
84 | View Code Duplication | public function withConstructorArgs($args) |
|
93 | |||
94 | /** |
||
95 | * @param mixed $args |
||
96 | * @return DependencyInjection |
||
97 | * @throws DependencyInjectionException |
||
98 | */ |
||
99 | View Code Duplication | public function withFactoryMethod($method, $args = []) |
|
110 | |||
111 | /** |
||
112 | * DependencyInjection constructor. |
||
113 | * @param $class |
||
114 | * @throws DependencyInjectionException |
||
115 | */ |
||
116 | protected function __construct($class) |
||
120 | |||
121 | /** |
||
122 | * @param $class |
||
123 | * @return DependencyInjection |
||
124 | * @throws DependencyInjectionException |
||
125 | */ |
||
126 | public static function bind($class) |
||
130 | |||
131 | /** |
||
132 | * @return DependencyInjection |
||
133 | * @throws DependencyInjectionException |
||
134 | * @throws ReflectionException |
||
135 | */ |
||
136 | public function withInjectedConstructor() |
||
155 | |||
156 | /** |
||
157 | * @return DependencyInjection |
||
158 | * @throws DependencyInjectionException |
||
159 | * @throws ReflectionException |
||
160 | */ |
||
161 | public function withInjectedLegacyConstructor() |
||
186 | |||
187 | /** |
||
188 | * @return DependencyInjection |
||
189 | */ |
||
190 | public function withNoConstructor() |
||
195 | |||
196 | public function withMethodCall($method, $args = []) |
||
201 | |||
202 | /** |
||
203 | * @return DependencyInjection |
||
204 | */ |
||
205 | public function toSingleton() |
||
210 | |||
211 | /** |
||
212 | * @return DependencyInjection |
||
213 | * @throws DependencyInjectionException |
||
214 | * @throws ReflectionException |
||
215 | */ |
||
216 | public function toEagerSingleton() |
||
222 | |||
223 | /** |
||
224 | * @return DependencyInjection |
||
225 | */ |
||
226 | public function toInstance() |
||
231 | |||
232 | /** |
||
233 | * @return object |
||
234 | * @throws DependencyInjectionException |
||
235 | * @throws ReflectionException |
||
236 | */ |
||
237 | public function getInstance() |
||
247 | |||
248 | /** |
||
249 | * @return object |
||
250 | * @throws ReflectionException |
||
251 | */ |
||
252 | protected function getInternalInstance() |
||
260 | |||
261 | /** |
||
262 | * @return object |
||
263 | * @throws ReflectionException |
||
264 | */ |
||
265 | protected function getNewInstance() |
||
279 | |||
280 | /** |
||
281 | * @param $instance |
||
282 | * @return mixed |
||
283 | */ |
||
284 | protected function callMethods($instance) |
||
296 | |||
297 | /** |
||
298 | * @return object |
||
299 | * @throws ReflectionException |
||
300 | */ |
||
301 | protected function getSingletonInstace() |
||
308 | } |
||
309 |
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.