Test Setup Failed
Push — master ( 423952...415317 )
by Gabriel
02:26 queued 10s
created

RouteResolverMiddleware::populateRequest()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 0
loc 19
ccs 0
cts 14
cp 0
crap 30
rs 9.3222
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\Middleware;
4
5
use Nip\Http\ServerMiddleware\Middlewares\ServerMiddlewareInterface;
6
use Nip\Request;
7
use Nip\Router\Router;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
12
/**
13
 * Class StartSession
14
 * @package Nip\Session\Middleware
15
 */
16
class RouteResolverMiddleware implements ServerMiddlewareInterface
17
{
18
19
    /**
20
     * The session manager.
21
     *
22
     * @var Router
23
     */
24
    protected $router;
25
26
    /**
27
     * Create a new session middleware.
28
     *
29
     * @param Router $router
30
     */
31 1
    public function __construct(Router $router)
32
    {
33 1
        $this->router = $router;
34 1
    }
35
36
    /**
37
     * @inheritdoc
38
     * @param Request $request
39
     */
40 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
41
    {
42 1
        $return = $this->getRouter()->matchRequest($request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Psr\Http\Message\ServerRequestInterface>, but the function expects a object<Symfony\Component\HttpFoundation\Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43 1
        if ($return['_route']) {
44
            $this->populateRequest($request, $return);
0 ignored issues
show
Compatibility introduced by
$request of type object<Psr\Http\Message\ServerRequestInterface> is not a sub-type of object<Nip\Request>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ServerRequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
45
        }
46
47 1
        return $handler->handle($request);
48
    }
49
50
    /**
51
     * @param Request $request
52
     * @param $params
53
     */
54
    protected function populateRequest($request, $params)
55
    {
56
        foreach ($params as $param => $value) {
57
            switch ($param) {
58
                case 'module':
59
                    $request->setModuleName($value);
60
                    break;
61
                case 'controller':
62
                    $request->setControllerName($value);
63
                    break;
64
                case 'action':
65
                    $request->setActionName($value);
66
                    break;
67
                default:
68
                    $request->attributes->set($param, $value);
69
                    break;
70
            }
71
        }
72
    }
73
74
    /**
75
     * @return Router
76
     */
77 1
    public function getRouter(): Router
78
    {
79 1
        return $this->router;
80
    }
81
}
82