1: <?php
2:
3: /**
4: * This file is part of the Nette Tester.
5: * Copyright (c) 2009 David Grudl (http://davidgrudl.com)
6: */
7:
8: namespace Tester\CodeCoverage;
9:
10:
11: /**
12: * Code coverage collector.
13: *
14: * @author David Grudl
15: */
16: class Collector
17: {
18: /** @var string */
19: static public $file;
20:
21:
22: /**
23: * Starts gathering the information for code coverage.
24: * @param string
25: * @return void
26: */
27: public static function start($file)
28: {
29: self::$file = $file;
30: xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
31: register_shutdown_function(function() {
32: register_shutdown_function(array(__CLASS__, 'save'));
33: });
34: }
35:
36:
37: /**
38: * Saves information about code coverage. Do not call directly.
39: * @return void
40: * @internal
41: */
42: public static function save()
43: {
44: $f = fopen(self::$file, 'a+');
45: flock($f, LOCK_EX);
46: fseek($f, 0);
47: $coverage = @unserialize(stream_get_contents($f));
48:
49: foreach (xdebug_get_code_coverage() as $filename => $lines) {
50: foreach ($lines as $num => $val) {
51: if (empty($coverage[$filename][$num]) || $val > 0) {
52: $coverage[$filename][$num] = $val; // -1 => untested; -2 => dead code
53: }
54: }
55: }
56:
57: ftruncate($f, 0);
58: fwrite($f, serialize($coverage));
59: fclose($f);
60: }
61:
62: }
63: