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 |
||
10 | class LoadedCollector extends DataCollector |
||
11 | { |
||
12 | /** |
||
13 | * I know we should try to not have Container sa dependency |
||
14 | * But i need it to get fresh data in collect() |
||
15 | * I need Container, not ContainerInterface |
||
16 | * @var Container |
||
17 | */ |
||
18 | protected $container; |
||
19 | |||
20 | public function __construct(Container $container) |
||
24 | |||
25 | /** @return string */ |
||
26 | public function getName() |
||
30 | |||
31 | public function collect(Request $request, Response $response, \Exception $exception = null) |
||
45 | |||
46 | /** @return array */ |
||
47 | public function getDeclaredClasses() |
||
57 | |||
58 | /** @return int */ |
||
59 | public function countDeclaredClasses() |
||
63 | |||
64 | /** @return array */ |
||
65 | public function getDeclaredInterfaces() |
||
75 | |||
76 | /** @return int */ |
||
77 | public function countDeclaredInterfaces() |
||
81 | |||
82 | /** @return array */ |
||
83 | public function getDeclaredTraits() |
||
93 | |||
94 | /** @return int */ |
||
95 | public function countDeclaredTraits() |
||
99 | |||
100 | /** @return array */ |
||
101 | View Code Duplication | public function getDefinedConstants() |
|
111 | |||
112 | /** @return int */ |
||
113 | public function countDefinedConstants() |
||
117 | |||
118 | /** @return array */ |
||
119 | public function getDefinedFunctions() |
||
129 | |||
130 | /** @return int */ |
||
131 | public function countDefinedFunctions() |
||
135 | |||
136 | /** @return array */ |
||
137 | public function getServiceIds(): array |
||
147 | |||
148 | /** @return int */ |
||
149 | public function countServiceIds() |
||
153 | |||
154 | /** @return array */ |
||
155 | View Code Duplication | public function getParameters() |
|
165 | |||
166 | /** @return int */ |
||
167 | public function countParameters() |
||
171 | |||
172 | /** @return array */ |
||
173 | View Code Duplication | public function getListeners() |
|
183 | |||
184 | /** @return int */ |
||
185 | public function countListeners() |
||
194 | |||
195 | /** @return array */ |
||
196 | View Code Duplication | public function getInstantiatedServices() |
|
206 | |||
207 | /** @return int */ |
||
208 | public function countInstantiatedServices() |
||
212 | |||
213 | /** @return array */ |
||
214 | protected function getListenersData() |
||
232 | |||
233 | /** @return array */ |
||
234 | protected function getInstantiatedServicesData() |
||
246 | } |
||
247 |
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.