|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Stratadox\CardGame\Match; |
|
4
|
|
|
|
|
5
|
|
|
use function array_filter; |
|
6
|
|
|
use function array_reduce; |
|
7
|
|
|
use function array_search; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use function count; |
|
10
|
|
|
use Stratadox\ImmutableCollection\ImmutableCollection; |
|
11
|
|
|
|
|
12
|
|
|
final class Cards extends ImmutableCollection |
|
13
|
|
|
{ |
|
14
|
|
|
public function __construct(Card ...$cards) |
|
15
|
|
|
{ |
|
16
|
|
|
parent::__construct(...$cards); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function current(): Card |
|
20
|
|
|
{ |
|
21
|
|
|
return parent::current(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function inHand(): Cards |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->filterBy(static function (Card $card): bool { |
|
27
|
|
|
return $card->isInHand(); |
|
28
|
|
|
}); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function inPlay(): Cards |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->filterBy(static function (Card $card): bool { |
|
34
|
|
|
return $card->isInPlay(); |
|
35
|
|
|
}); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function thatAttack(): Cards |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->filterBy(static function (Card $card): bool { |
|
41
|
|
|
return $card->isAttacking(); |
|
42
|
|
|
}); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function thatDefend(): Cards |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->filterBy(static function (Card $card): bool { |
|
48
|
|
|
return $card->isDefending(); |
|
49
|
|
|
}); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function drawFromTopOfDeck(MatchId $match, int $player): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->draw($this->topMostCardInDeck(), $match, $player); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function theOneThatAttacksTheAmbushOf(Card $defender): Card |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->filterBy(static function (Card $card) use ($defender): bool { |
|
60
|
|
|
return $card->isAttackingThe($defender); |
|
61
|
|
|
})[0]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function topMostCardInDeck(): Card |
|
65
|
|
|
{ |
|
66
|
|
|
return array_reduce( |
|
67
|
|
|
array_filter($this->items(), static function (Card $card): bool { |
|
68
|
|
|
return $card->isInDeck(); |
|
69
|
|
|
}), |
|
70
|
|
|
static function (?Card $topmost, Card $card): ?Card { |
|
71
|
|
|
if ($topmost === null || $card->hasHigherPositionThan($topmost)) { |
|
72
|
|
|
return $card; |
|
73
|
|
|
} |
|
74
|
|
|
return $topmost; |
|
75
|
|
|
} |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function draw(Card $card, MatchId $match, int $player): void |
|
80
|
|
|
{ |
|
81
|
|
|
$card->draw( |
|
82
|
|
|
$match, |
|
83
|
|
|
count($this->inHand()), |
|
84
|
|
|
$player, |
|
85
|
|
|
$this->offsetOf($card) |
|
|
|
|
|
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function offsetOf(Card $target): int |
|
90
|
|
|
{ |
|
91
|
|
|
return (int) array_search($target, $this->items(), true); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function filterBy(Closure $function): Cards |
|
95
|
|
|
{ |
|
96
|
|
|
return new self(...array_filter($this->items(), $function)); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.