Stratadox /
CardGame
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace Stratadox\CardGame\ReadModel\Match; |
||
| 4 | |||
| 5 | use Countable; |
||
| 6 | use Stratadox\CardGame\Match\MatchId; |
||
| 7 | use Stratadox\CardGame\Proposal\ProposalId; |
||
| 8 | use function count; |
||
| 9 | |||
| 10 | class OngoingMatches implements Countable |
||
| 11 | { |
||
| 12 | /** @var OngoingMatch[] */ |
||
| 13 | private $matches = []; |
||
| 14 | |||
| 15 | public function addFromProposal(ProposalId $proposal, OngoingMatch $match): void |
||
| 16 | { |
||
| 17 | $this->matches[$proposal->id()] = $match; |
||
| 18 | } |
||
| 19 | |||
| 20 | /** @throws NoSuchMatch */ |
||
| 21 | public function forProposal(ProposalId $proposal): OngoingMatch |
||
| 22 | { |
||
| 23 | if (!isset($this->matches[$proposal->id()])) { |
||
| 24 | throw NoSuchMatch::forProposal($proposal); |
||
| 25 | } |
||
| 26 | return $this->matches[$proposal->id()]; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function withId(MatchId $id): OngoingMatch |
||
| 30 | { |
||
| 31 | foreach ($this->matches as $ongoingMatch) { |
||
| 32 | if ($id->is($ongoingMatch->id())) { |
||
| 33 | return $ongoingMatch; |
||
| 34 | } |
||
| 35 | } |
||
|
0 ignored issues
–
show
|
|||
| 36 | // @todo throw |
||
| 37 | } |
||
| 38 | |||
| 39 | public function count(): int |
||
| 40 | { |
||
| 41 | return count($this->matches); |
||
| 42 | } |
||
| 43 | } |
||
| 44 |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: