DeckOfCards::addCard()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace App\Card;
4
5
use App\Card\Card;
6
7
/**
8
 * Deck of playing cards class.
9
 */
10
class DeckOfCards
11
{
12
    /**
13
     * @var Card[] $deck Cards in this deck.
14
     */
15
    private array $deck;
16
17
    /**
18
     * Create new deck object.
19
     *
20
     * @param Card[]|null $deck Array of cards to create deck from if given.
21
     */
22 15
    public function __construct($deck = null)
23
    {
24 15
        if (is_array($deck)) {
25 9
            $this->deck = $deck;
26 9
            return;
27
        }
28
29 6
        foreach (range(0, 3) as $suite) {
30 6
            foreach (range(0, 12) as $rank) {
31 6
                $this->deck[] = new CardGraphic($suite, $rank);
32
            }
33
        }
34
    }
35
36
    /**
37
     * Add card to deck.
38
     *
39
     * @param Card $card Card to add.
40
     */
41 2
    public function addCard(Card $card): void
42
    {
43 2
        $this->deck[] = $card;
44
    }
45
46
    /**
47
     * Get array of cards as strings.
48
     *
49
     * @return string[] Cards as strings.
50
     */
51 1
    public function getString(): array
52
    {
53 1
        return array_map('strval', $this->deck);
54
    }
55
56
    /**
57
     * Get array of card values.
58
     *
59
     * @return int[] Card values.
60
     */
61 2
    public function getValues(): array
62
    {
63 2
        return array_map(function ($card) {
64 2
            return $card->getRank();
65 2
        }, $this->deck);
66
    }
67
68
    /**
69
     * Get array of card objects.
70
     *
71
     * @return Card[] Card objects.
72
     */
73 6
    public function getDeck(): array
74
    {
75 6
        return $this->deck;
76
    }
77
78
    /**
79
     * Get number of cards.
80
     *
81
     * @return int Number of cards.
82
     */
83 5
    public function getCount(): int
84
    {
85 5
        return count($this->deck);
86
    }
87
88
    /**
89
     * Shuffle cards.
90
     */
91 1
    public function shuffleCards(): void
92
    {
93 1
        shuffle($this->deck);
94
    }
95
96
    /**
97
     * Get last number of cards and remove them from the deck.
98
     *
99
     * @param  int    $num Number of cards to draw.
100
     * @return Card[]      Drawn cards.
101
     */
102 7
    public function draw(int $num): array
103
    {
104 7
        return array_reverse(array_splice($this->deck, $num * -1));
105
    }
106
}
107