Completed
Push — master ( c8047b...ec489f )
by Dan
02:22
created

CashGame::removePlayer()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 5
cts 5
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Cysha\Casino\Holdem\Game;
4
5
use Cysha\Casino\Cards\Deck;
6
use Cysha\Casino\Exceptions\GameException;
7
use Cysha\Casino\Game\Chips;
8
use Cysha\Casino\Game\Client;
9
use Cysha\Casino\Game\Contracts\Game;
10
use Cysha\Casino\Game\Contracts\GameParameters;
11
use Cysha\Casino\Game\PlayerCollection;
12
use Cysha\Casino\Game\TableCollection;
13
use Cysha\Casino\Holdem\Cards\Evaluators\SevenCard;
14
use Ramsey\Uuid\Uuid;
15
use Ramsey\Uuid\UuidInterface;
16
17
final class CashGame implements Game
18
{
19
    /**
20
     * @var UuidInterface
21
     */
22
    private $id;
23
24
    /**
25
     * @var string
26
     */
27
    private $name;
28
29
    /**
30
     * @var DefaultParameters
31
     */
32
    private $rules;
33
34
    /**
35
     * @var PlayerCollection
36
     */
37
    private $players;
38
39
    /**
40
     * @var TableCollection
41
     */
42
    protected $tables;
43
44
    /**
45
     * CashGame constructor.
46
     *
47
     * @param UuidInterface  $id
48
     * @param string         $name
49 66
     * @param GameParameters $rules
50
     */
51 66
    public function __construct(UuidInterface $id, string $name, GameParameters $rules)
52 66
    {
53 66
        $this->id = $id;
54 66
        $this->name = $name;
55 66
        $this->players = PlayerCollection::make();
56 66
        $this->tables = TableCollection::make();
57
        $this->rules = $rules;
0 ignored issues
show
Documentation Bug introduced by
It seems like $rules of type object<Cysha\Casino\Game...ntracts\GameParameters> is incompatible with the declared type object<Cysha\Casino\Hold...Game\DefaultParameters> of property $rules.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
    }
59
60
    /**
61
     * @param UuidInterface  $id
62
     * @param string         $name
63
     * @param GameParameters $rules
64
     *
65 66
     * @return CashGame
66
     */
67 66
    public static function setUp(UuidInterface $id, string $name, GameParameters $rules)
68
    {
69
        return new self($id, $name, $rules);
70
    }
71
72
    /**
73 1
     * @return UuidInterface
74
     */
75 1
    public function id(): UuidInterface
76
    {
77
        return $this->id;
78
    }
79
80
    /**
81 1
     * @return string
82
     */
83 1
    public function name(): string
84
    {
85
        return $this->name;
86
    }
87
88
    /**
89 4
     * @return GameParameters
90
     */
91 4
    public function rules(): GameParameters
92
    {
93
        return $this->rules;
94
    }
95
96
    /**
97 63
     * @return PlayerCollection
98
     */
99 63
    public function players(): PlayerCollection
100
    {
101
        return $this->players;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function __toString(): string
108 62
    {
109
        return $this->name;
110 62
    }
111
112 62
    /**
113
     * @return TableCollection
114 59
     */
115 62
    public function tables(): TableCollection
116
    {
117 62
        return $this->tables;
118 1
    }
119
120
    /**
121 62
     * @param Client $client
122 1
     * @param Chips  $buyinAmount
123
     *
124
     * @throws GameException
125 61
     */
126
    public function registerPlayer(Client $client, Chips $buyinAmount = null)
127 61
    {
128 61
        $buyinAmount = $buyinAmount ?? $this->rules()->minimumBuyIn();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Cysha\Casino\Game\Contracts\GameParameters as the method minimumBuyIn() does only exist in the following implementations of said interface: Cysha\Casino\Holdem\Game...ters\CashGameParameters.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
129 61
130
        $playerRegistered = $this->players()
131
            ->filter(function (Client $player) use ($client) {
132
                return $client->name() === $player->name();
133
            });
134 2
135
        if ($playerRegistered->count() !== 0) {
136 2
            throw GameException::alreadyRegistered($client, $this);
137
        }
138
139
        if ($buyinAmount->amount() > $client->wallet()->amount()) {
140
            throw GameException::insufficientFunds($client, $this);
141
        }
142 57
143
        $client->wallet()->subtract($buyinAmount);
144
145 57
        $addPlayer = Player::fromClient($client, $buyinAmount);
146
        $this->players()->push($addPlayer);
147 57
    }
148 57
149
    public function removePlayer(Client $client)
150 57
    {
151 57
        $player = $this->players()
152 57
            ->filter(function (Player $player) use ($client) {
153
                return $player->name() === $client->name();
154
            })
155
            ->first()
156
        ;
157 54
158
        if ($player === null) {
159 54
            throw GameException::notRegistered($client, $this);
160
        }
161
162
        $client->wallet()->add($player->chipstack());
163
164
        $this->players = $this->players()
165
            ->reject(function (Player $player) use ($client) {
166
                return $player->name() === $client->name();
167
            })
168
            ->values();
169
170
        $this->tables()
171
            ->each(function (Table $table) use ($client) {
172
                try {
173
                    $table->removePlayer($client);
174
                } catch (Cysha\Casino\Holdem\Exceptions\TableException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class Cysha\Casino\Holdem\Game...ceptions\TableException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
175
176
                }
177
            });
178
    }
179
180
    public function assignPlayersToTables()
181
    {
182
        $groupedPlayers = $this->players()
183
        //->shuffle()
184
            ->chunk($this->rules()->tableSize())
185
            ->map(function (PlayerCollection $players) {
186
                $dealer = Dealer::startWork(new Deck(), new SevenCard());
187
188
                return Table::setUp(Uuid::uuid4(), $dealer, $players);
189
            })
190
            ->toArray();
191
192
        $this->tables = TableCollection::make($groupedPlayers);
193
    }
194
}
195