Completed
Push — develop ( b733eb...69cb61 )
by Alec
06:31
created

BenchmarkReportFormatter::col()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: alec
4
 * Date: 10.12.18
5
 * Time: 14:22
6
 */
7
declare(strict_types=1);
8
9
namespace AlecRabbit\Tools\Reports\Formatters;
10
11
use AlecRabbit\Tools\Reports\BenchmarkReport;
12
use function AlecRabbit\brackets;
13
use function AlecRabbit\format_time_auto;
14
use function AlecRabbit\typeOf;
15
16
class BenchmarkReportFormatter extends Formatter
17
{
18
    /** @var BenchmarkReport */
19
    protected $report;
20
21 4
    public function setStyles(): void
22
    {
23 4
    }
24
25
    /**
26
     * {@inheritdoc}
27
     * @throws \Throwable
28
     */
29 3
    public function getString(): string
30
    {
31 3
        $profilerReport = (string)$this->report->getProfiler()->getReport();
32 3
        $r = 'Benchmark:' . PHP_EOL;
33 3
        foreach ($this->report->getRelatives() as $indexName => $result) {
34 2
            [$relative, $average] = $result;
35 2
            $function = $this->report->getFunctionObject($indexName);
36 2
            $arguments = $function->getArgs();
37 2
            $types = [];
38 2
            if (!empty($arguments)) {
39 1
                foreach ($arguments as $argument) {
40 1
                    $types[] = typeOf($argument);
41
                }
42
            }
43 2
            $r .= sprintf(
44 2
                '%s (+%s) %s(%s) %s %s',
45 2
                $this->theme->yellow(format_time_auto($average)),
46 2
                $this->col($relative),
47 2
                $function->getIndexedName(),
48 2
                implode(', ', $types),
49 2
                $this->theme->comment($function->getComment()),
50 2
                PHP_EOL
51
            );
52 2
            if ($this->report->isWithResults()) {
53 1
                $result = $function->getResult();
54 1
                $r .= $this->theme->dark('return: ' . str_replace('double', 'float', typeOf($result)) . ' "'
55 2
                        . var_export($function->getResult(), true) . '" ') . PHP_EOL;
56
            }
57
        }
58 3
        if (!empty($exceptionMessages = $this->report->getExceptionMessages())) {
59 1
            $r .= 'Exceptions:' . PHP_EOL;
60 1
            foreach ($exceptionMessages as $name => $exceptionMessage) {
61 1
                $r .= brackets($name) . ': ' . $this->theme->red($exceptionMessage) . PHP_EOL;
62
            }
63
        }
64
        return
65 3
            $r . PHP_EOL . $profilerReport;
66
    }
67
68
    /**
69
     * @param float $relative
70
     * @return string
71
     * @throws \Throwable
72
     */
73 2
    private function col($relative): string
74
    {
75 2
        if ($relative > 1) {
76 1
            return $this->theme->red($this->percent($relative));
77
        }
78 2
        if ($relative >= 0.03) {
79 1
            return $this->theme->yellow($this->percent($relative));
80
        }
81 2
        return $this->theme->green($this->percent($relative));
82
    }
83
84
    /**
85
     * @param float $relative
86
     * @return string
87
     */
88 2
    private function percent(float $relative): string
89
    {
90
        return
91 2
            number_format($relative * 100, 1) . '%';
92
    }
93
}
94