FastRouteMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Burzum\FastRouteMiddleware;
6
7
use Burzum\FastRouteMiddleware\Handler\FoundHandlerInterface;
8
use Burzum\FastRouteMiddleware\Handler\NotAllowedHandlerInterface;
9
use Burzum\FastRouteMiddleware\Handler\NotFoundHandlerInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Server\RequestHandlerInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use FastRoute\Dispatcher as DispatcherInterface;
15
16
/**
17
 * FastRoute Dispatcher Middleware
18
 *
19
 * @link https://github.com/nikic/FastRoute
20
 */
21
class FastRouteMiddleware implements MiddlewareInterface
22
{
23
    /**
24
     * Fast Route Dispatcher
25
     *
26
     * @var \FastRoute\Dispatcher
27
     */
28
    protected $dispatcher;
29
30
    /**
31
     * Route not allowed Handler
32
     *
33
     * @var null|\Burzum\FastRouteMiddleware\Handler\NotAllowedHandlerInterface
34
     */
35
    protected $notAllowedHandler;
36
37
    /**
38
     * Route not found Handler
39
     *
40
     * @var null|\Burzum\FastRouteMiddleware\Handler\NotFoundHandlerInterface
41
     */
42
    protected $notFoundHandler;
43
44
    /**
45
     * Route Found Handler
46
     *
47
     * @var null|\Burzum\FastRouteMiddleware\Handler\FoundHandlerInterface
48
     */
49
    protected $foundHandler;
50
51
    /**
52
     * Constructor
53
     *
54
     * @param \FastRoute\Dispatcher $dispatcher Fastroute Dispatcher
55
     * @param null|\Burzum\FastRouteMiddleware\Handler\FoundHandlerInterface $foundHandler Found Handler
56
     * @param null|\Burzum\FastRouteMiddleware\Handler\NotFoundHandlerInterface $notAllowedHandler Not Found Handler
57
     * @param null|\Burzum\FastRouteMiddleware\Handler\NotAllowedHandlerInterface $notAllowedHandler Not Allowed Handler
58
     */
59 1
    public function __construct(
60
        DispatcherInterface $dispatcher,
61
        ?FoundHandlerInterface $foundHandler = null,
62
        ?NotFoundHandlerInterface $notFoundHandler = null,
63
        ?NotAllowedHandlerInterface $notAllowedHandler = null
64
    ) {
65 1
        $this->dispatcher = $dispatcher;
66 1
        $this->foundHandler = $foundHandler;
67 1
        $this->notFoundHandler = $notFoundHandler;
68 1
        $this->notAllowedHandler = $notAllowedHandler;
0 ignored issues
show
Documentation Bug introduced by
It seems like $notAllowedHandler can also be of type object<Burzum\FastRouteM...tFoundHandlerInterface>. However, the property $notAllowedHandler is declared as type null|object<Burzum\FastR...llowedHandlerInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69 1
    }
70
71
    /**
72
     * Process an incoming server request and return a response, optionally
73
     * delegating response creation to a handler.
74
     *
75
     * @param \Psr\Http\Message\ServerRequestInterface $request Request
76
     * @param \Psr\Http\Server\RequestHandlerInterface $requestHandler Request Handler
77
     * @return \Psr\Http\Message\ResponseInterface
78
     */
79 1
    public function process(
80
        ServerRequestInterface $request,
81
        RequestHandlerInterface $requestHandler
82
    ): ResponseInterface {
83 1
        $routeInfo = $this->dispatch();
84 1
        $result = null;
85
86 1
        switch ($routeInfo[0]) {
87 1
            case DispatcherInterface::NOT_FOUND:
88 1
                if ($this->notFoundHandler !== null) {
89 1
                    $result = $this->notFoundHandler->handle($request);
90
                }
91
92 1
                break;
93 1
            case DispatcherInterface::METHOD_NOT_ALLOWED:
94
                if ($this->notAllowedHandler !== null) {
95
                    $result = $this->notAllowedHandler->handle($request, $routeInfo[1]);
96
                }
97
98
                break;
99 1
            case DispatcherInterface::FOUND:
100 1
                if ($this->foundHandler !== null) {
101 1
                    $result = $this->foundHandler->handle($request, $routeInfo[1], $routeInfo[2]);
102
                }
103
104 1
                break;
105
        }
106
107 1
        if ($request instanceof ResponseInterface) {
108
            return $result;
109
        }
110
111 1
        return $requestHandler->handle($request);
112
    }
113
114
    /**
115
     * Dispatches the Request URI
116
     *
117
     * @return array
118
     */
119 1
    protected function dispatch(): array
120
    {
121 1
        $httpMethod = $_SERVER['REQUEST_METHOD'];
122 1
        $uri = $_SERVER['REQUEST_URI'];
123
124
        // Strip query string (?foo=bar) and decode URI
125 1
        if (false !== $pos = strpos($uri, '?')) {
126
            $uri = substr($uri, 0, $pos);
127
        }
128
129 1
        $uri = rawurldecode($uri);
130
131 1
        return $this->dispatcher->dispatch($httpMethod, $uri);
132
    }
133
}
134