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

Cell   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 62
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getRandomState() 0 4 1
A getState() 0 4 1
A computeNextState() 0 13 4
A getSuccessorState() 0 4 1
A willCycle() 0 4 1
A setNextState() 0 4 1
A __toString() 0 4 1
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