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

AbstractQuantity   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 226
Duplicated Lines 6.19 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 61.29%

Importance

Changes 2
Bugs 1 Features 2
Metric Value
wmc 24
c 2
b 1
f 2
lcom 1
cbo 3
dl 14
loc 226
ccs 38
cts 62
cp 0.6129
rs 10

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCalculator() 0 4 1
A getQuantity() 0 4 1
A getUnit() 0 4 1
A equals() 0 4 1
A isPositive() 0 4 1
A isNegative() 0 4 1
A isConvertible() 0 4 1
A convert() 0 6 1
A multiply() 0 6 1
A divide() 0 6 1
A ceil() 0 6 1
A floor() 0 6 1
A round() 0 6 1
A absolute() 0 6 1
A compare() 0 6 1
A repeat() 0 4 2
A add() 7 7 1
A subtract() 7 7 1
A __callStatic() 0 4 1
A create() 0 4 1
A findUnit() 0 4 1
A jsonSerialize() 0 7 1

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;
12
13
/**
14
 * Quantity with Unit.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
abstract class AbstractQuantity implements QuantityInterface, \JsonSerializable
19
{
20
    /**
21
     * @var UnitInterface
22
     */
23
    private $unit;
24
25
    /**
26
     * @var float|int|string
27
     */
28
    private $quantity;
29
30
    /**
31
     * @var UnitInterface
32
     * @var float|int|string $quantity
33
     */
34 10
    private function __construct(UnitInterface $unit, $quantity)
35
    {
36 10
        $this->unit = $unit;
37 10
        $this->quantity = $quantity;
38 10
    }
39
40
    /**
41
     * Creates quantity with same unit.
42
     * Optimized to return this if same quantity.
43
     * @var float|int|string
44
     */
45 5
    final protected function repeat($quantity)
46
    {
47 5
        return $this->quantity === $quantity ? $this : static::create($this->unit, $quantity);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 4
    private function getCalculator()
54
    {
55 4
        return $this->unit->getCalculator();
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 6
    final public function getQuantity()
62
    {
63 6
        return $this->quantity;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    final public function getUnit()
70
    {
71 1
        return $this->unit;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    final public function compare(QuantityInterface $other)
78
    {
79 2
        $arg = $other->convert($this->unit)->getQuantity();
80
81 2
        return $this->getCalculator()->compare($this->quantity, $arg);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    final public function equals(QuantityInterface $other)
88
    {
89 1
        return $this->compare($other) === 0;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 1
    final public function isPositive()
96
    {
97 1
        return $this->quantity > 0;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 1
    final public function isNegative()
104
    {
105 1
        return $this->quantity < 0;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    final public function isConvertible(UnitInterface $unit)
112
    {
113 1
        return $this->unit->isConvertible($unit);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->unit->isConvertible($unit); (boolean) is incompatible with the return type declared by the interface hiqdev\php\units\QuantityInterface::isConvertible of type hiqdev\php\units\QuantityInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 5
    final public function convert(UnitInterface $unit)
120
    {
121 5
        $res = $this->unit->convert($unit, $this->quantity);
122
123 5
        return $this->repeat($res);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 1 View Code Duplication
    final public function add(QuantityInterface $addend)
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...
130
    {
131 1
        $arg = $addend->convert($this->unit)->getQuantity();
132 1
        $res = $this->getCalculator()->add($this->quantity, $arg);
133
134 1
        return $this->repeat($res);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 1 View Code Duplication
    final public function subtract(QuantityInterface $subtrahend)
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...
141
    {
142 1
        $arg = $subtrahend->convert($this->unit)->getQuantity();
143 1
        $res = $this->getCalculator()->subtract($this->quantity, $arg);
144
145 1
        return $this->repeat($res);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    final public function multiply($multiplier)
152
    {
153
        $res = $this->getCalculator()->multiply($this->quantity, $multiplier);
154
155
        return $this->repeat($res);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    final public function divide($divisor)
162
    {
163
        $res = $this->getCalculator()->divide($this->quantity, $divisor);
164
165
        return $this->repeat($res);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    final public function ceil()
172
    {
173
        $res = $this->getCalculator()->ceil($this->quantity);
174
175
        return $this->repeat($res);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    final public function floor()
182
    {
183
        $res = $this->getCalculator()->floor($this->quantity);
184
185
        return $this->repeat($res);
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    final public function round($roundingMode)
192
    {
193
        $res = $this->getCalculator()->round($this->quantity, $roundingMode);
194
195
        return $this->repeat($res);
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    final public function absolute()
202
    {
203
        $res = $this->getCalculator()->absolute($this->quantity);
204
205
        return $this->repeat($res);
206
    }
207
208 10
    final public static function __callStatic($unit, $args)
209
    {
210 10
        return static::create($unit, $args[0]);
211
    }
212
213 10
    public static function create($unit, $quantity)
214
    {
215 10
        return new static(static::findUnit($unit), $quantity);
216
    }
217
218
    /**
219
     * Returns unit for given unit name.
220
     * The only function to change in child classes.
221
     * XXX Should be defined as abstract but `abstract static` is not supported in PHP5.
222
     * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
223
     * @throws InvalidConfigException
224
     * @return UnitInterface
225
     */
226
    protected static function findUnit($unit)
227
    {
228
        throw new InvalidConfigException('getUnit method must be redefined');
229
    }
230
231
    /**
232
     * {@inheritdoc}
233
     * @return array
234
     */
235
    public function jsonSerialize()
236
    {
237
        return [
238
            'unit' => $this->unit->getName(),
239
            'quantity' => $this->quantity,
240
        ];
241
    }
242
243
}
244