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 |
||
| 10 | class Toolkit |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Check if value is positive |
||
| 14 | */ |
||
| 15 | 236 | public function isPositive(string $value): bool |
|
| 19 | |||
| 20 | /** |
||
| 21 | * Check if value is even |
||
| 22 | */ |
||
| 23 | 13 | public function isEven(string $value): bool |
|
| 27 | |||
| 28 | /** |
||
| 29 | * Extract the precision used in $value |
||
| 30 | */ |
||
| 31 | 292 | public function parsePrecision(string $value): int |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Get one unit in the scale of $precision |
||
| 44 | */ |
||
| 45 | 55 | public function getOneUnit(int $precision): string |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Round value towards zero |
||
| 56 | */ |
||
| 57 | 261 | public function roundTowardsZero(string $value, int $precision): string |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Round value away from zero |
||
| 64 | */ |
||
| 65 | 22 | public function roundAwayFromZero(string $value, int $precision): string |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Round up value |
||
| 80 | */ |
||
| 81 | 44 | View Code Duplication | public function roundUp(string $value, int $precision): string |
| 97 | |||
| 98 | /** |
||
| 99 | * Round down value |
||
| 100 | */ |
||
| 101 | 198 | View Code Duplication | public function roundDown(string $value, int $precision): string |
| 117 | |||
| 118 | /** |
||
| 119 | * Get tiebreak value for round to nearest strategies |
||
| 120 | */ |
||
| 121 | 184 | public function getTiebreak(string $value, int $precision): string |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Round to nearest using callback for breaking ties |
||
| 128 | */ |
||
| 129 | 221 | public function roundToNearest(string $value, int $precision, callable $tiebreakCallback): string |
|
| 144 | } |
||
| 145 |