Completed
Push — develop ( e322b0...461e3c )
by Barry
02:41
created

Runner::getFirstLoop()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 4
nop 1
crap 4
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 3
    public function __construct(Config $config, CCA $cca)
16
    {
17 3
        $this->config = $config;
18
19 3
        $this->cca = $cca;
20 3
    }
21
22
    /**
23
     * Run the CCA and return the $numIterations-th state.
24
     *
25
     * @param int $numIterations
26
     *
27
     * @return State
28
     */
29 3
    public function getLastState(int $numIterations): State
30
    {
31
        do {
32 3
            $state = $this->cca->getState();
33
34 3
            $iteration = $this->cca->cycle();
35 3
        } while ($iteration < $numIterations);
36
37 3
        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 3
    public function getFirstStates(int $numIterations): array
48
    {
49 3
        $states = [];
50
51
        do {
52 3
            $states[] = $this->cca->getState();
53
54 3
            $iteration = $this->cca->cycle();
55 3
        } while ($iteration < $numIterations);
56
57 3
        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 6
    public function getFirstLoop(int $maxIterations)
71
    {
72 6
        $states = [];
73 6
        $hashes = [];
74
75
        do {
76 6
            $state = $this->cca->getState();
77 6
            $hash = $state->toHash();
78
79 6
            $cycleEnd = false;
80 6
            if ($cycleStart = array_search($hash, $hashes) !== false) {
81 3
                $cycleEnd = count($states)+1;
82
            }
83
84 6
            $states[] = $state;
85 6
            $hashes[] = $hash;
86
87 6
            if ($cycleEnd !== false) {
88 3
                $states = array_slice($states, $cycleStart, $cycleEnd);
0 ignored issues
show
Bug introduced by
$cycleStart of type true is incompatible with the type integer expected by parameter $offset of array_slice(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
                $states = array_slice($states, /** @scrutinizer ignore-type */ $cycleStart, $cycleEnd);
Loading history...
89
90 3
                return $states;
91
            }
92
93 6
            $iteration = $this->cca->cycle();
94 6
        } while ($iteration < $maxIterations);
95
96 3
        throw new LoopNotFoundException();
97
    }
98
}
99