Skip to content

Commit 7b4cf80

Browse files
committed
Start working on branch support in CodeCoverage
1 parent 4d64574 commit 7b4cf80

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

src/CodeCoverage.php

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -346,15 +346,36 @@ public function append(array $data, $id = null, bool $append = true, $linesToBeC
346346

347347
$this->tests[$id] = ['size' => $size, 'status' => $status];
348348

349-
foreach ($data as $file => $lines) {
349+
foreach ($data as $file => $fileData) {
350350
if (!$this->filter->isFile($file)) {
351351
continue;
352352
}
353353

354-
foreach ($lines as $k => $v) {
355-
if ($v === Driver::LINE_EXECUTED) {
356-
if (empty($this->data[$file][$k]) || !\in_array($id, $this->data[$file][$k])) {
357-
$this->data[$file][$k][] = $id;
354+
foreach ($fileData['lines'] as $line => $lineCoverage) {
355+
if ($lineCoverage === Driver::LINE_EXECUTED) {
356+
if ($this->data[$file]['lines'][$line] === null) {
357+
$this->data[$file]['lines'][$line] = [
358+
'pathCovered' => false,
359+
'tests' => [$id],
360+
];
361+
} elseif (!\in_array($id, $this->data[$file]['lines'][$line]['tests'], true)) {
362+
$this->data[$file]['lines'][$line]['tests'][] = [$id];
363+
}
364+
}
365+
}
366+
367+
foreach ($fileData['functions'] as $function => $functionCoverage) {
368+
foreach ($functionCoverage['branches'] as $branch => $branchCoverage) {
369+
if ($branchCoverage['hit'] === 1) {
370+
$this->data[$file]['branches'][$function][$branch]['hit'] = 1;
371+
if (!\in_array($id, $this->data[$file]['branches'][$function][$branch]['tests'], true)) {
372+
$this->data[$file]['branches'][$function][$branch]['tests'][] = $id;
373+
}
374+
}
375+
}
376+
foreach ($functionCoverage['paths'] as $path => $pathCoverage) {
377+
if ($pathCoverage['hit'] === 1 && $this->data[$file]['paths'][$function][$path]['hit'] === 0) {
378+
$this->data[$file]['paths'][$function][$path]['hit'] = 1;
358379
}
359380
}
360381
}
@@ -374,10 +395,11 @@ public function merge(self $that): void
374395
\array_merge($this->filter->getWhitelistedFiles(), $that->filter()->getWhitelistedFiles())
375396
);
376397

377-
foreach ($that->data as $file => $lines) {
398+
// I don't know how / why this works, but it should be refactored to ->getData()
399+
foreach ($that->getData() as $file => $fileData) {
378400
if (!isset($this->data[$file])) {
379401
if (!$this->filter->isFiltered($file)) {
380-
$this->data[$file] = $lines;
402+
$this->data[$file] = $fileData;
381403
}
382404

383405
continue;
@@ -386,20 +408,23 @@ public function merge(self $that): void
386408
// we should compare the lines if any of two contains data
387409
$compareLineNumbers = \array_unique(
388410
\array_merge(
389-
\array_keys($this->data[$file]),
390-
\array_keys($that->data[$file])
411+
\array_keys($this->data[$file]['lines']),
412+
\array_keys($that->data[$file]['lines']) // can this be $fileData?
391413
)
392414
);
393415

394416
foreach ($compareLineNumbers as $line) {
395-
$thatPriority = $this->getLinePriority($that->data[$file], $line);
396-
$thisPriority = $this->getLinePriority($this->data[$file], $line);
417+
$thatPriority = $this->getLinePriority($that->data[$file]['lines'], $line);
418+
$thisPriority = $this->getLinePriority($this->data[$file]['lines'], $line);
397419

398420
if ($thatPriority > $thisPriority) {
399-
$this->data[$file][$line] = $that->data[$file][$line];
400-
} elseif ($thatPriority === $thisPriority && \is_array($this->data[$file][$line])) {
401-
$this->data[$file][$line] = \array_unique(
402-
\array_merge($this->data[$file][$line], $that->data[$file][$line])
421+
$this->data[$file]['lines'][$line] = $that->data[$file]['lines'][$line];
422+
} elseif ($thatPriority === $thisPriority && \is_array($this->data[$file]['lines'][$line])) {
423+
if ($line['pathCovered'] === true) {
424+
$this->data[$file]['lines']['line']['pathCovered'] = $line['pathCovered'];
425+
}
426+
$this->data[$file]['lines'][$line] = \array_unique(
427+
\array_merge($this->data[$file]['lines'][$line], $that->data[$file]['lines'][$line])
403428
);
404429
}
405430
}

0 commit comments

Comments
 (0)