Collector   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 69.23%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 170
ccs 36
cts 52
cp 0.6923
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A activateStack() 0 8 2
A __construct() 0 4 1
A reset() 0 5 1
A getName() 0 4 1
A deactivateStack() 0 4 1
A getActiveStack() 0 4 1
A addStack() 0 4 1
A getChildrenStacks() 0 6 1
A getStacks() 0 4 1
A getSuccessfulStacks() 0 6 1
A getFailedStacks() 0 6 1
A getClients() 0 10 1
A getClientRootStacks() 0 6 2
A countClientMessages() 0 6 1
A countStackMessages() 0 6 1
A getTotalDuration() 0 6 1
A collect() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Collector;
6
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
10
11
/**
12
 * The Collector hold profiled Stacks pushed by StackPlugin. It also have a list of configured clients.
13
 * All those data are used to display the HTTPlug panel in the Symfony profiler.
14
 *
15
 * The collector is not designed for execution in a threaded application and does not support plugins that execute an
16
 * other request before the current one is sent by the client.
17
 *
18
 * @author Fabien Bourigault <[email protected]>
19
 *
20
 * @internal
21
 */
22
class Collector extends DataCollector
23
{
24
    /**
25
     * @var Stack|null
26
     */
27
    private $activeStack;
28
29 20
    public function __construct()
30
    {
31 20
        $this->reset();
32 20
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 19
    public function reset()
38
    {
39 19
        $this->data['stacks'] = [];
40 19
        $this->activeStack = null;
41 19
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 2
    public function getName()
47
    {
48 2
        return 'httplug';
49
    }
50
51
    /**
52
     * Mark the stack as active. If a stack was already active, use it as parent for our stack.
53
     */
54 8
    public function activateStack(Stack $stack)
55
    {
56 8
        if (null !== $this->activeStack) {
57 2
            $stack->setParent($this->activeStack);
0 ignored issues
show
Documentation introduced by
$this->activeStack is of type object<Http\HttplugBundle\Collector\Stack>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
        }
59
60 8
        $this->activeStack = $stack;
61 8
    }
62
63
    /**
64
     * Mark the stack as inactive.
65
     */
66 6
    public function deactivateStack(Stack $stack)
67
    {
68 6
        $this->activeStack = $stack->getParent();
69 6
    }
70
71
    /**
72
     * @return Stack|null
73
     */
74 8
    public function getActiveStack()
75
    {
76 8
        return $this->activeStack;
77
    }
78
79 7
    public function addStack(Stack $stack)
80
    {
81 7
        $this->data['stacks'][] = $stack;
82 7
    }
83
84
    /**
85
     * @return Stack[]
86
     */
87
    public function getChildrenStacks(Stack $parent)
88
    {
89
        return array_filter($this->data['stacks'], function (Stack $stack) use ($parent) {
90
            return $stack->getParent() === $parent;
91
        });
92
    }
93
94
    /**
95
     * @return Stack[]
96
     */
97 5
    public function getStacks()
98
    {
99 5
        return $this->data['stacks'];
100
    }
101
102
    /**
103
     * @return Stack[]
104
     */
105
    public function getSuccessfulStacks()
106
    {
107
        return array_filter($this->data['stacks'], function (Stack $stack) {
108
            return !$stack->isFailed();
109
        });
110
    }
111
112
    /**
113
     * @return Stack[]
114
     */
115 1
    public function getFailedStacks()
116
    {
117
        return array_filter($this->data['stacks'], function (Stack $stack) {
118
            return $stack->isFailed();
119 1
        });
120
    }
121
122
    /**
123
     * @return array
124
     */
125 3
    public function getClients()
126
    {
127
        $stacks = array_filter($this->data['stacks'], function (Stack $stack) {
128 2
            return null === $stack->getParent();
129 3
        });
130
131
        return array_unique(array_map(function (Stack $stack) {
132 2
            return $stack->getClient();
133 3
        }, $stacks));
134
    }
135
136
    /**
137
     * @param $client
138
     *
139
     * @return Stack[]
140
     */
141 1
    public function getClientRootStacks($client)
142
    {
143
        return array_filter($this->data['stacks'], function (Stack $stack) use ($client) {
144 1
            return $stack->getClient() == $client && null == $stack->getParent();
145 1
        });
146
    }
147
148
    /**
149
     * Count all messages for a client.
150
     *
151
     * @param $client
152
     *
153
     * @return int
154
     */
155
    public function countClientMessages($client)
156
    {
157
        return array_sum(array_map(function (Stack $stack) {
158
            return $this->countStackMessages($stack);
159
        }, $this->getClientRootStacks($client)));
160
    }
161
162
    /**
163
     * Recursively count message in stack.
164
     *
165
     * @return int
166
     */
167
    private function countStackMessages(Stack $stack)
168
    {
169
        return 1 + array_sum(array_map(function (Stack $child) {
170
            return $this->countStackMessages($child);
171
        }, $this->getChildrenStacks($stack)));
172
    }
173
174
    /**
175
     * @return int
176
     */
177
    public function getTotalDuration()
178
    {
179
        return array_reduce($this->data['stacks'], function ($carry, Stack $stack) {
180
            return $carry + $stack->getDuration();
181
        }, 0);
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 1
    public function collect(Request $request, Response $response, $exception = null)
188
    {
189
        // We do not need to collect any data from the Symfony Request and Response
190 1
    }
191
}
192