Test Failed
Push — master ( 646e1f...19314b )
by Gabriel
02:47
created

money_formatter()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
ccs 0
cts 5
cp 0
crap 12
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
if (!function_exists('money_formatter')) {
18
    /**
19
     * @return \ByTIC\Money\Formatter\Manager
20
     */
21
    function money_formatter()
22
    {
23
        if (function_exists('app') && app() instanceof Nip\Container\Container) {
24
            return app('money.formatter');
0 ignored issues
show
Unused Code introduced by
The call to app() has too many arguments starting with 'money.formatter'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
            return /** @scrutinizer ignore-call */ app('money.formatter');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug Best Practice introduced by
The expression return app('money.formatter') returns the type Nip\Container\Container which is incompatible with the documented return type ByTIC\Money\Formatter\Manager.
Loading history...
25
        }
26
27
        return \ByTIC\Money\Formatter\Manager::instance();
28
    }
29
}
30
31
if (!function_exists('money_format')) {
32
    function money_format($format, $number)
33
    {
34
        $regex = '/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?' .
35
            '(?:#([0-9]+))?(?:\.([0-9]+))?([in%])/';
36
        if (setlocale(LC_MONETARY, 0) == 'C') {
37
            setlocale(LC_MONETARY, '');
38
        }
39
        $locale = localeconv();
40
        preg_match_all($regex, $format, $matches, PREG_SET_ORDER);
41
        foreach ($matches as $fmatch) {
42
            $value = floatval($number);
43
            $flags = [
44
                'fillchar' => preg_match('/\=(.)/', $fmatch[1], $match) ?
45
                    $match[1] : ' ',
46
                'nogroup' => preg_match('/\^/', $fmatch[1]) > 0,
47
                'usesignal' => preg_match('/\+|\(/', $fmatch[1], $match) ?
48
                    $match[0] : '+',
49
                'nosimbol' => preg_match('/\!/', $fmatch[1]) > 0,
50
                'isleft' => preg_match('/\-/', $fmatch[1]) > 0,
51
            ];
52
            $width = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
53
            $left = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
54
            $right = trim($fmatch[4]) ? (int)$fmatch[4] : $locale['int_frac_digits'];
55
            $conversion = $fmatch[5];
56
57
            $positive = true;
58
            if ($value < 0) {
59
                $positive = false;
60
                $value *= -1;
61
            }
62
            $letter = $positive ? 'p' : 'n';
63
64
            $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...
65
66
            $signal = $positive ? $locale['positive_sign'] : $locale['negative_sign'];
67
            switch (true) {
68
                case $locale["{$letter}_sign_posn"] == 1 && $flags['usesignal'] == '+':
69
                    $prefix = $signal;
70
                    break;
71
                case $locale["{$letter}_sign_posn"] == 2 && $flags['usesignal'] == '+':
72
                    $suffix = $signal;
73
                    break;
74
                case $locale["{$letter}_sign_posn"] == 3 && $flags['usesignal'] == '+':
75
                    $cprefix = $signal;
76
                    break;
77
                case $locale["{$letter}_sign_posn"] == 4 && $flags['usesignal'] == '+':
78
                    $csuffix = $signal;
79
                    break;
80
                case $flags['usesignal'] == '(':
81
                case $locale["{$letter}_sign_posn"] == 0:
82
                    $prefix = '(';
83
                    $suffix = ')';
84
                    break;
85
            }
86
            if (!$flags['nosimbol']) {
87
                $currency = $cprefix .
88
                    ($conversion == 'i' ? $locale['int_curr_symbol'] : $locale['currency_symbol']) .
89
                    $csuffix;
90
            } else {
91
                $currency = '';
92
            }
93
            $space = $locale["{$letter}_sep_by_space"] ? ' ' : '';
94
95
            $value = number_format($value, $right, $locale['mon_decimal_point'],
96
                $flags['nogroup'] ? '' : $locale['mon_thousands_sep']);
97
            $value = @explode($locale['mon_decimal_point'], $value);
98
99
            $n = strlen($prefix) + strlen($currency) + strlen($value[0]);
100
            if ($left > 0 && $left > $n) {
101
                $value[0] = str_repeat($flags['fillchar'], $left - $n) . $value[0];
102
            }
103
            $value = implode($locale['mon_decimal_point'], $value);
104
            if ($locale["{$letter}_cs_precedes"]) {
105
                $value = $prefix . $currency . $space . $value . $suffix;
106
            } else {
107
                $value = $prefix . $value . $space . $currency . $suffix;
108
            }
109
            if ($width > 0) {
110
                $value = str_pad($value, $width, $flags['fillchar'], $flags['isleft'] ?
111
                    STR_PAD_RIGHT : STR_PAD_LEFT);
112
            }
113
114
            $format = str_replace($fmatch[0], $value, $format);
115
        }
116
117
        return $format;
118
    }
119
}