Test Failed
Push — develop ( f741b1...4c7784 )
by Alec
05:40
created

BenchmarkReportFormatter::setStyles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
ccs 1
cts 1
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\Pretty;
8
use AlecRabbit\Tools\Internal\BenchmarkRelative;
9
use AlecRabbit\Tools\Reports\BenchmarkReport;
10
use function AlecRabbit\brackets;
11
use function AlecRabbit\typeOf;
12
13
class BenchmarkReportFormatter extends Formatter
14
{
15
    /** @var BenchmarkReport */
16
    protected $report;
17
18 4
    /**
19
     * {@inheritdoc}
20 4
     */
21
    public function getString(): string
22
    {
23
        $rank = 0;
24
        $profilerReport = (string)$this->report->getProfiler()->getReport();
25
        $r = 'Benchmark:' . PHP_EOL;
26 3
        /** @var BenchmarkRelative $result */
27
        foreach ($this->report->getRelatives() as $indexName => $result) {
28 3
            $relative = $result->getRelative();
29 3
            $average = $result->getAverage();
30 3
            $function = $this->report->getFunctionObject($indexName);
31
            $function->setRank(++$rank);
32 3
            $arguments = $function->getArgs();
33 2
            $types = [];
34 2
            if (!empty($arguments)) {
35 2
                foreach ($arguments as $argument) {
36 2
                    $types[] = typeOf($argument);
37 2
                }
38 2
            }
39 2
            $r .= sprintf(
40 1
                '%s. %s (%s) %s(%s) %s %s',
41 1
                (string)$rank,
42
                $this->average($average),
43
                $this->relativePercent($relative),
44 2
                $function->getHumanReadableName(),
45 2
                implode(', ', $types),
46 2
                $function->getComment(),
47 2
                PHP_EOL
48 2
            );
49 2
            if ($this->report->isWithResults()) {
50 2
                $result = $function->getResult();
51 2
                var_dump($result);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($result) looks like debug code. Are you sure you do not want to remove it?
Loading history...
52 2
                dump($result);
53
                $r .= self::RESULT . ': ' . $this->typeOf($result) . ' "'
54
                    . var_export($function->getResult(), true) . '" ' . PHP_EOL;
55 2
            }
56 2
        }
57 2
        if (!empty($exceptionMessages = $this->report->getExceptionMessages())) {
58 2
            $r .= 'Exceptions:' . PHP_EOL;
59 2
            foreach ($exceptionMessages as $name => $exceptionMessage) {
60 2
                $r .= brackets($name) . ': ' . $exceptionMessage . PHP_EOL;
61
            }
62 2
        }
63
        return
64 2
            $r . PHP_EOL . $profilerReport;
65 2
    }
66 2
67 2
    /**
68
     * @param float $average
69 2
     * @return string
70 1
     */
71 1
    protected function average(float $average): string
72 2
    {
73
        return str_pad(
74
            Pretty::time($average),
75 3
            8,
76 1
            ' ',
77 1
            STR_PAD_LEFT
78 1
        );
79
    }
80
81
    /**
82 3
     * @param float $relative
83
     * @return string
84
     */
85
    protected function relativePercent(float $relative): string
86
    {
87
        return str_pad(
88
            Pretty::percent($relative),
89
            7,
90
            ' ',
91 2
            STR_PAD_LEFT
92
        );
93 2
    }
94 1
95
    /**
96 2
     * @param mixed $result
97 1
     * @return string
98
     */
99
    protected function typeOf($result): string
100 2
    {
101
        return str_replace('double', 'float', typeOf($result));
102
    }
103
}
104