Passed
Push — master ( f05eff...e96706 )
by Paweł
02:59
created

IntegerValue::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Shared;
6
7
class IntegerValue implements \Stringable
8
{
9 32
    final public function __construct(
10
        protected int $value,
11
    ) {
12 32
        $this->validate();
13 29
    }
14
15 28
    public function get(): int
16
    {
17 28
        return $this->value;
18
    }
19
20 3
    public function equals(self $value): bool
21
    {
22 3
        return $this->value === $value->get();
23
    }
24
25 1
    public function isLowerThan(self $value): bool
26
    {
27 1
        return $this->value < $value->get();
28
    }
29
30 8
    public function isGreaterThan(self $value): bool
31
    {
32 8
        return $this->value > $value->get();
33
    }
34
35 4
    public function isGreaterThanOrEqual(self $value): bool
36
    {
37 4
        return $this->value >= $value->get();
38
    }
39
40 3
    public function diff(self $value): static
41
    {
42 3
        return new static(abs($this->value - $value->get()));
0 ignored issues
show
Bug introduced by
It seems like abs($this->value - $value->get()) can also be of type double; however, parameter $value of AardsGerds\Game\Shared\IntegerValue::__construct() does only seem to accept integer, 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

42
        return new static(/** @scrutinizer ignore-type */ abs($this->value - $value->get()));
Loading history...
43
    }
44
45 5
    public function increment(): static
46
    {
47 5
        $this->value += 1;
48
49 5
        return $this;
50
    }
51
52 5
    public function increaseBy(self $value): static
53
    {
54 5
        $this->value += $value->get();
55
56 5
        return $this;
57
    }
58
59 1
    public function __toString(): string
60
    {
61 1
        return (string) $this->value;
62
    }
63
64 23
    protected function validate(): void
65
    {
66 23
        if ($this->value < 0) {
67 1
            throw IntegerValueException::onlyPositive();
68
        }
69 22
    }
70
}
71