PreciseMoney::lessThan()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Type;
6
7
class PreciseMoney extends Money
8
{
9
    const CALCULATION_SCALE = 10;
10
11
    public function __construct(float $amount = 0)
12
    {
13
        $this->amount = bcmul((string) $amount, '100', self::CALCULATION_SCALE);
0 ignored issues
show
Documentation Bug introduced by
The property $amount was declared of type integer, but bcmul((string) $amount, ...elf::CALCULATION_SCALE) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
14
    }
15
16
    public function add(Money $operand): Money
17
    {
18
        $result = bcadd($this->amount, (string) $operand->getAmount(), self::CALCULATION_SCALE);
19
20
        return Money::fromCents((float) $result);
21
    }
22
23
    public function subtract(Money $operand): Money
24
    {
25
        $result = bcsub($this->amount, (string) $operand->getAmount(), self::CALCULATION_SCALE);
26
27
        return Money::fromCents((float) $result);
28
    }
29
30
    public function multiply(float $multiplier): Money
31
    {
32
        $result = bcmul($this->amount, (string) $multiplier, self::CALCULATION_SCALE);
33
34
        return Money::fromCents((float) $result);
35
    }
36
37
    public function divide(float $divisor): Money
38
    {
39
        $result = bcdiv($this->amount, (string) $divisor, self::CALCULATION_SCALE);
40
41
        return Money::fromCents((float) $result);
42
    }
43
44 View Code Duplication
    public function getPercentage(float $percentage): Money
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $percentage = bcdiv((string) $percentage, '100', self::CALCULATION_SCALE);
47
        $result = bcmul($this->amount, (string) $percentage, self::CALCULATION_SCALE);
48
49
        return Money::fromCents((float) $result);
50
    }
51
52
    public function applyPercentage(float $percentage): Money
53
    {
54
        $percentage = $this->getPercentage($percentage);
55
56
        return $this->add($percentage);
57
    }
58
59 View Code Duplication
    public function getInterest(float $rate, int $duration): Money
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61
        $interest = bcdiv((string) $rate, '100', self::CALCULATION_SCALE);
62
        $result = bcmul(
63
            bcmul(
64
                $this->amount,
65
                (string) $duration,
66
                self::CALCULATION_SCALE
67
            ),
68
            (string) $interest,
69
            self::CALCULATION_SCALE
70
        );
71
72
        return Money::fromCents((float) $result);
73
    }
74
75
    public function applyInterest(float $rate, int $duration): Money
76
    {
77
        $interest = $this->getInterest($rate, $duration);
78
79
        return $this->add($interest);
80
    }
81
82
    public function equals($other): bool
83
    {
84
        if (!($other instanceof Money)) {
85
            return false;
86
        }
87
88
        return bccomp((string) $this->amount, (string) $other->getAmount()) === 0;
89
    }
90
91
    public function greaterThan(Money $other): bool
92
    {
93
        return bccomp((string) $this->amount, (string) $other->getAmount()) === 1;
94
    }
95
96
    public function lessThan(Money $other): bool
97
    {
98
        return bccomp((string) $this->amount, (string) $other->getAmount()) === -1;
99
    }
100
101
    public function getMoneyAmount(): float
102
    {
103
        $money = bcdiv($this->amount, '100', self::CALCULATION_SCALE);
104
105
        return round($money, $this->scale);
106
    }
107
108
    public function getAmount(): int
109
    {
110
        return (int) round($this->amount);
111
    }
112
113
    public function setAmount(float $amount)
114
    {
115
        $this->amount = (string) $amount;
0 ignored issues
show
Documentation Bug introduced by
The property $amount was declared of type integer, but (string) $amount is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
116
    }
117
}
118