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

Math::pow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 3
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 Math 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 $this->round($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 $this->round($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 $this->round($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 $this->round($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 $this->round($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
        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

81
        return ceil(/** @scrutinizer ignore-type */ $number);
Loading history...
82
    }
83
84
    /**
85
     * @param string|int|float $number
86
     * @return string
87
     */
88
    public function floor($number): string
89
    {
90
        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

90
        return floor(/** @scrutinizer ignore-type */ $number);
Loading history...
91
    }
92
93
    /**
94
     * @param string|int|float $number
95
     * @param int $precision
96
     * @return string
97
     */
98
    public function round($number, int $precision = 0): string
99
    {
100
        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

100
        return round(/** @scrutinizer ignore-type */ $number, $precision);
Loading history...
101
    }
102
103
    /**
104
     * @param $first
105
     * @param $second
106
     * @return int
107
     */
108
    public function compare($first, $second): int
109
    {
110
        return $first <=> $second;
111
    }
112
113
    /**
114
     * @param int $scale
115
     * @return int
116
     */
117
    protected function scale(?int $scale = null): int
118
    {
119
        if ($scale !== null) {
120
            return $scale;
121
        }
122
123
        if ($this->scale === null) {
124
            $this->scale = (int)config('wallet.math.scale', 64);
125
        }
126
127
        return $this->scale;
128
    }
129
130
}
131