Completed
Push — master ( 128ce6...9d6ccc )
by Andrii
02:17
created

NodeUnit::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 9
cp 0.7778
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 4
crap 2.0438
1
<?php
2
/**
3
 * PHP Units of Measure Library.
4
 *
5
 * @link      https://github.com/hiqdev/php-units
6
 * @package   php-units
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\units\tree;
12
13
use Closure;
14
15
/**
16
 * Units organized as tree.
17
 *
18
 * parent:
19
 *
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class NodeUnit extends RootUnit
23
{
24
    /**
25
     * @var null|RootUnit null when root
26
     */
27
    protected $parent;
28
29
    /**
30
     * @var int|float|string
31
     */
32
    protected $factor;
33
34
    /**
35
     * @var null|Closure
36
     */
37
    protected $method;
38
39
    /**
40
     * @param TreeConverter $tree
41
     * @param string $name
42
     * @param RootUnit $parent
43
     * @param int|float|string|callable $method
44
     */
45 4
    public function __construct(TreeConverter $tree, $name, RootUnit $parent, $method)
46
    {
47 4
        parent::__construct($tree, $name);
48 4
        $this->parent = $parent;
49 4
        if ($method instanceof Closure) {
50
            $this->method = $method;
51
            $this->factor = null;
52
        } else {
53 4
            $this->factor = $method;
0 ignored issues
show
Documentation Bug introduced by
It seems like $method can also be of type callable. However, the property $factor is declared as type integer|double|string. 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...
54 4
            $this->method = null;
55
        }
56 4
    }
57
58 3
    public function getFactor()
59
    {
60 3
        return $this->factor;
61
    }
62
63
    private $canon;
64
65
    /**
66
     * @var RootUnit
67
     */
68 6
    public function getCanon()
69
    {
70 6
        if ($this->canon === null) {
71 3
            $this->canon = $this->findCanon();
72
        }
73
74 6
        return $this->canon;
75
    }
76
77
    /**
78
     * Returns canonical unit.
79
     * @return RootUnit
80
     */
81 3
    public function findCanon()
82
    {
83 3
        return $this->getFactor() === 1 ? $this->getParent() : $this;
84
    }
85
86
    /**
87
     * @return RootUnit
88
     */
89 6
    public function getParent()
90
    {
91 6
        return $this->parent;
92
    }
93
94
    /**
95
     * @var string
96
     */
97
    protected $measure;
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 3
    public function getMeasure()
103
    {
104 3
        if ($this->measure === null) {
105 2
            $this->measure = $this->findMeasure();
106
        }
107
108 3
        return $this->measure;
109
    }
110
111 2
    public function findMeasure()
112
    {
113 2
        return $this->getRoot()->getName();
114
    }
115
116
    /**
117
     * @var RootUnit
118
     */
119
    protected $root;
120
121 2
    public function getRoot()
122
    {
123 2
        if ($this->root === null) {
124 1
            $this->root = $this->getParent()->getRoot();
125
        }
126
127 2
        return $this->root;
128
    }
129
}
130