DebuggerTimer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 83
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A getDelta() 0 4 1
A setName() 0 5 2
A start() 0 5 1
A stop() 0 6 3
A __toString() 0 4 1
1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDebug/ for the canonical source repository
6
 * @copyright   Copyright (c) 2018 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDebug\Debugger;
11
12
use Tracy\Debugger as Tracy;
13
14
/**
15
 * Class DebuggerTimer
16
 */
17
class DebuggerTimer
18
{
19
    /**
20
     * @var TimerPanel
21
     */
22
    protected $panel;
23
24
    /**
25
     * @var string
26
     */
27
    protected $name;
28
29
    /**
30
     * @var float
31
     */
32
    protected $delta;
33
34
    /**
35
     * @param TimerPanel|null $panel Debugger timer imer panel
36
     * @param string|null $name Timer name
37
     */
38
    public function __construct(TimerPanel $panel = null, string $name = null)
39
    {
40
        $this->panel = $panel;
41
        $this->name  = $name ?: md5(uniqid(rand()));
42
    }
43
44
    /**
45
     * Return delta time
46
     *
47
     * @return float
48
     */
49
    public function getDelta() : float
50
    {
51
        return $this->delta;
52
    }
53
54
    /**
55
     * Set timer name
56
     *
57
     * Do not overrides.
58
     *
59
     * @param string|null $name
60
     * @return $this
61
     */
62
    public function setName(string $name = null)
63
    {
64
        $name and $this->name = $name;
65
        return $this;
66
    }
67
68
    /**
69
     * Start the timer
70
     *
71
     * @return $this
72
     */
73
    public function start()
74
    {
75
        $this->delta = Tracy::timer($this->name);
76
        return $this;
77
    }
78
79
    /**
80
     * Stop the timer
81
     *
82
     * @param string $title Debugger bar timer value title
83
     * @return float Delta time
84
     */
85
    public function stop(string $title = null) : float
86
    {
87
        $this->delta = Tracy::timer($this->name);
88
        $this->panel && $title and $this->panel->setTimer($title, $this->delta);
0 ignored issues
show
Bug Best Practice introduced by
The expression $title of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
89
        return $this->delta;
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function __toString()
96
    {
97
        return (string) $this->stop();
98
    }
99
}
100