Conditions | 19 |
Paths | 523 |
Total Lines | 77 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
42 | public static function round($number) |
||
43 | { |
||
44 | $number = (string)$number; |
||
45 | |||
46 | if (strpos($number, ',') !== false) { |
||
47 | throw new CommaException(); |
||
48 | } |
||
49 | |||
50 | $numberArr = explode('.', "" . $number); |
||
51 | |||
52 | $intPart = $numberArr[0]; |
||
53 | $decimalsNumbers = isset($numberArr[1]) ? (string)$numberArr[1] : '00'; |
||
54 | |||
55 | $down = false; |
||
56 | $up = false; |
||
57 | |||
58 | if (strlen($decimalsNumbers) <= 2) { |
||
59 | $decimalsNumbers = str_pad($decimalsNumbers, 2, '0', STR_PAD_RIGHT); |
||
60 | return ((float)($intPart . '.' . $decimalsNumbers)); |
||
61 | } |
||
62 | |||
63 | $decimalsStr = substr($decimalsNumbers, 0, 2); |
||
64 | $restStr = substr($decimalsNumbers, 2); |
||
65 | |||
66 | if (strlen($restStr) == 1) { |
||
67 | $restStr = $restStr . '0'; |
||
68 | } |
||
69 | |||
70 | $strlenRest = strlen($restStr); |
||
71 | $finalRest = str_pad('5', $strlenRest, '0', STR_PAD_RIGHT); |
||
72 | |||
73 | if ($restStr < $finalRest) { |
||
74 | $down = true; |
||
75 | $up = false; |
||
76 | } else { |
||
77 | if ($restStr > $finalRest) { |
||
78 | $down = false; |
||
79 | $up = true; |
||
80 | } else { |
||
81 | if ($restStr == $finalRest) { |
||
82 | if (((int)$decimalsStr[1]) % 2 == 1) { |
||
83 | $down = false; |
||
84 | $up = true; |
||
85 | } else { |
||
86 | $down = true; |
||
87 | $up = false; |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | |||
93 | $final = 0; |
||
94 | if ($down) { |
||
95 | $final = ((float)($intPart . '.' . $decimalsStr)); |
||
96 | } else { |
||
97 | if ($up) { |
||
98 | $decimals = $decimalsStr + 1; |
||
99 | $sumInt = $decimals >= 100 ? 1 : 0; |
||
100 | if ($sumInt > 0 && $decimals == 100) { |
||
101 | $decimals = '00'; |
||
102 | } else if ($sumInt > 0 && $decimals > 100) { |
||
103 | $decimals = $decimals - 100; |
||
104 | } |
||
105 | |||
106 | if ($decimalsStr[0] == 0 && $decimals < 10) { |
||
107 | $decimals = '0' . $decimals; |
||
108 | } |
||
109 | |||
110 | $final = ((float)(($intPart + $sumInt) . '.' . ($decimals))); |
||
111 | |||
112 | if ($intPart === '-0') { |
||
113 | $final *= -1; |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | |||
118 | return $final; |
||
119 | } |
||
133 |