Completed
Push — master ( 4b2ce1...194c0f )
by Andrii
02:09
created

RootUnit::getFactor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\units\tree;
12
13
use hiqdev\php\units\CalculatorInterface;
14
use hiqdev\php\units\calculators\PhpCalculator;
15
use hiqdev\php\units\exceptions\InvalidArgumentException;
16
use hiqdev\php\units\exceptions\NotConvertibleException;
17
use hiqdev\php\units\UnitInterface;
18
19
/**
20
 * Root unit in tree.
21
 *
22
 * @author Andrii Vasyliev <[email protected]>
23
 */
24
class RootUnit implements UnitInterface
25
{
26
    /**
27
     * @var TreeConverter
28
     */
29
    protected $tree;
30
31
    /**
32
     * @var string
33
     */
34
    protected $name;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 10
    public function getName()
40
    {
41 10
        return $this->name;
42
    }
43
44
    /** @return UnitInterface */
45 7
    public function getParent()
46
    {
47 7
        return $this;
48
    }
49
50
    /** @return UnitInterface */
51 10
    public function getRoot()
52
    {
53 10
        return $this;
54
    }
55
56 8
    public function getFactor()
57
    {
58 8
        return null;
59
    }
60
61 7
    public function getMeasure()
62
    {
63 7
        return $this->name;
64
    }
65
66
    /**
67
     * @param TreeConverter $tree
68
     * @param string $name
69
     */
70 5
    public function __construct(TreeConverter $tree, $name)
71
    {
72 5
        $this->tree = $tree;
73 5
        $this->name = $name;
74 5
    }
75
76
    protected $calculator;
77
78
    /**
79
     * Returns calculator for this unit.
80
     * Units can have different calculators.
81
     * @return CalculatorInterface
82
     */
83 7
    public function getCalculator()
84
    {
85 7
        if ($this->calculator === null) {
86 3
            $this->calculator = new PhpCalculator();
87
        }
88
89 7
        return $this->calculator;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 9
    public function equals(UnitInterface $other)
96
    {
97 9
        return $this->getName() === $other->getName() || $this->isAlias($other);
98
    }
99
100 9
    public function isAlias(UnitInterface $other)
101
    {
102 9
        $node = $this->getNode($other);
103
104 9
        return $this === $node->getCanon()
105 9
            || $node === $this->getCanon()
106
            || (
107 9
                $this->getRoot() === $this->getNode($other)->getRoot()
108 9
                && $this->getFactor() === $this->getNode($other)->getFactor()
109
            );
110
    }
111
112 8
    public function getCanon()
113
    {
114 8
        return $this;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 7
    public function assertConvertible(UnitInterface $other)
121
    {
122 7
        if (!$this->isConvertible($other)) {
123
            throw new InvalidArgumentException('not convertible unit: ' . $other->getName());
124
        }
125 7
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 9
    final public function isConvertible(UnitInterface $other)
131
    {
132 9
        return $this->getMeasure() === $other->getMeasure();
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 7
    public function convert(UnitInterface $other, $quantity)
139
    {
140 7
        $this->assertConvertible($other);
141
142 7
        $other = $this->getNode($other);
143
144 7
        if ($this->equals($other)) {
145 4
            return $quantity;
146
        }
147
148 7
        if ($other->equals($this->getParent())) {
149 7
            return $this->multiplyByFactor($quantity);
150
        }
151
152 7
        if ($this->equals($other->getParent())) {
0 ignored issues
show
Bug introduced by
It seems like $other->getParent() can be null; however, equals() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
153 7
            return $other->divideByFactor($quantity);
154
        }
155
156 7
        return $this->getRoot()->convert($other, $this->convert($this->getRoot(), $quantity));
0 ignored issues
show
Documentation introduced by
$other is of type object<hiqdev\php\units\tree\NodeUnit>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
157
    }
158
159 7 View Code Duplication
    public function multiplyByFactor($quantity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161 7
        if (empty($this->factor)) {
0 ignored issues
show
Bug introduced by
The property factor does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
162
            throw new NotConvertibleException('method not yet implement: ' . $this->getName());
163
        }
164
165 7
        return $this->getCalculator()->multiply($quantity, $this->factor);
166
    }
167
168 7 View Code Duplication
    public function divideByFactor($quantity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
    {
170 7
        if (empty($this->factor)) {
171
            throw new NotConvertibleException('method not yet implement: ' . $this->getName());
172
        }
173
174 7
        return $this->getCalculator()->divide($quantity, $this->factor);
175
    }
176
177 9
    public function getNode(UnitInterface $unit)
178
    {
179 9
        return $this->tree->getNode($unit);
180
    }
181
}
182