Completed
Push — develop ( a36a32...a16bd0 )
by Barry
01:36
created

Runner::getLastState()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 1
nop 1
crap 6
1
<?php
2
3
namespace Barryvanveen\CCA;
4
5
use Barryvanveen\CCA\Exceptions\LoopNotFoundException;
6
7
class Runner
8
{
9
    /** @var Config */
10
    protected $config;
11
12
    /** @var CCA */
13
    protected $cca;
14
15
    public function __construct(Config $config)
16
    {
17
        $this->config = $config;
18
19
        $this->cca = new CCA($this->config);
20
    }
21
22
    /**
23
     * Run the CCA and return the $numIterations-th state.
24
     *
25
     * @param int $numIterations
26
     *
27
     * @return State
28
     */
29 View Code Duplication
    public function getLastState(int $numIterations): State
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        do {
32
            $state = $this->cca->getState();
33
34
            $iteration = $this->cca->cycle();
35
        } while ($iteration < $numIterations);
36
37
        return $state;
38
    }
39
40
    /**
41
     * Run the CCA and return an array with first $numIterations states.
42
     *
43
     * @param int $numIterations
44
     *
45
     * @return State[]
46
     */
47 View Code Duplication
    public function getFirstStates(int $numIterations): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        $states = [];
50
51
        do {
52
            $states[] = $this->cca->getState();
53
54
            $iteration = $this->cca->cycle();
55
        } while ($iteration < $numIterations);
56
57
        return $states;
58
    }
59
60
    /**
61
     * Run the CCA and return the first looping states it encounters. If no loop is found within $maxIterations,
62
     * a LoopNotFoundException exception will be thrown.
63
     *
64
     * @param int $maxIterations
65
     *
66
     * @throws LoopNotFoundException
67
     *
68
     * @return State[]
69
     */
70
    public function getFirstLoop(int $maxIterations)
71
    {
72
        $states = [];
73
        $hashes = [];
74
75
        do {
76
            $state = $this->cca->getState();
77
            $hash = $state->toHash();
78
79
            $cycleEnd = false;
80
            if ($cycleStart = array_search($hash, $hashes)) {
81
                $cycleEnd = count($states)+1;
82
            }
83
84
            $states[] = $state;
85
            $hashes[] = $hash;
86
87
            if ($cycleEnd !== false) {
88
                $states = array_slice($states, $cycleStart, $cycleEnd);
89
90
                return $states;
91
            }
92
93
            $iteration = $this->cca->cycle();
94
        } while ($iteration < $maxIterations);
95
96
        throw new LoopNotFoundException();
97
    }
98
}
99