GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RouterAwareTrait::newRouter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
Bug introduced by
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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