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