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 |
||
14 | class LoadedCollector extends DataCollector |
||
15 | { |
||
16 | /** |
||
17 | * I know we should try to not have Container as dependency |
||
18 | * But i need it to get fresh data in collect() |
||
19 | * I need Container, not ContainerInterface |
||
20 | * @var Container |
||
21 | */ |
||
22 | protected $container; |
||
23 | |||
24 | public function __construct(Container $container) |
||
25 | { |
||
26 | $this->container = $container; |
||
27 | } |
||
28 | |||
29 | public function getName(): string |
||
30 | { |
||
31 | return 'loaded_data_collector'; |
||
32 | } |
||
33 | |||
34 | public function collect(Request $request, Response $response, \Exception $exception = null): void |
||
35 | { |
||
36 | $this->data = [ |
||
37 | 'declaredClasses' => get_declared_classes(), |
||
38 | 'declaredInterfaces' => get_declared_interfaces(), |
||
39 | 'declaredTraits' => get_declared_traits(), |
||
40 | 'definedConstants' => get_defined_constants(), |
||
41 | 'definedFunctions' => get_defined_functions()['user'], |
||
42 | 'services' => $this->container->getServiceIds(), |
||
43 | 'instantiatedServices' => $this->getInstantiatedServicesData(), |
||
44 | 'parameters' => $this->container->getParameterBag()->all(), |
||
45 | 'listeners' => $this->getListenersData() |
||
46 | ]; |
||
47 | } |
||
48 | |||
49 | public function reset(): void |
||
50 | { |
||
51 | $this->data = []; |
||
52 | } |
||
53 | |||
54 | public function getDeclaredClasses(): array |
||
55 | { |
||
56 | static $sorted = false; |
||
57 | if ($sorted === false) { |
||
58 | $sorted = true; |
||
59 | sort($this->data['declaredClasses']); |
||
60 | } |
||
61 | |||
62 | return $this->data['declaredClasses']; |
||
63 | } |
||
64 | |||
65 | public function countDeclaredClasses(): int |
||
66 | { |
||
67 | return count($this->data['declaredClasses']); |
||
68 | } |
||
69 | |||
70 | public function getDeclaredInterfaces(): array |
||
71 | { |
||
72 | static $sorted = false; |
||
73 | if ($sorted === false) { |
||
74 | $sorted = true; |
||
75 | sort($this->data['declaredInterfaces']); |
||
76 | } |
||
77 | |||
78 | return $this->data['declaredInterfaces']; |
||
79 | } |
||
80 | |||
81 | public function countDeclaredInterfaces(): int |
||
82 | { |
||
83 | return count($this->data['declaredInterfaces']); |
||
84 | } |
||
85 | |||
86 | public function getDeclaredTraits(): array |
||
87 | { |
||
88 | static $sorted = false; |
||
89 | if ($sorted === false) { |
||
90 | $sorted = true; |
||
91 | sort($this->data['declaredTraits']); |
||
92 | } |
||
93 | |||
94 | return $this->data['declaredTraits']; |
||
95 | } |
||
96 | |||
97 | public function countDeclaredTraits(): int |
||
98 | { |
||
99 | return count($this->data['declaredTraits']); |
||
100 | } |
||
101 | |||
102 | View Code Duplication | public function getDefinedConstants(): array |
|
|
|||
103 | { |
||
104 | static $sorted = false; |
||
105 | if ($sorted === false) { |
||
106 | $sorted = true; |
||
107 | ksort($this->data['definedConstants']); |
||
108 | } |
||
109 | |||
110 | return $this->data['definedConstants']; |
||
111 | } |
||
112 | |||
113 | public function countDefinedConstants(): int |
||
117 | |||
118 | public function getDefinedFunctions(): array |
||
119 | { |
||
120 | static $sorted = false; |
||
121 | if ($sorted === false) { |
||
122 | $sorted = true; |
||
123 | sort($this->data['definedFunctions']); |
||
124 | } |
||
125 | |||
126 | return $this->data['definedFunctions']; |
||
127 | } |
||
128 | |||
129 | public function countDefinedFunctions(): int |
||
133 | |||
134 | public function getServiceIds(): array |
||
144 | |||
145 | public function countServiceIds(): int |
||
149 | |||
150 | View Code Duplication | public function getParameters(): array |
|
160 | |||
161 | public function countParameters(): int |
||
165 | |||
166 | View Code Duplication | public function getListeners(): array |
|
176 | |||
177 | public function countListeners(): int |
||
186 | |||
187 | View Code Duplication | public function getInstantiatedServices(): array |
|
197 | |||
198 | public function countInstantiatedServices(): int |
||
202 | |||
203 | protected function getListenersData(): array |
||
221 | |||
222 | protected function getInstantiatedServicesData(): array |
||
234 | } |
||
235 |
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.