Completed
Push — master ( 2ec02e...6482c3 )
by Andrii
14:31
created

RootUnit   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 149
Duplicated Lines 10.74 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 78.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
lcom 1
cbo 5
dl 16
loc 149
ccs 40
cts 51
cp 0.7843
rs 10
c 1
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getRoot() 0 4 1
A getFactor() 0 4 1
A getMeasure() 0 4 1
A __construct() 0 5 1
A equals() 0 4 2
A isAlias() 0 6 2
A getCanon() 0 4 1
A assertConvertible() 0 6 2
A isConvertible() 0 4 1
A convert() 0 18 4
A multiplyByFactor() 8 8 2
A divideByFactor() 8 8 2
A getNode() 0 4 1
A getParent() 0 4 1
A getCalculator() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 hiqdev\php\units\calculators\PhpCalculator;
14
use hiqdev\php\units\exceptions\NotConvertibleException;
15
use hiqdev\php\units\UnitInterface;
16
17
/**
18
 * Root unit in tree.
19
 *
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
class RootUnit implements UnitInterface
23
{
24
    /**
25
     * @var TreeConverter
26
     */
27
    protected $tree;
28
29
    /**
30
     * @var string
31
     */
32
    protected $name;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 9
    public function getName()
38
    {
39 9
        return $this->name;
40
    }
41
42 1
    public function getParent()
43
    {
44 1
        return $this;
45
    }
46
47 1
    public function getRoot()
48
    {
49 1
        return $this;
50
    }
51
52
    public function getFactor()
53
    {
54
        return null;
55
    }
56
57
    public function getMeasure()
58
    {
59
        return $this->name;
60
    }
61
62
    /**
63
     * @param TreeConverter $tree
64
     * @param string $name
65
     */
66 4
    public function __construct(TreeConverter $tree, $name)
67
    {
68 4
        $this->tree = $tree;
69 4
        $this->name = $name;
70 4
    }
71
72
    protected $calculator;
73
74
    /**
75
     * Returns calculator for this unit.
76
     * Units can have different calculators.
77
     * @return CalculatorInterface
78
     */
79 6
    public function getCalculator()
80
    {
81 6
        if ($this->calculator === null) {
82 2
            $this->calculator = new PhpCalculator();
83 2
        }
84
85 6
        return $this->calculator;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 7
    public function equals(UnitInterface $other)
92
    {
93 7
        return $this->getName() === $other->getName() || $this->isAlias($other);
94
    }
95
96 7
    public function isAlias(UnitInterface $other)
97
    {
98 7
        $node = $this->getNode($other);
99
100 7
        return $this === $node->getCanon() || $node === $this->getCanon();
101
    }
102
103 6
    public function getCanon()
104
    {
105 6
        return $this;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function assertConvertible(UnitInterface $other)
112
    {
113
        if (!$this->isConvertible($other)) {
114
            throw new InvalidArgumentException('not convertible unit: ' . $other->getName());
115
        }
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 2
    final public function isConvertible(UnitInterface $other)
122
    {
123 2
        return $this->getMeasure() === $other->getMeasure();
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 6
    public function convert(UnitInterface $other, $quantity)
130
    {
131 6
        $other = $this->getNode($other);
132
133 6
        if ($this->equals($other)) {
134 3
            return $quantity;
135
        }
136
137 6
        if ($other->equals($this->getParent())) {
138 6
            return $this->multiplyByFactor($quantity);
139
        }
140
141 5
        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...
142 5
            return $other->divideByFactor($quantity);
143
        }
144
145
        throw new NotConvertibleException('not yet implemented: ' . $this->getName() . ' -> ' . $other->getName());
146
    }
147
148 6 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...
149
    {
150 6
        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...
151
            throw new NotConvertibleException('method not yet implement: ' . $this->getName());
152
        }
153
154 6
        return $this->getCalculator()->multiply($quantity, $this->factor);
155
    }
156
157 5 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...
158
    {
159 5
        if (empty($this->factor)) {
160
            throw new NotConvertibleException('method not yet implement: ' . $this->getName());
161
        }
162
163 5
        return $this->getCalculator()->divide($quantity, $this->factor);
164
    }
165
166 7
    public function getNode(UnitInterface $unit)
167
    {
168 7
        return $this->tree->getNode($unit);
169
    }
170
}
171