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

Runner   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 23.91 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 22
loc 92
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getFirstLoop() 0 28 4
A getLastState() 10 10 2
A getFirstStates() 12 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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