Lines
100.00%
17 / 17
Methods
100.00%
4 / 4
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| put | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| at | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| holds | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 2 | ||
| 15 | abstract class HeroSlots | |
| 16 | { | |
| 17 | /** @var array<string, ItemId> */ | |
| 18 | private array $slots = []; | |
| 19 | ||
| 20 | final public function __construct( | |
| 21 | private readonly Stock $kept, | |
| 22 | ) {} | |
| 23 | ||
| 24 | /** @throws InvalidArgumentException */ | |
| 25 | protected function put(Equipment $item): void | |
| 26 | { | |
| 27 | if ($this->kept->find($item->id)->isNone()) { | |
| 28 | throw new InvalidArgumentException("Item {$item->id} is not kept, so it cannot be put on."); | |
| 29 | } | |
| 30 | ||
| 31 | $this->slots[$item->slot()->value] = $item->id; | |
| 32 | } | |
| 33 | ||
| 34 | /** @return Option\Option<Equipment> */ | |
| 35 | protected function at(EquipmentSlot $slot): Option\Option | |
| 36 | { | |
| 37 | return Option\from_nullable($this->slots[$slot->value] ?? null) | |
| 38 | ->andThen(fn(ItemId $id): Option\Option => $this->kept->find($id)) | |
| 39 | ->andThen(static fn(Item $item): Option\Option => $item instanceof Equipment | |
| 40 | ? Option\some($item) | |
| 41 | : Option\none()); | |
| 42 | } | |
| 43 | ||
| 44 | protected function holds(ItemId $id): bool | |
| 45 | { | |
| 46 | return $this->kept | |
| 47 | ->find($id) | |
| 48 | ->andThen(static fn(Item $item): Option\Option => $item instanceof Equipment | |
| 49 | ? Option\some($item) | |
| 50 | : Option\none()) | |
| 51 | ->isSomeAnd(fn(Equipment $item): bool => $this->at($item->slot())->isSomeAnd( | |
| 52 | static fn(Equipment $on): bool => $on === $item, | |
| 53 | )); | |
| 54 | } | |
| 55 | } |