CardHand   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 106
rs 10
c 1
b 0
f 0
ccs 33
cts 33
cp 1
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getString() 0 8 2
A cardCount() 0 3 1
A addCard() 0 3 1
A getBlackJackValue() 0 9 3
A getValueAceHigh() 0 9 3
A getBlackJackValueAceHigh() 0 10 4
A getValue() 0 8 2
1
<?php
2
3
namespace App\Cards;
4
5
/**
6
 * CardHand.
7
 */
8
class CardHand
9
{
10
    /**
11
     * @var array<Card> Is an array that contains the Card objects
12
     */
13
    protected array $hand = [];
14
15
    /**
16
     * addCard.
17
     *
18
     * Add a card to the hand array
19
     */
20 35
    public function addCard(Card $card): void
21
    {
22 35
        $this->hand[] = $card;
23
    }
24
25
    /**
26
     * cardCount.
27
     *
28
     * Returns the number of cards in hand
29
     */
30 1
    public function cardCount(): int
31
    {
32 1
        return count($this->hand);
33
    }
34
35
    /**
36
     * getValueOfHand'.
37
     *
38
     * Return the value of the hand
39
     */
40 1
    public function getValue(): int
41
    {
42 1
        $value = 0;
43 1
        foreach ($this->hand as $card) {
44 1
            $value += $card->getValue();
45
        }
46
47 1
        return $value;
48
    }
49
50
    /**
51
     * getValueOfHandAceHigh.
52
     *
53
     * Return the value of the hand if the Ace cards have a high value (14)
54
     */
55 1
    public function getValueAceHigh(): int
56
    {
57 1
        $valueHand = 0;
58 1
        foreach ($this->hand as $card) {
59 1
            $valueCard = $card->getValue();
60 1
            $valueHand += (1 == $valueCard) ? 14 : $valueCard;
61
        }
62
63 1
        return $valueHand;
64
    }
65
66
    /**
67
     * getBlackJackValueOfHand.
68
     *
69
     * Return the Black Jack value of the hand (J,Q,K = 10)
70
     */
71 24
    public function getBlackJackValue(): int
72
    {
73 24
        $valueHand = 0;
74 24
        foreach ($this->hand as $card) {
75 24
            $valueCard = $card->getValue();
76 24
            $valueHand += ($valueCard > 10) ? 10 : $valueCard;
77
        }
78
79 24
        return $valueHand;
80
    }
81
82
    /**
83
     * getBlackJackValueOfHandAceHigh.
84
     *
85
     * Return the Black Jack value of the hand (J,Q,K = 10) if the Ace cards have a high value (11)
86
     */
87 24
    public function getBlackJackValueAceHigh(): int
88
    {
89 24
        $valueHand = 0;
90 24
        foreach ($this->hand as $card) {
91 24
            $valueCard = $card->getValue();
92 24
            $valueHand += (1 == $valueCard) ? 11 :
93 24
             (($valueCard > 10) ? 10 : $valueCard);
94
        }
95
96 24
        return $valueHand;
97
    }
98
99
    /**
100
     * getString.
101
     *
102
     * Return the hand as an array of strings
103
     *
104
     * @return array<string>
105
     */
106 30
    public function getString(): array
107
    {
108 30
        $cards = [];
109 30
        foreach ($this->hand as $card) {
110 28
            $cards[] = $card->getString();
111
        }
112
113 30
        return $cards;
114
    }
115
}
116