Completed
Push — master ( 412c0b...ce3826 )
by Scott
11s
created

CoverageCheck::getDiff()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
use stdClass;
5
6
/**
7
 * Class CoverageCheck
8
 * @package exussum12\CoverageChecker
9
 */
10
class CoverageCheck
11
{
12
    /**
13
     * @var DiffFileLoader
14
     */
15
    protected $diff;
16
    /**
17
     * @var FileChecker
18
     */
19
    protected $fileChecker;
20
    /**
21
     * @var FileMatcher
22
     */
23
    protected $matcher;
24
    /**
25
     * @var stdClass
26
     */
27
    protected $cache;
28
    /**
29
     * @var array
30
     */
31
    protected $uncoveredLines = [];
32
    /**
33
     * @var array
34
     */
35
    protected $coveredLines = [];
36
37
    /**
38
     * CoverageCheck constructor.
39
     * This class is used for filtering the "checker" by the diff
40
     * For example if the checker is phpunit, this class filters the phpunit
41
     * output by the diff of the pull request. giving only the common lines in
42
     * each
43
     *
44
     * @param DiffFileLoader $diff
45
     * @param FileChecker $fileChecker
46
     * @param FileMatcher $matcher
47
     */
48
    public function __construct(
49
        DiffFileLoader $diff,
50
        FileChecker $fileChecker,
51
        FileMatcher $matcher
52
    ) {
53
        $this->diff = $diff;
54
        $this->fileChecker = $fileChecker;
55
        $this->matcher = $matcher;
56
        $this->cache = new stdClass;
57
    }
58
59
    /**
60
     * array of uncoveredLines and coveredLines
61
     * @return array
62
     */
63
    public function getCoveredLines()
64
    {
65
        $this->getDiff();
66
67
        $this->getCoverage();
68
69
        $this->uncoveredLines = [];
70
        $this->coveredLines = [];
71
72
        $diffFiles = array_keys($this->cache->diff);
73
        $coveredFiles = array_keys($this->cache->coveredLines);
74
        foreach ($diffFiles as $file) {
75
            $matchedFile = $this->findFile($file, $coveredFiles);
76
            if ($matchedFile !== false) {
77
                $this->matchLines($file, $matchedFile);
78
            }
79
        }
80
81
        return [
82
            'uncoveredLines' => $this->uncoveredLines,
83
            'coveredLines' => $this->coveredLines,
84
        ];
85
    }
86
87
    /**
88
     * @param string $file the filename containing the uncovered line
89
     * @param int $line the number of the uncovered line
90
     * @param string $message the message showing why its uncovered
91
     */
92
    protected function addUnCoveredLine($file, $line, $message)
93
    {
94
        if (!isset($this->uncoveredLines[$file])) {
95
            $this->uncoveredLines[$file] = [];
96
        }
97
98
        $this->uncoveredLines[$file][$line] = $message;
99
    }
100
101
    /**
102
     * @param string $file the filename containing the covered line
103
     * @param int $line the number of the covered line
104
     */
105
    protected function addCoveredLine($file, $line)
106
    {
107
        if (!isset($this->coveredLines[$file])) {
108
            $this->coveredLines[$file] = [];
109
        }
110
111
        $this->coveredLines[$file][] = $line;
112
    }
113
114
    /**
115
     * @param string $fileName the file name in the diff
116
     * @param string $matchedFile the file name of the matched file
117
     */
118
    protected function matchLines($fileName, $matchedFile)
119
    {
120
        foreach ($this->cache->diff[$fileName] as $line) {
121
            $validLine = $this->fileChecker->isValidLine($matchedFile, $line);
122
123
            if (is_null($validLine)) {
124
                continue;
125
            }
126
127
            if ($validLine) {
128
                $this->addCoveredLine($fileName, $line);
129
                continue;
130
            }
131
132
            $message = isset($this->cache->coveredLines[$matchedFile][$line])
133
                ? $this->cache->coveredLines[$matchedFile][$line] :
134
                "No cover"
135
            ;
136
137
            $this->addUnCoveredLine(
138
                $fileName,
139
                $line,
140
                $message
141
            );
142
        }
143
    }
144
145
    protected function addCoveredFile($file)
146
    {
147
        foreach ($this->cache->diff[$file] as $line) {
148
            $this->addCoveredLine($file, $line);
149
        }
150
    }
151
152
    protected function addUnCoveredFile($file)
153
    {
154
        foreach ($this->cache->diff[$file] as $line) {
155
            $this->addUnCoveredLine($file, $line, 0);
156
        }
157
    }
158
159
    protected function getDiff()
160
    {
161
        if (empty($this->cache->diff)) {
162
            $this->cache->diff = $this->diff->getChangedLines();
163
        }
164
165
        return $this->cache->diff;
166
    }
167
168
    protected function getCoverage()
169
    {
170
        if (empty($this->cache->coveredLines)) {
171
            $this->cache->coveredLines = $this->fileChecker->getLines();
172
        }
173
174
        return $this->cache->coveredLines;
175
    }
176
177
    protected function handleFileNotFound($file)
178
    {
179
        $unMatchedFile = $this->fileChecker->handleNotFoundFile();
180
181
        if ($unMatchedFile === true) {
182
            $this->addCoveredFile($file);
183
        }
184
185
        if ($unMatchedFile === false) {
186
            $this->addUnCoveredFile($file);
187
        }
188
    }
189
190
    protected function findFile($file, $coveredFiles)
191
    {
192
        try {
193
            return $this->matcher->match($file, $coveredFiles);
194
        } catch (Exceptions\FileNotFound $e) {
195
            $this->handleFileNotFound($file);
196
            return false;
197
        }
198
    }
199
}
200