-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathCollector.php
More file actions
198 lines (153 loc) · 3.98 KB
/
Collector.php
File metadata and controls
198 lines (153 loc) · 3.98 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* This file is part of the Nette Tester.
* Copyright (c) 2009 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Tester\CodeCoverage;
/**
* Code coverage collector.
*/
class Collector
{
public const
ENGINE_PCOV = 'PCOV',
ENGINE_PHPDBG = 'PHPDBG',
ENGINE_XDEBUG = 'Xdebug';
/** @var resource */
private static $file;
/** @var string */
private static $engine;
public static function detectEngines(): array
{
return array_filter([
extension_loaded('pcov') ? [self::ENGINE_PCOV, phpversion('pcov')] : null,
defined('PHPDBG_VERSION') ? [self::ENGINE_PHPDBG, PHPDBG_VERSION] : null,
extension_loaded('xdebug') ? [self::ENGINE_XDEBUG, phpversion('xdebug')] : null,
]);
}
public static function isStarted(): bool
{
return self::$file !== null;
}
/**
* Starts gathering the information for code coverage.
* @throws \LogicException
*/
public static function start(string $file, string $engine): void
{
if (self::isStarted()) {
throw new \LogicException('Code coverage collector has been already started.');
} elseif (!in_array(
$engine,
array_map(function (array $engineInfo) { return $engineInfo[0]; }, self::detectEngines()),
true
)) {
throw new \LogicException("Code coverage engine '$engine' is not supported.");
}
self::$file = fopen($file, 'c+');
self::$engine = $engine;
self::{'start' . $engine}();
register_shutdown_function(function (): void {
register_shutdown_function([self::class, 'save']);
});
}
/**
* Flushes all gathered information. Effective only with PHPDBG collector.
*/
public static function flush(): void
{
if (self::isStarted() && self::$engine === self::ENGINE_PHPDBG) {
self::save();
}
}
/**
* Saves information about code coverage. Can be called repeatedly to free memory.
* @throws \LogicException
*/
public static function save(): void
{
if (!self::isStarted()) {
throw new \LogicException('Code coverage collector has not been started.');
}
[$positive, $negative] = self::{'collect' . self::$engine}();
flock(self::$file, LOCK_EX);
fseek(self::$file, 0);
$rawContent = stream_get_contents(self::$file);
$original = $rawContent ? unserialize($rawContent) : [];
$coverage = array_replace_recursive($negative, $original, $positive);
fseek(self::$file, 0);
ftruncate(self::$file, 0);
fwrite(self::$file, serialize($coverage));
flock(self::$file, LOCK_UN);
}
private static function startPCOV(): void
{
\pcov\start();
}
/**
* Collects information about code coverage.
*/
private static function collectPCOV(): array
{
$positive = $negative = [];
\pcov\stop();
foreach (\pcov\collect() as $file => $lines) {
if (!file_exists($file)) {
continue;
}
foreach ($lines as $num => $val) {
if ($val > 0) {
$positive[$file][$num] = $val;
} else {
$negative[$file][$num] = $val;
}
}
}
return [$positive, $negative];
}
private static function startXdebug(): void
{
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
}
/**
* Collects information about code coverage.
*/
private static function collectXdebug(): array
{
$positive = $negative = [];
foreach (xdebug_get_code_coverage() as $file => $lines) {
if (!file_exists($file)) {
continue;
}
foreach ($lines as $num => $val) {
if ($val > 0) {
$positive[$file][$num] = $val;
} else {
$negative[$file][$num] = $val;
}
}
}
return [$positive, $negative];
}
private static function startPhpDbg(): void
{
phpdbg_start_oplog();
}
/**
* Collects information about code coverage.
*/
private static function collectPhpDbg(): array
{
$positive = phpdbg_end_oplog();
$negative = phpdbg_get_executable();
foreach ($positive as $file => &$lines) {
$lines = array_fill_keys(array_keys($lines), 1);
}
foreach ($negative as $file => &$lines) {
$lines = array_fill_keys(array_keys($lines), -1);
}
phpdbg_start_oplog();
return [$positive, $negative];
}
}