Issues (105)

src/RouterAwareTrait.php (4 issues)

1
<?php
2
3
namespace Nip\Router;
4
5
use Nip\Request;
6
7
/**
8
 * Class ConfigAwareTrait
9
 * @package Nip\Router
10
 */
11
trait RouterAwareTrait
12
{
13
    /**
14
     * @var Router|null
15
     */
16
    protected $router = null;
17
18
    /**
19
     * @param bool|Request $request
20
     * @return array
21
     */
22
    public function route($request = false)
23
    {
24
        $request = $request ? $request : $this->getRequest();
0 ignored issues
show
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        $request = $request ? $request : $this->/** @scrutinizer ignore-call */ getRequest();
Loading history...
25
26
        $router = $this->getRouter();
27
        $router->setContext((new RequestContext())->fromRequest($this->getRequest()));
28
29
        $params = $router->route($request);
0 ignored issues
show
Deprecated Code introduced by
The function Nip\Router\Router::route() has been deprecated: Use matchRequest($request) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

29
        $params = /** @scrutinizer ignore-deprecated */ $router->route($request);

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

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

Loading history...
It seems like $request can also be of type true; however, parameter $request of Nip\Router\Router::route() does only seem to accept Nip\Request|Psr\Http\Mes...\ServerRequestInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        $params = $router->route(/** @scrutinizer ignore-type */ $request);
Loading history...
30
31
        return $params;
32
    }
33
34
    /**
35
     * @return Router
36
     */
37
    public function getRouter()
38
    {
39
        if (!$this->router) {
40
            $this->initRouter();
41
        }
42
43
        return $this->router;
44
    }
45
46
    /**
47
     * @param Router $router
48
     * @return $this
49
     */
50
    public function setRouter($router = false)
51
    {
52
        $this->router = $router;
0 ignored issues
show
Documentation Bug introduced by
It seems like $router can also be of type false. However, the property $router is declared as type Nip\Router\Router|null. 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...
53
54
        return $this;
55
    }
56
57
    protected function initRouter()
58
    {
59
        $this->setRouter($this->newRouter());
60
    }
61
62
    /**
63
     * @return Router
64
     */
65
    protected function newRouter()
66
    {
67
        return app()->get('router');
68
    }
69
}
70