Skip to content

Commit 2b1d909

Browse files
committed
Add branch & path coverage flag to Text reporter
1 parent 9103910 commit 2b1d909

File tree

1 file changed

+81
-52
lines changed

1 file changed

+81
-52
lines changed

src/Report/Text.php

Lines changed: 81 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,18 @@ final class Text
7070
*/
7171
private $showOnlySummary;
7272

73-
public function __construct(int $lowUpperBound = 50, int $highLowerBound = 90, bool $showUncoveredFiles = false, bool $showOnlySummary = false)
73+
/**
74+
* @var bool
75+
*/
76+
private $determineBranchCoverage;
77+
78+
public function __construct(int $lowUpperBound = 50, int $highLowerBound = 90, bool $showUncoveredFiles = false, bool $showOnlySummary = false, bool $determineBranchCoverage = false)
7479
{
75-
$this->lowUpperBound = $lowUpperBound;
76-
$this->highLowerBound = $highLowerBound;
77-
$this->showUncoveredFiles = $showUncoveredFiles;
78-
$this->showOnlySummary = $showOnlySummary;
80+
$this->lowUpperBound = $lowUpperBound;
81+
$this->highLowerBound = $highLowerBound;
82+
$this->showUncoveredFiles = $showUncoveredFiles;
83+
$this->showOnlySummary = $showOnlySummary;
84+
$this->determineBranchCoverage = $determineBranchCoverage;
7985
}
8086

8187
public function process(CodeCoverage $coverage, bool $showColors = false): string
@@ -110,15 +116,17 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
110116
$report->getNumExecutableLines()
111117
);
112118

113-
$colors['branches'] = $this->getCoverageColor(
114-
$report->getNumTestedBranches(),
115-
$report->getNumBranches()
116-
);
119+
if ($this->determineBranchCoverage) {
120+
$colors['branches'] = $this->getCoverageColor(
121+
$report->getNumTestedBranches(),
122+
$report->getNumBranches()
123+
);
117124

118-
$colors['paths'] = $this->getCoverageColor(
119-
$report->getNumTestedPaths(),
120-
$report->getNumPaths()
121-
);
125+
$colors['paths'] = $this->getCoverageColor(
126+
$report->getNumTestedPaths(),
127+
$report->getNumPaths()
128+
);
129+
}
122130

123131
$colors['reset'] = self::COLOR_RESET;
124132
$colors['header'] = self::COLOR_HEADER;
@@ -158,27 +166,32 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
158166
$report->getNumExecutableLines()
159167
);
160168

161-
$branches = \sprintf(
162-
' Branches: %6s (%d/%d)',
163-
Util::percent(
169+
$paths = '';
170+
$branches = '';
171+
172+
if ($this->determineBranchCoverage) {
173+
$branches = \sprintf(
174+
' Branches: %6s (%d/%d)',
175+
Util::percent(
176+
$report->getNumTestedBranches(),
177+
$report->getNumBranches(),
178+
true
179+
),
164180
$report->getNumTestedBranches(),
165-
$report->getNumBranches(),
166-
true
167-
),
168-
$report->getNumTestedBranches(),
169-
$report->getNumBranches()
170-
);
181+
$report->getNumBranches()
182+
);
171183

172-
$paths = \sprintf(
173-
' Paths: %6s (%d/%d)',
174-
Util::percent(
184+
$paths = \sprintf(
185+
' Paths: %6s (%d/%d)',
186+
Util::percent(
187+
$report->getNumTestedPaths(),
188+
$report->getNumPaths(),
189+
true
190+
),
175191
$report->getNumTestedPaths(),
176-
$report->getNumPaths(),
177-
true
178-
),
179-
$report->getNumTestedPaths(),
180-
$report->getNumPaths()
181-
);
192+
$report->getNumPaths()
193+
);
194+
}
182195

183196
$padding = \max(\array_map('strlen', [$classes, $methods, $lines]));
184197

@@ -200,8 +213,11 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
200213
$output .= $this->format($colors['classes'], $padding, $classes);
201214
$output .= $this->format($colors['methods'], $padding, $methods);
202215
$output .= $this->format($colors['lines'], $padding, $lines);
203-
$output .= $this->format($colors['branches'], $padding, $branches);
204-
$output .= $this->format($colors['paths'], $padding, $paths);
216+
217+
if ($this->determineBranchCoverage) {
218+
$output .= $this->format($colors['branches'], $padding, $branches);
219+
$output .= $this->format($colors['paths'], $padding, $paths);
220+
}
205221

206222
if ($this->showOnlySummary) {
207223
return $output . \PHP_EOL;
@@ -239,10 +255,13 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
239255
$classMethods++;
240256
$classStatements += $method['executableLines'];
241257
$coveredClassStatements += $method['executedLines'];
242-
$classPaths += $method['executablePaths'];
243-
$coveredClassPaths += $method['executedPaths'];
244-
$classBranches += $method['executableBranches'];
245-
$coveredClassBranches += $method['executedBranches'];
258+
259+
if ($this->determineBranchCoverage) {
260+
$classPaths += $method['executablePaths'];
261+
$coveredClassPaths += $method['executedPaths'];
262+
$classBranches += $method['executableBranches'];
263+
$coveredClassBranches += $method['executedBranches'];
264+
}
246265

247266
if ($method['coverage'] === 100) {
248267
$coveredMethods++;
@@ -259,16 +278,19 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
259278
\strlen((string) $classStatements),
260279
\strlen((string) $coveredClassStatements)
261280
);
262-
$maxBranches = \max(
263-
$maxBranches,
264-
\strlen((string) $classBranches),
265-
\strlen((string) $coveredClassBranches)
266-
);
267-
$maxPaths = \max(
268-
$maxPaths,
269-
\strlen((string) $classPaths),
270-
\strlen((string) $coveredClassPaths)
271-
);
281+
282+
if ($this->determineBranchCoverage) {
283+
$maxBranches = \max(
284+
$maxBranches,
285+
\strlen((string) $classBranches),
286+
\strlen((string) $coveredClassBranches)
287+
);
288+
$maxPaths = \max(
289+
$maxPaths,
290+
\strlen((string) $classPaths),
291+
\strlen((string) $coveredClassPaths)
292+
);
293+
}
272294

273295
$namespace = '';
274296

@@ -306,16 +328,23 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin
306328
if ($showColors) {
307329
$methodColor = $this->getCoverageColor($classInfo['methodsCovered'], $classInfo['methodCount']);
308330
$linesColor = $this->getCoverageColor($classInfo['statementsCovered'], $classInfo['statementCount']);
309-
$branchesColor = $this->getCoverageColor($classInfo['branchesCovered'], $classInfo['branchCount']);
310-
$pathsColor = $this->getCoverageColor($classInfo['pathsCovered'], $classInfo['pathCount']);
331+
332+
if ($this->determineBranchCoverage) {
333+
$branchesColor = $this->getCoverageColor($classInfo['branchesCovered'], $classInfo['branchCount']);
334+
$pathsColor = $this->getCoverageColor($classInfo['pathsCovered'], $classInfo['pathCount']);
335+
}
311336
$resetColor = $colors['reset'];
312337
}
313338

314339
$output .= \PHP_EOL . $fullQualifiedPath . \PHP_EOL
315340
. ' ' . $methodColor . 'Methods: ' . $this->printCoverageCounts($classInfo['methodsCovered'], $classInfo['methodCount'], $maxMethods) . $resetColor . ' '
316-
. ' ' . $linesColor . 'Lines: ' . $this->printCoverageCounts($classInfo['statementsCovered'], $classInfo['statementCount'], $maxLines) . $resetColor . ' '
317-
. ' ' . $branchesColor . 'Branches: ' . $this->printCoverageCounts($classInfo['branchesCovered'], $classInfo['branchCount'], $maxBranches) . $resetColor . ' '
318-
. ' ' . $pathsColor . 'Paths: ' . $this->printCoverageCounts($classInfo['pathsCovered'], $classInfo['pathCount'], $maxPaths) . $resetColor;
341+
. ' ' . $linesColor . 'Lines: ' . $this->printCoverageCounts($classInfo['statementsCovered'], $classInfo['statementCount'], $maxLines) . $resetColor;
342+
343+
if ($this->determineBranchCoverage) {
344+
$output .= ''
345+
. ' ' . $branchesColor . 'Branches: ' . $this->printCoverageCounts($classInfo['branchesCovered'], $classInfo['branchCount'], $maxBranches) . $resetColor . ' '
346+
. ' ' . $pathsColor . 'Paths: ' . $this->printCoverageCounts($classInfo['pathsCovered'], $classInfo['pathCount'], $maxPaths) . $resetColor;
347+
}
319348
}
320349
}
321350

0 commit comments

Comments
 (0)