1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools\Reports\Formatters; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Exception\InvalidStyleException; |
6
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkFunction; |
7
|
|
|
use AlecRabbit\Tools\Internal\BenchmarkRelative; |
8
|
|
|
use AlecRabbit\Tools\Reports\Formatters\Colour\Theme; |
9
|
|
|
use function AlecRabbit\str_wrap; |
10
|
|
|
use function AlecRabbit\typeOf; |
11
|
|
|
|
12
|
|
|
class BenchmarkFunctionSymfonyFormatter extends BenchmarkFunctionFormatter |
13
|
|
|
{ |
14
|
|
|
/** @var Theme */ |
15
|
|
|
protected $theme; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* BenchmarkFunctionSymfonyFormatter constructor. |
19
|
|
|
* @throws InvalidStyleException |
20
|
|
|
*/ |
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->theme = new Theme(true); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** {@inheritdoc} */ |
27
|
|
|
public function returnToString($executionReturn): string |
28
|
|
|
{ |
29
|
|
|
$type = typeOf($executionReturn); |
30
|
|
|
$str = static::getExporter()->export($executionReturn); |
31
|
|
|
return |
32
|
|
|
$this->theme->dark( |
33
|
|
|
'array' === $type ? |
34
|
|
|
$str : |
35
|
|
|
sprintf( |
36
|
|
|
'%s(%s)', |
37
|
|
|
$type, |
38
|
|
|
$str |
39
|
|
|
) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param BenchmarkRelative $br |
45
|
|
|
* @param BenchmarkFunction $function |
46
|
|
|
* @param array $argumentsTypes |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
protected function preformatFunction( |
51
|
|
|
BenchmarkRelative $br, |
52
|
|
|
BenchmarkFunction $function, |
53
|
|
|
array $argumentsTypes |
54
|
|
|
): string { |
55
|
|
|
$rank = $br->getRank(); |
56
|
|
|
$average = $this->average($br->getAverage()); |
57
|
|
|
return |
58
|
|
|
sprintf( |
59
|
|
|
'%s. %s (%s) %s %s', |
60
|
|
|
(string)$rank, |
61
|
|
|
$rank !== 1 ? $average : $this->theme->underline($average), |
62
|
|
|
$this->relativePercent($br->getRelative()), |
63
|
|
|
$this->prepName($function, $argumentsTypes), |
64
|
|
|
$this->theme->yellow($function->comment()) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param float $relative |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
protected function relativePercent(float $relative): string |
73
|
|
|
{ |
74
|
|
|
$color = 'green'; |
75
|
|
|
if ($relative > 1) { |
76
|
|
|
$color = 'red'; |
77
|
|
|
} |
78
|
|
|
if ($relative >= 0.03) { |
79
|
|
|
$color = 'yellow'; |
80
|
|
|
} |
81
|
|
|
return |
82
|
|
|
$this->theme->$color( |
83
|
|
|
parent::relativePercent($relative) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param BenchmarkFunction $function |
89
|
|
|
* @param array $argumentsTypes |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
protected function prepName(BenchmarkFunction $function, array $argumentsTypes) |
93
|
|
|
{ |
94
|
|
|
return |
95
|
|
|
sprintf( |
96
|
|
|
'%s%s%s%s', |
97
|
|
|
$this->theme->italic($function->humanReadableName()), |
98
|
|
|
$this->theme->italic('('), |
99
|
|
|
$this->theme->darkItalic(implode(', ', $argumentsTypes)), |
100
|
|
|
$this->theme->italic(')') |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param BenchmarkFunction $function |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function formatException(BenchmarkFunction $function): string |
109
|
|
|
{ |
110
|
|
|
|
111
|
|
|
if ($e = $function->getException()) { |
112
|
|
|
$argumentsTypes = $this->extractArgumentsTypes($function->getArgs()); |
113
|
|
|
|
114
|
|
|
return |
115
|
|
|
sprintf( |
116
|
|
|
'%s %s%s[%s%s%s]%s', |
117
|
|
|
$this->prepName($function, $argumentsTypes), |
118
|
|
|
$this->theme->yellow($function->comment()), |
119
|
|
|
' ', |
120
|
|
|
$this->theme->error(str_wrap(typeOf($e), ' ')), |
121
|
|
|
' : ', |
122
|
|
|
$this->theme->dark($e->getMessage()), |
123
|
|
|
PHP_EOL |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return ''; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|