1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools\Internal; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Traits\GettableName; |
6
|
|
|
use function AlecRabbit\typeOf; |
7
|
|
|
|
8
|
|
|
class BenchmarkFunction |
9
|
|
|
{ |
10
|
|
|
use GettableName; |
11
|
|
|
|
12
|
|
|
public const CLOSURE_NAME = 'λ'; |
13
|
|
|
|
14
|
|
|
/** @var bool */ |
15
|
|
|
protected $withReturns = false; |
16
|
|
|
/** @var callable */ |
17
|
|
|
protected $callable; |
18
|
|
|
/** @var int */ |
19
|
|
|
protected $index; |
20
|
|
|
/** @var array */ |
21
|
|
|
protected $args; |
22
|
|
|
/** @var mixed */ |
23
|
|
|
protected $return; |
24
|
|
|
/** @var null|string */ |
25
|
|
|
protected $comment; |
26
|
|
|
/** @var null|string */ |
27
|
|
|
protected $assignedName; |
28
|
|
|
/** @var \Throwable|null */ |
29
|
|
|
protected $exception; |
30
|
|
|
|
31
|
|
|
/** @var null|BenchmarkResult */ |
32
|
|
|
protected $result; |
33
|
|
|
/** @var null|BenchmarkRelative */ |
34
|
|
|
protected $relative; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* BenchmarkFunction constructor. |
38
|
|
|
* @param callable $func |
39
|
|
|
* @param array $args |
40
|
|
|
* @param int $index |
41
|
|
|
* @param null|string $assignName |
42
|
|
|
* @param null|string $comment |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
$func, |
46
|
|
|
array $args, |
47
|
|
|
int $index, |
48
|
|
|
?string $assignName = null, |
49
|
|
|
?string $comment = null |
50
|
|
|
) { |
51
|
|
|
// parent::__construct(); |
52
|
|
|
if (!\is_callable($func, false, $name)) { |
53
|
|
|
throw new \InvalidArgumentException( |
54
|
|
|
sprintf( |
55
|
|
|
'\'%s\' is NOT callable. Function must be callable. Type of "%s" provided instead.', |
56
|
|
|
$name, |
57
|
|
|
typeOf($func) |
58
|
|
|
) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
$this->callable = $func; |
62
|
|
|
$this->name = $this->refineName($func, $name); |
63
|
|
|
$this->index = $index; |
64
|
|
|
$this->args = $args; |
65
|
|
|
$this->comment = $comment; |
66
|
|
|
$this->assignedName = $assignName; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param callable $func |
71
|
|
|
* @param string $name |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
protected function refineName($func, string $name): string |
75
|
|
|
{ |
76
|
|
|
if ($func instanceof \Closure) { |
77
|
|
|
$name = self::CLOSURE_NAME; |
78
|
|
|
} |
79
|
|
|
return $name; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function execute(): bool |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
|
|
$this->setReturn( |
89
|
|
|
($this->callable)(...$this->args) |
90
|
|
|
); |
91
|
|
|
} catch (\Throwable $e) { |
92
|
|
|
$this->setException($e); |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return callable |
100
|
|
|
*/ |
101
|
|
|
public function getCallable(): callable |
102
|
|
|
{ |
103
|
|
|
return $this->callable; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function getArgs(): array |
110
|
|
|
{ |
111
|
|
|
return $this->args; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getAssignedName(): string |
118
|
|
|
{ |
119
|
|
|
return $this->assignedName ?? $this->getIndexedName(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getIndexedName(): string |
126
|
|
|
{ |
127
|
|
|
return "⟨{$this->getIndex()}⟩ {$this->getName()}"; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return int |
132
|
|
|
*/ |
133
|
|
|
public function getIndex(): int |
134
|
|
|
{ |
135
|
|
|
return $this->index; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return null|\Throwable |
140
|
|
|
*/ |
141
|
|
|
public function getException(): ?\Throwable |
142
|
|
|
{ |
143
|
|
|
return $this->exception; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param \Throwable $exception |
148
|
|
|
* @return BenchmarkFunction |
149
|
|
|
*/ |
150
|
|
|
public function setException(\Throwable $exception): self |
151
|
|
|
{ |
152
|
|
|
$this->exception = $exception; |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return null|string |
158
|
|
|
*/ |
159
|
|
|
public function getComment(): ?string |
160
|
|
|
{ |
161
|
|
|
return $this->comment; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return BenchmarkResult|null |
166
|
|
|
*/ |
167
|
|
|
public function getResult(): ?BenchmarkResult |
168
|
|
|
{ |
169
|
|
|
return $this->result; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param BenchmarkResult $result |
174
|
|
|
*/ |
175
|
|
|
public function setResult(BenchmarkResult $result): void |
176
|
|
|
{ |
177
|
|
|
$this->result = $result; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return mixed |
182
|
|
|
*/ |
183
|
|
|
public function getReturn() |
184
|
|
|
{ |
185
|
|
|
return $this->return; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param mixed $return |
190
|
|
|
*/ |
191
|
|
|
public function setReturn($return): void |
192
|
|
|
{ |
193
|
|
|
$this->return = $return; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function setBenchmarkRelative(BenchmarkRelative $relative): void |
197
|
|
|
{ |
198
|
|
|
$this->relative = $relative; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return BenchmarkRelative|null |
203
|
|
|
*/ |
204
|
|
|
public function getRelative(): ?BenchmarkRelative |
205
|
|
|
{ |
206
|
|
|
return $this->relative; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|