Passed
Push — master ( 16f2ad...0b7e9e )
by Бабичев
01:40 queued 12s
created

BCMath::isNegative()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Wallet\Simple;
4
5
/**
6
 * Class BCMath
7
 * @package Bavix\Wallet\Simple
8
 */
9
class BCMath extends Math
10
{
11
12
    /**
13
     * @inheritDoc
14
     */
15 102
    public function add($first, $second, ?int $scale = null): string
16
    {
17 102
        return bcadd($first, $second, $this->scale($scale));
18
    }
19
20
    /**
21
     * @inheritDoc
22
     */
23 51
    public function sub($first, $second, ?int $scale = null): string
24
    {
25 51
        return bcsub($first, $second, $this->scale($scale));
26
    }
27
28
    /**
29
     * @inheritDoc
30
     */
31 29
    public function div($first, $second, ?int $scale = null): string
32
    {
33 29
        return bcdiv($first, $second, $this->scale($scale));
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39 31
    public function mul($first, $second, ?int $scale = null): string
40
    {
41 31
        return bcmul($first, $second, $this->scale($scale));
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47 15
    public function pow($first, $second, ?int $scale = null): string
48
    {
49 15
        return bcpow($first, $second, $this->scale($scale));
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55 1
    public function ceil($number): string
56
    {
57 1
        if (strpos($number, '.') === false) {
58 1
            return $number;
59
        }
60
61 1
        if (preg_match("~\.[0]+$~", $number)) {
62 1
            return $this->round($number, 0);
63
        }
64
65 1
        if ($this->isNegative($number)) {
66 1
            return bcsub($number, 0, 0);
67
        }
68
69 1
        return bcadd($number, 1, 0);
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 16
    public function floor($number): string
76
    {
77 16
        if (strpos($number, '.') === false) {
78 1
            return $number;
79
        }
80
81 16
        if (preg_match("~\.[0]+$~", $number)) {
82 4
            return $this->round($number, 0);
83
        }
84
85 13
        if ($this->isNegative($number)) {
86 1
            return bcsub($number, 1, 0);
87
        }
88
89 13
        return bcadd($number, 0, 0);
90
    }
91
92
    /**
93
     * @inheritDoc
94
     */
95 122
    public function round($number, int $precision = 0): string
96
    {
97 122
        if (strpos($number, '.') === false) {
98 120
            return $number;
99
        }
100
101 104
        if ($this->isNegative($number)) {
102 23
            return bcsub($number, '0.' . str_repeat('0', $precision) . '5', $precision);
103
        }
104
105 100
        return bcadd($number, '0.' . str_repeat('0', $precision) . '5', $precision);
106
    }
107
108
    /**
109
     * @param float|int|string $number
110
     * @return string
111
     */
112 50
    public function abs($number): string
113
    {
114 50
        if (!preg_match('~^-?\d*(\.\d*)?$~', $number, $matches)) {
115 1
            return 0;
116
        }
117
118 50
        $digits = $matches[0] ?? '0';
119 50
        $division = $matches[1] ?? '.';
120 50
        if ($digits === '.' && $division === '.') {
121 1
            return 0;
122
        }
123
124 50
        if ($this->isNegative($number)) {
125 50
            return substr($number, 1);
126
        }
127
128 50
        return $number;
129
    }
130
131
    /**
132
     * @inheritDoc
133
     */
134 106
    public function compare($first, $second): int
135
    {
136 106
        return bccomp($first, $second, $this->scale());
137
    }
138
139
    /**
140
     * @param $number
141
     * @return bool
142
     */
143 105
    protected function isNegative($number): bool
144
    {
145 105
        return strpos($number, '-') === 0;
146
    }
147
148
}
149