Completed
Push — master ( c7a3c9...eed046 )
by Alec
16:25
created

BenchmarkFunctionSymfonyFormatter::prepName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools\Formatters;
4
5
use AlecRabbit\Accessories\Pretty;
6
use AlecRabbit\ConsoleColour\Exception\InvalidStyleException;
7
use AlecRabbit\ConsoleColour\Theme;
8
use AlecRabbit\Tools\Internal\BenchmarkFunction;
9
use AlecRabbit\Tools\Internal\BenchmarkRelative;
10
use function AlecRabbit\str_wrap;
11
use function AlecRabbit\typeOf;
12
13
class BenchmarkFunctionSymfonyFormatter extends BenchmarkFunctionFormatter
14
{
15
    /** @var Theme */
16
    protected $theme;
17
    /** @var float */
18
    protected $yellowThreshold;
19
    /** @var float */
20
    protected $redThreshold;
21
22
    /**
23
     * BenchmarkFunctionSymfonyFormatter constructor.
24
     * @throws InvalidStyleException
25
     */
26
    public function __construct()
27
    {
28
        $this->theme = new Theme(true);
29
        $this->yellowThreshold = 0.05;
30
        $this->redThreshold = 0.9;
31
    }
32
33
    /** {@inheritdoc} */
34
    public function returnToString($executionReturn): string
35
    {
36
        $type = typeOf($executionReturn);
37
        $str = static::getExporter()->export($executionReturn);
38
        return
39
            $this->theme->dark(
40
                'array' === $type ?
41
                    $str :
42
                    sprintf(
43
                        '%s(%s)',
44
                        $type,
45
                        $str
46
                    )
47
            );
48
    }
49
50
    /**
51
     * @param BenchmarkRelative $br
52
     * @param BenchmarkFunction $function
53
     * @param array $argumentsTypes
54
     *
55
     * @return string
56
     */
57
    protected function preformatFunction(
58
        BenchmarkRelative $br,
59
        BenchmarkFunction $function,
60
        array $argumentsTypes
61
    ): string {
62
        $rank = $br->getRank();
63
        return
64
            sprintf(
65
                '%s. %s(%s) %s %s',
66
                (string)$rank,
67
                $this->prepAverage($rank, $br->getAverage()),
68
                $this->relativePercent($br->getRelative()),
69
                $this->prepName($function, $argumentsTypes),
70
                $this->theme->yellow($function->comment())
71
            );
72
    }
73
74
    /**
75
     * @param int $rank
76
     * @param float $average
77
     * @return string
78
     */
79
    protected function prepAverage(int $rank, float $average): string
80
    {
81
        return
82
            $rank === 1 ?
83
                $this->averageFirst($average) :
84
                $this->average($average);
85
    }
86
87
    protected function averageFirst(float $average): string
88
    {
89
//        $this->theme->underline($avg);
90
        $str = Pretty::time($average);
91
        $len = strlen($str);
92
        $proto = str_repeat('X', $len);
93
        $res = str_pad(
94
            $proto,
95
            8,
96
            ' ',
97
            STR_PAD_LEFT
98
        );
99
        return
100
            str_replace($proto, $this->theme->underlineBold($str), $res);
101
    }
102
103
    /**
104
     * @param float $relative
105
     * @param string $prefix
106
     * @return string
107
     */
108
    protected function relativePercent(float $relative, string $prefix = '+'): string
109
    {
110
        $color = 'green';
111
112
        if ($relative >= $this->yellowThreshold) {
113
            $color = 'yellow';
114
        }
115
        if ($relative > $this->redThreshold) {
116
            $color = 'red';
117
        }
118
        return
119
            $this->theme->$color(
120
                parent::relativePercent($relative, $prefix)
121
            );
122
    }
123
124
    /**
125
     * @param BenchmarkFunction $function
126
     * @param array $argumentsTypes
127
     * @return mixed
128
     */
129
    protected function prepName(BenchmarkFunction $function, array $argumentsTypes)
130
    {
131
        return
132
            sprintf(
133
                '%s%s%s%s',
134
                $this->theme->italic($function->humanReadableName()),
135
                $this->theme->italic('('),
136
                $this->theme->darkItalic(implode(', ', $argumentsTypes)),
137
                $this->theme->italic(')')
138
            );
139
    }
140
141
    /**
142
     * @param BenchmarkFunction $function
143
     * @return string
144
     */
145
    protected function formatException(BenchmarkFunction $function): string
146
    {
147
148
        if ($e = $function->getException()) {
149
            $argumentsTypes = $this->extractArgumentsTypes($function->getArgs());
150
151
            return
152
                sprintf(
153
                    '%s %s%s[%s%s%s]%s',
154
                    $this->prepName($function, $argumentsTypes),
155
                    $this->theme->yellow($function->comment()),
156
                    ' ',
157
                    $this->theme->error(str_wrap(typeOf($e), ' ')),
158
                    ' : ',
159
                    $this->theme->dark($e->getMessage()),
160
                    PHP_EOL
161
                );
162
        }
163
164
        return '';
165
    }
166
}
167