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.
Completed
Pull Request — master (#7)
by Cees-Jan
03:02
created

MiddlewareRunner   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 28.91 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.83%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 37
loc 128
ccs 45
cts 46
cp 0.9783
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A orderMiddlewares() 0 8 1
A getPriority() 0 13 3
A pre() 0 17 2
A post() 18 18 2
A error() 19 19 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Foundation\Middleware;
4
5
use ApiClients\Foundation\Middleware\Annotation\Priority as PriorityAnnotation;
6
use Doctrine\Common\Annotations\AnnotationReader;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use React\Promise\CancellablePromiseInterface;
10
use ReflectionMethod;
11
use Throwable;
12
use function React\Promise\reject;
13
use function React\Promise\resolve;
14
15
final class MiddlewareRunner
16
{
17
    /**
18
     * @var array
19
     */
20
    private $options;
21
22
    /**
23
     * @var MiddlewareInterface[]
24
     */
25
    private $middlewares;
26
27
    /**
28
     * @var AnnotationReader
29
     */
30
    private $annotationReader;
31
32
    /**
33
     * MiddlewareRunner constructor.
34
     * @param array $options
35
     * @param MiddlewareInterface[] $middlewares
36
     */
37 1
    public function __construct(array $options, MiddlewareInterface ...$middlewares)
38
    {
39 1
        $this->options = $options;
40 1
        $this->middlewares = $middlewares;
0 ignored issues
show
Documentation Bug introduced by
It seems like $middlewares of type array<integer,array<inte...\MiddlewareInterface>>> is incompatible with the declared type array<integer,object<Api...e\MiddlewareInterface>> of property $middlewares.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41 1
        $this->annotationReader = new AnnotationReader();
42 1
    }
43
44
    /**
45
     * Sort the middlewares by priority
46
     *
47
     * @param string $method
48
     * @param MiddlewareInterface[] $middlewares
49
     * @return array
50
     */
51 1
    protected function orderMiddlewares(string $method, MiddlewareInterface ...$middlewares): array
52
    {
53
        usort($middlewares, function (MiddlewareInterface $left, MiddlewareInterface $right) use ($method) {
54 1
            return $this->getPriority($method, $right) <=> $this->getPriority($method, $left);
55 1
        });
56
57 1
        return $middlewares;
58
    }
59
60 1
    private function getPriority(string $method, MiddlewareInterface $middleware): int
61
    {
62 1
        $methodReflection = new ReflectionMethod($middleware, $method);
63 1
        $annotation = $this->annotationReader->getMethodAnnotation($methodReflection, PriorityAnnotation::class);
64
65 1
        if ($annotation !== null &&
66 1
            get_class($annotation) === PriorityAnnotation::class
67
        ) {
68
            return $annotation->property;
69
        }
70
71 1
        return $middleware->priority();
0 ignored issues
show
Deprecated Code introduced by
The method ApiClients\Foundation\Mi...reInterface::priority() has been deprecated with message: Use annotations for more finegrained control

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
72
    }
73
74
    /**
75
     * @param RequestInterface $request
76
     * @return CancellablePromiseInterface
77
     */
78 1
    public function pre(
79
        RequestInterface $request
80
    ): CancellablePromiseInterface {
81 1
        $promise = resolve($request);
82
83 1
        $middlewares = $this->middlewares;
84 1
        $middlewares = $this->orderMiddlewares('pre', ...$middlewares);
85
86 1
        foreach ($middlewares as $middleware) {
87 1
            $requestMiddleware = $middleware;
88
            $promise = $promise->then(function (RequestInterface $request) use ($requestMiddleware) {
89 1
                return $requestMiddleware->pre($request, $this->options);
90 1
            });
91
        }
92
93 1
        return $promise;
94
    }
95
96
    /**
97
     * @param ResponseInterface $response
98
     * @return CancellablePromiseInterface
99
     */
100 1 View Code Duplication
    public function post(
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...
101
        ResponseInterface $response
102
    ): CancellablePromiseInterface {
103 1
        $promise = resolve($response);
104
105 1
        $middlewares = $this->middlewares;
106 1
        $middlewares = $this->orderMiddlewares('post', ...$middlewares);
107 1
        $middlewares = array_reverse($middlewares);
108
109 1
        foreach ($middlewares as $middleware) {
110 1
            $responseMiddleware = $middleware;
111
            $promise = $promise->then(function (ResponseInterface $response) use ($responseMiddleware) {
112 1
                return $responseMiddleware->post($response, $this->options);
113 1
            });
114
        }
115
116 1
        return $promise;
117
    }
118
119
    /**
120
     * @param Throwable $throwable
121
     * @return CancellablePromiseInterface
122
     */
123 1 View Code Duplication
    public function error(
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...
124
        Throwable $throwable
125
    ): CancellablePromiseInterface {
126
127 1
        $promise = reject($throwable);
128
129 1
        $middlewares = $this->middlewares;
130 1
        $middlewares = $this->orderMiddlewares('error', ...$middlewares);
131 1
        $middlewares = array_reverse($middlewares);
132
133 1
        foreach ($middlewares as $middleware) {
134 1
            $errorMiddleware = $middleware;
135 1
            $promise = $promise->then(null, function (Throwable $throwable) use ($errorMiddleware) {
136 1
                return reject($errorMiddleware->error($throwable, $this->options));
137 1
            });
138
        }
139
140 1
        return $promise;
141
    }
142
}
143