Issues (977)

src/Card/DeckOfCards.php (1 issue)

Severity
1
<?php
2
3
namespace App\Card;
4
5
/**
6
 * Class DeckOfCards.
7
 *
8
 * Represents a standard deck of playing cards.
9
 */
10
class DeckOfCards
11
{
12
    /**
13
     * @var Card[] array holding the cards in the deck
14
     */
15
    private array $cards = [];
16
17
    /**
18
     * DeckOfCards constructor.
19
     *
20
     * Initializes the deck with 52 cards.
21
     * If $factory is true or null, uses default CardFactory.
22
     *
23
     * @param CardFactory|bool|null $factory optional factory to create cards or a boolean to use default
24
     */
25 31
    public function __construct(CardFactory|bool|null $factory = null)
26
    {
27 31
        if (true === $factory || null === $factory) {
28 18
            $factory = new CardFactory();
0 ignored issues
show
The assignment to $factory is dead and can be removed.
Loading history...
29
        }
30
31 31
        $suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades'];
32 31
        $values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'];
33
34 31
        foreach ($suits as $suit) {
35 31
            foreach ($values as $value) {
36
                // Create card with CardGraphic regardless of factory param?
37
                // You might want to use $factory->createCard($suit, $value) instead.
38 31
                $this->cards[] = new CardGraphic($suit, $value);
39
            }
40
        }
41
    }
42
43
    /**
44
     * Shuffles the deck of cards randomly.
45
     */
46 19
    public function shuffle(): void
47
    {
48 19
        shuffle($this->cards);
49
    }
50
51
    /**
52
     * Draws a number of cards from the top of the deck.
53
     *
54
     * @param int $number number of cards to draw (default 1)
55
     *
56
     * @return Card[] array of drawn cards
57
     */
58 18
    public function draw(int $number = 1): array
59
    {
60 18
        return array_splice($this->cards, 0, $number);
61
    }
62
63
    /**
64
     * Returns all remaining cards in the deck.
65
     *
66
     * @return Card[] array of cards in the deck
67
     */
68 13
    public function getCards(): array
69
    {
70 13
        return $this->cards;
71
    }
72
73
    /**
74
     * Returns the count of remaining cards in the deck.
75
     *
76
     * @return int number of cards left
77
     */
78 8
    public function count(): int
79
    {
80 8
        return count($this->cards);
81
    }
82
}
83