Passed
Push — master ( 4c8245...5bb496 )
by Scott
02:18
created

PhpunitFilter::matchFuzzyLines()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 5
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace exussum12\coverageChecker;
3
4
use Exception;
5
6
class PhpunitFilter
7
{
8
    protected $diff;
9
    protected $matcher;
10
    protected $coverage;
11
    public function __construct(DiffFileLoader $diff, FileMatcher $matcher, $coveragePhp)
12
    {
13
        if (!is_readable(($coveragePhp))) {
14
            throw new Exception("Coverage File not found");
15
        }
16
        $this->coverage = include($coveragePhp);
17
        $this->diff = $diff;
18
        $this->matcher = $matcher;
19
    }
20
21
    public function getTestsForRunning($fuzziness = 0)
22
    {
23
        $changes = $this->diff->getChangedLines();
24
        $testData = $this->coverage->getData();
25
        $fileNames = array_keys($testData);
26
        $runTests = [];
27
        foreach ($changes as $file => $lines) {
28
            try {
29
                if ($found = $this->matcher->match($file, $fileNames)) {
30
                    foreach ($lines as $line) {
31
                        $runTests = $this->matchFuzzyLines($fuzziness, $testData, $found, $line, $runTests);
32
                    }
33
                }
34
            } catch (Exception $e) {
35
                if ($this->endsWith($file, ".php")) {
36
                    $runTests[] = $this->stripFileExtension($file);
37
                }
38
            }
39
        }
40
        return $this->groupTestsBySuite($runTests);
41
    }
42
43
    protected function endsWith($haystack, $needle)
44
    {
45
        $length = strlen($needle);
46
        return (substr($haystack, -$length) === $needle);
47
    }
48
49
    protected function stripFileExtension($file)
50
    {
51
        $ext = ".php";
52
        return str_replace('/', '\\', substr($file, 0, -strlen($ext)));
53
    }
54
55
    protected function groupTestsBySuite($tests)
56
    {
57
        $groupedTests = [];
58
        foreach ($tests as $test) {
59
            $suite = $test;
60
            $testName = '';
61
62
            if (strpos($test, '::') > 0) {
63
                list ($suite, $testName) = explode('::', $test);
64
            }
65
            $groupedTests[$suite][] = $testName;
66
        }
67
        return $groupedTests;
68
    }
69
70
    public function matchFuzzyLines($fuzziness, $testData, $found, $line, $runTests): array
71
    {
72
        $i = -$fuzziness;
73
        do {
74
            if (isset($testData[$found][$line + $i])) {
75
                $runTests = array_unique(
76
                    array_merge(
77
                        $runTests,
78
                        $testData[$found][$line]
79
                    )
80
                );
81
            }
82
        } while (++$i < $fuzziness);
83
84
        return $runTests;
85
    }
86
}
87