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 |
||
24 | class Container implements ContainerInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var callable[] |
||
28 | */ |
||
29 | private $serviceFactory = []; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | private $services = []; |
||
35 | |||
36 | /** |
||
37 | * @var ContainerInterface |
||
38 | */ |
||
39 | private $delegateContainer; |
||
40 | |||
41 | /** |
||
42 | * @var DefinitionSourceInterface |
||
43 | */ |
||
44 | private $definitionSource; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $aliasDefinitions = []; |
||
50 | |||
51 | /** |
||
52 | * @var bool |
||
53 | */ |
||
54 | private $useServiceFactory = false; |
||
55 | |||
56 | /** |
||
57 | * Container constructor. |
||
58 | * |
||
59 | * @param DefinitionSourceInterface $definitionSource |
||
60 | * @param null|ContainerInterface $delegateContainer |
||
61 | */ |
||
62 | public function __construct( |
||
71 | |||
72 | /** |
||
73 | * Finds an entry of the container by its identifier and returns it. |
||
74 | * |
||
75 | * @param string $name Identifier of the entry to look for. |
||
76 | * |
||
77 | * @throws NotFoundException No entry was found for this identifier. |
||
78 | * @throws ContainerException Error while retrieving the entry. |
||
79 | * |
||
80 | * @return mixed Entry. |
||
81 | */ |
||
82 | public function get($name) |
||
115 | |||
116 | /** |
||
117 | * Returns true if the container can return an entry for the given identifier. |
||
118 | * Returns false otherwise. |
||
119 | * |
||
120 | * @param string $name Identifier of the entry to look for. |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function has($name) |
||
141 | |||
142 | /** |
||
143 | * Define an object in the container. |
||
144 | * |
||
145 | * @param string $name Entry name |
||
146 | * @param mixed $value Value |
||
147 | */ |
||
148 | public function set($name, $value) |
||
164 | |||
165 | /** |
||
166 | * @param string $name |
||
167 | * |
||
168 | * @return mixed |
||
169 | */ |
||
170 | private function getAlias($name) |
||
178 | |||
179 | /** |
||
180 | * @param string $name |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | private function hasAlias($name) |
||
188 | |||
189 | /** |
||
190 | * @param string $name |
||
191 | */ |
||
192 | private function setAlias($name) |
||
199 | |||
200 | /** |
||
201 | * Get service from Closure object. |
||
202 | * |
||
203 | * @param string $name |
||
204 | * |
||
205 | * @return mixed |
||
206 | */ |
||
207 | private function getServiceFromFactory($name) |
||
214 | |||
215 | private function hasServiceInFactory($name) |
||
219 | } |
||
220 |
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.