Completed
Push — master ( e68dfe...c32fa6 )
by Steevan
09:39
created

LoadedCollector::countDeclaredClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace steevanb\DevBundle\DataCollector;
4
5
use Symfony\Component\DependencyInjection\Container;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
9
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)
21
    {
22
        $this->container = $container;
23
    }
24
25
    /** @return string */
26
    public function getName()
27
    {
28
        return 'loaded_data_collector';
29
    }
30
31
    public function collect(Request $request, Response $response, \Exception $exception = null)
32
    {
33
        $this->data = [
34
            'declaredClasses' => get_declared_classes(),
35
            'declaredInterfaces' => get_declared_interfaces(),
36
            'declaredTraits' => get_declared_traits(),
37
            'definedConstants' => get_defined_constants(),
38
            'definedFunctions' => get_defined_functions()['user'],
39
            'services' => $this->container->getServiceIds(),
40
            'instantiatedServices' => $this->getInstantiatedServicesData(),
41
            'parameters' => $this->container->getParameterBag()->all(),
42
            'listeners' => $this->getListenersData()
43
        ];
44
    }
45
46
    /** @return array */
47
    public function getDeclaredClasses()
48
    {
49
        static $sorted = false;
50
        if ($sorted === false) {
51
            $sorted = true;
52
            sort($this->data['declaredClasses']);
53
        }
54
55
        return $this->data['declaredClasses'];
56
    }
57
58
    /** @return int */
59
    public function countDeclaredClasses()
60
    {
61
        return count($this->data['declaredClasses']);
62
    }
63
64
    /** @return array */
65
    public function getDeclaredInterfaces()
66
    {
67
        static $sorted = false;
68
        if ($sorted === false) {
69
            $sorted = true;
70
            sort($this->data['declaredInterfaces']);
71
        }
72
73
        return $this->data['declaredInterfaces'];
74
    }
75
76
    /** @return int */
77
    public function countDeclaredInterfaces()
78
    {
79
        return count($this->data['declaredInterfaces']);
80
    }
81
82
    /** @return array */
83
    public function getDeclaredTraits()
84
    {
85
        static $sorted = false;
86
        if ($sorted === false) {
87
            $sorted = true;
88
            sort($this->data['declaredTraits']);
89
        }
90
91
        return $this->data['declaredTraits'];
92
    }
93
94
    /** @return int */
95
    public function countDeclaredTraits()
96
    {
97
        return count($this->data['declaredTraits']);
98
    }
99
100
    /** @return array */
101 View Code Duplication
    public function getDefinedConstants()
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...
102
    {
103
        static $sorted = false;
104
        if ($sorted === false) {
105
            $sorted = true;
106
            ksort($this->data['definedConstants']);
107
        }
108
109
        return $this->data['definedConstants'];
110
    }
111
112
    /** @return int */
113
    public function countDefinedConstants()
114
    {
115
        return count($this->data['definedConstants']);
116
    }
117
118
    /** @return array */
119
    public function getDefinedFunctions()
120
    {
121
        static $sorted = false;
122
        if ($sorted === false) {
123
            $sorted = true;
124
            sort($this->data['definedFunctions']);
125
        }
126
127
        return $this->data['definedFunctions'];
128
    }
129
130
    /** @return int */
131
    public function countDefinedFunctions()
132
    {
133
        return count($this->data['definedFunctions']);
134
    }
135
136
    /** @return array */
137
    public function getServiceIds(): array
138
    {
139
        static $sorted = false;
140
        if ($sorted === false) {
141
            $sorted = true;
142
            sort($this->data['services']);
143
        }
144
145
        return $this->data['services'];
146
    }
147
148
    /** @return int */
149
    public function countServiceIds()
150
    {
151
        return count($this->data['services']);
152
    }
153
154
    /** @return array */
155 View Code Duplication
    public function getParameters()
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...
156
    {
157
        static $sorted = false;
158
        if ($sorted === false) {
159
            $sorted = true;
160
            ksort($this->data['parameters']);
161
        }
162
163
        return $this->data['parameters'];
164
    }
165
166
    /** @return int */
167
    public function countParameters()
168
    {
169
        return count($this->data['parameters']);
170
    }
171
172
    /** @return array */
173 View Code Duplication
    public function getListeners()
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...
174
    {
175
        static $sorted = false;
176
        if ($sorted === false) {
177
            $sorted = true;
178
            ksort($this->data['listeners']);
179
        }
180
181
        return $this->data['listeners'];
182
    }
183
184
    /** @return int */
185
    public function countListeners()
186
    {
187
        $return = 0;
188
        foreach ($this->getListeners() as $listeners) {
189
            $return += count($listeners);
190
        }
191
192
        return $return;
193
    }
194
195
    /** @return array */
196 View Code Duplication
    public function getInstantiatedServices()
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...
197
    {
198
        static $sorted = false;
199
        if ($sorted === false) {
200
            $sorted = true;
201
            ksort($this->data['instantiatedServices']);
202
        }
203
204
        return $this->data['instantiatedServices'];
205
    }
206
207
    /** @return int */
208
    public function countInstantiatedServices()
209
    {
210
        return count($this->data['instantiatedServices']);
211
    }
212
213
    /** @return array */
214
    protected function getListenersData()
215
    {
216
        $return = [];
217
        foreach ($this->container->get('event_dispatcher')->getListeners() as $eventId => $listeners) {
218
            $return[$eventId] = [];
219
            if (is_array($listeners[0])) {
220
                foreach ($listeners as $listener) {
221
                    $return[$eventId][] = get_class($listener[0]);
222
                }
223
            } else {
224
                foreach ($listeners as $listener) {
225
                    $return[$eventId][] = get_class($listener);
226
                }
227
            }
228
        }
229
230
        return $return;
231
    }
232
233
    /** @return array */
234
    protected function getInstantiatedServicesData()
235
    {
236
        $reflectionProperty = new \ReflectionProperty(get_class($this->container), 'services');
237
        $reflectionProperty->setAccessible(true);
238
        $return = [];
239
        foreach ($reflectionProperty->getValue($this->container) as $id => $service) {
240
            $return[$id] = get_class($service);
241
        }
242
        $reflectionProperty->setAccessible(false);
243
244
        return $return;
245
    }
246
}
247