StateCollection::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.1481
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dflydev\FiniteStateMachine\State;
6
7
class StateCollection
8
{
9
    /**
10
     * @var array<int, State>
11
     */
12
    private array $states = [];
13
14
    /**
15
     * @var array<string, State>
16
     */
17
    private array $statesByName = [];
18
19 21
    public function __construct(State ...$states)
20
    {
21 21
        foreach ($states as $state) {
22
            $this->add($state);
23
        }
24 21
    }
25
26 21
    public function add(State $state): void
27
    {
28 21
        $this->states[] = $state;
29 21
        $this->statesByName[$state->name()] = $state;
30 21
    }
31
32 15
    public function named(string $name): State
33
    {
34 15
        if (! isset($this->statesByName[$name])) {
35
            throw new \RuntimeException(sprintf('No state named "%s"', $name));
36
        }
37
38 15
        return $this->statesByName[$name];
39
    }
40
41 1
    public function names(): array
42
    {
43 1
        return array_keys($this->statesByName);
44
    }
45
}
46