Test Failed
Push — master ( 62d342...1e26c0 )
by Ivan
03:05
created

NavigationItem::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
27
    public function __construct(string $label, array $attributes = [], Item $parent = null, array $children = [])
28
    {
29
        $this->label = $label;
30
        $this->attributes = $attributes;
31
        $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...
32
33
        foreach ($children as $child) {
34
            $this->addChild($child);
35
        }
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getLabel(): string
42
    {
43
        return $this->label;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function getAttributes(): array
50
    {
51
        return $this->attributes;
52
    }
53
54
    /**
55
     * @param Item $parent
56
     * @return NavigationItem
57
     */
58
    public function setParent(Item $parent): NavigationItem
59
    {
60
        $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...
61
62
        return $this;
63
    }
64
65
    /**
66
     * @return NavigationItem
67
     */
68
    public function getParent(): NavigationItem
69
    {
70
        return $this->parent;
71
    }
72
73
    /**
74
     * @param Item $item
75
     * @return Item
76
     */
77
    public function addChild(Item $item): Item
78
    {
79
        $this->children[] = $item->setParent($this);
80
81
        return $this;
82
    }
83
84
    /**
85
     * @return Item[]
86
     */
87
    public function getChildren(): array
88
    {
89
        return $this->children;
90
    }
91
92
    /**
93
     * @param Match $match
94
     * @return Item
95
     */
96
    public function addMatch(Match $match): Item
97
    {
98
        $this->matches[] = $match;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return Match[]
105
     */
106
    public function getMatches(): array
107
    {
108
        return $this->matches;
109
    }
110
111
    /**
112
     * @return Uri
113
     */
114
    abstract public function getUri(): Uri;
115
}
116