Skip to content

Commit 89e93c4

Browse files
author
Gabriel Zerbib
committed
Optionally remove the project's absolute path ___location from the HTML reports.
1 parent aef655b commit 89e93c4

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

src/CodeCoverage/Report/HTML.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,25 @@ class PHP_CodeCoverage_Report_HTML
7676
*/
7777
private $highLowerBound;
7878

79+
/**
80+
* @var string
81+
*/
82+
private $absoluteRoot;
83+
7984
/**
8085
* Constructor.
8186
*
8287
* @param integer $lowUpperBound
8388
* @param integer $highLowerBound
8489
* @param string $generator
90+
* @param string $absoluteRoot Optional: root path of the sources under analysis
8591
*/
86-
public function __construct($lowUpperBound = 50, $highLowerBound = 90, $generator = '')
92+
public function __construct($lowUpperBound = 50, $highLowerBound = 90, $generator = '', $absoluteRoot = null)
8793
{
8894
$this->generator = $generator;
8995
$this->highLowerBound = $highLowerBound;
9096
$this->lowUpperBound = $lowUpperBound;
97+
$this->absoluteRoot = $absoluteRoot;
9198

9299
$this->templatePath = sprintf(
93100
'%s%sHTML%sRenderer%sTemplate%s',
@@ -121,23 +128,26 @@ public function process(PHP_CodeCoverage $coverage, $target)
121128
$this->generator,
122129
$date,
123130
$this->lowUpperBound,
124-
$this->highLowerBound
131+
$this->highLowerBound,
132+
$this->absoluteRoot
125133
);
126134

127135
$directory = new PHP_CodeCoverage_Report_HTML_Renderer_Directory(
128136
$this->templatePath,
129137
$this->generator,
130138
$date,
131139
$this->lowUpperBound,
132-
$this->highLowerBound
140+
$this->highLowerBound,
141+
$this->absoluteRoot
133142
);
134143

135144
$file = new PHP_CodeCoverage_Report_HTML_Renderer_File(
136145
$this->templatePath,
137146
$this->generator,
138147
$date,
139148
$this->lowUpperBound,
140-
$this->highLowerBound
149+
$this->highLowerBound,
150+
$this->absoluteRoot
141151
);
142152

143153
$directory->render($report, $target . 'index.html');

src/CodeCoverage/Report/HTML/Renderer.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ abstract class PHP_CodeCoverage_Report_HTML_Renderer
8888
*/
8989
protected $version;
9090

91+
/**
92+
* @var string
93+
*/
94+
protected $absoluteRoot;
95+
9196
/**
9297
* Constructor.
9398
*
@@ -96,8 +101,9 @@ abstract class PHP_CodeCoverage_Report_HTML_Renderer
96101
* @param string $date
97102
* @param integer $lowUpperBound
98103
* @param integer $highLowerBound
104+
* @param string $absoluteRoot Optional: root path of the sources under analysis
99105
*/
100-
public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound)
106+
public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound, $absoluteRoot = null)
101107
{
102108
$version = new SebastianBergmann\Version('2.0.5', dirname(dirname(dirname(dirname(__DIR__)))));
103109

@@ -107,6 +113,7 @@ public function __construct($templatePath, $generator, $date, $lowUpperBound, $h
107113
$this->lowUpperBound = $lowUpperBound;
108114
$this->highLowerBound = $highLowerBound;
109115
$this->version = $version->getVersion();
116+
$this->absoluteRoot = $absoluteRoot;
110117
}
111118

112119
/**
@@ -243,7 +250,7 @@ protected function getActiveBreadcrumb(PHP_CodeCoverage_Report_Node $node)
243250
{
244251
$buffer = sprintf(
245252
' <li class="active">%s</li>' . "\n",
246-
$node->getName()
253+
$this->stripProjectPrefixFromNodeName($node)
247254
);
248255

249256
if ($node instanceof PHP_CodeCoverage_Report_Node_Directory) {
@@ -253,6 +260,25 @@ protected function getActiveBreadcrumb(PHP_CodeCoverage_Report_Node $node)
253260
return $buffer;
254261
}
255262

263+
/**
264+
* An evolved version of PHP_CodeCoverage_Report_Node::getName(), which removes
265+
* the optional absoluteRoot prefix in the node's full path.
266+
* @param PHP_CodeCoverage_Report_Node $node
267+
* @return string
268+
*/
269+
protected function stripProjectPrefixFromNodeName(PHP_CodeCoverage_Report_Node $node)
270+
{
271+
if ($this->absoluteRoot) {
272+
return preg_replace(
273+
'#^' . $this->absoluteRoot . '#',
274+
'',
275+
$node->getName());
276+
}
277+
else
278+
return $node->getName();
279+
}
280+
281+
256282
protected function getInactiveBreadcrumb(PHP_CodeCoverage_Report_Node $node, $pathToRoot)
257283
{
258284
return sprintf(

src/CodeCoverage/Report/HTML/Renderer/Dashboard.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ protected function getActiveBreadcrumb(PHP_CodeCoverage_Report_Node $node)
324324
' <li><a href="%s.html">%s</a></li>' . "\n" .
325325
' <li class="active">(Dashboard)</li>' . "\n",
326326
$node->getId(),
327-
$node->getName()
327+
//Do not always print the full absolute path of $node here:
328+
//optionally replace prefix
329+
$this->stripProjectPrefixFromNodeName($node)
328330
);
329331
}
330332
}

src/CodeCoverage/Report/HTML/Renderer/File.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,17 @@ class PHP_CodeCoverage_Report_HTML_Renderer_File extends PHP_CodeCoverage_Report
8686
* @param string $date
8787
* @param integer $lowUpperBound
8888
* @param integer $highLowerBound
89+
* @param string $absoluteRoot Optional: see parent
8990
*/
90-
public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound)
91+
public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound, $absoluteRoot = null)
9192
{
9293
parent::__construct(
9394
$templatePath,
9495
$generator,
9596
$date,
9697
$lowUpperBound,
97-
$highLowerBound
98+
$highLowerBound,
99+
$absoluteRoot
98100
);
99101
}
100102

0 commit comments

Comments
 (0)