Skip to content

Commit 47fd3ed

Browse files
committed
Make Xml reporter work with paths and branches
1 parent 825fc00 commit 47fd3ed

File tree

11 files changed

+183
-37
lines changed

11 files changed

+183
-37
lines changed

src/Report/Clover.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* Generates a Clover XML logfile from a code coverage object.
1818
*/
19-
final class Clover
19+
final class Clover extends Reporter
2020
{
2121
/**
2222
* @throws \RuntimeException

src/Report/Crap4j.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
use SebastianBergmann\CodeCoverage\CodeCoverage;
1313
use SebastianBergmann\CodeCoverage\Node\File;
1414
use SebastianBergmann\CodeCoverage\RuntimeException;
15+
use SebastianBergmann\CodeCoverage\Report\Reporter;
1516

16-
final class Crap4j
17+
final class Crap4j extends Reporter
1718
{
1819
/**
1920
* @var int

src/Report/Html/Facade.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111

1212
use SebastianBergmann\CodeCoverage\CodeCoverage;
1313
use SebastianBergmann\CodeCoverage\Node\Directory as DirectoryNode;
14+
use SebastianBergmann\CodeCoverage\Report\Reporter;
1415
use SebastianBergmann\CodeCoverage\RuntimeException;
1516

1617
/**
1718
* Generates an HTML report from a code coverage object.
1819
*/
19-
final class Facade
20+
final class Facade extends Reporter
2021
{
2122
/**
2223
* @var string
@@ -38,11 +39,6 @@ final class Facade
3839
*/
3940
private $highLowerBound;
4041

41-
/**
42-
* @var bool
43-
*/
44-
private $determineBranchCoverage = false;
45-
4642
public function __construct(int $lowUpperBound = 50, int $highLowerBound = 90, string $generator = '')
4743
{
4844
$this->generator = $generator;
@@ -121,11 +117,6 @@ public function process(CodeCoverage $coverage, string $target): void
121117
$this->copyFiles($target);
122118
}
123119

124-
public function setDetermineBranchCoverage(bool $determineBranchCoverage): void
125-
{
126-
$this->determineBranchCoverage = $determineBranchCoverage;
127-
}
128-
129120
/**
130121
* @throws RuntimeException
131122
*/

src/Report/PHP.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
use SebastianBergmann\CodeCoverage\CodeCoverage;
1313
use SebastianBergmann\CodeCoverage\RuntimeException;
14+
use SebastianBergmann\CodeCoverage\Report\Reporter;
1415

1516
/**
1617
* Uses var_export() to write a SebastianBergmann\CodeCoverage\CodeCoverage object to a file.
1718
*/
18-
final class PHP
19+
final class PHP extends Reporter
1920
{
2021
/**
2122
* @throws \SebastianBergmann\CodeCoverage\RuntimeException

src/Report/Reporter.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of the php-code-coverage package.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage\Report;
11+
12+
abstract class Reporter
13+
{
14+
/**
15+
* @var bool
16+
*/
17+
protected $determineBranchCoverage = false;
18+
19+
public function setDetermineBranchCoverage(bool $determineBranchCoverage): void
20+
{
21+
$this->determineBranchCoverage = $determineBranchCoverage;
22+
}
23+
}

src/Report/Text.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
use SebastianBergmann\CodeCoverage\CodeCoverage;
1313
use SebastianBergmann\CodeCoverage\Node\File;
1414
use SebastianBergmann\CodeCoverage\Util;
15+
use SebastianBergmann\CodeCoverage\Report\Reporter;
1516

1617
/**
1718
* Generates human readable output from a code coverage object.
1819
*
1920
* The output gets put into a text file our written to the CLI.
2021
*/
21-
final class Text
22+
final class Text extends Reporter
2223
{
2324
/**
2425
* @var string
@@ -70,11 +71,6 @@ final class Text
7071
*/
7172
private $showOnlySummary;
7273

73-
/**
74-
* @var bool
75-
*/
76-
private $determineBranchCoverage = false;
77-
7874
public function __construct(int $lowUpperBound = 50, int $highLowerBound = 90, bool $showUncoveredFiles = false, bool $showOnlySummary = false)
7975
{
8076
$this->lowUpperBound = $lowUpperBound;
@@ -350,11 +346,6 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
350346
return $output . \PHP_EOL;
351347
}
352348

353-
public function setDetermineBranchCoverage(bool $determineBranchCoverage): void
354-
{
355-
$this->determineBranchCoverage = $determineBranchCoverage;
356-
}
357-
358349
private function getCoverageColor(int $numberOfCoveredElements, int $totalNumberOfElements): string
359350
{
360351
$coverage = Util::percent(

src/Report/Xml/Facade.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
use SebastianBergmann\CodeCoverage\Node\AbstractNode;
1414
use SebastianBergmann\CodeCoverage\Node\Directory as DirectoryNode;
1515
use SebastianBergmann\CodeCoverage\Node\File as FileNode;
16+
use SebastianBergmann\CodeCoverage\Report\Reporter;
1617
use SebastianBergmann\CodeCoverage\RuntimeException;
1718
use SebastianBergmann\CodeCoverage\Version;
1819
use SebastianBergmann\Environment\Runtime;
1920

20-
final class Facade
21+
final class Facade extends Reporter
2122
{
2223
/**
2324
* @var string
@@ -56,6 +57,7 @@ public function process(CodeCoverage $coverage, string $target): void
5657
$this->project = new Project(
5758
$coverage->getReport()->getName()
5859
);
60+
$this->project->setDetermineBranchCoverage($this->determineBranchCoverage);
5961

6062
$this->setBuildInformation();
6163
$this->processTests($coverage->getTests());
@@ -135,6 +137,7 @@ private function processFile(FileNode $file, Directory $context): void
135137
);
136138

137139
$fileReport = new Report($path);
140+
$fileReport->setDetermineBranchCoverage($this->determineBranchCoverage);
138141

139142
$this->setTotals($file, $fileReport->getTotals());
140143

@@ -146,7 +149,13 @@ private function processFile(FileNode $file, Directory $context): void
146149
$this->processFunction($function, $fileReport);
147150
}
148151

149-
foreach ($file->getCoverageData() as $line => $tests) {
152+
$fileCoverageData = $file->getCoverageData();
153+
foreach ($fileCoverageData['lines'] as $line => $lineData) {
154+
if ($lineData === null) {
155+
continue;
156+
}
157+
158+
$tests = $lineData['tests'];
150159
if (!\is_array($tests) || \count($tests) === 0) {
151160
continue;
152161
}
@@ -202,6 +211,17 @@ private function processUnit(array $unit, Report $report): void
202211
(string) $method['executedLines'],
203212
(string) $method['coverage']
204213
);
214+
215+
if ($this->determineBranchCoverage) {
216+
$methodObject->setPathTotals(
217+
(string) $method['executablePaths'],
218+
(string) $method['executedPaths']
219+
);
220+
$methodObject->setBranchTotals(
221+
(string) $method['executableBranches'],
222+
(string) $method['executedBranches']
223+
);
224+
}
205225
}
206226
}
207227

@@ -212,7 +232,22 @@ private function processFunction(array $function, Report $report): void
212232
$functionObject->setSignature($function['signature']);
213233
$functionObject->setLines((string) $function['startLine']);
214234
$functionObject->setCrap($function['crap']);
215-
$functionObject->setTotals((string) $function['executableLines'], (string) $function['executedLines'], (string) $function['coverage']);
235+
$functionObject->setTotals(
236+
(string) $function['executableLines'],
237+
(string) $function['executedLines'],
238+
(string) $function['coverage']
239+
);
240+
241+
if ($this->determineBranchCoverage) {
242+
$functionObject->setPathTotals(
243+
(string) $function['executablePaths'],
244+
(string) $function['executedPaths']
245+
);
246+
$functionObject->setBranchTotals(
247+
(string) $function['executableBranches'],
248+
(string) $function['executedBranches']
249+
);
250+
}
216251
}
217252

218253
private function processTests(array $tests): void
@@ -259,6 +294,18 @@ private function setTotals(AbstractNode $node, Totals $totals): void
259294
$node->getNumFunctions(),
260295
$node->getNumTestedFunctions()
261296
);
297+
298+
if ($this->determineBranchCoverage) {
299+
$totals->setNumPaths(
300+
$node->getNumPaths(),
301+
$node->getNumTestedPaths()
302+
);
303+
304+
$totals->setNumBranches(
305+
$node->getNumBranches(),
306+
$node->getNumTestedBranches()
307+
);
308+
}
262309
}
263310

264311
private function getTargetDirectory(): string

src/Report/Xml/File.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class File
2121
*/
2222
private $contextNode;
2323

24+
/**
25+
* @var bool
26+
*/
27+
private $determineBranchCoverage = false;
28+
2429
public function __construct(\DOMElement $context)
2530
{
2631
$this->dom = $context->ownerDocument;
@@ -40,7 +45,7 @@ public function getTotals(): Totals
4045
);
4146
}
4247

43-
return new Totals($totalsContainer);
48+
return new Totals($totalsContainer, $this->determineBranchCoverage);
4449
}
4550

4651
public function getLineCoverage(string $line): Coverage
@@ -69,6 +74,11 @@ public function getLineCoverage(string $line): Coverage
6974
return new Coverage($lineNode, $line);
7075
}
7176

77+
public function setDetermineBranchCoverage(bool $determineBranchCoverage): void
78+
{
79+
$this->determineBranchCoverage = $determineBranchCoverage;
80+
}
81+
7282
protected function getContextNode(): \DOMElement
7383
{
7484
return $this->contextNode;

src/Report/Xml/Method.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,28 @@ public function setLines(string $start, ?string $end = null): void
3737
}
3838
}
3939

40-
public function setTotals(string $executable, string $executed, string $coverage): void
41-
{
42-
$this->contextNode->setAttribute('executable', $executable);
43-
$this->contextNode->setAttribute('executed', $executed);
40+
public function setTotals(
41+
string $executableLines,
42+
string $executedLines,
43+
string $coverage
44+
): void {
45+
$this->contextNode->setAttribute('executable', $executableLines);
46+
$this->contextNode->setAttribute('executed', $executedLines);
4447
$this->contextNode->setAttribute('coverage', $coverage);
4548
}
4649

50+
public function setPathTotals(string $executablePaths, string $executedPaths): void
51+
{
52+
$this->contextNode->setAttribute('executablePaths', $executablePaths);
53+
$this->contextNode->setAttribute('executedPaths', $executedPaths);
54+
}
55+
56+
public function setBranchTotals(string $executableBranches, string $executedBranches): void
57+
{
58+
$this->contextNode->setAttribute('executableBranches', $executableBranches);
59+
$this->contextNode->setAttribute('executedBranches', $executedBranches);
60+
}
61+
4762
public function setCrap(string $crap): void
4863
{
4964
$this->contextNode->setAttribute('crap', $crap);

src/Report/Xml/Node.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ abstract class Node
2121
*/
2222
private $contextNode;
2323

24+
/**
25+
* @var bool
26+
*/
27+
protected $determineBranchCoverage = false;
28+
2429
public function __construct(\DOMElement $context)
2530
{
2631
$this->setContextNode($context);
@@ -44,7 +49,7 @@ public function getTotals(): Totals
4449
);
4550
}
4651

47-
return new Totals($totalsContainer);
52+
return new Totals($totalsContainer, $this->determineBranchCoverage);
4853
}
4954

5055
public function addDirectory(string $name): Directory
@@ -57,7 +62,10 @@ public function addDirectory(string $name): Directory
5762
$dirNode->setAttribute('name', $name);
5863
$this->getContextNode()->appendChild($dirNode);
5964

60-
return new Directory($dirNode);
65+
$directory = new Directory($dirNode);
66+
$directory->setDetermineBranchCoverage($this->determineBranchCoverage);
67+
68+
return $directory;
6169
}
6270

6371
public function addFile(string $name, string $href): File
@@ -71,7 +79,15 @@ public function addFile(string $name, string $href): File
7179
$fileNode->setAttribute('href', $href);
7280
$this->getContextNode()->appendChild($fileNode);
7381

74-
return new File($fileNode);
82+
$file = new File($fileNode);
83+
$file->setDetermineBranchCoverage($this->determineBranchCoverage);
84+
85+
return $file;
86+
}
87+
88+
public function setDetermineBranchCoverage(bool $determineBranchCoverage): void
89+
{
90+
$this->determineBranchCoverage = $determineBranchCoverage;
7591
}
7692

7793
protected function setContextNode(\DOMElement $context): void

0 commit comments

Comments
 (0)