|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kint\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Kint\Object\BasicObject; |
|
6
|
|
|
use Kint\Object\Representation\MicrotimeRepresentation; |
|
7
|
|
|
|
|
8
|
|
|
class MicrotimePlugin extends Plugin |
|
9
|
|
|
{ |
|
10
|
|
|
private static $last = null; |
|
11
|
|
|
private static $start = null; |
|
12
|
|
|
private static $times = 0; |
|
13
|
|
|
private static $group = 0; |
|
14
|
|
|
|
|
15
|
|
|
public function getTypes() |
|
16
|
|
|
{ |
|
17
|
|
|
return array('string', 'double'); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function getTriggers() |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
return Parser::TRIGGER_SUCCESS; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function parse(&$var, BasicObject &$o, $trigger) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
if ($o->depth !== 0) { |
|
28
|
|
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (is_string($var)) { |
|
32
|
|
|
if ($o->name !== 'microtime()' || !preg_match('/^0\.[0-9]{8} [0-9]{10}$/', $var)) { |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
list($usec, $sec) = explode(' ', $var); |
|
37
|
|
|
$usec = substr($usec, 2, 6); |
|
38
|
|
|
} else { |
|
39
|
|
|
if ($o->name !== 'microtime(...)') { |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$sec = floor($var); |
|
44
|
|
|
$usec = $var - $sec; |
|
45
|
|
|
$usec = floor($usec * 1000000); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$time = $sec + ($usec / 1000000); |
|
49
|
|
|
|
|
50
|
|
|
if (self::$last !== null) { |
|
51
|
|
|
$last_time = self::$last[0] + (self::$last[1] / 1000000); |
|
|
|
|
|
|
52
|
|
|
$lap = $time - $last_time; |
|
|
|
|
|
|
53
|
|
|
++self::$times; |
|
54
|
|
|
} else { |
|
55
|
|
|
$lap = null; |
|
56
|
|
|
self::$start = $time; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
self::$last = array($sec, $usec); |
|
60
|
|
|
|
|
61
|
|
|
if ($lap !== null) { |
|
62
|
|
|
$total = $time - self::$start; |
|
63
|
|
|
$r = new MicrotimeRepresentation($sec, $usec, self::$group, $lap, $total, self::$times); |
|
|
|
|
|
|
64
|
|
|
} else { |
|
65
|
|
|
$r = new MicrotimeRepresentation($sec, $usec, self::$group); |
|
66
|
|
|
} |
|
67
|
|
|
$r->contents = $var; |
|
|
|
|
|
|
68
|
|
|
$r->implicit_label = true; |
|
69
|
|
|
|
|
70
|
|
|
$o->removeRepresentation($o->value); |
|
71
|
|
|
$o->addRepresentation($r); |
|
72
|
|
|
$o->hints[] = 'microtime'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public static function clean() |
|
76
|
|
|
{ |
|
77
|
|
|
self::$last = null; |
|
78
|
|
|
self::$start = null; |
|
79
|
|
|
self::$times = 0; |
|
80
|
|
|
++self::$group; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.