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 |
||
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) |
|
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) |
|
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | */ |
||
55 | 5 | private function getCalculator() |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 12 | final public function getQuantity() |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 7 | final public function getUnit() |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 2 | final public function compare(QuantityInterface $other) |
|
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 1 | final public function equals(QuantityInterface $other) |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 1 | final public function isPositive() |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 1 | final public function isNegative() |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 1 | final public function isConvertible(UnitInterface $unit) |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 5 | final public function convert(UnitInterface $unit) |
|
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 1 | View Code Duplication | final public function add(QuantityInterface $addend) |
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 1 | View Code Duplication | final public function subtract(QuantityInterface $subtrahend) |
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) |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | final public function ceil() |
||
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | final public function floor() |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | final public function round($roundingMode) |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | final public function absolute() |
||
209 | |||
210 | 11 | final public static function __callStatic($unit, $args) |
|
214 | |||
215 | /** |
||
216 | * @param string $unit |
||
217 | * @var float|int|string $quantity |
||
218 | * @return static |
||
219 | */ |
||
220 | 11 | public static function create($unit, $quantity) |
|
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) |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | * @return array |
||
240 | */ |
||
241 | public function jsonSerialize() |
||
248 | } |
||
249 |
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: