This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
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 | 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); |
|
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
|
|||
116 | } |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 5 | final public function convert(UnitInterface $unit) |
|
122 | { |
||
123 | 5 | $res = $this->unit->convert($unit, $this->quantity); |
|
124 | |||
125 | 5 | return static::create($unit, $res); |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 1 | View Code Duplication | final public function add(QuantityInterface $addend) |
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) |
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 |
||
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 |
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.