ChipPot::total()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
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\Chips;
6
use Cysha\Casino\Game\ChipStackCollection;
7
use Cysha\Casino\Game\Contracts\Player;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Cysha\Casino\Holdem\Game\Player.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Cysha\Casino\Game\PlayerCollection;
9
10
class ChipPot
11
{
12
    /**
13
     * @var ChipStackCollection
14
     */
15
    private $chips;
16
17
    /**
18
     * @var PlayerCollection
19
     */
20
    private $players;
21
22 52
    private function __construct()
23
    {
24 52
        $this->chips = new ChipStackCollection();
25 52
        $this->players = new PlayerCollection();
26 52
    }
27
28
    /**
29
     * @return ChipPot
30
     */
31 52
    public static function create(): ChipPot
32
    {
33 52
        return new self();
34
    }
35
36
    /**
37
     * @param Chips  $chips
38
     * @param Player $player
39
     *
40
     * @return ChipPot
41
     */
42 21
    public function addChips(Chips $chips, Player $player): ChipPot
43
    {
44 21
        $existingChips = $this->chips()->get($player->name()) ?? Chips::zero();
45
46 21
        $this->chips->put($player->name(), Chips::fromAmount($existingChips->amount() + $chips->amount()));
47
48 21
        if ($this->players()->findByName($player->name()) === null) {
49 21
            $this->players->push($player);
50
        }
51
52 21
        return $this;
53
    }
54
55
    /**
56
     * @return ChipStackCollection
57
     */
58 21
    public function chips(): ChipStackCollection
59
    {
60 21
        return $this->chips;
61
    }
62
63
    /**
64
     * @return PlayerCollection
65
     */
66 21
    public function players(): PlayerCollection
67
    {
68 21
        return $this->players;
69
    }
70
71
    /**
72
     * @return Chips
73
     */
74 13
    public function total(): Chips
75
    {
76 13
        return $this->chips->total();
77
    }
78
79
    /**
80
     * @return int
81
     */
82 3
    public function totalAmount(): int
83
    {
84 3
        return $this->chips->total()->amount();
85
    }
86
87
    /**
88
     * @param ChipPot $object
89
     *
90
     * @return bool
91
     */
92 11
    public function equals(ChipPot $object): bool
93
    {
94 11
        return static::class === get_class($object)
95 11
        && $this->chips() === $object->chips()
96 11
        && $this->players() === $object->players()
97 11
        && $this->total() === $object->total();
98
    }
99
100
    /**
101
     * @return string
102
     */
103 1
    public function __toString()
104
    {
105 1
        $players = $this->players()
106 1
            ->map(function (Player $player) {
107 1
                return $player->name();
108 1
            })
109 1
            ->implode(', ');
110
111 1
        return sprintf('[%d] [%s]', $this->total()->amount(), $players);
112
    }
113
}
114