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 |
||
18 | class Numbers |
||
19 | { |
||
20 | |||
21 | const MUTABLE = MutableNumber::class; |
||
22 | const IMMUTABLE = ImmutableNumber::class; |
||
23 | const MUTABLE_FRACTION = MutableFraction::class; |
||
24 | const IMMUTABLE_FRACTION = ImmutableFraction::class; |
||
25 | const CARTESIAN_COORDINATE = CartesianCoordinate::class; |
||
26 | /* 105 digits after decimal, which is going to be overkill in almost all places */ |
||
27 | const PI = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679'; |
||
28 | /* Tau (2pi) to 100 digits */ |
||
29 | const TAU = '6.283185307179586476925286766559005768394338798750211641949889184615632812572417997256069650684234136'; |
||
30 | /* Euler's Number to 100 digits */ |
||
31 | const E = '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427'; |
||
32 | /* Golden Ratio to 100 digits */ |
||
33 | const GOLDEN_RATIO = '1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137'; |
||
34 | /* Natural log of 10 to 100 digits */ |
||
35 | const LN_10 = '2.302585092994045684017991454684364207601101488628772976033327900967572609677352480235997205089598298'; |
||
36 | |||
37 | /** |
||
38 | * @param $type |
||
39 | * @param $value |
||
40 | * @param int|null $precision |
||
41 | * @param int $base |
||
42 | * |
||
43 | * @throws IntegrityConstraint |
||
44 | * @return ImmutableNumber|MutableNumber|ImmutableFraction|MutableFraction|CartesianCoordinate|NumberInterface|FractionInterface|CoordinateInterface |
||
45 | */ |
||
46 | 23 | public static function make($type, $value, $precision = null, $base = 10) |
|
103 | |||
104 | /** |
||
105 | * @param $type |
||
106 | * @param $value |
||
107 | * @param int|null $precision |
||
108 | * @param int $base |
||
109 | * @return NumberInterface |
||
110 | */ |
||
111 | public static function makeFromBase10($type, $value, $precision = null, $base = 10) |
||
120 | |||
121 | /** |
||
122 | * @param $type |
||
123 | * @param int|float|string|NumberInterface|DecimalInterface|FractionInterface $value |
||
124 | * @param int|null $precision |
||
125 | * @param int $base |
||
126 | * |
||
127 | * @throws IntegrityConstraint |
||
128 | * @return ImmutableNumber|MutableNumber|NumberInterface|ImmutableNumber[]|MutableNumber[]|NumberInterface[] |
||
129 | */ |
||
130 | 25 | public static function makeOrDont($type, $value, $precision = null, $base = 10) |
|
162 | |||
163 | /** |
||
164 | * @param $value |
||
165 | * @param $type |
||
166 | * |
||
167 | * @return ImmutableFraction|MutableFraction|FractionInterface |
||
168 | * @throws IntegrityConstraint |
||
169 | */ |
||
170 | public static function makeFractionFromString($value, $type = self::IMMUTABLE_FRACTION, $base = 10) |
||
211 | |||
212 | /** |
||
213 | * @param int|null $precision |
||
214 | * |
||
215 | * @throws IntegrityConstraint |
||
216 | * @return NumberInterface |
||
217 | */ |
||
218 | 5 | View Code Duplication | public static function makePi($precision = null) |
236 | |||
237 | /** |
||
238 | * @param int|null $precision |
||
239 | * |
||
240 | * @throws IntegrityConstraint |
||
241 | * @return NumberInterface |
||
242 | */ |
||
243 | 6 | View Code Duplication | public static function makeTau($precision = null) |
259 | |||
260 | /** |
||
261 | * @param int|null $precision |
||
262 | * |
||
263 | * @return NumberInterface |
||
264 | */ |
||
265 | 6 | public static function make2Pi($precision = null) |
|
269 | |||
270 | /** |
||
271 | * @param int|null $precision |
||
272 | * |
||
273 | * @throws IntegrityConstraint |
||
274 | * @return NumberInterface |
||
275 | */ |
||
276 | 4 | View Code Duplication | public static function makeE($precision = null) |
294 | |||
295 | /** |
||
296 | * @param int|null $precision |
||
297 | * |
||
298 | * @throws IntegrityConstraint |
||
299 | * @return NumberInterface |
||
300 | */ |
||
301 | View Code Duplication | public static function makeGoldenRatio($precision = null) |
|
319 | |||
320 | /** |
||
321 | * @param int|null $precision |
||
322 | * |
||
323 | * @throws IntegrityConstraint |
||
324 | * @return NumberInterface |
||
325 | */ |
||
326 | 1 | View Code Duplication | public static function makeNaturalLog10($precision = null) |
344 | |||
345 | /** |
||
346 | * @return ImmutableNumber |
||
347 | */ |
||
348 | 12 | public static function makeOne($precision = null) |
|
352 | |||
353 | /** |
||
354 | * @return ImmutableNumber |
||
355 | */ |
||
356 | 12 | public static function makeZero($precision = null) |
|
360 | |||
361 | } |