Completed
Push — develop ( 500e55...0a2c3f )
by Alec
02:54
created

BenchmarkFunctionFormatter::formatException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

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