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 |
||
55 | { |
||
56 | // $treshold2 = 999; // If you want 0.9k for 999 |
||
57 | $suffix = ''; |
||
58 | $clean_number = ''; |
||
59 | // Check if value is a valid integer and does not start with 0, and force user to pass value as string |
||
60 | self::validateNumber($value); |
||
61 | self::validateRange($value, $range); |
||
|
|||
62 | foreach(self::$tresholds as $suffix => $treshold) { |
||
63 | if ($value < $treshold) { |
||
64 | //if (isset(self::$thousand_format)) |
||
65 | // $clean_number = $value / self::$thousand_format; |
||
66 | $clean_number = $value / self::$formats[$suffix]; |
||
67 | // Round and format number |
||
68 | //die(var_dump($clean_number, $formats[$suffix])); |
||
69 | break; |
||
70 | } |
||
71 | } |
||
72 | $formated_number = number_format($clean_number,$precision); |
||
73 | |||
74 | return (preg_match('/\d\.0$/', $formated_number)) ? rtrim(rtrim($formated_number,'0'), '.').$suffix : $formated_number.$suffix; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Convert 1000 => 1K, 10000 => 10K & 1000000 => 100K |
||
79 | * |
||
80 | * @param string $value |
||
81 | * @param int $precision Number of number after decimal point. |
||
82 | * @return string A string number formated as 1K-1.5K |
||
83 | */ |
||
84 | View Code Duplication | public static function formatThousand($value, $precision): string |
|
102 | |||
103 | /** |
||
104 | * Convert 1,000,000 place to 1M |
||
105 | * @param string $value |
||
106 | * @param int $precision Number of number after decimal point. |
||
107 | * @return string A string number formated as 1M-1.5M |
||
108 | */ |
||
109 | View Code Duplication | public static function formatMillion($value, $precision): string |
|
125 | |||
126 | /** |
||
127 | * Convert 1000,000,000 place to 1B |
||
128 | * @param string $value |
||
129 | * @param int $precision Number of number after decimal point. |
||
130 | * @return string A string number formated as 1B-1.5B |
||
131 | */ |
||
132 | View Code Duplication | public static function formatBillion($value, $precision): string |
|
148 | |||
149 | /** |
||
150 | * Convert 1,000,000,000,000 place to 1T |
||
151 | * @param string $value |
||
152 | * @param int $precision Number of number after decimal point. |
||
153 | * @return string A string number formated as 1B-1.5B |
||
154 | */ |
||
155 | View Code Duplication | public static function formatTrillion($value, $precision): string |
|
171 | |||
172 | |||
173 | /** |
||
174 | * The Current value is not yet suppoerted, typically it greater than the supported values |
||
175 | * @param string $value the invalid number |
||
176 | * @return string |
||
177 | */ |
||
178 | private static function notSupported($value): string |
||
182 | |||
183 | /** |
||
184 | * Validate number |
||
185 | * @param string $value |
||
186 | */ |
||
187 | private static function validateNumber($value) |
||
200 | |||
201 | private static function validateRange($value, $range) |
||
206 | } |
||
207 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.