Deck   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 61
ccs 26
cts 26
cp 1
rs 10
c 1
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A shuffle() 0 4 2
A remainingCardsCount() 0 7 2
A getCards() 0 3 1
A drawCard() 0 22 3
1
<?php
2
3
namespace App\Card;
4
5
class Deck
6
{
7
    protected $cards = [];
8
9 8
    public function __construct()
10
    {
11 8
        $suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades'];
12 8
        $values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace'];
13
14 8
        foreach ($suits as $suit) {
15 8
            foreach ($values as $value) {
16 8
                $this->cards[$suit][] = new CardGraphic($suit, $value);
17
            }
18
        }
19
    }
20
21 4
    public function shuffle(): void
22
    {
23 4
        foreach ($this->cards as &$cards) {
24 4
            shuffle($cards);
25
        }
26
    }
27
28 4
    public function drawCard(): ?Card
29
    {
30
        // Return null if no cards left
31 4
        if (empty($this->cards)) {
32 1
            return null;
33
        }
34
35
        // Get a random suit from available suits
36 4
        $suits = array_keys($this->cards);
37 4
        $randomSuit = $suits[array_rand($suits)];
38
39 4
        shuffle($this->cards[$randomSuit]);
40
41
        // Draw a card from the suit
42 4
        $card = array_shift($this->cards[$randomSuit]);
43
44
        // If suit is empty, remove it from deck
45 4
        if (empty($this->cards[$randomSuit])) {
46 1
            unset($this->cards[$randomSuit]);
47
        }
48
49 4
        return $card;
50
    }
51
52
53
54 3
    public function remainingCardsCount(): int
55
    {
56 3
        $totalCards = 0;
57 3
        foreach ($this->cards as $cardsInSuit) {
58 2
            $totalCards += count($cardsInSuit);
59
        }
60 3
        return $totalCards;
61
    }
62
63 1
    public function getCards(): array
64
    {
65 1
        return $this->cards;
66
    }
67
}
68