Conditions | 11 |
Paths | 9 |
Total Lines | 35 |
Code Lines | 27 |
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 |
||
116 | public static function toDigit(string $cnAmount, string $prefix = '¥', string $cnPrefix = '人民币'): string |
||
117 | { |
||
118 | $amount = preg_replace("/^{$cnPrefix}/", '', $cnAmount); |
||
119 | $amount = preg_replace('/' . self::UNIT[0] . self::SYMBOL[''] . '$/', self::UNIT[0], $amount); |
||
120 | $amounts = mb_str_split($amount); |
||
121 | $amount = $maxUnit = 0; |
||
122 | // 断定是否是正数,默认是 |
||
123 | $plus = 1; |
||
124 | $decimal = 0; |
||
125 | $un = null; |
||
126 | while ($chr = array_pop($amounts)) { |
||
127 | if (($key = array_search($chr, self::DIGITAL)) !== false) { |
||
128 | if (is_null($un)) { |
||
129 | throw new InvalidArgumentException(sprintf('%s is not a valid chinese number text', $cnAmount)); |
||
130 | } |
||
131 | if ($un >= 0) { |
||
132 | $amount = gmp_add($amount, gmp_mul($key, $un < 0 ? 10 ** $un : gmp_pow('10', $un))); |
||
133 | } else { |
||
134 | $decimal += $key * (10 ** $un); |
||
135 | } |
||
136 | $un = null; |
||
137 | } elseif (($key = array_search($chr, self::UNIT)) !== false) { |
||
138 | if (!is_null($un) && $un != 0) { |
||
139 | throw new InvalidArgumentException(sprintf('%s is not a valid chinese number text', $cnAmount)); |
||
140 | } |
||
141 | $un = $key; |
||
142 | $maxUnit = max($maxUnit, $un); |
||
143 | $un = $maxUnit > $un ? $maxUnit + $un : $un; |
||
144 | } elseif ($chr == self::SYMBOL['-']) { |
||
145 | $plus = -1; |
||
146 | } else { |
||
147 | throw new InvalidArgumentException(sprintf('%s is not a valid chinese number text', $cnAmount)); |
||
148 | } |
||
149 | } |
||
150 | return $prefix . gmp_strval(gmp_mul($amount, $plus)) . ltrim(strval($decimal), '0'); |
||
151 | } |
||
152 | } |