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

Math::round()   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 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\Simple
10
 */
11
class Math implements Mathable
12
{
13
14
    /**
15
     * @var int
16
     */
17
    protected $scale;
18
19
    /**
20
     * @param string|int|float $first
21
     * @param string|int|float $second
22
     * @param null|int $scale
23
     * @return string
24
     */
25 1
    public function add($first, $second, ?int $scale = null): string
26
    {
27 1
        return $this->round($first + $second, $this->scale($scale));
28
    }
29
30
    /**
31
     * @param string|int|float $first
32
     * @param string|int|float $second
33
     * @param null|int $scale
34
     * @return string
35
     */
36 1
    public function sub($first, $second, ?int $scale = null): string
37
    {
38 1
        return $this->round($first - $second, $this->scale($scale));
39
    }
40
41
    /**
42
     * @param string|int|float $first
43
     * @param string|int|float $second
44
     * @param null|int $scale
45
     * @return float|int|string|null
46
     */
47 1
    public function div($first, $second, ?int $scale = null): string
48
    {
49 1
        return $this->round($first / $second, $this->scale($scale));
50
    }
51
52
    /**
53
     * @param string|int|float $first
54
     * @param string|int|float $second
55
     * @param null|int $scale
56
     * @return float|int|string
57
     */
58 1
    public function mul($first, $second, ?int $scale = null): string
59
    {
60 1
        return $this->round($first * $second, $this->scale($scale));
61
    }
62
63
    /**
64
     * @param string|int|float $first
65
     * @param string|int|float $second
66
     * @param null|int $scale
67
     * @return string
68
     */
69 1
    public function pow($first, $second, ?int $scale = null): string
70
    {
71 1
        return $this->round($first ** $second, $this->scale($scale));
72
    }
73
74
    /**
75
     * @param string|int|float $number
76
     * @return string
77
     */
78 1
    public function ceil($number): string
79
    {
80 1
        return ceil($number);
0 ignored issues
show
Bug introduced by
It seems like $number can also be of type string; however, parameter $value of ceil() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

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

80
        return ceil(/** @scrutinizer ignore-type */ $number);
Loading history...
81
    }
82
83
    /**
84
     * @param string|int|float $number
85
     * @return string
86
     */
87 1
    public function floor($number): string
88
    {
89 1
        return floor($number);
0 ignored issues
show
Bug introduced by
It seems like $number can also be of type string; however, parameter $value of floor() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

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

89
        return floor(/** @scrutinizer ignore-type */ $number);
Loading history...
90
    }
91
92
    /**
93
     * @param float|int|string $number
94
     * @return string
95
     */
96 1
    public function abs($number): string
97
    {
98 1
        return abs($number);
99
    }
100
101
    /**
102
     * @param string|int|float $number
103
     * @param int $precision
104
     * @return string
105
     */
106 6
    public function round($number, int $precision = 0): string
107
    {
108 6
        return round($number, $precision);
0 ignored issues
show
Bug introduced by
It seems like $number can also be of type string; however, parameter $val of round() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

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

108
        return round(/** @scrutinizer ignore-type */ $number, $precision);
Loading history...
109
    }
110
111
    /**
112
     * @param $first
113
     * @param $second
114
     * @return int
115
     */
116 2
    public function compare($first, $second): int
117
    {
118 2
        return $first <=> $second;
119
    }
120
121
    /**
122
     * @param int $scale
123
     * @return int
124
     */
125 120
    protected function scale(?int $scale = null): int
126
    {
127 120
        if ($scale !== null) {
128 59
            return $scale;
129
        }
130
131 120
        if ($this->scale === null) {
132 120
            $this->scale = (int)config('wallet.math.scale', 64);
133
        }
134
135 120
        return $this->scale;
136
    }
137
138
}
139