Completed
Push — develop ( 7a0fb5...7cd2b2 )
by Alec
03:03
created

BenchmarkFunctionFormatter::noResultsIf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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