Mutator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
lcom 0
cbo 3
dl 0
loc 62
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testMutations() 0 14 4
A findWorkingPermutations() 0 20 4
A iterate() 0 14 3
1
<?php
2
3
namespace PHPRealCoverage\Mutator;
4
5
use PHPRealCoverage\Mutator\Exception\NoMoreMutationsException;
6
7
class Mutator
8
{
9
    public function testMutations(MutationTester $tester, MutationGenerator $generator)
10
    {
11
        try {
12
            while (true) {
13
                $progress = $generator->getProgress();
14
                if (!is_null($progress)) {
15
                    echo "Mutation Progress: " . (int)(100 * $progress) . "%\r";
16
                }
17
                $this->iterate($tester, $generator);
18
            }
19
        } catch (NoMoreMutationsException $e) {
20
            // stop when all permutations have been tested
21
        }
22
    }
23
24
    /**
25
     * @param MutationCommand[] $commands
26
     * @param MutationTester $tester
27
     * @return bool
28
     */
29
    private function findWorkingPermutations(array $commands, MutationTester $tester)
30
    {
31
        if (empty($commands)) {
32
            return false;
33
        }
34
35
        $lineToTest = $commands[0];
36
        $lineToTest->execute();
37
        if ($tester->isValid()) {
38
            return true;
39
        }
40
41
        $rest = array_slice($commands, 1);
42
        if ($this->findWorkingPermutations($rest, $tester)) {
43
            return true;
44
        }
45
46
        $lineToTest->undo();
47
        return $this->findWorkingPermutations($rest, $tester);
48
    }
49
50
    /**
51
     * @param MutationTester $tester
52
     * @param MutationGenerator $generator
53
     */
54
    public function iterate(MutationTester $tester, MutationGenerator $generator)
55
    {
56
        $commands = $generator->getMutationStack();
57
        $lineToTest = $commands[0];
58
        $lineToTest->execute();
59
60
        $rest = array_slice($commands, 1);
61
        $this->findWorkingPermutations($rest, $tester);
62
        if (!$tester->isValid()) {
63
            foreach ($commands as $command) {
64
                $command->undo();
65
            }
66
        }
67
    }
68
}
69