Dice   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 54
rs 10
ccs 11
cts 11
cp 1
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A roll() 0 4 1
A getValue() 0 7 2
A getAsString() 0 3 1
1
<?php
2
3
namespace App\Dice;
4
5
use LogicException;
6
7
/**
8
 * Class Dice
9
 *
10
 * Represents a single six-sided dice that can be rolled to get a value between 1 and 6.
11
 */
12
class Dice
13
{
14
    /**
15
     * Dice constructor.
16
     *
17
     * Initializes the dice with no value (not rolled yet).
18
     */
19
    /** @var int|null */
20
    protected ?int $value = null;
21
22
    /**
23
     * Dice constructor.
24
     *
25
     * Init the dice with no value.
26
     */
27 9
    public function __construct()
28
    {
29 9
        $this->value = null;
30
    }
31
32
    /**
33
     * Rolls the dice, random value between 1 and 6.
34
     *
35
     * @return int The value of the dice after rolling.
36
     */
37 5
    public function roll(): int
38
    {
39 5
        $this->value = random_int(1, 6);
40 5
        return $this->value;
41
    }
42
43
    /**
44
     * Get the current value of the dice.
45
     *
46
     * @throws LogicException if the dice has not been rolled yet.
47
     * @return int The face value of the dice.
48
     */
49 4
    public function getValue(): int
50
    {
51 4
        if ($this->value === null) {
52 1
            throw new LogicException("Dice has not been rolled yet.");
53
        }
54
55 3
        return $this->value;
56
    }
57
58
    /**
59
     * Get a string representation of the dice's current value.
60
     *
61
     * @return string
62
     */
63 2
    public function getAsString(): string
64
    {
65 2
        return "[{$this->value}]";
66
    }
67
}
68