IntegerItem::compareTo()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
/*
4
 * Copyright (C) 2019 Sebastian Böttger <[email protected]>
5
 * You may use, distribute and modify this code under the
6
 * terms of the MIT license.
7
 *
8
 * You should have received a copy of the MIT license with
9
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
10
 */
11
12
namespace Seboettg\Forest\Item;
13
14
use Seboettg\Collection\Comparable\Comparable;
15
16
class IntegerItem implements ItemInterface {
17
18
    /**
19
     * @var int
20
     */
21
    protected $value;
22
23
    /**
24
     * Item constructor.
25
     * @param int $value
26
     */
27 10
    public function __construct(int $value)
28
    {
29 10
        $this->value = $value;
30 10
    }
31
32
    /**
33
     * @return int
34
     */
35 11
    public function getValue(): int
36
    {
37 11
        return $this->value;
38
    }
39
40
    /**
41
     * @param Comparable|IntegerItem $b
42
     * @return int
43
     */
44 11
    public function compareTo(Comparable $b): int
45
    {
46
        /** @var IntegerItem $b */
47 11
        if ($this->value === $b->getValue()) {
48 4
            return 0;
49
        }
50 11
        return $this->value < $b->getValue() ? -1 : 1;
51
52
    }
53
54
    /**
55
     * @param ItemInterface|IntegerItem $item
56
     * @return bool
57
     */
58
    public function equals(ItemInterface $item): bool
59
    {
60
        return $this->compareTo($item) === 0;
61
    }
62
}
63