1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlecRabbit\Tools\Reports\Formatters; |
6
|
|
|
|
7
|
|
|
use AlecRabbit\Accessories\Pretty; |
8
|
|
|
use AlecRabbit\Tools\Contracts\StringConstants; |
9
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkFunction; |
10
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkRelative; |
11
|
|
|
use AlecRabbit\Tools\Reports\Formatters\Contracts\BenchmarkFunctionFormatterInterface; |
12
|
|
|
use AlecRabbit\Tools\Reports\Formatters\Contracts\Formatter; |
13
|
|
|
use function AlecRabbit\typeOf; |
14
|
|
|
|
15
|
|
|
class BenchmarkFunctionFormatter implements BenchmarkFunctionFormatterInterface, Formatter, StringConstants |
16
|
|
|
{ |
17
|
|
|
/** @var BenchmarkFunction */ |
18
|
|
|
protected $function; |
19
|
|
|
|
20
|
|
|
/** @var bool */ |
21
|
|
|
protected $withResults = true; |
22
|
|
|
|
23
|
5 |
|
public function __construct(BenchmarkFunction $function) |
24
|
|
|
{ |
25
|
5 |
|
$this->function = $function; |
26
|
5 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
4 |
|
public function noResultsIf(bool $equalReturns = false): BenchmarkFunctionFormatter |
32
|
|
|
{ |
33
|
4 |
|
$this->withResults = !$equalReturns; |
34
|
4 |
|
return $this; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
4 |
|
public function process(): string |
41
|
|
|
{ |
42
|
|
|
return |
43
|
4 |
|
$this->formatBenchmarkRelative() . |
44
|
4 |
|
(empty($exception = $this->formatException()) ? |
45
|
4 |
|
PHP_EOL : |
46
|
4 |
|
static::EXCEPTIONS . PHP_EOL . $exception); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
4 |
|
protected function formatBenchmarkRelative(): string |
53
|
|
|
{ |
54
|
4 |
|
$function = $this->function; |
55
|
4 |
|
if ($br = $function->getBenchmarkRelative()) { |
56
|
4 |
|
$argumentsTypes = $this->extractArgumentsTypes($function->getArgs()); |
57
|
4 |
|
$executionReturn = $function->getReturn(); |
58
|
|
|
|
59
|
4 |
|
if ($this->withResults && $this->function->isShowReturns()) { |
60
|
|
|
return |
61
|
3 |
|
sprintf( |
62
|
3 |
|
'%s %s %s %s', |
63
|
3 |
|
$this->preformatFunction($br, $function, $argumentsTypes), |
64
|
3 |
|
PHP_EOL, |
65
|
3 |
|
static::returnToString($executionReturn), |
66
|
3 |
|
PHP_EOL |
67
|
|
|
); |
68
|
|
|
} |
69
|
1 |
|
return $this->preformatFunction($br, $function, $argumentsTypes); |
70
|
|
|
} |
71
|
3 |
|
return ''; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $arguments |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
4 |
|
protected function extractArgumentsTypes(array $arguments): array |
79
|
|
|
{ |
80
|
4 |
|
$types = []; |
81
|
4 |
|
if (!empty($arguments)) { |
82
|
1 |
|
foreach ($arguments as $argument) { |
83
|
1 |
|
$types[] = typeOf($argument); |
84
|
|
|
} |
85
|
|
|
} |
86
|
4 |
|
return $types; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param float $average |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
4 |
|
protected function average(float $average): string |
94
|
|
|
{ |
95
|
4 |
|
return str_pad( |
96
|
4 |
|
Pretty::time($average), |
97
|
4 |
|
8, |
98
|
4 |
|
' ', |
99
|
4 |
|
STR_PAD_LEFT |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param float $relative |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
4 |
|
protected function relativePercent(float $relative): string |
108
|
|
|
{ |
109
|
4 |
|
return str_pad( |
110
|
4 |
|
Pretty::percent($relative), |
111
|
4 |
|
7, |
112
|
4 |
|
' ', |
113
|
4 |
|
STR_PAD_LEFT |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
5 |
|
public static function returnToString($executionReturn): string |
121
|
|
|
{ |
122
|
5 |
|
$type = typeOf($executionReturn); |
123
|
|
|
try { |
124
|
5 |
|
$str = var_export($executionReturn, true); |
125
|
1 |
|
} catch (\Exception $e) { |
126
|
1 |
|
$str = '[' . typeOf($e) . '] ' . $e->getMessage(); |
127
|
|
|
} |
128
|
|
|
return |
129
|
5 |
|
$type === 'array' ? |
130
|
1 |
|
$str : |
131
|
5 |
|
sprintf( |
132
|
5 |
|
'%s(%s)', |
133
|
5 |
|
$type, |
134
|
5 |
|
$str |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param BenchmarkRelative $br |
140
|
|
|
* @param BenchmarkFunction $function |
141
|
|
|
* @param array $argumentsTypes |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
4 |
|
protected function preformatFunction( |
145
|
|
|
BenchmarkRelative $br, |
146
|
|
|
BenchmarkFunction $function, |
147
|
|
|
array $argumentsTypes |
148
|
|
|
): string { |
149
|
|
|
return |
150
|
4 |
|
sprintf( |
151
|
4 |
|
'%s. %s (%s) %s(%s) %s', |
152
|
4 |
|
(string)$br->getRank(), |
153
|
4 |
|
$this->average($br->getAverage()), |
154
|
4 |
|
$this->relativePercent($br->getRelative()), |
155
|
4 |
|
$function->humanReadableName(), |
156
|
4 |
|
implode(', ', $argumentsTypes), |
157
|
4 |
|
$function->comment() |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
4 |
|
protected function formatException(): string |
165
|
|
|
{ |
166
|
|
|
|
167
|
4 |
|
if ($e = $this->function->getException()) { |
168
|
3 |
|
$argumentsTypes = $this->extractArgumentsTypes($this->function->getArgs()); |
169
|
|
|
|
170
|
|
|
return |
171
|
3 |
|
sprintf( |
172
|
3 |
|
'%s(%s) %s [%s: %s] %s', |
173
|
3 |
|
$this->function->humanReadableName(), |
174
|
3 |
|
implode(', ', $argumentsTypes), |
175
|
3 |
|
$this->function->comment(), |
176
|
3 |
|
typeOf($e), |
177
|
3 |
|
$e->getMessage(), |
178
|
3 |
|
PHP_EOL |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
4 |
|
return ''; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|