| Total Complexity | 7 |
| Total Lines | 49 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php declare(strict_types=1); |
||
| 8 | final class Purchase |
||
| 9 | { |
||
| 10 | private const WOLF = 'wolf'; |
||
| 11 | private const GOAT = 'goat'; |
||
| 12 | private const CABBAGE = 'cabbage'; |
||
| 13 | private const ALLOWED = [self::WOLF, self::GOAT, self::CABBAGE]; |
||
| 14 | private const EATS = [ |
||
| 15 | self::WOLF => self::GOAT, |
||
| 16 | self::GOAT => self::CABBAGE, |
||
| 17 | self::CABBAGE => null, |
||
| 18 | ]; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | private $type; |
||
| 22 | |||
| 23 | private function __construct(string $type) |
||
| 24 | { |
||
| 25 | assert(in_array($type, self::ALLOWED)); |
||
| 26 | $this->type = $type; |
||
| 27 | } |
||
| 28 | |||
| 29 | public static function wolf(): self |
||
| 30 | { |
||
| 31 | return new self(self::WOLF); |
||
| 32 | } |
||
| 33 | |||
| 34 | public static function goat(): self |
||
| 35 | { |
||
| 36 | return new self(self::GOAT); |
||
| 37 | } |
||
| 38 | |||
| 39 | public static function cabbage(): self |
||
| 40 | { |
||
| 41 | return new self(self::CABBAGE); |
||
| 42 | } |
||
| 43 | |||
| 44 | public static function fromString(string $purchase): self |
||
| 45 | { |
||
| 46 | return new self($purchase); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function eats(Purchase $other): bool |
||
| 50 | { |
||
| 51 | return self::EATS[$this->type] === $other->type; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function __toString(): string |
||
| 57 | } |
||
| 58 | } |
||
| 59 |