Completed
Push — develop ( 8b5afb...2fbff0 )
by Barry
01:32
created

Cell::computeNextState()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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