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 | //private static $thousand_format; |
||
22 | //private static $million_format; |
||
23 | //private static $billion_format; |
||
24 | //private static $trillion_format; |
||
25 | // TODO: Add method to use $value / 999 for 999-999999 to return 0.9K instead of rounded it to 1K |
||
26 | /** |
||
27 | * |
||
28 | * @var array Max supported numbers. |
||
29 | */ |
||
30 | private static $tresholds = [ |
||
31 | 'K' => '999999', |
||
32 | 'M' => '999999999', |
||
33 | 'B' => '999999999999', |
||
34 | 'T' => '999999999999000', |
||
35 | ]; |
||
36 | /** |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | private static $formats = [ |
||
41 | 'K' => 1000, |
||
42 | 'M' => 1000000, |
||
43 | 'B' => 1000000000, |
||
44 | 'T' => 1000000000000, |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Convert long number to readable Numbers 1000 => 1K |
||
49 | * |
||
50 | * @param string $value This should be pass as a string for now. |
||
51 | * @param int $precision Number of number after decimal point. |
||
52 | * @return string |
||
53 | */ |
||
54 | public static function formatNumber($value, $precision): string |
||
75 | |||
76 | /** |
||
77 | * Convert 1000 => 1K, 10000 => 10K & 1000000 => 100K |
||
78 | * |
||
79 | * @param string $value |
||
80 | * @param int $precision Number of number after decimal point. |
||
81 | * @return string A string number formated as 1K-1.5K |
||
82 | */ |
||
83 | View Code Duplication | public static function formatThousand($value, $precision): string |
|
101 | |||
102 | /** |
||
103 | * Convert 1,000,000 place to 1M |
||
104 | * @param string $value |
||
105 | * @param int $precision Number of number after decimal point. |
||
106 | * @return string A string number formated as 1M-1.5M |
||
107 | */ |
||
108 | View Code Duplication | public static function formatMillion($value, $precision): string |
|
124 | |||
125 | /** |
||
126 | * Convert 1000,000,000 place to 1B |
||
127 | * @param string $value |
||
128 | * @param int $precision Number of number after decimal point. |
||
129 | * @return string A string number formated as 1B-1.5B |
||
130 | */ |
||
131 | View Code Duplication | public static function formatBillion($value, $precision): string |
|
147 | |||
148 | /** |
||
149 | * Convert 1,000,000,000,000 place to 1T |
||
150 | * @param string $value |
||
151 | * @param int $precision Number of number after decimal point. |
||
152 | * @return string A string number formated as 1B-1.5B |
||
153 | */ |
||
154 | View Code Duplication | public static function formatTrillion($value, $precision): string |
|
170 | |||
171 | |||
172 | /** |
||
173 | * The Current value is not yet suppoerted, typically it greater than the supported values |
||
174 | * @param string $value the invalid number |
||
175 | * @return string |
||
176 | */ |
||
177 | // private static function notSupported($value): string |
||
178 | // { |
||
179 | // return 'Sorry ' . $value . ' is not Supported!'; |
||
180 | // } |
||
181 | |||
182 | /** |
||
183 | * Validate number |
||
184 | * @param string $value |
||
185 | */ |
||
186 | private static function validateNumber($value) |
||
199 | |||
200 | private static function validateRange($value, $range) |
||
205 | } |
||
206 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.