Test Failed
Push — master ( 3eb0e4...9196b6 )
by Ivan
02:13
created

NavigationItem   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getLabel() 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
25
    public function __construct(string $label, Item $parent = null, array $children = [])
26
    {
27
        $this->label = $label;
28
        $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...
29
30
        foreach ($children as $child) {
31
            $this->addChild($child);
32
        }
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function getLabel(): string
39
    {
40
        return $this->label;
41
    }
42
43
    /**
44
     * @param Item $parent
45
     * @return NavigationItem
46
     */
47
    public function setParent(Item $parent): NavigationItem
48
    {
49
        $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...
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return NavigationItem
56
     */
57
    public function getParent(): NavigationItem
58
    {
59
        return $this->parent;
60
    }
61
62
    /**
63
     * @param Item $item
64
     * @return Item
65
     */
66
    public function addChild(Item $item): Item
67
    {
68
        $this->children[] = $item->setParent($this);
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return Item[]
75
     */
76
    public function getChildren(): array
77
    {
78
        return $this->children;
79
    }
80
81
    /**
82
     * @param Match $match
83
     * @return Item
84
     */
85
    public function addMatch(Match $match): Item
86
    {
87
        $this->matches[] = $match;
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return Match[]
94
     */
95
    public function getMatches(): array
96
    {
97
        return $this->matches;
98
    }
99
100
    /**
101
     * @return Uri
102
     */
103
    abstract public function getUri(): Uri;
104
}
105