Passed
Push — master ( e0fc2c...aef815 )
by Kevin
02:28
created

Number::value()   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 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Aeq\LargestRemainder\Math;
4
5
use Aeq\LargestRemainder\Exception\AlreadyNormalizedException;
6
use Aeq\LargestRemainder\Exception\NotYetNormalizedException;
7
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;
0 ignored issues
show
Documentation Bug introduced by
The property $isNormalized was declared of type double, but true is of type true. 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...
46 14
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Aeq\LargestRemainder\Math\Number which is incompatible with the documented return type integer|double.
Loading history...
47
    }
48
49
    /**
50
     * @return Number
51
     * @throws NotYetNormalizedException
52
     */
53 12
    public function denormalize(): self
54
    {
55 12
        if (false === $this->isNormalized) {
0 ignored issues
show
introduced by
The condition $this->isNormalized is always false. If $this->isNormalized can have other possible types, add them to src/Math/Number.php:21
Loading history...
56 1
            throw new NotYetNormalizedException(1538928762);
57
        }
58 11
        $this->number = $this->number / pow(10, $this->precision);
59 11
        $this->isNormalized = false;
0 ignored issues
show
Documentation Bug introduced by
The property $isNormalized was declared of type double, but false is of type false. 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...
60 11
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Aeq\LargestRemainder\Math\Number which is incompatible with the documented return type integer|double.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Aeq\LargestRemainder\Math\Number which is incompatible with the documented return type integer|double.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Aeq\LargestRemainder\Math\Number which is incompatible with the documented return type integer|double.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Aeq\LargestRemainder\Math\Number which is incompatible with the documented return type integer|double.
Loading history...
90
    }
91
92
    /**
93
     * @return float
94
     */
95 17
    public function value(): float
96
    {
97 17
        return $this->number;
98
    }
99
}
100