Passed
Pull Request — master (#140)
by Бабичев
05:29
created

BCMath::scale()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Bavix\Wallet\Simple;
4
5
use Bavix\Wallet\Interfaces\Mathable;
6
7
/**
8
 * Class MathService
9
 * @package Bavix\Wallet\Services
10
 * @codeCoverageIgnore
11
 */
12
class BCMath implements Mathable
13
{
14
15
    /**
16
     * @var int
17
     */
18
    protected $scale;
19
20
    /**
21
     * @param string|int|float $first
22
     * @param string|int|float $second
23
     * @param null|int $scale
24
     * @return string
25
     */
26
    public function add($first, $second, ?int $scale = null): string
27
    {
28
        return bcadd($first, $second, $this->scale($scale));
29
    }
30
31
    /**
32
     * @param string|int|float $first
33
     * @param string|int|float $second
34
     * @param null|int $scale
35
     * @return string
36
     */
37
    public function sub($first, $second, ?int $scale = null): string
38
    {
39
        return bcsub($first, $second, $this->scale($scale));
40
    }
41
42
    /**
43
     * @param string|int|float $first
44
     * @param string|int|float $second
45
     * @param null|int $scale
46
     * @return float|int|string|null
47
     */
48
    public function div($first, $second, ?int $scale = null): string
49
    {
50
        return bcdiv($first, $second, $this->scale($scale));
51
    }
52
53
    /**
54
     * @param string|int|float $first
55
     * @param string|int|float $second
56
     * @param null|int $scale
57
     * @return float|int|string
58
     */
59
    public function mul($first, $second, ?int $scale = null): string
60
    {
61
        return bcmul($first, $second, $this->scale($scale));
62
    }
63
64
    /**
65
     * @param string|int|float $first
66
     * @param string|int|float $second
67
     * @param null|int $scale
68
     * @return string
69
     */
70
    public function pow($first, $second, ?int $scale = null): string
71
    {
72
        return bcpow($first, $second, $this->scale($scale));
73
    }
74
75
    /**
76
     * @param string|int|float $number
77
     * @return string
78
     */
79
    public function ceil($number): string
80
    {
81
        if (strpos($number, '.') !== false) {
82
            if (preg_match("~\.[0]+$~", $number)) {
83
                return $this->bcround($number, 0);
0 ignored issues
show
Bug introduced by
The method bcround() does not exist on Bavix\Wallet\Simple\BCMath. Did you maybe mean round()? ( Ignorable by Annotation )

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

83
                return $this->/** @scrutinizer ignore-call */ bcround($number, 0);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
            }
85
            if ($number[0] !== '-') {
86
                return bcadd($number, 1, 0);
87
            }
88
            return bcsub($number, 0, 0);
89
        }
90
        return $number;
91
    }
92
93
    /**
94
     * @param string|int|float $number
95
     * @return string
96
     */
97
    public function floor($number): string
98
    {
99
        if (strpos($number, '.') === false) {
100
            return $number;
101
        }
102
103
        if (preg_match("~\.[0]+$~", $number)) {
104
            return $this->round($number, 0);
105
        }
106
107
        if ($number[0] !== '-') {
108
            return bcadd($number, 0, 0);
109
        }
110
111
        return bcsub($number, 1, 0);
112
    }
113
114
    /**
115
     * @param string|int|float $number
116
     * @param int $precision
117
     * @return string
118
     */
119
    public function round($number, int $precision = 0): string
120
    {
121
        if (strpos($number, '.') === false) {
122
            return $number;
123
        }
124
125
        if ($number[0] !== '-') {
126
            return bcadd($number, '0.' . str_repeat('0', $precision) . '5', $precision);
127
        }
128
129
        return bcsub($number, '0.' . str_repeat('0', $precision) . '5', $precision);
130
    }
131
132
    /**
133
     * @param $first
134
     * @param $second
135
     * @return int
136
     */
137
    public function compare($first, $second): int
138
    {
139
        return bccomp($first, $second, $this->scale());
140
    }
141
142
    /**
143
     * @param int $scale
144
     * @return int
145
     */
146
    protected function scale(?int $scale = null): int
147
    {
148
        if ($scale !== null) {
149
            return $scale;
150
        }
151
152
        if ($this->scale === null) {
153
            $this->scale = (int)config('wallet.math.scale', 64);
154
        }
155
156
        return $this->scale;
157
    }
158
159
}
160