/
HtmlGenerator.php
109 lines (89 loc) 路 2.36 KB
/
HtmlGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* This file is part of the Nette Tester.
* Copyright (c) 2009 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Tester\CodeCoverage\Generators;
/**
* Code coverage report generator.
*/
class HtmlGenerator extends AbstractGenerator
{
/** @var array */
public static $classes = [
self::CODE_TESTED => 't', // tested
self::CODE_UNTESTED => 'u', // untested
self::CODE_DEAD => 'dead', // dead code
];
/** @var string */
private $title;
/** @var array */
private $files = [];
/**
* @param string $file path to coverage.dat file
* @param array $sources files/directories
*/
public function __construct(string $file, array $sources = [], string $title = null)
{
parent::__construct($file, $sources);
$this->title = $title;
}
protected function renderSelf(): void
{
$this->setupHighlight();
$this->parse();
$title = $this->title;
$classes = self::$classes;
$files = $this->files;
$coveredPercent = $this->getCoveredPercent();
include __DIR__ . '/template.phtml';
}
private function setupHighlight(): void
{
ini_set('highlight.comment', 'hc');
ini_set('highlight.default', 'hd');
ini_set('highlight.html', 'hh');
ini_set('highlight.keyword', 'hk');
ini_set('highlight.string', 'hs');
}
private function parse(): void
{
if (count($this->files) > 0) {
return;
}
$this->files = [];
$commonSourcesPath = self::getCommonFilesPath($this->sources) . DIRECTORY_SEPARATOR;
foreach ($this->getSourceIterator() as $entry) {
$entry = (string) $entry;
$coverage = $covered = $total = 0;
$loaded = !empty($this->data[$entry]);
$lines = [];
if ($loaded) {
$lines = $this->data[$entry];
foreach ($lines as $flag) {
if ($flag >= self::CODE_UNTESTED) {
$total++;
}
if ($flag >= self::CODE_TESTED) {
$covered++;
}
}
$coverage = round($covered * 100 / $total);
$this->totalSum += $total;
$this->coveredSum += $covered;
} else {
$this->totalSum += count(file($entry, FILE_SKIP_EMPTY_LINES));
}
$light = $total ? $total < 5 : count(file($entry)) < 50;
$this->files[] = (object) [
'name' => str_replace($commonSourcesPath, '', $entry),
'file' => $entry,
'lines' => $lines,
'coverage' => $coverage,
'total' => $total,
'class' => $light ? 'light' : ($loaded ? null : 'not-loaded'),
];
}
}
}