Passed
Push — master ( b46a2b...672db4 )
by Maxim
03:14
created

TreeItemTrait::addChild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace WebComplete\core\utils\tree;
4
5
use WebComplete\core\entity\AbstractEntity;
6
7
trait TreeItemTrait
8
{
9
    /** @var self|null */
10
    protected $parent;
11
    /** @var self[] */
12
    protected $children = [];
13
14
    /**
15
     * @param self|AbstractEntity $item
16
     */
17
    public function setParent($item)
18
    {
19
        $this->parent = $item;
0 ignored issues
show
Documentation Bug introduced by
It seems like $item can also be of type WebComplete\core\entity\AbstractEntity. However, the property $parent is declared as type null|WebComplete\core\utils\tree\TreeItemTrait. 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...
20
    }
21
22
    /**
23
     * @return self|null
24
     */
25
    public function getParent()
26
    {
27
        return $this->parent;
28
    }
29
30
    /**
31
     * @param self|AbstractEntity $item
32
     */
33
    public function addChild($item)
34
    {
35
        $this->children[$item->getId()] = $item;
0 ignored issues
show
Bug introduced by
It seems like getId() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        $this->children[$item->/** @scrutinizer ignore-call */ getId()] = $item;
Loading history...
36
    }
37
38
    /**
39
     * @return self[]
40
     */
41
    public function getChildren(): array
42
    {
43
        return $this->children;
44
    }
45
}
46