Completed
Push — master ( 9b22ab...6029c9 )
by Andrii
01:55
created

AbstractQuantity   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 229
Duplicated Lines 6.11 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 67.21%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 14
loc 229
ccs 41
cts 61
cp 0.6721
rs 10
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A repeat() 0 4 2
A getCalculator() 0 4 1
A getQuantity() 0 4 1
A getUnit() 0 4 1
A compare() 0 6 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 add() 7 7 1
A subtract() 7 7 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 __callStatic() 0 4 1
A create() 0 4 1
A findUnit() 0 4 1
A jsonSerialize() 0 7 1
A multiply() 0 6 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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\units;
12
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * Quantity with Unit.
17
 *
18
 * @author Andrii Vasyliev <[email protected]>
19
 */
20
abstract class AbstractQuantity implements QuantityInterface, \JsonSerializable
21
{
22
    /**
23
     * @var UnitInterface
24
     */
25
    private $unit;
26
27
    /**
28
     * @var float|int|string
29
     */
30
    private $quantity;
31
32
    /**
33
     * @var UnitInterface
34
     * @var float|int|string $quantity
35
     */
36 11
    private function __construct(UnitInterface $unit, $quantity)
37
    {
38 11
        $this->unit = $unit;
39 11
        $this->quantity = $quantity;
40 11
    }
41
42
    /**
43
     * Creates quantity with same unit.
44
     * Optimized to return this if same quantity.
45
     * @var float|int|string
46
     */
47 3
    final protected function repeat($quantity)
48
    {
49 3
        return $this->quantity === $quantity ? $this : static::create($this->unit, $quantity);
0 ignored issues
show
Documentation introduced by
$this->unit is of type object<hiqdev\php\units\UnitInterface>, but the function expects a string.

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...
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 5
    private function getCalculator()
56
    {
57 5
        return $this->unit->getCalculator();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 12
    final public function getQuantity()
64
    {
65 12
        return $this->quantity;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 7
    final public function getUnit()
72
    {
73 7
        return $this->unit;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 2
    final public function compare(QuantityInterface $other)
80
    {
81 2
        $arg = $other->convert($this->unit)->getQuantity();
82
83 2
        return $this->getCalculator()->compare($this->quantity, $arg);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    final public function equals(QuantityInterface $other)
90
    {
91 1
        return $this->compare($other) === 0;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 1
    final public function isPositive()
98
    {
99 1
        return $this->quantity > 0;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 1
    final public function isNegative()
106
    {
107 1
        return $this->quantity < 0;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 1
    final public function isConvertible(UnitInterface $unit)
114
    {
115 1
        return $this->unit->isConvertible($unit);
0 ignored issues
show
Documentation introduced by
$unit is of type object<hiqdev\php\units\UnitInterface>, 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...
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...
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 5
    final public function convert(UnitInterface $unit)
122
    {
123 5
        $res = $this->unit->convert($unit, $this->quantity);
0 ignored issues
show
Documentation introduced by
$unit is of type object<hiqdev\php\units\UnitInterface>, 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...
124
125 5
        return static::create($unit, $res);
0 ignored issues
show
Documentation introduced by
$unit is of type object<hiqdev\php\units\UnitInterface>, but the function expects a string.

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...
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 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...
132
    {
133 1
        $arg = $addend->convert($this->unit)->getQuantity();
134 1
        $res = $this->getCalculator()->add($this->quantity, $arg);
135
136 1
        return $this->repeat($res);
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 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...
143
    {
144 1
        $arg = $subtrahend->convert($this->unit)->getQuantity();
145 1
        $res = $this->getCalculator()->subtract($this->quantity, $arg);
146
147 1
        return $this->repeat($res);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153 1
    final public function multiply($multiplier)
154
    {
155 1
        $res = $this->getCalculator()->multiply($this->quantity, $multiplier);
156
157 1
        return $this->repeat($res);
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    final public function divide($divisor)
164
    {
165
        $res = $this->getCalculator()->divide($this->quantity, $divisor);
166
167
        return $this->repeat($res);
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    final public function ceil()
174
    {
175
        $res = $this->getCalculator()->ceil($this->quantity);
176
177
        return $this->repeat($res);
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    final public function floor()
184
    {
185
        $res = $this->getCalculator()->floor($this->quantity);
186
187
        return $this->repeat($res);
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    final public function round($roundingMode)
194
    {
195
        $res = $this->getCalculator()->round($this->quantity, $roundingMode);
196
197
        return $this->repeat($res);
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    final public function absolute()
204
    {
205
        $res = $this->getCalculator()->absolute($this->quantity);
206
207
        return $this->repeat($res);
208
    }
209
210 11
    final public static function __callStatic($unit, $args)
211
    {
212 11
        return static::create($unit, $args[0]);
213
    }
214
215
    /**
216
     * @param string $unit
217
     * @var float|int|string $quantity
218
     * @return static
219
     */
220 11
    public static function create($unit, $quantity)
221
    {
222 11
        return new static(static::findUnit($unit), $quantity);
223
    }
224
225
    /**
226
     * Returns unit for given unit name.
227
     * The only function to change in child classes.
228
     * XXX Should be defined as abstract but `abstract static` is not supported in PHP5.
229
     * @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...
230
     * @return UnitInterface
231
     */
232
    protected static function findUnit($unit)
233
    {
234
        throw new InvalidConfigException('getUnit method must be redefined');
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     * @return array
240
     */
241
    public function jsonSerialize()
242
    {
243
        return [
244
            'unit' => $this->unit->getName(),
245
            'quantity' => $this->quantity,
246
        ];
247
    }
248
}
249