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 | const CURRENCY = Currency::class; |
||
| 27 | /* 105 digits after decimal, which is going to be overkill in almost all places */ |
||
| 28 | const PI = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679'; |
||
| 29 | /* Tau (2pi) to 100 digits */ |
||
| 30 | const TAU = '6.283185307179586476925286766559005768394338798750211641949889184615632812572417997256069650684234136'; |
||
| 31 | /* Euler's Number to 100 digits */ |
||
| 32 | const E = '2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427'; |
||
| 33 | /* Golden Ratio to 100 digits */ |
||
| 34 | const GOLDEN_RATIO = '1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137'; |
||
| 35 | /* Natural log of 10 to 100 digits */ |
||
| 36 | const LN_10 = '2.302585092994045684017991454684364207601101488628772976033327900967572609677352480235997205089598298'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param $type |
||
| 40 | * @param $value |
||
| 41 | * @param int|null $precision |
||
| 42 | * @param int $base |
||
| 43 | * |
||
| 44 | * @throws IntegrityConstraint |
||
| 45 | * @return ImmutableNumber|MutableNumber|ImmutableFraction|MutableFraction|Currency|CartesianCoordinate|NumberInterface|FractionInterface|CoordinateInterface |
||
| 46 | */ |
||
| 47 | 23 | public static function make($type, $value, $precision = null, $base = 10) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param $type |
||
| 109 | * @param $value |
||
| 110 | * @param int|null $precision |
||
| 111 | * @param int $base |
||
| 112 | * @return NumberInterface |
||
| 113 | */ |
||
| 114 | public static function makeFromBase10($type, $value, $precision = null, $base = 10) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param $type |
||
| 126 | * @param int|float|string|NumberInterface|DecimalInterface|FractionInterface $value |
||
| 127 | * @param int|null $precision |
||
| 128 | * @param int $base |
||
| 129 | * |
||
| 130 | * @throws IntegrityConstraint |
||
| 131 | * @return ImmutableNumber|MutableNumber|NumberInterface|ImmutableNumber[]|MutableNumber[]|NumberInterface[] |
||
| 132 | */ |
||
| 133 | 25 | public static function makeOrDont($type, $value, $precision = null, $base = 10) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param $value |
||
| 168 | * @param $type |
||
| 169 | * |
||
| 170 | * @return ImmutableFraction|MutableFraction|FractionInterface |
||
| 171 | * @throws IntegrityConstraint |
||
| 172 | */ |
||
| 173 | public static function makeFractionFromString($value, $type = self::IMMUTABLE_FRACTION, $base = 10) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param int|null $precision |
||
| 217 | * |
||
| 218 | * @throws IntegrityConstraint |
||
| 219 | * @return NumberInterface |
||
| 220 | */ |
||
| 221 | 5 | View Code Duplication | public static function makePi($precision = null) |
| 239 | |||
| 240 | /** |
||
| 241 | * @param int|null $precision |
||
| 242 | * |
||
| 243 | * @throws IntegrityConstraint |
||
| 244 | * @return NumberInterface |
||
| 245 | */ |
||
| 246 | 6 | View Code Duplication | public static function makeTau($precision = null) |
| 262 | |||
| 263 | /** |
||
| 264 | * @param int|null $precision |
||
| 265 | * |
||
| 266 | * @return NumberInterface |
||
| 267 | */ |
||
| 268 | 6 | public static function make2Pi($precision = null) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @param int|null $precision |
||
| 275 | * |
||
| 276 | * @throws IntegrityConstraint |
||
| 277 | * @return NumberInterface |
||
| 278 | */ |
||
| 279 | 4 | View Code Duplication | public static function makeE($precision = null) |
| 297 | |||
| 298 | /** |
||
| 299 | * @param int|null $precision |
||
| 300 | * |
||
| 301 | * @throws IntegrityConstraint |
||
| 302 | * @return NumberInterface |
||
| 303 | */ |
||
| 304 | View Code Duplication | public static function makeGoldenRatio($precision = null) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * @param int|null $precision |
||
| 325 | * |
||
| 326 | * @throws IntegrityConstraint |
||
| 327 | * @return NumberInterface |
||
| 328 | */ |
||
| 329 | 1 | View Code Duplication | public static function makeNaturalLog10($precision = null) |
| 347 | |||
| 348 | /** |
||
| 349 | * @return ImmutableNumber |
||
| 350 | */ |
||
| 351 | 12 | public static function makeOne($precision = null) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @return ImmutableNumber |
||
| 358 | */ |
||
| 359 | 12 | public static function makeZero($precision = null) |
|
| 363 | |||
| 364 | } |