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 |
||
19 | class ShortenNums |
||
20 | { |
||
21 | // TODO: Add method to use $value / 999 for 999-999999 to return 0.9K instead of rounded it to 1K |
||
22 | /** |
||
23 | * |
||
24 | * @var array Max supported numbers. |
||
25 | */ |
||
26 | private static $tresholds = [ |
||
27 | 'K' => '999999', |
||
28 | 'M' => '999999999', |
||
29 | 'B' => '999999999999', |
||
30 | 'T' => '999999999999000', |
||
31 | ]; |
||
32 | /** |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | private static $formats = [ |
||
37 | 'K' => 1000, |
||
38 | 'M' => 1000000, |
||
39 | 'B' => 1000000000, |
||
40 | 'T' => 1000000000000, |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Convert long number to readable Numbers 1000 => 1K |
||
45 | * |
||
46 | * @param int|string $value This should be pass as a string for now. |
||
47 | * @param int $precision Number of number after decimal point. |
||
48 | * @return string |
||
49 | */ |
||
50 | public static function formatNumber($value, $precision): string |
||
67 | |||
68 | /** |
||
69 | * Convert 1000 => 1K, 10000 => 10K & 1000000 => 100K |
||
70 | * |
||
71 | * @param int|string $value |
||
72 | * @param int $precision Number of number after decimal point. |
||
73 | * @return string A string number formated as 1K-1.5K |
||
74 | */ |
||
75 | View Code Duplication | public static function formatThousand($value, $precision): string |
|
87 | |||
88 | /** |
||
89 | * Convert 1,000,000 place to 1M |
||
90 | * @param int|string $value |
||
91 | * @param int $precision Number of number after decimal point. |
||
92 | */ |
||
93 | View Code Duplication | public static function formatMillion($value, $precision): string |
|
105 | |||
106 | /** |
||
107 | * Convert 1000,000,000 place to 1B |
||
108 | * @param int|string $value |
||
109 | * @param int $precision Number of number after decimal point. |
||
110 | */ |
||
111 | View Code Duplication | public static function formatBillion($value, $precision): string |
|
123 | |||
124 | /** |
||
125 | * Convert 1,000,000,000,000 place to 1T |
||
126 | * @param int|string $value |
||
127 | * @param int $precision Number of number after decimal point. |
||
128 | */ |
||
129 | View Code Duplication | public static function formatTrillion($value, $precision): string |
|
141 | |||
142 | |||
143 | /** |
||
144 | * The Current value is not yet suppoerted, typically it greater than the supported values |
||
145 | * @param int|string $value the invalid number |
||
146 | */ |
||
147 | private static function notSupported($value): string |
||
156 | |||
157 | /** |
||
158 | * Validate number |
||
159 | * @param int|string $value |
||
160 | */ |
||
161 | private static function isNotValid($value) |
||
175 | |||
176 | /** |
||
177 | * @param int|string $value |
||
178 | * @param mixed $range |
||
179 | */ |
||
180 | private static function validateRange($value, $range) |
||
191 | } |
||
192 |
It seems like you are relying on a variable being defined by an iteration: