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\Reports\Formatters\Contracts\Formatter; |
11
|
|
|
use function AlecRabbit\typeOf; |
12
|
|
|
|
13
|
|
|
class BenchmarkFunctionFormatter implements Formatter, StringConstants |
14
|
|
|
{ |
15
|
|
|
/** @var BenchmarkFunction */ |
16
|
|
|
protected $function; |
17
|
|
|
|
18
|
4 |
|
public function __construct(BenchmarkFunction $function) |
19
|
|
|
{ |
20
|
4 |
|
$this->function = $function; |
21
|
4 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
4 |
|
public function getString(): string |
27
|
|
|
{ |
28
|
|
|
return |
29
|
4 |
|
$this->formatBenchmarkRelative() . |
30
|
4 |
|
(empty($exception = $this->formatException()) ? |
31
|
4 |
|
PHP_EOL : |
32
|
4 |
|
static::EXCEPTIONS . PHP_EOL . $exception); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
4 |
|
protected function formatBenchmarkRelative(): string |
39
|
|
|
{ |
40
|
4 |
|
$function = $this->function; |
41
|
4 |
|
if ($br = $function->getBenchmarkRelative()) { |
42
|
4 |
|
$argumentsTypes = $this->extractArgumentsTypes($function->getArgs()); |
43
|
4 |
|
$executionReturn = $function->getReturn(); |
44
|
|
|
|
45
|
|
|
return |
46
|
4 |
|
sprintf( |
47
|
4 |
|
'%s. %s (%s) %s(%s) %s %s %s(%s) %s', |
48
|
4 |
|
(string)$br->getRank(), |
49
|
4 |
|
$this->average($br->getAverage()), |
50
|
4 |
|
$this->relativePercent($br->getRelative()), |
51
|
4 |
|
$function->humanReadableName(), |
52
|
4 |
|
implode(', ', $argumentsTypes), |
53
|
4 |
|
$function->comment(), |
54
|
4 |
|
PHP_EOL, |
55
|
4 |
|
typeOf($executionReturn), |
56
|
4 |
|
var_export($executionReturn, true), |
57
|
4 |
|
PHP_EOL |
58
|
|
|
); |
59
|
|
|
} |
60
|
3 |
|
return ''; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $arguments |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
4 |
|
protected function extractArgumentsTypes(array $arguments): array |
68
|
|
|
{ |
69
|
4 |
|
$types = []; |
70
|
4 |
|
if (!empty($arguments)) { |
71
|
1 |
|
foreach ($arguments as $argument) { |
72
|
1 |
|
$types[] = typeOf($argument); |
73
|
|
|
} |
74
|
|
|
} |
75
|
4 |
|
return $types; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param float $average |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
4 |
|
protected function average(float $average): string |
83
|
|
|
{ |
84
|
4 |
|
return str_pad( |
85
|
4 |
|
Pretty::time($average), |
86
|
4 |
|
8, |
87
|
4 |
|
' ', |
88
|
4 |
|
STR_PAD_LEFT |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param float $relative |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
4 |
|
protected function relativePercent(float $relative): string |
97
|
|
|
{ |
98
|
4 |
|
return str_pad( |
99
|
4 |
|
Pretty::percent($relative), |
100
|
4 |
|
7, |
101
|
4 |
|
' ', |
102
|
4 |
|
STR_PAD_LEFT |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
4 |
|
protected function formatException(): string |
110
|
|
|
{ |
111
|
|
|
|
112
|
4 |
|
if ($e = $this->function->getException()) { |
113
|
3 |
|
$argumentsTypes = $this->extractArgumentsTypes($this->function->getArgs()); |
114
|
|
|
|
115
|
|
|
return |
116
|
3 |
|
sprintf( |
117
|
3 |
|
'%s(%s) %s [%s] %s', |
118
|
3 |
|
$this->function->humanReadableName(), |
119
|
3 |
|
implode(', ', $argumentsTypes), |
120
|
3 |
|
$this->function->comment(), |
121
|
3 |
|
$e->getMessage(), |
122
|
3 |
|
PHP_EOL |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
4 |
|
return ''; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|