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, 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 | 4 | final public function getQuantity() |
|
62 | { |
||
63 | 4 | 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
|
|||
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)->getQuantity(); |
||
132 | $res = $this->getCalculator()->add($this->quantity, $arg); |
||
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)->getQuantity(); |
||
143 | $res = $this->getCalculator()->subtract($this->quantity, $arg); |
||
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
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 /**
* @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. ![]() |
|||
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 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.