Passed
Push — master ( 9d8cbf...595f13 )
by Бабичев
05:05 queued 10s
created

BrickMath::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Bavix\Wallet\Simple;
4
5
use Brick\Math\BigDecimal;
6
use Brick\Math\RoundingMode;
7
8
/**
9
 * Class BrickMath.
10
 */
11
class BrickMath extends BCMath
0 ignored issues
show
Deprecated Code introduced by
The class Bavix\Wallet\Simple\BCMath has been deprecated: Will be removed in 6.x. ( Ignorable by Annotation )

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

11
class BrickMath extends /** @scrutinizer ignore-deprecated */ BCMath
Loading history...
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 1
    public function add($first, $second, ?int $scale = null): string
17
    {
18 1
        return (string) BigDecimal::of($first)
19 1
            ->plus(BigDecimal::of($second))
20 1
            ->toScale($this->scale($scale), RoundingMode::DOWN);
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 1
    public function sub($first, $second, ?int $scale = null): string
27
    {
28 1
        return (string) BigDecimal::of($first)
29 1
            ->minus(BigDecimal::of($second))
30 1
            ->toScale($this->scale($scale), RoundingMode::DOWN);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function div($first, $second, ?int $scale = null): string
37
    {
38 1
        return (string) BigDecimal::of($first)
39 1
            ->dividedBy(BigDecimal::of($second), $this->scale($scale), RoundingMode::DOWN);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public function mul($first, $second, ?int $scale = null): string
46
    {
47 1
        return (string) BigDecimal::of($first)
48 1
            ->multipliedBy(BigDecimal::of($second))
49 1
            ->toScale($this->scale($scale), RoundingMode::DOWN);
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function pow($first, $second, ?int $scale = null): string
56
    {
57 1
        return (string) BigDecimal::of($first)
58 1
            ->power($second)
59 1
            ->toScale($this->scale($scale), RoundingMode::DOWN);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function ceil($number): string
66
    {
67 1
        return BigDecimal::of($number)
68 1
            ->dividedBy(BigDecimal::one(), 0, RoundingMode::CEILING);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 1
    public function floor($number): string
75
    {
76 1
        return BigDecimal::of($number)
77 1
            ->dividedBy(BigDecimal::one(), 0, RoundingMode::FLOOR);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 1
    public function round($number, int $precision = 0): string
84
    {
85 1
        return BigDecimal::of($number)
86 1
            ->dividedBy(BigDecimal::one(), $precision, RoundingMode::HALF_UP);
87
    }
88
89
    /**
90
     * @param float|int|string $number
91
     * @return string
92
     */
93 1
    public function abs($number): string
94
    {
95
        try {
96 1
            return (string) BigDecimal::of($number)->abs();
97 1
        } catch (\Throwable $throwable) {
98 1
            return '0'; // fixme: 6.x
99
        }
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 6
    public function compare($first, $second): int
106
    {
107 6
        return BigDecimal::of($first)->compareTo(BigDecimal::of($second));
108
    }
109
}
110