/
Collector.php
159 lines (128 loc) 路 3.1 KB
/
Collector.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
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
<?php
/**
* This file is part of the Nette Tester.
* Copyright (c) 2009 David Grudl (https://davidgrudl.com)
*/
namespace Tester\CodeCoverage;
/**
* Code coverage collector.
*/
class Collector
{
const
STRATEGY_AUTO = 'Auto',
STRATEGY_XDEBUG = 'Xdebug',
STRATEGY_PHPDBG = 'PhpDbg';
/** @var resource */
private static $file;
/**
* Starts gathering the information for code coverage.
* @param string
* @param string self::STRATEGY_*
* @return void
*/
public static function start($file, $strategy = self::STRATEGY_AUTO)
{
if (self::$file) {
throw new \LogicException('Code coverage collector has been already started.');
}
$method = "start$strategy";
if (!method_exists(__CLASS__, $method)) {
throw new \LogicException("Unknown strategy '$strategy'");
}
self::$file = fopen($file, 'a+');
self::$method();
}
/**
* @return void
*/
private static function startAuto()
{
if (defined('PHPDBG_VERSION')) {
self::startPhpDbg();
} else {
self::startXdebug();
}
}
/**
* @return void
*/
private static function startXdebug()
{
if (!extension_loaded('xdebug')) {
throw new \Exception('Code coverage functionality requires Xdebug extension.');
}
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
register_shutdown_function(function () {
register_shutdown_function(array(__CLASS__, 'saveXdebug'));
});
}
/**
* @return void
*/
private static function startPhpDbg()
{
if (!defined('PHPDBG_VERSION')) {
throw new \Exception('Code coverage functionality requires phpdbg SAPI.');
}
phpdbg_start_oplog();
register_shutdown_function(function () {
register_shutdown_function(array(__CLASS__, 'savePhpDbg'));
});
}
/**
* Saves information about code coverage. Do not call directly.
* @return void
* @internal
*/
public static function saveXdebug()
{
$positive = array();
$negative = array();
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;
}
}
}
self::save($positive, $negative);
}
/**
* Saves information about code coverage. Do not call directly.
* @return void
* @internal
*/
public static function savePhpDbg()
{
$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);
}
self::save($positive, $negative);
}
/**
* @param array
* @param array
* @return void
*/
private static function save(array $positive, array $negative)
{
flock(self::$file, LOCK_EX);
fseek(self::$file, 0);
$original = @unserialize(stream_get_contents(self::$file)) ?: array(); // @ file may be empty
$coverage = array_replace_recursive($negative, $original, $positive);
ftruncate(self::$file, 0);
fwrite(self::$file, serialize($coverage));
fclose(self::$file);
}
}