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:
Complex classes like Numbers often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Numbers, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Numbers |
||
16 | { |
||
17 | |||
18 | const MUTABLE = MutableNumber::class; |
||
19 | const IMMUTABLE = ImmutableNumber::class; |
||
20 | const MUTABLE_FRACTION = MutableFraction::class; |
||
21 | const IMMUTABLE_FRACTION = ImmutableFraction::class; |
||
22 | const CURRENCY = Currency::class; |
||
23 | /* 105 digits after decimal, which is going to be overkill in almost all places */ |
||
24 | const PI = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679'; |
||
25 | /* Tau (2pi) to 100 digits */ |
||
26 | const TAU = '6.283185307179586476925286766559005768394338798750211641949889184615632812572417997256069650684234136'; |
||
27 | /* Euler's Number to 100 digits */ |
||
28 | const E = '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427'; |
||
29 | /* Golden Ratio to 100 digits */ |
||
30 | const GOLDEN_RATIO = '1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137'; |
||
31 | /* Natural log of 10 to 100 digits */ |
||
32 | const LN_10 = '2.302585092994045684017991454684364207601101488628772976033327900967572609677352480235997205089598298'; |
||
33 | |||
34 | /** |
||
35 | * @param $type |
||
36 | * @param $value |
||
37 | * @param int|null $precision |
||
38 | * @param int $base |
||
39 | * |
||
40 | * @throws IntegrityConstraint |
||
41 | * @return ImmutableNumber|MutableNumber|ImmutableFraction|MutableFraction|NumberInterface |
||
42 | */ |
||
43 | public static function make($type, $value, $precision = null, $base = 10) |
||
78 | |||
79 | /** |
||
80 | * @param $type |
||
81 | * @param $value |
||
82 | * @param int|null $precision |
||
83 | * @param int $base |
||
84 | * @return NumberInterface |
||
85 | */ |
||
86 | public static function makeFromBase10($type, $value, $precision = null, $base = 10) |
||
95 | |||
96 | /** |
||
97 | * @param $type |
||
98 | * @param int|float|string|NumberInterface|DecimalInterface|FractionInterface $value |
||
99 | * @param int|null $precision |
||
100 | * @param int $base |
||
101 | * |
||
102 | * @throws IntegrityConstraint |
||
103 | * @return ImmutableNumber|MutableNumber|NumberInterface|ImmutableNumber[]|MutableNumber[]|NumberInterface[] |
||
104 | */ |
||
105 | public static function makeOrDont($type, $value, $precision = null, $base = 10) |
||
137 | |||
138 | /** |
||
139 | * @param $value |
||
140 | * @param $type |
||
141 | * |
||
142 | * @return ImmutableFraction|MutableFraction |
||
143 | * @throws IntegrityConstraint |
||
144 | */ |
||
145 | public static function makeFractionFromString($value, $type = self::IMMUTABLE_FRACTION) |
||
174 | |||
175 | /** |
||
176 | * @param int|null $precision |
||
177 | * |
||
178 | * @throws IntegrityConstraint |
||
179 | * @return NumberInterface |
||
180 | */ |
||
181 | View Code Duplication | public static function makePi($precision = null) |
|
199 | |||
200 | /** |
||
201 | * @param int|null $precision |
||
202 | * |
||
203 | * @throws IntegrityConstraint |
||
204 | * @return NumberInterface |
||
205 | */ |
||
206 | View Code Duplication | public static function makeTau($precision = null) |
|
222 | |||
223 | /** |
||
224 | * @param int|null $precision |
||
225 | * |
||
226 | * @return NumberInterface |
||
227 | */ |
||
228 | public static function make2Pi($precision = null) |
||
232 | |||
233 | /** |
||
234 | * @param int|null $precision |
||
235 | * |
||
236 | * @throws IntegrityConstraint |
||
237 | * @return NumberInterface |
||
238 | */ |
||
239 | View Code Duplication | public static function makeE($precision = null) |
|
257 | |||
258 | /** |
||
259 | * @param int|null $precision |
||
260 | * |
||
261 | * @throws IntegrityConstraint |
||
262 | * @return NumberInterface |
||
263 | */ |
||
264 | View Code Duplication | public static function makeGoldenRatio($precision = null) |
|
282 | |||
283 | /** |
||
284 | * @param int|null $precision |
||
285 | * |
||
286 | * @throws IntegrityConstraint |
||
287 | * @return NumberInterface |
||
288 | */ |
||
289 | View Code Duplication | public static function makeNaturalLog10($precision = null) |
|
307 | |||
308 | /** |
||
309 | * @return ImmutableNumber |
||
310 | */ |
||
311 | public static function makeOne($precision = null) |
||
315 | |||
316 | /** |
||
317 | * @return ImmutableNumber |
||
318 | */ |
||
319 | public static function makeZero($precision = null) |
||
323 | |||
324 | } |
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.