Passed
Push — master ( e26094...28949d )
by Paul
02:50
created

Profiler::getNameString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\BlackBar\Modules;
4
5
use GeminiLabs\BlackBar\Application;
6
7
class Profiler implements Module
8
{
9
    /**
10
     * @var Application
11
     */
12
    protected $app;
13
    /**
14
     * @var float
15
     */
16
    protected $noise;
17
    /**
18
     * @var float
19
     */
20
    protected $start;
21
    /**
22
     * @var float
23
     */
24
    protected $stop;
25
    /**
26
     * @var array
27
     */
28
    protected $timers;
29
30
    public function __construct(Application $app)
31
    {
32
        $this->app = $app;
33
        $this->noise = (float) 0; // This is the time that WordPress takes to execute the all hook.
34
        $this->start = (float) 0;
35
        $this->stop = (float) 0;
36
        $this->timers = [];
37
    }
38
39
    public function entries(): array
40
    {
41
        $entries = [];
42
        foreach ($this->timers as $timer) {
43
            $entries[] = [
44
                'memory' => $this->getMemoryString($timer),
45
                'name' => $this->getNameString($timer),
46
                'time' => $this->getTimeString($timer),
47
            ];
48
        }
49
        return $entries;
50
    }
51
52
    public function getMeasure(): array
53
    {
54
        return $this->timers;
55
    }
56
57
    public function getMemoryString(array $timer): string
58
    {
59
        return (string) round($this->normalize($timer)['memory'] / 1000);
60
    }
61
62
    public function getNameString(array $timer): string
63
    {
64
        return $this->normalize($timer)['name'];
65
    }
66
67
    public function getTimeString(array $timer): string
68
    {
69
        $timer = $this->normalize($timer);
70
        $index = array_search($timer['name'], array_column($this->timers, 'name'));
71
        $start = $this->start + ($index * $this->noise);
72
        return number_format(round(($timer['time'] - $start) * 1000, 4), 4);
73
    }
74
75
    public function getTotalTime(): float
76
    {
77
        $totalNoise = (count($this->timers) - 1) * $this->noise;
78
        return $this->stop - $this->start - $totalNoise;
79
    }
80
81
    public function hasEntries(): bool
82
    {
83
        return !empty($this->timers);
84
    }
85
86
    public function id(): string
87
    {
88
        return 'glbb-profiler';
89
    }
90
91
    public function isVisible(): bool
92
    {
93
        return true;
94
    }
95
96
    public function label(): string
97
    {
98
        $label = __('Profiler', 'blackbar');
99
        $time = number_format($this->getTotalTime() * 1000, 0);
100
        if ($time > 0) {
101
            $label .= sprintf(' (%s %s)', $time, __('ms', 'blackbar'));
102
        }
103
        return $label;
104
    }
105
106
    public function render(): void
107
    {
108
        $this->app->render('panels/profiler', ['profiler' => $this]);
109
    }
110
111
    public function trace(string $name): void
112
    {
113
        $microtime = microtime(true); // float
114
        if (!$this->start) {
115
            $this->start = $microtime;
1 ignored issue
show
Documentation Bug introduced by
It seems like $microtime can also be of type string. However, the property $start 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...
116
        }
117
        if ('blackbar/profiler/noise' === $name) {
118
            $this->noise = $microtime - $this->start;
119
            return;
120
        }
121
        $this->timers[] = [
122
            'memory' => memory_get_peak_usage(),
123
            'name' => $name,
124
            'time' => $microtime,
125
        ];
126
        $this->stop = $microtime;
1 ignored issue
show
Documentation Bug introduced by
It seems like $microtime can also be of type string. However, the property $stop 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...
127
    }
128
129
    protected function normalize(array $timer): array
130
    {
131
        return wp_parse_args($timer, [
132
            'memory' => 0,
133
            'name' => '',
134
            'time' => (float) 0,
135
        ]);
136
    }
137
}
138