Test Failed
Push — trunk ( 0d5d14...30e273 )
by SuperNova.WS
11:46
created

Timer::mark()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 3
nop 1
dl 0
loc 16
rs 9.9666
c 1
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 05.08.2018 8:05
4
 */
5
6
class Timer
7
{
8
    /**
9
     * @var array[] $times [
10
     *                        'time' => (float)microtime(true),
11
     *                        'location' => (string)filename&line,
12
     *                        'message' => (string)attachedMessage
13
     *                     ]
14
     */
15
    private static $times = [];
16
17
    public static function init()
18
    {
19
        if (empty(static::$times)) {
0 ignored issues
show
Bug introduced by
Since $times is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $times to at least protected.
Loading history...
20
            static::$times[] = ['time' => microtime(true)];
21
        }
22
    }
23
24
    /**
25
     * @param string $message Message to attach to timestamp
26
     */
27
    public static function mark($message = '')
28
    {
29
        static::init();
30
31
        foreach (debug_backtrace() as $trace) {
32
            if (empty($trace['class']) || $trace['class'] != static::class) {
33
                break;
34
            }
35
36
            $realCall = $trace;
37
        }
38
39
        static::$times[] = [
0 ignored issues
show
Bug introduced by
Since $times is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $times to at least protected.
Loading history...
40
            'time'     => microtime(true),
41
            'location' => $realCall['file'] . '@' . $realCall['line'],
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $realCall seems to be defined by a foreach iteration on line 31. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
42
            'message'  => $message,
43
        ];
44
    }
45
46
47
    /**
48
     * @param string $message
49
     *
50
     * @param bool   $fromStart
51
     *
52
     * @return string
53
     */
54
    public static function elapsed($message = '', $fromStart = false)
55
    {
56
        $prevTime = $fromStart ? static::first()['time'] : static::last()['time'];
57
58
        static::mark($message);
59
60
        empty($prevTime) ? $prevTime = static::first()['time'] : false;
61
62
        return number_format(static::last()['time'] - $prevTime, 6) . 's';
63
    }
64
65
    public static function msg($message, $fromStart = false)
66
    {
67
        print
68
            $message .
69
            ($fromStart ? ' FROM START ' : ' in ') .
70
            static::elapsed($message, $fromStart) .
71
            ' ---- ' . static::last()['location'] .
72
            "\n<br />";
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78
    public static function last()
79
    {
80
        return end(static::$times);
0 ignored issues
show
Bug introduced by
Since $times is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $times to at least protected.
Loading history...
81
    }
82
83
    /**
84
     * @return mixed
85
     */
86
    public static function first()
87
    {
88
        return reset(static::$times);
0 ignored issues
show
Bug introduced by
Since $times is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $times to at least protected.
Loading history...
89
    }
90
91
    public static function getLog()
92
    {
93
        return static::$times;
0 ignored issues
show
Bug introduced by
Since $times is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $times to at least protected.
Loading history...
94
    }
95
96
}
97