Skip to content

Commit 9d29ee5

Browse files
authored
Merge pull request #31 from jmrieger/29-honor-determine-branch-coverage
29 - Flag checking of path / branch coverage in CodeCoverage
2 parents e6db5c4 + 78fdd0c commit 9d29ee5

File tree

1 file changed

+51
-23
lines changed

1 file changed

+51
-23
lines changed

src/CodeCoverage.php

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,18 @@ public function append(array $data, $id = null, bool $append = true, $linesToBeC
360360
}
361361
}
362362

363-
foreach ($fileData['functions'] as $function => $functionCoverage) {
364-
foreach ($functionCoverage['branches'] as $branch => $branchCoverage) {
365-
if (($branchCoverage['hit'] ?? 0) === 1) {
366-
$this->addCoverageBranchHit($file, $function, $branch, $branchCoverage['hit'] ?? 0);
367-
$this->addCoverageBranchTest($file, $function, $branch, $id);
363+
if ($this->determineBranchCoverage) {
364+
foreach ($fileData['functions'] as $function => $functionCoverage) {
365+
foreach ($functionCoverage['branches'] as $branch => $branchCoverage) {
366+
if (($branchCoverage['hit'] ?? 0) === 1) {
367+
$this->addCoverageBranchHit($file, $function, $branch, $branchCoverage['hit'] ?? 0);
368+
$this->addCoverageBranchTest($file, $function, $branch, $id);
369+
}
368370
}
369-
}
370371

371-
foreach ($functionCoverage['paths'] as $path => $pathCoverage) {
372-
$this->addCoveragePathHit($file, $function, $path, $pathCoverage['hit'] ?? 0);
372+
foreach ($functionCoverage['paths'] as $path => $pathCoverage) {
373+
$this->addCoveragePathHit($file, $function, $path, $pathCoverage['hit'] ?? 0);
374+
}
373375
}
374376
}
375377
}
@@ -622,20 +624,21 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data): void
622624
} else {
623625
$this->addCoverageLinePathCovered($file, $lineNumber, false);
624626
}
625-
626627
}
627628

628-
foreach ($fileData['functions'] as $functionName => $functionData) {
629-
$this->data[$file]['paths'][$functionName] = $functionData['paths'];
629+
if ($this->determineBranchCoverage) {
630+
foreach ($fileData['functions'] as $functionName => $functionData) {
631+
$this->data[$file]['paths'][$functionName] = $functionData['paths'];
630632

631-
foreach ($functionData['branches'] as $branchIndex => $branchData) {
632-
$this->addCoverageBranchHit($file, $functionName, $branchIndex, $branchData['hit']);
633-
$this->addCoverageBranchLineStart($file, $functionName, $branchIndex, $branchData['line_start']);
634-
$this->addCoverageBranchLineEnd($file, $functionName, $branchIndex, $branchData['line_end']);
633+
foreach ($functionData['branches'] as $branchIndex => $branchData) {
634+
$this->addCoverageBranchHit($file, $functionName, $branchIndex, $branchData['hit']);
635+
$this->addCoverageBranchLineStart($file, $functionName, $branchIndex, $branchData['line_start']);
636+
$this->addCoverageBranchLineEnd($file, $functionName, $branchIndex, $branchData['line_end']);
635637

636-
for ($curLine = $branchData['line_start']; $curLine < $branchData['line_end']; $curLine++) {
637-
if (isset($this->data[$file]['lines'][$curLine])) {
638-
$this->addCoverageLinePathCovered($file, $curLine, (bool) $branchData['hit']);
638+
for ($curLine = $branchData['line_start']; $curLine < $branchData['line_end']; $curLine++) {
639+
if (isset($this->data[$file]['lines'][$curLine])) {
640+
$this->addCoverageLinePathCovered($file, $curLine, (bool) $branchData['hit']);
641+
}
639642
}
640643
}
641644
}
@@ -646,11 +649,17 @@ private function initializeFilesThatAreSeenTheFirstTime(array $data): void
646649
private function initializeFileCoverageData(string $file): void
647650
{
648651
if (!isset($this->data[$file]) && $this->filter->isFile($file)) {
649-
$this->data[$file] = [
650-
'lines' => [],
651-
'branches' => [],
652-
'paths' => [],
653-
];
652+
$default = ['lines' => []];
653+
654+
if ($this->determineBranchCoverage) {
655+
$default = [
656+
'lines' => [],
657+
'branches' => [],
658+
'paths' => [],
659+
];
660+
}
661+
662+
$this->data[$file] = $default;
654663
}
655664
}
656665

@@ -689,6 +698,9 @@ private function addCoverageLineTest(string $file, int $lineNumber, string $test
689698
private function addCoverageBranchHit(string $file, string $functionName, int $branchIndex, int $hit): void
690699
{
691700
$this->initializeFileCoverageData($file);
701+
if (!$this->determineBranchCoverage) {
702+
return;
703+
}
692704

693705
if (!\array_key_exists($functionName, $this->data[$file]['branches'])) {
694706
$this->data[$file]['branches'][$functionName] = [];
@@ -717,6 +729,10 @@ private function addCoverageBranchLineStart(
717729
): void {
718730
$this->initializeFileCoverageData($file);
719731

732+
if (!$this->determineBranchCoverage) {
733+
return;
734+
}
735+
720736
if (!\array_key_exists($functionName, $this->data[$file]['branches'])) {
721737
$this->data[$file]['branches'][$functionName] = [];
722738
}
@@ -741,6 +757,10 @@ private function addCoverageBranchLineEnd(
741757
): void {
742758
$this->initializeFileCoverageData($file);
743759

760+
if (!$this->determineBranchCoverage) {
761+
return;
762+
}
763+
744764
if (!\array_key_exists($functionName, $this->data[$file]['branches'])) {
745765
$this->data[$file]['branches'][$functionName] = [];
746766
}
@@ -765,6 +785,10 @@ private function addCoverageBranchTest(
765785
): void {
766786
$this->initializeFileCoverageData($file);
767787

788+
if (!$this->determineBranchCoverage) {
789+
return;
790+
}
791+
768792
if (!\array_key_exists($functionName, $this->data[$file]['branches'])) {
769793
$this->data[$file]['branches'][$functionName] = [];
770794
}
@@ -791,6 +815,10 @@ private function addCoveragePathHit(
791815
): void {
792816
$this->initializeFileCoverageData($file);
793817

818+
if (!$this->determineBranchCoverage) {
819+
return;
820+
}
821+
794822
if (!\array_key_exists($functionName, $this->data[$file]['paths'])) {
795823
$this->data[$file]['paths'][$functionName] = [];
796824
}

0 commit comments

Comments
 (0)