1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace App\BenchMark\Reporter; |
19
|
|
|
|
20
|
|
|
class Task |
21
|
|
|
{ |
22
|
|
|
protected string $name = ''; |
23
|
|
|
|
24
|
|
|
protected int $repeat = 1; |
25
|
|
|
|
26
|
|
|
/** @var float|int Duration time (in seconds). */ |
27
|
|
|
protected $duration = 0; |
28
|
|
|
|
29
|
|
|
/** @var float|int The average taken time per task (in seconds). */ |
30
|
|
|
protected $average = 0; |
31
|
|
|
|
32
|
|
|
/** @var float|int The time rate. */ |
33
|
|
|
protected $rate = 0; |
34
|
|
|
|
35
|
|
|
/** The memory usage (in bytes) */ |
36
|
|
|
protected int $memory = 0; |
37
|
|
|
|
38
|
|
|
/** @var float Start time (in seconds). */ |
39
|
|
|
protected $startTime = 0; |
40
|
|
|
|
41
|
|
|
/** @var int Start up memory usage (in bytes) */ |
42
|
|
|
protected $startMemory = 0; |
43
|
|
|
|
44
|
|
|
/** @var bool Indicates if the task failed */ |
45
|
|
|
protected $failed = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Starts the timer |
49
|
|
|
*/ |
50
|
|
|
public function start(): void |
51
|
|
|
{ |
52
|
|
|
$this->startTime = \microtime(true); |
|
|
|
|
53
|
|
|
$this->startMemory = \memory_get_usage(true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Stops the timer. |
58
|
|
|
*/ |
59
|
|
|
public function end(): void |
60
|
|
|
{ |
61
|
|
|
$this->duration = \microtime(true) - $this->startTime; |
62
|
|
|
$this->average = $this->duration / $this->repeat; |
63
|
|
|
$this->rate = $this->repeat / $this->duration; |
64
|
|
|
$this->memory = \memory_get_usage(true) - $this->startMemory; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets/sets the name. |
69
|
|
|
* |
70
|
|
|
* @param null|string the task name to set or none the get the current one |
|
|
|
|
71
|
|
|
* |
72
|
|
|
* @return self|string name or `$this` on set |
73
|
|
|
*/ |
74
|
|
|
public function name(?string $name = null) |
75
|
|
|
{ |
76
|
|
|
if (null !== $name) { |
77
|
|
|
$this->name = $name; |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->name; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets/sets the repeat number. |
87
|
|
|
* |
88
|
|
|
* @param int $repeat the repeat value to set or none the get the current one |
89
|
|
|
* |
90
|
|
|
* @return int|self the repeat value or `$this` on set |
91
|
|
|
*/ |
92
|
|
|
public function repeat(?int $repeat = null) |
93
|
|
|
{ |
94
|
|
|
if (null !== $repeat) { |
95
|
|
|
$this->clear(); |
96
|
|
|
$this->repeat = $repeat; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->repeat; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns the average taken time. |
106
|
|
|
* |
107
|
|
|
* @return float|int the time taken per task, in average (in seconds) |
108
|
|
|
*/ |
109
|
|
|
public function average() |
110
|
|
|
{ |
111
|
|
|
return $this->average; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the time rate. |
116
|
|
|
* |
117
|
|
|
* @return float|int the time rate |
118
|
|
|
*/ |
119
|
|
|
public function rate() |
120
|
|
|
{ |
121
|
|
|
return $this->rate; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns the whole taken time. |
126
|
|
|
* |
127
|
|
|
* @return float|int the time taken (in seconds) |
128
|
|
|
*/ |
129
|
|
|
public function duration() |
130
|
|
|
{ |
131
|
|
|
return $this->duration; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the memory usage. |
136
|
|
|
* |
137
|
|
|
* @return int the memory usage (in bytes) |
138
|
|
|
*/ |
139
|
|
|
public function memory(): int |
140
|
|
|
{ |
141
|
|
|
return $this->memory; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Indicates whether the task failed or not. |
146
|
|
|
* |
147
|
|
|
* @var null|bool the failing value |
148
|
|
|
* |
149
|
|
|
* @return bool|self |
150
|
|
|
*/ |
151
|
|
|
public function failed(?bool $fail = null) |
152
|
|
|
{ |
153
|
|
|
if (\is_bool($fail)) { |
154
|
|
|
$this->failed = $fail; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->failed; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Clears the stats. |
164
|
|
|
*/ |
165
|
|
|
public function clear(): void |
166
|
|
|
{ |
167
|
|
|
$this->repeat = 1; |
168
|
|
|
$this->startTime = 0; |
169
|
|
|
$this->duration = 0; |
170
|
|
|
$this->average = 0; |
171
|
|
|
$this->rate = 0; |
172
|
|
|
$this->startMem = 0; |
|
|
|
|
173
|
|
|
$this->memory = 0; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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 theid
property of an instance of theAccount
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.