Test Setup Failed
Branch master (47e707)
by Alec
02:22
created

BenchmarkFunctionFormatter::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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