Passed
Push — master ( 3c05f7...479341 )
by Mr
07:34
created

IntValue   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 22
c 3
b 0
f 0
dl 0
loc 86
ccs 25
cts 35
cp 0.7143
rs 10
wmc 20

15 Methods

Rating   Name   Duplication   Size   Complexity  
A isGreaterThan() 0 3 1
A equals() 0 4 1
A isEmpty() 0 3 1
A add() 0 5 2
A isZero() 0 3 1
A __toString() 0 3 2
A subtract() 0 5 2
A fromNative() 0 5 3
A isGreaterThanOrEqualTo() 0 3 1
A zero() 0 3 1
A __construct() 0 3 1
A isLessThanOrEqualTo() 0 3 1
A toNative() 0 3 1
A makeEmpty() 0 3 1
A isLessThan() 0 3 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/value-object project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\ValueObject;
10
11
use Daikon\Interop\Assertion;
12
use Daikon\Interop\MakeEmptyInterface;
13
14
final class IntValue implements MakeEmptyInterface, ValueObjectInterface
15
{
16
    private ?int $value;
17
18
    public function isZero(): bool
19
    {
20
        return $this->value === 0;
21
    }
22
23 8
    public function isEmpty(): bool
24
    {
25 8
        return is_null($this->value);
26
    }
27
28 1
    public function add(self $amount): self
29
    {
30 1
        Assertion::false($this->isEmpty() || $amount->isEmpty(), 'Operands must not be null.');
31
        /** @psalm-suppress PossiblyNullOperand */
32 1
        return self::fromNative($this->toNative() + $amount->toNative());
33
    }
34
35 1
    public function subtract(self $amount): self
36
    {
37 1
        Assertion::false($this->isEmpty() || $amount->isEmpty(), 'Operands must not be null.');
38
        /** @psalm-suppress PossiblyNullOperand */
39 1
        return self::fromNative($this->toNative() - $amount->toNative());
40
    }
41
42
    public function isGreaterThanOrEqualTo(self $comparator): bool
43
    {
44
        return $this->toNative() >= $comparator->toNative();
45
    }
46
47
    public function isLessThanOrEqualTo(self $comparator): bool
48
    {
49
        return $this->toNative() <= $comparator->toNative();
50
    }
51
52
    public function isGreaterThan(self $comparator): bool
53
    {
54
        return $this->toNative() > $comparator->toNative();
55
    }
56
57
    public function isLessThan(self $comparator): bool
58
    {
59
        return $this->toNative() < $comparator->toNative();
60
    }
61
62 4
    public static function makeEmpty(): self
63
    {
64 4
        return new self;
65
    }
66
67 1
    public static function zero(): self
68
    {
69 1
        return new self(0);
70
    }
71
72
    /** @param null|int|string $value  */
73 17
    public static function fromNative($value): self
74
    {
75 17
        $value = $value === '' ? null : $value;
76 17
        Assertion::nullOrIntegerish($value, 'Trying to create IntValue VO from unsupported value type.');
77 17
        return new self(is_null($value) ? null : (int)$value);
78
    }
79
80 11
    public function toNative(): ?int
81
    {
82 11
        return $this->value;
83
    }
84
85
    /** @param self $comparator */
86 1
    public function equals($comparator): bool
87
    {
88 1
        Assertion::isInstanceOf($comparator, self::class);
89 1
        return $this->toNative() === $comparator->toNative();
90
    }
91
92 6
    public function __toString(): string
93
    {
94 6
        return $this->isEmpty() ? '' : (string)$this->value;
95
    }
96
97 17
    private function __construct(int $value = null)
98
    {
99 17
        $this->value = $value;
100 17
    }
101
}
102