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 (#280)
by Vlad
01:45
created

HttpDataCollector::doCollect()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 25
rs 9.5222
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 $logger;
22
23
    /**
24
     * @var float
25
     */
26
    private $slowResponseTime;
27
28
    /**
29
     * @param \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface $logger
30
     * @param float|int $slowResponseTime Time in seconds
31
     *
32
     * @TODO: remove in v8, PR #228
33
     */
34
    public function __construct(LoggerInterface $logger, $slowResponseTime = 0)
35
    {
36
        $this->logger = $logger;
37
        $this->slowResponseTime = $slowResponseTime;
38
39
        $this->reset();
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    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

45
    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

45
    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...
46
    {
47
        $messages = $this->logger->getMessages();
48
49
        if ($this->slowResponseTime > 0) {
50
            foreach ($messages as $message) {
51
                if (!$message instanceof LogMessage) {
52
                    continue;
53
                }
54
55
                if ($message->getTransferTime() >= $this->slowResponseTime) {
56
                    $this->data['hasSlowResponse'] = true;
57
                    break;
58
                }
59
            }
60
        }
61
62
        $requestId = $request->getUri();
63
64
        // clear log to have only messages related to Symfony request context
65
        $this->logger->clear();
66
67
        $logGroup = $this->getLogGroup($requestId);
68
        $logGroup->setRequestName($request->getPathInfo());
69
        $logGroup->addMessages($messages);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getName() : string
76
    {
77
        return 'eight_points_guzzle';
78
    }
79
80
    /**
81
     * Resets this data collector to its initial state.
82
     *
83
     * @return void
84
     */
85
    public function reset() : void
86
    {
87
        $this->data = [
88
            'logs' => [],
89
            'callCount' => 0,
90
            'totalTime' => 0,
91
            'hasSlowResponse' => false,
92
        ];
93
    }
94
95
    /**
96
     * Returning log entries
97
     *
98
     * @return array
99
     */
100
    public function getLogs() : array
101
    {
102
        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

102
        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...
103
    }
104
105
    /**
106
     * Get all messages
107
     *
108
     * @return array
109
     */
110
    public function getMessages() : array
111
    {
112
        $messages = [];
113
114
        foreach ($this->getLogs() as $log) {
115
            foreach ($log->getMessages() as $message) {
116
                $messages[] = $message;
117
            }
118
        }
119
120
        return $messages;
121
    }
122
123
    /**
124
     * Return amount of http calls
125
     *
126
     * @return integer
127
     */
128
    public function getCallCount() : int
129
    {
130
        return count($this->getMessages());
131
    }
132
133
    /**
134
     * Get Error Count
135
     *
136
     * @return integer
137
     */
138
    public function getErrorCount() : int
139
    {
140
        return count($this->getErrorsByType(LogLevel::ERROR));
141
    }
142
143
    /**
144
     * @param string $type
145
     *
146
     * @return array
147
     */
148
    public function getErrorsByType(string $type) : array
149
    {
150
        return array_filter(
151
            $this->getMessages(),
152
            function (LogMessage $message) use ($type) {
153
                return $message->getLevel() === $type;
154
            }
155
        );
156
    }
157
158
    /**
159
     * Get total time of all requests
160
     *
161
     * @return float
162
     */
163
    public function getTotalTime() : float
164
    {
165
        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...
166
    }
167
168
    /**
169
     * Check if there were any slow responses
170
     *
171
     * @return bool
172
     */
173
    public function hasSlowResponses() : bool
174
    {
175
        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...
176
    }
177
178
    /**
179
     * @param float $time
180
     *
181
     * @return void
182
     */
183
    public function addTotalTime(float $time) : void
184
    {
185
        $this->data['totalTime'] += $time;
186
    }
187
188
    /**
189
     * Returns (new) LogGroup based on given id
190
     *
191
     * @param string $id
192
     *
193
     * @return \EightPoints\Bundle\GuzzleBundle\Log\LogGroup
194
     */
195
    protected function getLogGroup(string $id) : LogGroup
196
    {
197
        if (!isset($this->data['logs'][$id])) {
198
            $this->data['logs'][$id] = new LogGroup();
199
        }
200
201
        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...
202
    }
203
}
204