Passed
Push — master ( 7d3520...28b042 )
by Sebastian
24:23 queued 16:06
created

StringItem   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 70.59%

Importance

Changes 0
Metric Value
wmc 8
eloc 11
dl 0
loc 48
ccs 12
cts 17
cp 0.7059
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 3 1
A __construct() 0 3 1
A getValue() 0 3 1
A __toString() 0 3 1
A compareTo() 0 8 3
A equals() 0 3 1
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 StringItem implements ItemInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $value;
22
23 12
    public function __construct(string $value)
24
    {
25 12
        $this->value = $value;
26 12
    }
27
28
    public function setValue(string $value)
29
    {
30
        $this->value = $value;
31
    }
32
33 27
    public function getValue(): ?string
34
    {
35 27
        return $this->value;
36
    }
37
38
    /**
39
     * @param Comparable|StringItem $b
40
     * @return int
41
     */
42 26
    public function compareTo(Comparable $b): int
43
    {
44
        /** @var StringItem $b */
45 26
        $diff = strcasecmp($this->getValue(), $b->getValue());
46 26
        if ($diff === 0) {
47 11
            return $diff;
48
        }
49 26
        return $diff > 0 ? 1 : -1;
50
    }
51
52 10
    public function __toString()
53
    {
54 10
        return (string)$this->value;
55
    }
56
57
    /**
58
     * @param ItemInterface|StringItem $item
59
     * @return bool
60
     */
61
    public function equals(ItemInterface $item): bool
62
    {
63
        return $this->compareTo($item) === 0;
64
    }
65
}
66