GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#289)
by Asmir
03:33
created

HttpDataCollector::doCollect()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 16
nc 16
nop 3
dl 0
loc 30
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace EightPoints\Bundle\GuzzleBundle\DataCollector;
4
5
use EightPoints\Bundle\GuzzleBundle\Log\LogGroup;
6
use EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface;
7
use EightPoints\Bundle\GuzzleBundle\Log\LogMessage;
8
use Psr\Log\LogLevel;
9
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
/**
14
 * Collecting http data for Symfony profiler
15
 */
16
class HttpDataCollector extends DataCollector
17
{
18
    use DataCollectorSymfonyCompatibilityTrait;
19
20
    /** @var \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface[] */
21
    protected $loggers;
22
23
    /** @var float */
24
    private $slowResponseTime;
25
26
    /**
27
     * @param \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface[] $loggers
28
     * @param float|int $slowResponseTime Time in seconds
29
     */
30
    public function __construct(array $loggers, float $slowResponseTime)
31
    {
32
        $this->loggers = $loggers;
33
        $this->slowResponseTime = $slowResponseTime;
34
35
        $this->reset();
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function doCollect(Request $request, Response $response, \Throwable $exception = null)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
    protected function doCollect(Request $request, /** @scrutinizer ignore-unused */ Response $response, \Throwable $exception = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $exception is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
    protected function doCollect(Request $request, Response $response, /** @scrutinizer ignore-unused */ \Throwable $exception = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        $messages = [];
44
        foreach ($this->loggers as $logger) {
45
            $messages = array_merge($messages, $logger->getMessages());
46
        }
47
48
        if ($this->slowResponseTime > 0) {
49
            foreach ($messages as $message) {
50
                if (!$message instanceof LogMessage) {
51
                    continue;
52
                }
53
54
                if ($message->getTransferTime() >= $this->slowResponseTime) {
55
                    $this->data['hasSlowResponse'] = true;
56
                    break;
57
                }
58
            }
59
        }
60
61
        $requestId = $request->getUri();
62
63
        // clear log to have only messages related to Symfony request context
64
        foreach ($this->loggers as $logger) {
65
            $logger->clear();
66
        }
67
68
        $logGroup = $this->getLogGroup($requestId);
69
        $logGroup->setRequestName($request->getPathInfo());
70
        $logGroup->addMessages($messages);
71
72
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getName() : string
79
    {
80
        return 'eight_points_guzzle';
81
    }
82
83
    /**
84
     * Resets this data collector to its initial state.
85
     *
86
     * @return void
87
     */
88
    public function reset() : void
89
    {
90
        $this->data = [
91
            'logs' => [],
92
            'callCount' => 0,
93
            'totalTime' => 0,
94
            'hasSlowResponse' => false,
95
        ];
96
    }
97
98
    /**
99
     * Returning log entries
100
     *
101
     * @return array
102
     */
103
    public function getLogs() : array
104
    {
105
        return array_key_exists('logs', $this->data) ? $this->data['logs'] : [];
0 ignored issues
show
Bug introduced by
It seems like $this->data can also be of type Symfony\Component\VarDumper\Cloner\Data; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
        return array_key_exists('logs', /** @scrutinizer ignore-type */ $this->data) ? $this->data['logs'] : [];
Loading history...
Bug Best Practice introduced by
The expression return array_key_exists(...>data['logs'] : array() could return the type Symfony\Component\VarDumper\Cloner\Data|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
106
    }
107
108
    /**
109
     * Get all messages
110
     *
111
     * @return array
112
     */
113
    public function getMessages() : array
114
    {
115
        $messages = [];
116
117
        foreach ($this->getLogs() as $log) {
118
            foreach ($log->getMessages() as $message) {
119
                $messages[] = $message;
120
            }
121
        }
122
123
        return $messages;
124
    }
125
126
    /**
127
     * Return amount of http calls
128
     *
129
     * @return integer
130
     */
131
    public function getCallCount() : int
132
    {
133
        return count($this->getMessages());
134
    }
135
136
    /**
137
     * Get Error Count
138
     *
139
     * @return integer
140
     */
141
    public function getErrorCount() : int
142
    {
143
        return count($this->getErrorsByType(LogLevel::ERROR));
144
    }
145
146
    /**
147
     * @param string $type
148
     *
149
     * @return array
150
     */
151
    public function getErrorsByType(string $type) : array
152
    {
153
        return array_filter(
154
            $this->getMessages(),
155
            function (LogMessage $message) use ($type) {
156
                return $message->getLevel() === $type;
157
            }
158
        );
159
    }
160
161
    /**
162
     * Get total time of all requests
163
     *
164
     * @return float
165
     */
166
    public function getTotalTime() : float
167
    {
168
        return $this->data['totalTime'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['totalTime'] could return the type Symfony\Component\VarDumper\Cloner\Data|null which is incompatible with the type-hinted return double. Consider adding an additional type-check to rule them out.
Loading history...
169
    }
170
171
    /**
172
     * Check if there were any slow responses
173
     *
174
     * @return bool
175
     */
176
    public function hasSlowResponses() : bool
177
    {
178
        return $this->data['hasSlowResponse'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['hasSlowResponse'] could return the type Symfony\Component\VarDumper\Cloner\Data|null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
179
    }
180
181
    /**
182
     * @param float $time
183
     *
184
     * @return void
185
     */
186
    public function addTotalTime(float $time) : void
187
    {
188
        $this->data['totalTime'] += $time;
189
    }
190
191
    /**
192
     * Returns (new) LogGroup based on given id
193
     *
194
     * @param string $id
195
     *
196
     * @return \EightPoints\Bundle\GuzzleBundle\Log\LogGroup
197
     */
198
    protected function getLogGroup(string $id) : LogGroup
199
    {
200
        if (!isset($this->data['logs'][$id])) {
201
            $this->data['logs'][$id] = new LogGroup();
202
        }
203
204
        return $this->data['logs'][$id];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['logs'][$id] could return the type Symfony\Component\VarDumper\Cloner\Data|null which is incompatible with the type-hinted return EightPoints\Bundle\GuzzleBundle\Log\LogGroup. Consider adding an additional type-check to rule them out.
Loading history...
205
    }
206
}
207