Test Setup Failed
Push — master ( 0769f8...73b5f8 )
by Gabriel
02:32 queued 11s
created

RouteResolverMiddleware::populateRequest()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 19
loc 19
rs 9.3222
c 0
b 0
f 0
ccs 0
cts 15
cp 0
crap 30
1
<?php
2
3
namespace Nip\Router\Middleware;
4
5
use Nip\Http\ServerMiddleware\Middlewares\ServerMiddlewareInterface;
6
use Nip\Request;
7
use Nip\Router\RequestContext;
8
use Nip\Router\Router;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
13
/**
14
 * Class StartSession
15
 * @package Nip\Session\Middleware
16
 */
17
class RouteResolverMiddleware implements ServerMiddlewareInterface
18
{
19
20
    /**
21
     * The session manager.
22
     *
23
     * @var Router
24
     */
25
    protected $router;
26
27
    /**
28
     * Create a new session middleware.
29
     *
30
     * @param Router $router
31
     */
32 1
    public function __construct(Router $router)
33
    {
34 1
        $this->router = $router;
35 1
    }
36
37
    /**
38
     * @inheritdoc
39
     * @param Request $request
40
     */
41 1
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
42
    {
43 1
        $this->getRouter()->setContext((new RequestContext())->fromRequest($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...
44 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...
45 1
        if ($return['_route']) {
46
            $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...
47
        }
48
49 1
        return $handler->handle($request);
50
    }
51
52
    /**
53
     * @param Request $request
54
     * @param $params
55
     */
56 View Code Duplication
    protected function populateRequest($request, $params)
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...
57
    {
58
        foreach ($params as $param => $value) {
59
            switch ($param) {
60
                case 'module':
61
                    $request->setModuleName($value);
62
                    break;
63
                case 'controller':
64
                    $request->setControllerName($value);
65
                    break;
66
                case 'action':
67
                    $request->setActionName($value);
68
                    break;
69
                default:
70
                    $request->attributes->set($param, $value);
71
                    break;
72
            }
73
        }
74
    }
75
76
    /**
77
     * @return Router
78
     */
79 1
    public function getRouter(): Router
80
    {
81 1
        return $this->router;
82
    }
83
}
84