DiceHand::getValues()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace App\Dice;
4
5
class DiceHand
6
{
7
    /**
8
     * @var array<Dice> Is an array that contains the Dice objects
9
     */
10
    private array $hand = [];
11
12
    /**
13
     * addDie.
14
     *
15
     * Add a die to the hand
16
     */
17 7
    public function addDie(Dice $die): void
18
    {
19 7
        $this->hand[] = $die;
20
    }
21
22
    /**
23
     * removeDice.
24
     *
25
     * Remove the top dice from hand
26
     */
27 1
    public function removeDie(): void
28
    {
29 1
        if (count($this->hand) > 0) {
30 1
            array_shift($this->hand);
31
        }
32
    }
33
34
    /**
35
     * roll.
36
     *
37
     * Roll all the Dice in the hand
38
     */
39 4
    public function roll(): void
40
    {
41 4
        foreach ($this->hand as $die) {
42 4
            $die->roll();
43
        }
44
    }
45
46
    /**
47
     * getNumberDices.
48
     *
49
     * Return the number of dice in the hand
50
     */
51 2
    public function getNumberDices(): int
52
    {
53 2
        return count($this->hand);
54
    }
55
56
    /**
57
     * getValues.
58
     *
59
     * Return an array of all the Dice values in hand
60
     *
61
     * @return array<int>
62
     */
63 2
    public function getValues(): array
64
    {
65 2
        $values = [];
66 2
        foreach ($this->hand as $die) {
67 2
            $values[] = $die->getValue();
68
        }
69
70 2
        return $values;
71
    }
72
73
    /**
74
     * sum.
75
     *
76
     * Returns the sum of all Dice values in hand
77
     */
78 1
    public function sum(): int
79
    {
80 1
        $sum = 0;
81 1
        foreach ($this->hand as $die) {
82 1
            $sum += $die->getValue();
83
        }
84
85 1
        return $sum;
86
    }
87
88
    /**
89
     * getString.
90
     *
91
     * Returns an string array containing all the Dice
92
     *
93
     * @return array<string>
94
     */
95 3
    public function getString(): array
96
    {
97 3
        $values = [];
98 3
        foreach ($this->hand as $die) {
99 3
            $values[] = $die->getString();
100
        }
101
102 3
        return $values;
103
    }
104
}
105