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 |
||
18 | class Container implements ContainerInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $dicValues = []; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $aliases = []; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $dicObjects = []; |
||
34 | |||
35 | /** |
||
36 | * Set a DIC value. |
||
37 | * |
||
38 | * Wrap objects provided in a closure for lazy loading. |
||
39 | * |
||
40 | * @param string $key |
||
41 | * @param mixed $value |
||
42 | * @return Container |
||
43 | */ |
||
44 | 21 | public function set(string $key, $value): self |
|
55 | |||
56 | public function unset(string $key): void |
||
63 | |||
64 | 21 | public function alias(string $alias, string $key): self |
|
70 | |||
71 | public function unalias(string $alias): void |
||
75 | |||
76 | /** |
||
77 | * Check if a DIC value/object exists. |
||
78 | * |
||
79 | * @param string $key |
||
80 | * @return bool |
||
81 | */ |
||
82 | 1 | public function has($key): bool |
|
90 | |||
91 | /** |
||
92 | * @param string $key |
||
93 | * @return bool |
||
94 | */ |
||
95 | 2 | View Code Duplication | public function hasInstance($key): bool |
|
|||
96 | { |
||
97 | 2 | if (isset($this->aliases[$key])) { |
|
98 | $key = $this->aliases[$key]; |
||
99 | } |
||
100 | |||
101 | 2 | return isset($this->dicObjects[$key]); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get the shared instance of a DIC object |
||
106 | * |
||
107 | * @param string $key |
||
108 | * @return mixed |
||
109 | */ |
||
110 | 21 | public function get($key) |
|
135 | |||
136 | /** |
||
137 | * Get a new instance of a DIC object |
||
138 | * |
||
139 | * @param string $key |
||
140 | * @return mixed |
||
141 | */ |
||
142 | 3 | public function getNew(string $key) |
|
157 | |||
158 | 21 | private function getValueInstance(string $key) |
|
170 | |||
171 | /** |
||
172 | * Destroy a DIC object instance. |
||
173 | * |
||
174 | * Will force a new object to be created on next call. |
||
175 | * |
||
176 | * @param string $key |
||
177 | */ |
||
178 | 1 | View Code Duplication | public function destroyInstance($key): void |
186 | |||
187 | /** |
||
188 | * Destroy all DIC object instances. |
||
189 | * |
||
190 | * Will force new objects to be created on next call. |
||
191 | */ |
||
192 | 1 | public function destroyAllInstances(): void |
|
200 | |||
201 | /** |
||
202 | * Magic method to get or set DIC values. |
||
203 | * |
||
204 | * @param string $name |
||
205 | * @param array $arguments |
||
206 | * @return mixed |
||
207 | */ |
||
208 | 3 | public function __call($name, $arguments) |
|
232 | |||
233 | /** |
||
234 | * Instantiate an object of named class, recursively resolving dependencies |
||
235 | * |
||
236 | * @param string $className Fully qualified class name |
||
237 | * @return mixed |
||
238 | * @throws \ReflectionException |
||
239 | */ |
||
240 | 3 | public function resolveInstance(string $className) |
|
257 | |||
258 | /** |
||
259 | * Recursively resolve function parameters using type hints |
||
260 | * |
||
261 | * @param \ReflectionParameter[] |
||
262 | * @return mixed |
||
263 | */ |
||
264 | private function resolveParameters(array $parameters): array |
||
286 | } |
||
287 |
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.