Cell   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
eloc 18
dl 0
loc 60
c 0
b 0
f 0
ccs 25
cts 25
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A willCycle() 0 3 1
A getState() 0 3 1
A getRandomState() 0 3 1
A setNextState() 0 3 1
A __toString() 0 3 1
A getSuccessorState() 0 3 1
A __construct() 0 5 1
A computeNextState() 0 12 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Barryvanveen\CCA;
6
7
class Cell
8
{
9
    /** @var Config */
10
    protected $config;
11
12
    /** @var int */
13
    protected $state;
14
15
    /** @var int */
16
    protected $nextState;
17
18 12
    public function __construct(Config $config)
19
    {
20 12
        $this->config = $config;
21
22 12
        $this->state = $this->getRandomState();
23 12
    }
24
25 12
    protected function getRandomState(): int
26
    {
27 12
        return mt_rand(0, $this->config->states() - 1);
28
    }
29
30 12
    public function getState(): int
31
    {
32 12
        return $this->state;
33
    }
34
35 6
    public function computeNextState(array $neighborStates)
36
    {
37 6
        $successorState = $this->getSuccessorState();
38 6
        $count = 0;
39
40 6
        foreach ($neighborStates as $neighborState) {
41 3
            if ($neighborState === $successorState) {
42 3
                $count++;
43
            }
44
        }
45
46 6
        $this->nextState = $this->willCycle($count) ? $successorState : $this->state;
47 6
    }
48
49 6
    protected function getSuccessorState(): int
50
    {
51 6
        return ($this->state + 1) % $this->config->states();
52
    }
53
54 6
    protected function willCycle(int $count): bool
55
    {
56 6
        return $count >= $this->config->threshold();
57
    }
58
59 6
    public function setNextState()
60
    {
61 6
        $this->state = $this->nextState;
62 6
    }
63
64 3
    public function __toString(): string
65
    {
66 3
        return sprintf("%d", $this->state);
67
    }
68
}
69