Test Failed
Push — master ( 7fb687...840826 )
by Gabriel
12:03
created

Money::convertResult()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
namespace ByTIC\Money;
4
5
use Money\Currency;
6
7
/**
8
 * Class Money
9
 * @package ByTIC\Money
10
 */
11
class Money
12
{
13
    use Traits\CurrenciesTrait;
14
    use Traits\HasMoneyTrait;
15
    use Traits\LocaleTrait;
16
    use Traits\MoneyFactory {
17
        Traits\MoneyFactory::__callStatic as factoryCallStatic;
18
    }
19
    use Traits\MoneyFormatterTrait;
20
    use Traits\MoneyParserTrait;
21
22
    public const DEFAULT_CURRENCY = 'USD';
23
24
    /**
25
     * Money.
26
     *
27
     * @param int|string      $amount
28
     * @param \Money\Currency $currency
29
     */
30
    public function __construct($amount, Currency $currency)
31
    {
32
        $this->money = new \Money\Money($amount, $currency);
33
    }
34
35
    /**
36
     * __call.
37
     *
38
     * @param string $method
39
     * @param array  $arguments
40
     *
41
     * @return \ByTIC\Money\Money|\ByTIC\Money\Money[]|mixed
42
     */
43
    public function __call($method, array $arguments)
44
    {
45
//        if (static::hasMacro($method)) {
46
//            return $this->macroCall($method, $arguments);
47
//        }
48
49
        if (!method_exists($this->money, $method)) {
50
            return $this;
51
        }
52
53
        $result = call_user_func_array([$this->money, $method], static::getArguments($arguments));
54
55
        $methods = [
56
            'add', 'subtract',
57
            'multiply', 'divide', 'mod',
58
            'absolute', 'negative',
59
            'allocate', 'allocateTo',
60
        ];
61
62
        if (!in_array($method, $methods)) {
63
            return $result;
64
        }
65
66
        return static::convertResult($result);
67
    }
68
69
70
    /**
71
     * __toString.
72
     *
73
     * @return string
74
     */
75
    public function __toString(): string
76
    {
77
        return $this->render();
78
    }
79
80
    /**
81
     * __callStatic.
82
     *
83
     * @param string $method
84
     * @param array $arguments
85
     *
86
     * @return \ByTIC\Money\Money
87
     */
88
    public static function __callStatic(string $method, array $arguments): Money
89
    {
90
        if (in_array($method, ['min', 'max', 'avg', 'sum'])) {
91
            $result = call_user_func_array([\Money\Money::class, $method], static::getArguments($arguments));
92
93
            return static::convert($result);
94
        }
95
96
        return static::factoryCallStatic($method, $arguments);
0 ignored issues
show
Bug introduced by
The method factoryCallStatic() does not exist on ByTIC\Money\Money. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

96
        return static::/** @scrutinizer ignore-call */ factoryCallStatic($method, $arguments);
Loading history...
97
    }
98
99
    /**
100
     * Convert.
101
     *
102
     * @param \Money\Money $instance
103
     *
104
     * @return \ByTIC\Money\Money
105
     */
106
    public static function convert(\Money\Money $instance): Money
107
    {
108
        return static::fromMoney($instance);
109
    }
110
    
111
    /**
112
     * Get the evaluated contents of the object.
113
     *
114
     * @return string
115
     */
116
    public function render()
117
    {
118
        return $this->format();
119
    }
120
121
    /**
122
     * Get arguments.
123
     *
124
     * @param array $arguments
125
     *
126
     * @return array
127
     */
128
    private static function getArguments(array $arguments = [])
129
    {
130
        $args = [];
131
132
        foreach ($arguments as $argument) {
133
            $args[] = $argument instanceof static ? $argument->getMoney() : $argument;
134
        }
135
136
        return $args;
137
    }
138
139
    /**
140
     * Convert result.
141
     *
142
     * @param mixed $result
143
     *
144
     * @return \ByTIC\Money\Money|\ByTIC\Money\Money[]
145
     */
146
    private static function convertResult($result)
147
    {
148
        if (!is_array($result)) {
149
            return static::convert($result);
150
        }
151
152
        $results = [];
153
154
        foreach ($result as $item) {
155
            $results[] = static::convert($item);
156
        }
157
158
        return $results;
159
    }
160
}