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.

MiddlewareCollector   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 31
c 2
b 0
f 0
dl 0
loc 50
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 28 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReactInspector\HttpMiddleware;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use React\Promise\PromiseInterface;
0 ignored issues
show
Bug introduced by
The type React\Promise\PromiseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use WyriHaximus\Metrics\Label;
11
use WyriHaximus\Metrics\Registry\Counters;
12
use WyriHaximus\Metrics\Registry\Gauges;
13
use WyriHaximus\Metrics\Registry\Summaries;
14
15
use function React\Promise\resolve;
16
use function Safe\hrtime;
17
use function strtoupper;
18
19
final class MiddlewareCollector
20
{
21
    public const LABELS_ATTRIBUTE = 'react-inspector-http-middleware-labels-iugioawfe';
22
23
    /** @var array<Label> */
24
    private array $defaultLabels;
25
26
    private Gauges $inflight;
27
28
    private Counters $requests;
29
30
    private Summaries $responseTime;
31
32
    public function __construct(Metrics $metrics)
33
    {
34
        $this->defaultLabels = $metrics->defaultLabels();
35
        $this->inflight      = $metrics->inflight();
36
        $this->requests      = $metrics->requests();
37
        $this->responseTime  = $metrics->responseTime();
38
    }
39
40
    /** @return PromiseInterface<ResponseInterface> */
41
    public function __invoke(ServerRequestInterface $request, callable $next): PromiseInterface
42
    {
43
        $method = strtoupper($request->getMethod());
44
        $gauge  = $this->inflight->gauge(new Label('method', $method), ...$this->defaultLabels);
45
        $gauge->incr();
46
        $labels  = new Labels();
47
        $request = $request->withAttribute(self::LABELS_ATTRIBUTE, $labels);
48
        $time    = hrtime(true);
49
50
        return resolve($next($request))->then(function (ResponseInterface $response) use ($method, $gauge, $time, $labels): ResponseInterface {
51
            /** @psalm-suppress PossiblyInvalidOperand */
52
            $this->responseTime->summary(
53
                new Label('method', $method),
54
                new Label('code', (string) $response->getStatusCode()),
55
                ...$this->defaultLabels,
56
                ...$labels->labels(),
57
            )->observe(
58
                (hrtime(true) - $time) / 1e+9, /** @phpstan-ignore-line */
59
            );
60
            $this->requests->counter(
61
                new Label('method', $method),
62
                new Label('code', (string) $response->getStatusCode()),
63
                ...$this->defaultLabels,
64
                ...$labels->labels(),
65
            )->incr();
66
            $gauge->dcr();
67
68
            return $response;
69
        });
70
    }
71
}
72