Total Complexity | 9 |
Total Lines | 90 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
8 | class Number |
||
9 | { |
||
10 | /** |
||
11 | * @var float |
||
12 | */ |
||
13 | private $number = 0.0; |
||
14 | |||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | private $precision = 0; |
||
19 | |||
20 | /** |
||
21 | * @var float |
||
22 | */ |
||
23 | private $isNormalized = false; |
||
24 | |||
25 | /** |
||
26 | * @param float $number |
||
27 | * @param int $precision |
||
28 | */ |
||
29 | 19 | public function __construct(float $number, int $precision = 0) |
|
30 | { |
||
31 | 19 | $this->number = $number; |
|
32 | 19 | $this->precision = $precision; |
|
33 | 19 | } |
|
34 | |||
35 | /** |
||
36 | * @return Number |
||
37 | * @throws AlreadyNormalizedException |
||
38 | */ |
||
39 | 14 | public function normalize(): self |
|
40 | { |
||
41 | 14 | if ($this->isNormalized) { |
|
42 | 1 | throw new AlreadyNormalizedException(1538928749); |
|
43 | } |
||
44 | 14 | $this->number = $this->number * pow(10, $this->precision); |
|
45 | 14 | $this->isNormalized = true; |
|
|
|||
46 | 14 | return $this; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * @return Number |
||
51 | * @throws NotYetNormalizedException |
||
52 | */ |
||
53 | 12 | public function denormalize(): self |
|
54 | { |
||
55 | 12 | if (false === $this->isNormalized) { |
|
56 | 1 | throw new NotYetNormalizedException(1538928762); |
|
57 | } |
||
58 | 11 | $this->number = $this->number / pow(10, $this->precision); |
|
59 | 11 | $this->isNormalized = false; |
|
60 | 11 | return $this; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param $val |
||
65 | * @return Number |
||
66 | */ |
||
67 | 6 | public function add($val): self |
|
68 | { |
||
69 | 6 | $this->number += $val; |
|
70 | 6 | return $this; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param $val |
||
75 | * @return Number |
||
76 | */ |
||
77 | 5 | public function sub($val): self |
|
78 | { |
||
79 | 5 | $this->number -= $val; |
|
80 | 5 | return $this; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return Number |
||
85 | */ |
||
86 | 12 | public function floor(): self |
|
87 | { |
||
88 | 12 | $this->number = floor($this->number); |
|
89 | 12 | return $this; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return float |
||
94 | */ |
||
95 | 17 | public function value(): float |
|
98 | } |
||
99 | } |
||
100 |
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.