| Conditions | 15 |
| Paths | 134 |
| Total Lines | 35 |
| Code Lines | 26 |
| 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 |
||
| 71 | public static function toCn($amount, $prefix = '¥', string $cnPrefix = '人民币'): string |
||
| 72 | { |
||
| 73 | if (!preg_match(sprintf('/^(%s)?[+\-]?([1-9]\d{0,2}([,]?\d{3})*|0)(\.\d{0,4})?$/', $prefix), $amount)) { |
||
| 74 | throw new InvalidArgumentException(sprintf('%s is not a valid chinese number text', $amount)); |
||
| 75 | } |
||
| 76 | $amount = strtr($amount, [',' => '', $prefix => '']); |
||
| 77 | list($integer, $decimals) = explode('.', strval($amount) . '.', 2); |
||
| 78 | if (($len = strlen($integer)) > 48) { |
||
| 79 | throw new InvalidArgumentException(sprintf('%s is not a valid chinese number text', $amount)); |
||
| 80 | } |
||
| 81 | $integerStr = ''; |
||
| 82 | $i = $len; |
||
| 83 | $unit = 0; |
||
| 84 | while ($i) { |
||
| 85 | $num = $integer[$len - $i--]; |
||
| 86 | if (in_array($num, array_keys(self::SYMBOL), true)) { |
||
| 87 | $integerStr .= self::SYMBOL[$num]; |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | if ($num > 0 || Arr::exists(self::UNIT, $i) && $unit <= $i) { |
||
| 91 | $integerStr .= $num > 0 || $i == 0 ? self::DIGITAL[$num] : ''; |
||
| 92 | $unit = Arr::exists(self::UNIT, $i) ? $i : $i % 4; |
||
| 93 | $integerStr .= self::UNIT[$unit]; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | $decimalStr = ''; |
||
| 97 | $len = strlen($decimals); |
||
| 98 | for ($i = 0; $i < $len; $i++) { |
||
| 99 | $num = $decimals[$i]; |
||
| 100 | if ($num > 0) { |
||
| 101 | $decimalStr .= self::DIGITAL[$num] . self::UNIT[-1 - $i]; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | $integerStr = strpos($integerStr, self::UNIT[0]) ? $integerStr : $integerStr . self::UNIT[0]; |
||
| 105 | return $cnPrefix . $integerStr . ($decimalStr != '' ? $decimalStr : self::SYMBOL['']); |
||
| 106 | } |
||
| 152 | } |