Issues (105)

src/RouterAwareTrait.php (1 issue)

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();
25
26
        $router = $this->getRouter();
27
        $router->setContext((new RequestContext())->fromRequest($this->getRequest()));
28
29
        $params = $router->route($request);
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