Skip to content

Commit 82fdf3a

Browse files
Gabriel Zerbibfigdice
authored andcommitted
Display optional project name instead of absolute root in HTML renderer. Fixes sebastianbergmann#235 in sebastianbergmann/php-code-coverage
Indent with space phpdoc
1 parent 818ec8b commit 82fdf3a

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

src/CodeCoverage/Report/HTML.php

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

79+
/**
80+
* @var string
81+
*/
82+
private $absoluteRoot;
83+
/**
84+
* @var string
85+
*/
86+
private $projectPrefix;
87+
7988
/**
8089
* Constructor.
8190
*
8291
* @param integer $lowUpperBound
8392
* @param integer $highLowerBound
8493
* @param string $generator
94+
* @param string $absoluteRoot Optional: root path of the sources under analysis
95+
* @param string $projectPrefix Optional: name of project, to display as replacement for absoluteRoot
8596
*/
86-
public function __construct($lowUpperBound = 50, $highLowerBound = 90, $generator = '')
97+
public function __construct($lowUpperBound = 50, $highLowerBound = 90, $generator = '', $absoluteRoot = null, $projectPrefix = null)
8798
{
8899
$this->generator = $generator;
89100
$this->highLowerBound = $highLowerBound;
90101
$this->lowUpperBound = $lowUpperBound;
102+
$this->absoluteRoot = $absoluteRoot;
103+
$this->projectPrefix = $projectPrefix;
91104

92105
$this->templatePath = sprintf(
93106
'%s%sHTML%sRenderer%sTemplate%s',
@@ -121,23 +134,29 @@ public function process(PHP_CodeCoverage $coverage, $target)
121134
$this->generator,
122135
$date,
123136
$this->lowUpperBound,
124-
$this->highLowerBound
137+
$this->highLowerBound,
138+
$this->absoluteRoot,
139+
$this->projectPrefix
125140
);
126141

127142
$directory = new PHP_CodeCoverage_Report_HTML_Renderer_Directory(
128143
$this->templatePath,
129144
$this->generator,
130145
$date,
131146
$this->lowUpperBound,
132-
$this->highLowerBound
147+
$this->highLowerBound,
148+
$this->absoluteRoot,
149+
$this->projectPrefix
133150
);
134151

135152
$file = new PHP_CodeCoverage_Report_HTML_Renderer_File(
136153
$this->templatePath,
137154
$this->generator,
138155
$date,
139156
$this->lowUpperBound,
140-
$this->highLowerBound
157+
$this->highLowerBound,
158+
$this->absoluteRoot,
159+
$this->projectPrefix
141160
);
142161

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

src/CodeCoverage/Report/HTML/Renderer.php

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

91+
/**
92+
* @var string
93+
*/
94+
protected $absoluteRoot;
95+
96+
/**
97+
* @var string
98+
*/
99+
protected $projectPrefix;
100+
91101
/**
92102
* Constructor.
93103
*
@@ -96,8 +106,10 @@ abstract class PHP_CodeCoverage_Report_HTML_Renderer
96106
* @param string $date
97107
* @param integer $lowUpperBound
98108
* @param integer $highLowerBound
109+
* @param string $absoluteRoot Optional: root path of the sources under analysis
110+
* @param string $projectPrefix Optional: name of project, to display as replacement for absoluteRoot
99111
*/
100-
public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound)
112+
public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound, $absoluteRoot = null, $projectPrefix = null)
101113
{
102114
$version = new SebastianBergmann\Version('3.0', __DIR__);
103115

@@ -107,6 +119,8 @@ public function __construct($templatePath, $generator, $date, $lowUpperBound, $h
107119
$this->lowUpperBound = $lowUpperBound;
108120
$this->highLowerBound = $highLowerBound;
109121
$this->version = $version->getVersion();
122+
$this->absoluteRoot = $absoluteRoot;
123+
$this->projectPrefix = $projectPrefix;
110124
}
111125

112126
/**
@@ -251,7 +265,7 @@ protected function getActiveBreadcrumb(PHP_CodeCoverage_Report_Node $node)
251265
{
252266
$buffer = sprintf(
253267
' <li class="active">%s</li>' . "\n",
254-
$node->getName()
268+
$this->stripProjectPrefixFromNodeName($node)
255269
);
256270

257271
if ($node instanceof PHP_CodeCoverage_Report_Node_Directory) {
@@ -261,6 +275,26 @@ protected function getActiveBreadcrumb(PHP_CodeCoverage_Report_Node $node)
261275
return $buffer;
262276
}
263277

278+
/**
279+
* An evolved version of PHP_CodeCoverage_Report_Node::getName(), which replaces
280+
* the optional absoluteRoot prefix in the node's full path, with specified
281+
* project name.
282+
* @param PHP_CodeCoverage_Report_Node $node
283+
* @return string
284+
*/
285+
protected function stripProjectPrefixFromNodeName(PHP_CodeCoverage_Report_Node $node)
286+
{
287+
if ($this->absoluteRoot) {
288+
return preg_replace(
289+
'#^' . $this->absoluteRoot . '#',
290+
$this->projectPrefix,
291+
$node->getName());
292+
}
293+
else
294+
return $node->getName();
295+
}
296+
297+
264298
/**
265299
* @param PHP_CodeCoverage_Report_Node $node
266300
* @param $pathToRoot

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ protected function getActiveBreadcrumb(PHP_CodeCoverage_Report_Node $node)
328328
' <li><a href="%s.html">%s</a></li>' . "\n" .
329329
' <li class="active">(Dashboard)</li>' . "\n",
330330
$node->getId(),
331-
$node->getName()
331+
//Do not always print the full absolute path of $node here:
332+
//optionally replace prefix
333+
$this->stripProjectPrefixFromNodeName($node)
332334
);
333335
}
334336
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,19 @@ 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
90+
* @param string $projectPrefix Optional: see parent
8991
*/
90-
public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound)
92+
public function __construct($templatePath, $generator, $date, $lowUpperBound, $highLowerBound, $absoluteRoot = null, $projectPrefix = null)
9193
{
9294
parent::__construct(
9395
$templatePath,
9496
$generator,
9597
$date,
9698
$lowUpperBound,
97-
$highLowerBound
99+
$highLowerBound,
100+
$absoluteRoot,
101+
$projectPrefix
98102
);
99103
}
100104

0 commit comments

Comments
 (0)