Dice::roll()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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