| Conditions | 31 |
| Paths | > 20000 |
| Total Lines | 95 |
| Code Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 15 | function money_format($format, $number) |
||
| 16 | { |
||
| 17 | $regex = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'. |
||
| 18 | '(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/'; |
||
| 19 | if (setlocale(LC_MONETARY, 0) == 'C') { |
||
| 20 | setlocale(LC_MONETARY, ''); |
||
| 21 | } |
||
| 22 | $locale = localeconv(); |
||
| 23 | preg_match_all($regex, $format, $matches, PREG_SET_ORDER); |
||
| 24 | foreach ($matches as $fmatch) { |
||
| 25 | $value = floatval($number); |
||
| 26 | $flags = array( |
||
| 27 | 'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ? |
||
| 28 | $match[1] : ' ', |
||
| 29 | 'nogroup' => preg_match('/\^/', $fmatch[1]) > 0, |
||
| 30 | 'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ? |
||
| 31 | $match[0] : '+', |
||
| 32 | 'nosimbol' => preg_match('/\!/', $fmatch[1]) > 0, |
||
| 33 | 'isleft' => preg_match('/\-/', $fmatch[1]) > 0 |
||
| 34 | ); |
||
| 35 | $width = trim($fmatch[2]) ? (int)$fmatch[2] : 0; |
||
| 36 | $left = trim($fmatch[3]) ? (int)$fmatch[3] : 0; |
||
| 37 | $right = trim($fmatch[4]) === '' |
||
| 38 | ? $locale['int_frac_digits'] : (int)$fmatch[4]; |
||
| 39 | $conversion = $fmatch[5]; |
||
| 40 | |||
| 41 | $positive = true; |
||
| 42 | if ($value < 0) { |
||
| 43 | $positive = false; |
||
| 44 | $value *= -1; |
||
| 45 | } |
||
| 46 | $letter = $positive ? 'p' : 'n'; |
||
| 47 | |||
| 48 | $prefix = $suffix = $cprefix = $csuffix = $signal = ''; |
||
|
|
|||
| 49 | |||
| 50 | $signal = $positive |
||
| 51 | ? $locale['positive_sign'] : $locale['negative_sign']; |
||
| 52 | switch (true) { |
||
| 53 | case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+': |
||
| 54 | $prefix = $signal; |
||
| 55 | break; |
||
| 56 | case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+': |
||
| 57 | $suffix = $signal; |
||
| 58 | break; |
||
| 59 | case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+': |
||
| 60 | $cprefix = $signal; |
||
| 61 | break; |
||
| 62 | case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+': |
||
| 63 | $csuffix = $signal; |
||
| 64 | break; |
||
| 65 | case $flags['usesignal'] == '(' && $letter === 'n': |
||
| 66 | case $locale["{$letter}_sign_posn"] == 0: |
||
| 67 | $prefix = '('; |
||
| 68 | $suffix = ')'; |
||
| 69 | break; |
||
| 70 | } |
||
| 71 | if (!$flags['nosimbol']) { |
||
| 72 | $currency = $cprefix . ($conversion == 'i' |
||
| 73 | ? $locale['int_curr_symbol'] : $locale['currency_symbol'] |
||
| 74 | ) . $csuffix; |
||
| 75 | } else { |
||
| 76 | $currency = ''; |
||
| 77 | } |
||
| 78 | $space = $locale["{$letter}_sep_by_space"] ? ' ' : ''; |
||
| 79 | |||
| 80 | $value = number_format( |
||
| 81 | $value, |
||
| 82 | $right, |
||
| 83 | $locale['mon_decimal_point'], |
||
| 84 | $flags['nogroup'] ? '' : $locale['mon_thousands_sep'] |
||
| 85 | ); |
||
| 86 | $value = @explode($locale['mon_decimal_point'], $value); |
||
| 87 | |||
| 88 | $n = strlen($prefix) + strlen($currency) + strlen($value[0]); |
||
| 89 | if ($left > 0 && $left > $n) { |
||
| 90 | $value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0]; |
||
| 91 | } |
||
| 92 | $value = implode($locale['mon_decimal_point'], $value); |
||
| 93 | if ($locale["{$letter}_cs_precedes"]) { |
||
| 94 | $value = $prefix . $currency . $space . $value . $suffix; |
||
| 95 | } else { |
||
| 96 | $value = $prefix . $value . $space . $currency . $suffix; |
||
| 97 | } |
||
| 98 | if ($width > 0) { |
||
| 99 | $value = str_pad( |
||
| 100 | $value, |
||
| 101 | $width, |
||
| 102 | $flags['fillchar'], |
||
| 103 | $flags['isleft'] ? STR_PAD_RIGHT : STR_PAD_LEFT |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | |||
| 107 | $format = str_replace($fmatch[0], $value, $format); |
||
| 108 | } |
||
| 109 | return $format; |
||
| 110 | } |
||
| 112 |