Skip to content

Commit 9a9b685

Browse files
committed
If one analysis of the file is based on executed code, and one is based only on reviewing the unexecuted code, accept the one from execution automatically as it will have the most accurate list of executable lines, and prevents incorrect stats following a merge where a file was executed in one run, but not executed in another
1 parent eabf68b commit 9a9b685

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/CodeCoverage.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,35 @@ public function merge(PHP_CodeCoverage $that)
357357
continue;
358358
}
359359

360+
// Check if there are some lines already present in the two
361+
// versions being merged, and also that the number of lines
362+
// in each is different. This indicates that one is based on
363+
// pulling lines in from glancing through the code in unexecuted
364+
// code (the one with more lines in), and one is based on using Xdebug
365+
// to properly analyse the code (the one with less lines in).
366+
// In this scenario, ditch the lines from the unexecuted analysis,
367+
// as it will contain lines which are not classed as executable by
368+
// Xdebug, and those lines will throw the code analysis by appearing
369+
// as if they should be run but weren't.
370+
if ((count($lines) > 0)
371+
&& (count($this->data[$file]) > 0)
372+
&& (count($lines) != count($this->data[$file]))
373+
) {
374+
if (count($lines) > count($this->data[$file])) {
375+
// More lines in the one being added in, so just ignore them
376+
// and move on to the next file.
377+
continue;
378+
} else {
379+
// More lines in the one we currently have, so ditch what we
380+
// currently have and take what's being merged in exactly as
381+
// it is.
382+
$this->data[$file] = $lines;
383+
384+
// Move on to the next file.
385+
continue;
386+
}
387+
}
388+
360389
foreach ($lines as $line => $data) {
361390
if ($data !== null) {
362391
if (!isset($this->data[$file][$line])) {

0 commit comments

Comments
 (0)