1 | <?php |
||
10 | class CoverageCheck |
||
11 | { |
||
12 | /** |
||
13 | * @var DiffFileLoader |
||
14 | */ |
||
15 | protected $diff; |
||
16 | |||
17 | /** |
||
18 | * @var FileChecker |
||
19 | */ |
||
20 | protected $fileChecker; |
||
21 | |||
22 | /** |
||
23 | * @var FileMatcher |
||
24 | */ |
||
25 | protected $matcher; |
||
26 | |||
27 | /** |
||
28 | * @var stdClass |
||
29 | */ |
||
30 | protected $cache; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $uncoveredLines = []; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $coveredLines = []; |
||
41 | |||
42 | /** |
||
43 | * CoverageCheck constructor. |
||
44 | * This class is used for filtering the "checker" by the diff |
||
45 | * For example if the checker is phpunit, this class filters the phpunit |
||
46 | * output by the diff of the pull request. giving only the common lines in |
||
47 | * each |
||
48 | * |
||
49 | * @param DiffFileLoader $diff |
||
50 | * @param FileChecker $fileChecker |
||
51 | * @param FileMatcher $matcher |
||
52 | */ |
||
53 | public function __construct( |
||
54 | DiffFileLoader $diff, |
||
55 | FileChecker $fileChecker, |
||
56 | FileMatcher $matcher |
||
57 | ) { |
||
58 | $this->diff = $diff; |
||
59 | $this->fileChecker = $fileChecker; |
||
60 | $this->matcher = $matcher; |
||
61 | $this->cache = new stdClass; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * array of uncoveredLines and coveredLines |
||
66 | * @return array |
||
67 | */ |
||
68 | public function getCoveredLines() |
||
69 | { |
||
70 | $this->getDiff(); |
||
71 | |||
72 | $coveredFiles = $this->fileChecker->parseLines(); |
||
73 | $this->uncoveredLines = []; |
||
74 | $this->coveredLines = []; |
||
75 | |||
76 | $diffFiles = array_keys($this->cache->diff); |
||
77 | foreach ($diffFiles as $file) { |
||
78 | $matchedFile = $this->findFile($file, $coveredFiles); |
||
79 | if ($matchedFile !== false) { |
||
80 | $this->matchLines($file, $matchedFile); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | return [ |
||
85 | 'uncoveredLines' => $this->uncoveredLines, |
||
86 | 'coveredLines' => $this->coveredLines, |
||
87 | ]; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param string $file the filename containing the uncovered line |
||
92 | * @param int $line the number of the uncovered line |
||
93 | * @param array $message a list of messages showing why its uncovered |
||
94 | */ |
||
95 | protected function addUnCoveredLine($file, $line, $message) |
||
96 | { |
||
97 | if (!isset($this->uncoveredLines[$file])) { |
||
98 | $this->uncoveredLines[$file] = []; |
||
99 | } |
||
100 | |||
101 | $this->uncoveredLines[$file][$line] = $message; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $file the filename containing the covered line |
||
106 | * @param int $line the number of the covered line |
||
107 | */ |
||
108 | protected function addCoveredLine($file, $line) |
||
109 | { |
||
110 | if (!isset($this->coveredLines[$file])) { |
||
111 | $this->coveredLines[$file] = []; |
||
112 | } |
||
113 | |||
114 | $this->coveredLines[$file][] = $line; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param string $fileName the file name in the diff |
||
119 | * @param string $matchedFile the file name of the matched file |
||
120 | */ |
||
121 | protected function matchLines($fileName, $matchedFile) |
||
122 | { |
||
123 | foreach ($this->cache->diff[$fileName] as $line) { |
||
124 | $messages = $this->fileChecker->getErrorsOnLine($matchedFile, $line); |
||
125 | |||
126 | if (is_null($messages)) { |
||
127 | continue; |
||
128 | } |
||
129 | |||
130 | if (count($messages) == 0) { |
||
131 | $this->addCoveredLine($fileName, $line); |
||
132 | continue; |
||
133 | } |
||
134 | |||
135 | |||
136 | $this->addUnCoveredLine( |
||
137 | $fileName, |
||
138 | $line, |
||
139 | $messages |
||
140 | ); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | protected function addCoveredFile($file) |
||
145 | { |
||
146 | foreach ($this->cache->diff[$file] as $line) { |
||
147 | $this->addCoveredLine($file, $line); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | protected function addUnCoveredFile($file) |
||
152 | { |
||
153 | foreach ($this->cache->diff[$file] as $line) { |
||
154 | $this->addUnCoveredLine($file, $line, ['No Cover']); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | protected function getDiff() |
||
159 | { |
||
160 | if (empty($this->cache->diff)) { |
||
161 | $this->cache->diff = $this->diff->getChangedLines(); |
||
162 | } |
||
163 | |||
164 | return $this->cache->diff; |
||
165 | } |
||
166 | |||
167 | protected function handleFileNotFound($file) |
||
179 | |||
180 | protected function findFile($file, $coveredFiles) |
||
189 | } |
||
190 |