Completed
Pull Request — master (#23)
by Scott
01:52
created

CoverageCheck::handleFileNotFound()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 12
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
            try {
76
                $matchedFile = $this->matcher->match($file, $coveredFiles);
77
            } catch (Exceptions\FileNotFound $e) {
78
                $this->handleFileNotFound($file);
79
80
                continue;
81
            }
82
83
            $this->matchLines($file, $matchedFile);
84
        }
85
86
        return [
87
            'uncoveredLines' => $this->uncoveredLines,
88
            'coveredLines' => $this->coveredLines,
89
        ];
90
    }
91
92
    /**
93
     * @param string $file the filename containing the uncovered line
94
     * @param int $line the number of the uncovered line
95
     * @param string $message the message showing why its uncovered
96
     */
97
    protected function addUnCoveredLine($file, $line, $message)
98
    {
99
        if (!isset($this->uncoveredLines[$file])) {
100
            $this->uncoveredLines[$file] = [];
101
        }
102
103
        $this->uncoveredLines[$file][$line] = $message;
104
    }
105
106
    /**
107
     * @param string $file the filename containing the covered line
108
     * @param int $line the number of the covered line
109
     */
110
    protected function addCoveredLine($file, $line)
111
    {
112
        if (!isset($this->coveredLines[$file])) {
113
            $this->coveredLines[$file] = [];
114
        }
115
116
        $this->coveredLines[$file][] = $line;
117
    }
118
119
    /**
120
     * @param string $fileName the file name in the diff
121
     * @param string $matchedFile the file name of the matched file
122
     */
123
    protected function matchLines($fileName, $matchedFile)
124
    {
125
        foreach ($this->cache->diff[$fileName] as $line) {
126
            $validLine = $this->fileChecker->isValidLine($matchedFile, $line);
127
128
            if (is_null($validLine)) {
129
                continue;
130
            }
131
132
            if ($validLine) {
133
                $this->addCoveredLine($fileName, $line);
134
                continue;
135
            }
136
137
            $message = isset($this->cache->coveredLines[$matchedFile][$line])
138
                ? $this->cache->coveredLines[$matchedFile][$line] :
139
                "No cover"
140
            ;
141
142
            $this->addUnCoveredLine(
143
                $fileName,
144
                $line,
145
                $message
146
            );
147
        }
148
    }
149
150
    protected function addCoveredFile($file)
151
    {
152
        foreach ($this->cache->diff[$file] as $line) {
153
            $this->addCoveredLine($file, $line);
154
        }
155
    }
156
157
    protected function addUnCoveredFile($file)
158
    {
159
        foreach ($this->cache->diff[$file] as $line) {
160
            $this->addUnCoveredLine($file, $line, 0);
161
        }
162
    }
163
164
    protected function getDiff()
165
    {
166
        if (empty($this->cache->diff)) {
167
            $this->cache->diff = $this->diff->getChangedLines();
168
        }
169
170
        return $this->cache->diff;
171
    }
172
173
    protected function getCoverage()
174
    {
175
        if (empty($this->cache->coveredLines)) {
176
            $this->cache->coveredLines = $this->fileChecker->getLines();
177
        }
178
179
        return $this->cache->coveredLines;
180
    }
181
182
    protected function handleFileNotFound($file)
183
    {
184
        $unMatchedFile = $this->fileChecker->handleNotFoundFile();
185
186
        if ($unMatchedFile === true) {
187
            $this->addCoveredFile($file);
188
        }
189
190
        if ($unMatchedFile === false) {
191
            $this->addUnCoveredFile($file);
192
        }
193
    }
194
}
195