Issues (11)

puzzles/SlidingCrates/Crates.php (1 issue)

1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PuzzleSolver\Puzzle\SlidingCrates;
4
5
use RuntimeException;
6
use Stratadox\ImmutableCollection\ImmutableCollection;
7
use Stratadox\ImmutableCollection\Purging;
8
use function implode;
9
use const PHP_EOL;
10
11
final class Crates extends ImmutableCollection
12
{
13
    use Purging;
14
15
    public function __construct(Crate ...$crates)
16
    {
17
        parent::__construct(...$crates);
18
    }
19
20
    public function current(): Crate
21
    {
22
        return parent::current();
23
    }
24
25
    public function after(Push $push): self
26
    {
27
        $crates = $this->items();
28
        foreach ($this as $i => $crate) {
29
            if ($crate->id() === $push->crateId()) {
30
                $crates[$i] = $crate->after($push);
31
                break;
32
            }
33
        }
34
        return $this->newCopy($crates);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->newCopy($crates) returns the type Stratadox\Collection\Collection which includes types incompatible with the type-hinted return Stratadox\PuzzleSolver\Puzzle\SlidingCrates\Crates.
Loading history...
35
    }
36
37
    public function withId(string $id): Crate
38
    {
39
        foreach ($this as $crate) {
40
            if ($crate->id() === $id) {
41
                return $crate;
42
            }
43
        }
44
        throw new RuntimeException("No crate with id $id");
45
    }
46
47
    public function __toString(): string
48
    {
49
        return implode(PHP_EOL, $this->items());
50
    }
51
}
52