Completed
Push — develop ( 7c4ff2...984a6e )
by Peter
08:26
created

DebuggerTimer::setName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 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
use WebinoDebug\Debugger\Bar\TimerPanel;
14
15
/**
16
 * Class DebuggerTimer
17
 */
18
class DebuggerTimer
19
{
20
    /**
21
     * @var TimerPanel
22
     */
23
    protected $panel;
24
25
    /**
26
     * @var string
27
     */
28
    protected $name;
29
30
    /**
31
     * @var float
32
     */
33
    protected $delta;
34
35
    /**
36
     * @param TimerPanel $panel Debugger timer imer panel
37
     * @param string|null $name Timer name
38
     */
39
    public function __construct(TimerPanel $panel, string $name = null)
40
    {
41
        $this->panel = $panel;
42
        $this->name  = $name ?: md5(uniqid(rand()));
43
    }
44
45
    /**
46
     * Return delta time
47
     *
48
     * @return float
49
     */
50
    public function getDelta() : float
51
    {
52
        return $this->delta;
53
    }
54
55
    /**
56
     * Set timer name
57
     *
58
     * Do not overrides.
59
     *
60
     * @param string|null $name
61
     * @return $this
62
     */
63
    public function setName(string $name = null)
64
    {
65
        $name and $this->name = $name;
66
        return $this;
67
    }
68
69
    /**
70
     * Start the timer
71
     *
72
     * @return $this
73
     */
74
    public function start()
75
    {
76
        $this->delta = Tracy::timer($this->name);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Tracy\Debugger::timer($this->name) can also be of type integer. However, the property $delta is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
77
        return $this;
78
    }
79
80
    /**
81
     * Stop the timer
82
     *
83
     * @param string $title Debugger bar timer value title
84
     * @return float Delta time
85
     */
86
    public function stop(string $title = null) : float
87
    {
88
        $this->delta = Tracy::timer($this->name);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Tracy\Debugger::timer($this->name) can also be of type integer. However, the property $delta is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
89
        $title and $this->panel->setTimer($title, $this->delta);
90
        return $this->delta;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function __toString()
97
    {
98
        return (string) $this->stop();
99
    }
100
}
101