1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPRealCoverage\Mutator; |
4
|
|
|
|
5
|
|
|
use PHPRealCoverage\Mutator\Exception\NoMoreMutationsException; |
6
|
|
|
|
7
|
|
|
class MutationGenerator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var MutatableClass |
11
|
|
|
*/ |
12
|
|
|
private $class; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
private $curentLine = 0; |
18
|
|
|
|
19
|
|
|
public function __construct(MutatableClass $class) |
20
|
|
|
{ |
21
|
|
|
$this->class = $class; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @throws Exception\NoMoreMutationsException |
26
|
|
|
* @return MutationCommand[] |
27
|
|
|
*/ |
28
|
|
|
public function getMutationStack() |
29
|
|
|
{ |
30
|
|
|
$affectedLines = array(); |
31
|
|
|
$mutatableLines = $this->class->getMutatableLines(); |
32
|
|
|
|
33
|
|
|
for ($i = 0; $i <= $this->curentLine; $i++) { |
34
|
|
|
$this->addCommandForLine($i, $mutatableLines, $affectedLines); |
35
|
|
|
} |
36
|
|
|
$this->curentLine++; |
37
|
|
|
return array_reverse($affectedLines); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param integer $lineNumber |
42
|
|
|
* @param MutatableLine[] $mutatableLines |
43
|
|
|
* @param $affectedLines |
44
|
|
|
*/ |
45
|
|
|
public function addCommandForLine($lineNumber, $mutatableLines, &$affectedLines) |
46
|
|
|
{ |
47
|
|
|
$line = $this->getLine($mutatableLines, $lineNumber); |
48
|
|
|
|
49
|
|
|
if (!$line->isEnabled()) { |
50
|
|
|
$this->increaseMaxLineIfMaxLineIsReached($lineNumber); |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$affectedLines[] = new DefaultMutationCommand($line); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param MutatableLine[] $mutatableLines |
59
|
|
|
* @param integer $processedLine |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
private function maxLineReached($mutatableLines, $processedLine) |
63
|
|
|
{ |
64
|
|
|
return !isset($mutatableLines[$processedLine]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param integer $processedLine |
69
|
|
|
*/ |
70
|
|
|
private function increaseMaxLineIfMaxLineIsReached($processedLine) |
71
|
|
|
{ |
72
|
|
|
if ($processedLine == $this->curentLine) { |
73
|
|
|
$this->curentLine++; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param MutatableLine[] $mutatableLines |
79
|
|
|
* @param integer $requestedLine |
80
|
|
|
* @return MutatableLine |
81
|
|
|
* @throws Exception\NoMoreMutationsException |
82
|
|
|
*/ |
83
|
|
|
private function getLine($mutatableLines, $requestedLine) |
84
|
|
|
{ |
85
|
|
|
if ($this->maxLineReached($mutatableLines, $requestedLine)) { |
86
|
|
|
throw new NoMoreMutationsException(); |
87
|
|
|
} |
88
|
|
|
$line = $mutatableLines[$requestedLine]; |
89
|
|
|
return $line; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getProgress() |
93
|
|
|
{ |
94
|
|
|
return $this->curentLine / count($this->class->getMutatableLines()); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|