Completed
Push — master ( 4554c8...3722a4 )
by Dan
02:38
created

Action::__construct()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 4
cts 4
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 3
nop 3
crap 5
1
<?php
2
3
namespace Cysha\Casino\Holdem\Game;
4
5
use Cysha\Casino\Cards\CardCollection;
6
use Cysha\Casino\Game\Chips;
7
use Cysha\Casino\Game\Contracts\Name as NameContract;
8
use InvalidArgumentException;
9
10
class Action
11
{
12
    const DEALT_RIVER = 9;
13
    const DEALT_TURN = 8;
14
    const DEALT_FLOP = 7;
15
16
    const BIG_BLIND = 6;
17
    const SMALL_BLIND = 5;
18
19
    const ALLIN = 4;
20
    const FOLD = 3;
21
    const RAISE = 2;
22
    const CALL = 1;
23
    const CHECK = 0;
24
25
    /**
26
     * @var
27
     */
28
    private $player;
29
30
    /**
31
     * @var int
32 45
     */
33
    private $action;
34 45
35 45
    /**
36 45
     * @var Chips
37 45
     */
38
    private $chips;
39
40
    public function __construct(NameContract $player, int $action, $attributes = [])
41
    {
42 1
        if (isset($attributes['chips']) && !($attributes['chips'] instanceof Chips)) {
43
            throw new InvalidArgumentException('Chip attribute should be instance of Chips');
44 1
        }
45
46
        if (isset($attributes['communityCards']) && !($attributes['communityCards'] instanceof CardCollection)) {
47
            throw new InvalidArgumentException('communityCards attribute should be instance of CardCollection');
48
        }
49
50 1
        $this->player = $player;
51
        $this->action = $action;
52 1
        $this->communityCards = $attributes['communityCards'] ?? CardCollection::make();
0 ignored issues
show
Bug introduced by
The property communityCards does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53
        $this->chips = $attributes['chips'] ?? Chips::zero();
0 ignored issues
show
Documentation Bug introduced by
It seems like $attributes['chips'] ?? ...sino\Game\Chips::zero() can also be of type object<self>. However, the property $chips is declared as type object<Cysha\Casino\Game\Chips>. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
54
    }
55
56
    /**
57
     * @return int
58 1
     */
59
    public function action(): int
60 1
    {
61
        return $this->action;
62
    }
63 7
64
    /**
65 7
     * @return
66 7
     */
67 7
    public function player()
68 1
    {
69 1
        return $this->player;
70
    }
71 6
72 1
    /**
73 1
     * @return Chips
74
     */
75 5
    public function chips(): Chips
76 1
    {
77 1
        return $this->chips;
78
    }
79 4
80 1
    /**
81 1
     * @return CardCollection
82
     */
83 3
    public function communityCards(): CardCollection
84 1
    {
85 1
        return $this->communityCards;
86
    }
87 2
88 1
    public function toString()
89 1
    {
90
        $message = null;
91 1
        switch ($this->action) {
92 1
            case static::DEALT_RIVER:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
93 1
                $message = sprintf('%s has dealt the river (%s).', $this->player->name(), $this->communityCards()->__toString());
94
                break;
95
96 7
            case static::DEALT_TURN:
97
                $message = sprintf('%s has dealt the turn (%s).', $this->player->name(), $this->communityCards()->__toString());
98
                break;
99 7
100
            case static::DEALT_FLOP:
101 7
                $message = sprintf('%s has dealt the flop (%s).', $this->player->name(), $this->communityCards()->__toString());
102
                break;
103
104
            case static::BIG_BLIND:
105
                $message = sprintf('%s has posted Big Blind (%d).', $this->player->name(), $this->chips->amount());
106
                break;
107
108
            case static::SMALL_BLIND:
109
                $message = sprintf('%s has posted Small Blind (%d).', $this->player->name(), $this->chips->amount());
110
                break;
111
112
            case static::ALLIN:
113
                $message = sprintf('%s has pushed ALL IN (%d).', $this->player->name(), $this->chips->amount());
114
                break;
115
116
            case static::FOLD:
117
                $message = sprintf('%s has folded.', $this->player->name());
118
                break;
119
120
            case static::RAISE:
121
                $message = sprintf('%s has raised %d.', $this->player->name(), $this->chips->amount());
122
                break;
123
124
            case static::CALL:
125
                $message = sprintf('%s has called %d.', $this->player->name(), $this->chips->amount());
126
                break;
127
128
            case static::CHECK:
129
                $message = sprintf('%s has checked.', $this->player->name());
130
                break;
131
        }
132
133
        return $message;
134
    }
135
136
    public function __toString()
137
    {
138
        return $this->toString();
139
    }
140
}
141