Passed
Push — master ( bc9194...646e1f )
by Gabriel
11:53
created

money_format()   F

Complexity

Conditions 30
Paths > 20000

Size

Total Lines 86
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 930

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 30
eloc 68
c 1
b 0
f 1
nc 172034
nop 2
dl 0
loc 86
ccs 0
cts 62
cp 0
crap 930
rs 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
use ByTIC\Money\Models\Currencies\CurrenciesTrait;
4
use Nip\Records\AbstractModels\RecordManager;
5
use Nip\Records\Locator\ModelLocator;
6
7
if (!function_exists('currencyManager')) {
8
    /**
9
     * @return CurrenciesTrait|RecordManager
10
     */
11
    function currencyManager()
12
    {
13
        return ModelLocator::get('currencies');
14
    }
15
}
16
17
18
if (!function_exists('money_format')) {
19
    function money_format($format, $number)
20
    {
21
        $regex = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?'.
22
            '(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/';
23
        if (setlocale(LC_MONETARY, 0) == 'C') {
24
            setlocale(LC_MONETARY, '');
25
        }
26
        $locale = localeconv();
27
        preg_match_all($regex, $format, $matches, PREG_SET_ORDER);
28
        foreach ($matches as $fmatch) {
29
            $value = floatval($number);
30
            $flags = [
31
                'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ?
32
                    $match[1] : ' ',
33
                'nogroup' => preg_match('/\^/', $fmatch[1]) > 0,
34
                'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ?
35
                    $match[0] : '+',
36
                'nosimbol' => preg_match('/\!/', $fmatch[1]) > 0,
37
                'isleft' => preg_match('/\-/', $fmatch[1]) > 0,
38
            ];
39
            $width = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
40
            $left = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
41
            $right = trim($fmatch[4]) ? (int)$fmatch[4] : $locale['int_frac_digits'];
42
            $conversion = $fmatch[5];
43
44
            $positive = true;
45
            if ($value < 0) {
46
                $positive = false;
47
                $value *= -1;
48
            }
49
            $letter = $positive ? 'p' : 'n';
50
51
            $prefix = $suffix = $cprefix = $csuffix = $signal = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $signal is dead and can be removed.
Loading history...
52
53
            $signal = $positive ? $locale['positive_sign'] : $locale['negative_sign'];
54
            switch (true) {
55
                case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+':
56
                    $prefix = $signal;
57
                    break;
58
                case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+':
59
                    $suffix = $signal;
60
                    break;
61
                case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+':
62
                    $cprefix = $signal;
63
                    break;
64
                case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+':
65
                    $csuffix = $signal;
66
                    break;
67
                case $flags['usesignal'] == '(':
68
                case $locale["{$letter}_sign_posn"] == 0:
69
                    $prefix = '(';
70
                    $suffix = ')';
71
                    break;
72
            }
73
            if (!$flags['nosimbol']) {
74
                $currency = $cprefix.
75
                    ($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']).
76
                    $csuffix;
77
            } else {
78
                $currency = '';
79
            }
80
            $space = $locale["{$letter}_sep_by_space"] ? ' ' : '';
81
82
            $value = number_format($value, $right, $locale['mon_decimal_point'],
83
                $flags['nogroup'] ? '' : $locale['mon_thousands_sep']);
84
            $value = @explode($locale['mon_decimal_point'], $value);
85
86
            $n = strlen($prefix) + strlen($currency) + strlen($value[0]);
87
            if ($left > 0 && $left > $n) {
88
                $value[0] = str_repeat($flags['fillchar'], $left - $n).$value[0];
89
            }
90
            $value = implode($locale['mon_decimal_point'], $value);
91
            if ($locale["{$letter}_cs_precedes"]) {
92
                $value = $prefix.$currency.$space.$value.$suffix;
93
            } else {
94
                $value = $prefix.$value.$space.$currency.$suffix;
95
            }
96
            if ($width > 0) {
97
                $value = str_pad($value, $width, $flags['fillchar'], $flags['isleft'] ?
98
                    STR_PAD_RIGHT : STR_PAD_LEFT);
99
            }
100
101
            $format = str_replace($fmatch[0], $value, $format);
102
        }
103
104
        return $format;
105
    }
106
}