These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | /** |
||
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 | 2 | final protected function repeat($quantity) |
|
46 | { |
||
47 | 2 | 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 | 2 | final public function getUnit() |
|
70 | { |
||
71 | 2 | 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
|
|||
114 | } |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | 5 | final public function convert(UnitInterface $unit) |
|
120 | { |
||
121 | 5 | $res = $this->unit->convert($unit, $this->quantity); |
|
0 ignored issues
–
show
$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...
|
|||
122 | |||
123 | 5 | return static::create($unit, $res); |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | 1 | View Code Duplication | final public function add(QuantityInterface $addend) |
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) |
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 |
||
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 |
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: