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 |
||
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 $unit |
||
32 | * @var float|int|string $quantity |
||
33 | */ |
||
34 | 6 | private function __construct(UnitInterface $unit, $quantity) |
|
39 | |||
40 | /** |
||
41 | * Creates quantity with same unit. |
||
42 | * Optimized to return this if same quantity. |
||
43 | * @var float|int|string $quantity |
||
44 | */ |
||
45 | 3 | final protected function create($quantity) |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 2 | private function getCalculator() |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 2 | final public function getQuantity() |
|
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 1 | final public function getUnit() |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 2 | final public function compare(QuantityInterface $other) |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 1 | final public function equals(QuantityInterface $other) |
|
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 1 | final public function isConvertible(UnitInterface $unit) |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 3 | final public function convert(UnitInterface $unit) |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | View Code Duplication | final public function add(QuantityInterface $addend) |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | View Code Duplication | final public function subtract(QuantityInterface $subtrahend) |
|
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | final public function multiply($multiplier) |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | final public function divide($divisor) |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | final public function ceil() |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | final public function floor() |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | final public function round($roundingMode) |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | final public function absolute() |
||
191 | |||
192 | 6 | final public static function __callStatic($unit, $args) |
|
196 | |||
197 | /** |
||
198 | * Returns unit for given unit name. |
||
199 | * The only function to change in child classes. |
||
200 | * XXX Should be defined as abstract but `abstract static` is not supported in PHP5. |
||
201 | * @param string $name |
||
202 | * @return UnitInterface |
||
203 | * @throws InvalidConfigException |
||
204 | */ |
||
205 | protected static function findUnit($unit) |
||
209 | } |
||
210 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: