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 |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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.