Test Failed
Push — master ( 1e26c0...29031d )
by Ivan
02:45
created

NavigationItem   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 1
dl 0
loc 118
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 3
A getLabel() 0 4 1
A getAttributes() 0 4 1
A getRoles() 0 4 1
A setParent() 0 6 1
A getParent() 0 4 1
A addChild() 0 6 1
A getChildren() 0 4 1
A addMatch() 0 6 1
A getMatches() 0 4 1
getUri() 0 1 ?
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Everlution\Navigation;
6
7
use Everlution\Navigation\Uri\Uri;
8
use Everlution\Navigation\Voter\Match;
9
10
/**
11
 * Class NavigationItem.
12
 * @author Ivan Barlog <[email protected]>
13
 */
14
abstract class NavigationItem implements Item, MatchableItem
15
{
16
    /** @var Match[] */
17
    private $matches = [];
18
    /** @var string */
19
    private $label;
20
    /** @var NavigationItem */
21
    private $parent;
22
    /** @var array */
23
    private $children = [];
24
    /** @var array */
25
    private $attributes = [];
26
    /** @var string|array */
27
    private $roles = [];
28
29
    public function __construct(
30
        string $label,
31
        array $attributes = [],
32
        Item $parent = null,
33
        array $children = [],
34
        $roles = []
35
    ) {
36
        $this->label = $label;
37
        $this->attributes = $attributes;
38
        $this->parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parent can also be of type object<Everlution\Navigation\Item>. However, the property $parent is declared as type object<Everlution\Navigation\NavigationItem>. 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...
39
        $this->roles = is_array($roles) ? $roles : [$roles];
40
41
        foreach ($children as $child) {
42
            $this->addChild($child);
43
        }
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function getLabel(): string
50
    {
51
        return $this->label;
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getAttributes(): array
58
    {
59
        return $this->attributes;
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getRoles(): array
66
    {
67
        return $this->roles;
68
    }
69
70
    /**
71
     * @param Item $parent
72
     * @return NavigationItem
73
     */
74
    public function setParent(Item $parent): NavigationItem
75
    {
76
        $this->parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
$parent is of type object<Everlution\Navigation\Item>, but the property $parent was declared to be of type object<Everlution\Navigation\NavigationItem>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return NavigationItem
83
     */
84
    public function getParent(): NavigationItem
85
    {
86
        return $this->parent;
87
    }
88
89
    /**
90
     * @param Item $item
91
     * @return Item
92
     */
93
    public function addChild(Item $item): Item
94
    {
95
        $this->children[] = $item->setParent($this);
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return Item[]
102
     */
103
    public function getChildren(): array
104
    {
105
        return $this->children;
106
    }
107
108
    /**
109
     * @param Match $match
110
     * @return Item
111
     */
112
    public function addMatch(Match $match): Item
113
    {
114
        $this->matches[] = $match;
115
116
        return $this;
117
    }
118
119
    /**
120
     * @return Match[]
121
     */
122
    public function getMatches(): array
123
    {
124
        return $this->matches;
125
    }
126
127
    /**
128
     * @return Uri
129
     */
130
    abstract public function getUri(): Uri;
131
}
132