Completed
Push — develop ( fe1195...b733eb )
by Alec
05:48
created

BenchmarkReportFormatter::percent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
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 3
    public function setStyles(): void
22
    {
23 3
    }
24
25 2
    public function getString($colored = true): string
26
    {
27 2
        $profilerReport = (string)$this->report->getProfiler()->getReport();
28 2
        $r = 'Benchmark:' . PHP_EOL;
29 2
        foreach ($this->report->getRelatives() as $indexName => $result) {
30 1
            [$relative, $average] = $result;
31 1
            $function = $this->report->getFunctionObject($indexName);
32 1
            $arguments = $function->getArgs();
33 1
            $types = [];
34 1
            if (!empty($arguments)) {
35 1
                foreach ($arguments as $argument) {
36 1
                    $types[] = typeOf($argument);
37
                }
38
            }
39 1
            $r .= sprintf(
40 1
                '%s (+%s) %s(%s) %s %s',
41 1
                $this->theme->yellow(format_time_auto($average)),
42 1
                $this->col($relative),
43 1
                $function->getIndexedName(),
44 1
                implode(', ', $types),
45 1
                $this->theme->comment($function->getComment()),
46 1
                PHP_EOL
47
            );
48 1
            if ($this->report->isWithResults()) {
49
                $result = $function->getResult();
50
                $r .= $this->theme->dark('return: '. str_replace('double', 'float', typeOf($result)) . ' "'
51 1
                        . var_export($function->getResult(), true) . '" ') . PHP_EOL;
52
            }
53
        }
54 2
        if (!empty($exceptionMessages = $this->report->getExceptionMessages())) {
55
            $r .= 'Exceptions:' . PHP_EOL;
56
            foreach ($exceptionMessages as $name => $exceptionMessage) {
57
                $r .= brackets($name) . ': ' . $this->theme->red($exceptionMessage) . PHP_EOL;
58
            }
59
        }
60
        return
61 2
            $r . PHP_EOL . $profilerReport;
62
    }
63
64
    /**
65
     * @param float $relative
66
     * @return string
67
     */
68 1
    private function col($relative): string
69
    {
70 1
        if ($relative > 1) {
71
            return $this->theme->red($this->percent($relative));
72
        }
73 1
        if ($relative >= 0.03) {
74 1
            return $this->theme->yellow($this->percent($relative));
75
        }
76 1
        return $this->theme->green($this->percent($relative));
77
    }
78
79
    /**
80
     * @param float $relative
81
     * @return string
82
     */
83 1
    private function percent(float $relative): string
84
    {
85
        return
86 1
            number_format($relative * 100, 1) . '%';
87
    }
88
}
89