Completed
Push — master ( 8fe4e7...fafc98 )
by Dan
23:10
created

Table::playersSatDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Cysha\Casino\Holdem\Game;
4
5
use Cysha\Casino\Game\Contracts\Dealer as DealerContract;
6
use Cysha\Casino\Game\Contracts\Player as PlayerContract;
7
use Cysha\Casino\Game\PlayerCollection;
8
use Cysha\Casino\Game\Table as BaseTable;
9
use Cysha\Casino\Holdem\Exceptions\TableException;
10
use Ramsey\Uuid\Uuid;
11
12
class Table extends BaseTable
13
{
14
    /**
15
     * @var Uuid
16
     */
17
    private $id;
18
19
    /**
20
     * @var Dealer
21
     */
22
    private $dealer;
23
24
    /**
25
     * @var PlayerCollection
26
     */
27
    private $players;
28
29
    /**
30
     * @var PlayerCollection
31
     */
32
    private $playersSatOut;
33
34
    /**
35
     * @var int
36
     */
37
    private $button = 0;
38
39
    /**
40
     * Table constructor.
41
     *
42 66
     * @param Uuid $id
43
     * @param DealerContract $dealer
44 66
     * @param PlayerCollection $players
45 66
     */
46 66
    private function __construct(Uuid $id, DealerContract $dealer, PlayerCollection $players)
47 66
    {
48
        $this->id = $id;
49
        $this->players = $players;
50
        $this->playersSatOut = PlayerCollection::make();
51
        $this->dealer = $dealer;
0 ignored issues
show
Documentation Bug introduced by
$dealer is of type object<Cysha\Casino\Game\Contracts\Dealer>, but the property $dealer was declared to be of type object<Cysha\Casino\Holdem\Game\Dealer>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
52
    }
53
54
    /**
55 66
     * @param Uuid $id
56
     * @param DealerContract $dealer
57 66
     * @param PlayerCollection $players
58
     *
59
     * @return Table
60
     */
61
    public static function setUp(Uuid $id, DealerContract $dealer, PlayerCollection $players)
62
    {
63 64
        return new self($id, $dealer, $players);
64
    }
65 64
66
    /**
67
     * @return Uuid
68
     */
69
    public function id(): Uuid
70
    {
71 52
        return $this->id;
72
    }
73 52
74
    /**
75
     * @return PlayerCollection
76
     */
77
    public function players(): PlayerCollection
78
    {
79 46
        return $this->players;
80
    }
81 46
82
    /**
83
     * @return DealerContract
84
     */
85
    public function dealer(): DealerContract
86
    {
87 12
        return $this->dealer;
88
    }
89 12
90
    /**
91
     * @return int
92
     */
93
    public function button(): int
94
    {
95 9
        return $this->button;
96
    }
97 9
98 9
    /**
99
     * @return PlayerContract
100
     */
101
    public function locatePlayerWithButton(): PlayerContract
102
    {
103 61
        return $this->playersSatDown()->get($this->button);
104
    }
105 61
106
    /**
107
     * @param PlayerContract $player
108
     */
109
    public function sitPlayerOut(PlayerContract $player)
110
    {
111
        $this->playersSatOut = $this->playersSatOut->push($player);
112
    }
113 3
114
    /**
115 3
     * @return PlayerCollection
116 3
     */
117 3
    public function playersSatDown(): PlayerCollection
118 3
    {
119 3
        return $this->players()->diff($this->playersSatOut)->values();
120
    }
121 3
122 1
    /**
123
     * @param PlayerContract $player
124
     *
125 2
     * @throws TableException
126 2
     */
127
    public function giveButtonToPlayer(PlayerContract $player)
128
    {
129
        $playerIndex = $this->playersSatDown()
130
            ->filter
131 15
            ->equals($player)
132
            ->keys()
133 15
            ->first();
134
135 15
        if ($playerIndex === null) {
136 2
            throw TableException::invalidButtonPosition();
137
        }
138 15
139
        $this->button = $playerIndex;
140
    }
141
142
    /**
143 22
     * Moves the button along the table seats.
144
     */
145 22
    public function moveButton()
146
    {
147
        ++$this->button;
148 22
149 22
        if ($this->button >= $this->playersSatDown()->count()) {
150
            $this->button = 0;
151 22
        }
152 22
    }
153
154 22
    /**
155
     * @param PlayerContract $findPlayer
156
     *
157 22
     * @return int
158 22
     */
159
    public function findSeat(PlayerContract $findPlayer): int
160
    {
161 22
        return $this->players()
162 22
            ->filter(function (PlayerContract $player) use ($findPlayer) {
163 22
                return $player->equals($findPlayer);
164
            })
165 22
            ->keys()
166
            ->first();
167
    }
168
169
    /**
170
     * @param string $playerName
171
     *
172
     * @return Player
173 16
     */
174
    public function findPlayerByName($playerName): PlayerContract
175 16
    {
176
        return $this->players()
177 16
            ->filter(function (PlayerContract $player) use ($playerName) {
178 16
                return $player->name() === $playerName;
179 16
            })
180 16
            ->first();
181
    }
182
}
183