Completed
Push — master ( 501485...128ce6 )
by Andrii
01:56
created

AbstractQuantity::isNegative()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
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, 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
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 8
    private function __construct(UnitInterface $unit, $quantity)
35
    {
36 8
        $this->unit = $unit;
37 8
        $this->quantity = $quantity;
38 8
    }
39
40
    /**
41
     * Creates quantity with same unit.
42
     * Optimized to return this if same quantity.
43
     * @var float|int|string
44
     */
45 3
    final protected function create($quantity)
46
    {
47 3
        return $this->quantity === $quantity ? $this : new static($this->unit, $quantity);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    private function getCalculator()
54
    {
55 2
        return $this->unit->getCalculator();
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 2
    final public function getQuantity()
62
    {
63 2
        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);
80
81 2
        return $this->getCalculator()->compare($this->quantity, $arg->quantity);
0 ignored issues
show
Bug introduced by
Accessing quantity on the interface hiqdev\php\units\QuantityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
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 3
    final public function convert(UnitInterface $unit)
120
    {
121 3
        $res = $this->unit->convert($unit, $this->quantity);
122
123 3
        return $this->create($res);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    final public function add(QuantityInterface $addend)
130
    {
131
        $arg = $addend->convert($this->unit);
132
        $res = $this->getCalculator()->add($this->quantity, $arg);
0 ignored issues
show
Documentation introduced by
$arg is of type object<hiqdev\php\units\QuantityInterface>, but the function expects a integer|double|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...
133
134
        return $this->create($res);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    final public function subtract(QuantityInterface $subtrahend)
141
    {
142
        $arg = $subtrahend->convert($this->unit);
143
        $res = $this->getCalculator()->subtract($this->quantity, $arg);
0 ignored issues
show
Documentation introduced by
$arg is of type object<hiqdev\php\units\QuantityInterface>, but the function expects a integer|double|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...
144
145
        return $this->create($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->create($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->create($res);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    final public function ceil()
172
    {
173
        $res = $this->getCalculator()->ceil($this->quantity);
174
175
        return $this->create($res);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    final public function floor()
182
    {
183
        $res = $this->getCalculator()->floor($this->quantity);
184
185
        return $this->create($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->create($res);
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    final public function absolute()
202
    {
203
        $res = $this->getCalculator()->absolute($this->quantity);
204
205
        return $this->create($res);
206
    }
207
208 8
    final public static function __callStatic($unit, $args)
209
    {
210 8
        return new static(static::findUnit($unit), $args[0]);
211
    }
212
213
    /**
214
     * Returns unit for given unit name.
215
     * The only function to change in child classes.
216
     * XXX Should be defined as abstract but `abstract static` is not supported in PHP5.
217
     * @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...
218
     * @throws InvalidConfigException
219
     * @return UnitInterface
220
     */
221
    protected static function findUnit($unit)
222
    {
223
        throw new InvalidConfigException('getUnit method must be redefined');
224
    }
225
}
226