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:
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 | */ |
||
214 | public function toInstance() |
||
219 | |||
220 | /** |
||
221 | * @return object |
||
222 | * @throws DependencyInjectionException |
||
223 | * @throws ReflectionException |
||
224 | */ |
||
225 | public function getInstance() |
||
235 | |||
236 | /** |
||
237 | * @return object |
||
238 | * @throws ReflectionException |
||
239 | */ |
||
240 | protected function getInternalInstance() |
||
248 | |||
249 | /** |
||
250 | * @return object |
||
251 | * @throws ReflectionException |
||
252 | */ |
||
253 | protected function getNewInstance() |
||
267 | |||
268 | /** |
||
269 | * @param $instance |
||
270 | * @return mixed |
||
271 | */ |
||
272 | protected function callMethods($instance) |
||
284 | |||
285 | /** |
||
286 | * @return object |
||
287 | * @throws ReflectionException |
||
288 | */ |
||
289 | protected function getSingletonInstace() |
||
296 | } |
||
297 |
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.