Completed
Push — master ( 97e5be...7f308f )
by Steevan
11:51
created

LoadedCollector::getDeclaredTraits()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace steevanb\DevBundle\DataCollector;
6
7
use Symfony\Component\DependencyInjection\Container;
8
use Symfony\Component\HttpFoundation\{
9
    Request,
10
    Response
11
};
12
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
13
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
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
114
    {
115
        return count($this->data['definedConstants']);
116
    }
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
130
    {
131
        return count($this->data['definedFunctions']);
132
    }
133
134
    public function getServiceIds(): array
135
    {
136
        static $sorted = false;
137
        if ($sorted === false) {
138
            $sorted = true;
139
            sort($this->data['services']);
140
        }
141
142
        return $this->data['services'];
143
    }
144
145
    public function countServiceIds(): int
146
    {
147
        return count($this->data['services']);
148
    }
149
150 View Code Duplication
    public function getParameters(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
151
    {
152
        static $sorted = false;
153
        if ($sorted === false) {
154
            $sorted = true;
155
            ksort($this->data['parameters']);
156
        }
157
158
        return $this->data['parameters'];
159
    }
160
161
    public function countParameters(): int
162
    {
163
        return count($this->data['parameters']);
164
    }
165
166 View Code Duplication
    public function getListeners(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
167
    {
168
        static $sorted = false;
169
        if ($sorted === false) {
170
            $sorted = true;
171
            ksort($this->data['listeners']);
172
        }
173
174
        return $this->data['listeners'];
175
    }
176
177
    public function countListeners(): int
178
    {
179
        $return = 0;
180
        foreach ($this->getListeners() as $listeners) {
181
            $return += count($listeners);
182
        }
183
184
        return $return;
185
    }
186
187 View Code Duplication
    public function getInstantiatedServices(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
188
    {
189
        static $sorted = false;
190
        if ($sorted === false) {
191
            $sorted = true;
192
            ksort($this->data['instantiatedServices']);
193
        }
194
195
        return $this->data['instantiatedServices'];
196
    }
197
198
    public function countInstantiatedServices(): int
199
    {
200
        return count($this->data['instantiatedServices']);
201
    }
202
203
    protected function getListenersData(): array
204
    {
205
        $return = [];
206
        foreach ($this->container->get('event_dispatcher')->getListeners() as $eventId => $listeners) {
207
            $return[$eventId] = [];
208
            if (is_array($listeners[0])) {
209
                foreach ($listeners as $listener) {
210
                    $return[$eventId][] = get_class($listener[0]);
211
                }
212
            } else {
213
                foreach ($listeners as $listener) {
214
                    $return[$eventId][] = get_class($listener);
215
                }
216
            }
217
        }
218
219
        return $return;
220
    }
221
222
    protected function getInstantiatedServicesData(): array
223
    {
224
        $reflectionProperty = new \ReflectionProperty(get_class($this->container), 'services');
225
        $reflectionProperty->setAccessible(true);
226
        $return = [];
227
        foreach ($reflectionProperty->getValue($this->container) as $id => $service) {
228
            $return[$id] = get_class($service);
229
        }
230
        $reflectionProperty->setAccessible(false);
231
232
        return $return;
233
    }
234
}
235