|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of project-quality-inspector. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Alexandre GESLIN <[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
|
|
|
|
|
11
|
|
|
namespace ProjectQualityInspector\Application\Output; |
|
12
|
|
|
|
|
13
|
|
|
use ProjectQualityInspector\Exception\RuleViolationException; |
|
14
|
|
|
use ProjectQualityInspector\Iterator\RuleFilterIterator; |
|
15
|
|
|
|
|
16
|
|
|
class HtmlReportHelper |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
const PAGE_HEADER = <<<EOT |
|
22
|
|
|
<!doctype html> |
|
23
|
|
|
<html lang="en"> |
|
24
|
|
|
<head> |
|
25
|
|
|
<meta charset="utf-8"/> |
|
26
|
|
|
<title>Test Documentation</title> |
|
27
|
|
|
<style> |
|
28
|
|
|
body { |
|
29
|
|
|
text-rendering: optimizeLegibility; |
|
30
|
|
|
font-variant-ligatures: common-ligatures; |
|
31
|
|
|
font-kerning: normal; |
|
32
|
|
|
margin-left: 2em; |
|
33
|
|
|
font-family: Verdana, Arial, sans-serif; |
|
34
|
|
|
} |
|
35
|
|
|
h2 { |
|
36
|
|
|
font-family: Tahoma, Helvetica, Arial; |
|
37
|
|
|
font-size: 1em; |
|
38
|
|
|
} |
|
39
|
|
|
ul { |
|
40
|
|
|
list-style: none; |
|
41
|
|
|
margin-bottom: 1em; |
|
42
|
|
|
} |
|
43
|
|
|
table { |
|
44
|
|
|
border-collapse: collapse; |
|
45
|
|
|
width: 100%; |
|
46
|
|
|
} |
|
47
|
|
|
table, th, td { |
|
48
|
|
|
border: 1px solid black; |
|
49
|
|
|
font-size: 0.95em; |
|
50
|
|
|
} |
|
51
|
|
|
th { |
|
52
|
|
|
height: 2em; |
|
53
|
|
|
text-align: center; |
|
54
|
|
|
background-color:#4CAF50; |
|
55
|
|
|
color:white; |
|
56
|
|
|
} |
|
57
|
|
|
td { |
|
58
|
|
|
padding: 0.4em; |
|
59
|
|
|
} |
|
60
|
|
|
tr:nth-child(even) { |
|
61
|
|
|
background-color: #f2f2f2 |
|
62
|
|
|
} |
|
63
|
|
|
</style> |
|
64
|
|
|
</head> |
|
65
|
|
|
<body> |
|
66
|
|
|
EOT; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var string |
|
70
|
|
|
*/ |
|
71
|
|
|
const PAGE_FOOTER = <<<EOT |
|
72
|
|
|
</body> |
|
73
|
|
|
</html> |
|
74
|
|
|
EOT; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param RuleInterface[] $rules |
|
78
|
|
|
* @param string $htmlFile |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function generateHtmlFile(RuleFilterIterator $rules, $htmlFile) |
|
81
|
|
|
{ |
|
82
|
|
|
$html = self::PAGE_HEADER . '<div> |
|
83
|
|
|
<h1>PQI</h1>'; |
|
84
|
|
|
|
|
85
|
|
|
foreach ($rules as $rule) { |
|
86
|
|
|
$html .= static::createTestSuite($rule::getRuleName(), $rule->getAssertions()); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$html .= '</div>' . self::PAGE_FOOTER; |
|
90
|
|
|
|
|
91
|
|
|
file_put_contents($htmlFile, $html); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $name |
|
96
|
|
|
* @param array $tests |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
private static function createTestSuite($name, array $tests) |
|
100
|
|
|
{ |
|
101
|
|
|
$testSuite = '<h2> '.strtoupper($name).', tests: '.count($tests).' failures: '.static::sumArraysKey('failures', $tests, 'sum').', errors: '.static::sumArraysKey('errors', $tests, 'sum').', time: '.static::sumArraysKey('time', $tests).'</h2>'; |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
$testSuite .= '<table class="testsuite"> |
|
104
|
|
|
<tr> |
|
105
|
|
|
<th>Type</th> |
|
106
|
|
|
<th>message</th> |
|
107
|
|
|
</tr>'; |
|
108
|
|
|
|
|
109
|
|
|
foreach ($tests as $test) { |
|
110
|
|
|
$testSuite .= static::createTestCase($test); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$testSuite .= '</table>'; |
|
114
|
|
|
|
|
115
|
|
|
return $testSuite; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param array $test |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
private static function createTestCase(array $test) |
|
123
|
|
|
{ |
|
124
|
|
|
$testCase = ''; |
|
125
|
|
|
|
|
126
|
|
View Code Duplication |
if ($test['failures']['sum'] > 0) { |
|
|
|
|
|
|
127
|
|
|
foreach ($test['failures']['list'] as $failure) { |
|
128
|
|
|
$testCase .= '<tr class="failure"> |
|
129
|
|
|
<td>'.$failure['type'].'</td> |
|
130
|
|
|
<td>'.self::sanitizeTags($failure['message']).'</td> |
|
131
|
|
|
</tr>'; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
View Code Duplication |
if ($test['errors']['sum'] > 0) { |
|
|
|
|
|
|
136
|
|
|
foreach ($test['errors']['list'] as $error) { |
|
137
|
|
|
$testCase .= '<tr class="error"> |
|
138
|
|
|
<td>'.$error['type'].'</td> |
|
139
|
|
|
<td>'.self::sanitizeTags($error['message']).'</td> |
|
140
|
|
|
</tr>'; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $testCase; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $key |
|
149
|
|
|
* @param array $arrays |
|
150
|
|
|
* @param string $subKey |
|
151
|
|
|
* @return integer |
|
152
|
|
|
*/ |
|
153
|
|
View Code Duplication |
private static function sumArraysKey($key, array $arrays, $subKey = null) |
|
|
|
|
|
|
154
|
|
|
{ |
|
155
|
|
|
$sum = 0; |
|
156
|
|
|
|
|
157
|
|
|
foreach ($arrays as $array) { |
|
158
|
|
|
$sum += ($subKey) ? $array[$key][$subKey] : $array[$key]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $sum; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
private static function sanitizeTags($string) |
|
165
|
|
|
{ |
|
166
|
|
|
$string = str_replace('<fg=green>', '<span style="color: green;">', $string); |
|
167
|
|
|
$string = str_replace('</>', '</span>', $string); |
|
168
|
|
|
$string = preg_replace('/\t+/', '<br />', $string); |
|
169
|
|
|
|
|
170
|
|
|
return $string; |
|
171
|
|
|
} |
|
172
|
|
|
} |
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: