Riverbank   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isFull() 0 3 1
A isStart() 0 3 1
A purchases() 0 3 1
A __toString() 0 6 2
A without() 0 8 2
A canTakeAway() 0 11 4
A isGoal() 0 3 1
A with() 0 8 2
A __construct() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PuzzleSolver\Puzzle\WolfGoatCabbage;
4
5
use function array_diff;
6
use function array_merge;
7
use function count;
8
use function implode;
9
use function sprintf;
10
11
final class Riverbank
12
{
13
    /** @var bool */
14
    private $isStart;
15
    /** @var Purchase[] */
16
    private $purchases;
17
18
    public function __construct(bool $isStart, Purchase ...$purchases)
19
    {
20
        $this->isStart = $isStart;
21
        $this->purchases = $purchases;
22
    }
23
24
    /** @return Purchase[] */
25
    public function purchases(): array
26
    {
27
        return $this->purchases;
28
    }
29
30
    public function isStart(): bool
31
    {
32
        return $this->isStart;
33
    }
34
35
    public function isGoal(): bool
36
    {
37
        return !$this->isStart;
38
    }
39
40
    public function isFull(): bool
41
    {
42
        return count($this->purchases) === 3;
43
    }
44
45
    public function canTakeAway(?Purchase $bringAlong): bool
46
    {
47
        $remaining = array_diff($this->purchases, [$bringAlong]);
48
        foreach ($remaining as $theOne) {
49
            foreach ($remaining as $theOther) {
50
                if ($theOne->eats($theOther)) {
51
                    return false;
52
                }
53
            }
54
        }
55
        return true;
56
    }
57
58
    public function with(?Purchase $purchase): self
59
    {
60
        if (!$purchase) {
61
            return $this;
62
        }
63
        return new self(
64
            $this->isStart,
65
            ...array_merge($this->purchases, [$purchase])
66
        );
67
    }
68
69
    public function without(?Purchase $purchase): self
70
    {
71
        if (!$purchase) {
72
            return $this;
73
        }
74
        return new self(
75
            $this->isStart,
76
            ...array_diff($this->purchases, [$purchase])
77
        );
78
    }
79
80
    public function __toString(): string
81
    {
82
        return sprintf(
83
            '%s bank with %s',
84
            $this->isStart ? 'Start' : 'Goal',
85
            implode(', ', $this->purchases)
86
        );
87
    }
88
}
89