Test Failed
Push — master ( 772f18...8c594b )
by Alec
03:05
created

BenchmarkReportFormatter::getString()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 37
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 28
nc 10
nop 1
dl 0
loc 37
rs 8.5386
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
    public function setStyles(): void
22
    {
23
    }
24
25
    public function getString($colored = true): string
26
    {
27
        $profilerReport = (string)$this->report->getProfiler()->getReport();
28
        $r = 'Benchmark:' . PHP_EOL;
29
        foreach ($this->report->getRelatives() as $indexName => $result) {
30
            [$relative, $average] = $result;
31
            $function = $this->report->getFunctionObject($indexName);
32
            $arguments = $function->getArgs();
33
            $types = [];
34
            if (!empty($arguments)) {
35
                foreach ($arguments as $argument) {
36
                    $types[] = typeOf($argument);
37
                }
38
            }
39
            $r .= sprintf(
40
                '%s (+%s) %s(%s) %s %s',
41
                $this->theme->yellow(format_time_auto($average)),
0 ignored issues
show
Bug introduced by
The method yellow() does not exist on AlecRabbit\ConsoleColour. It seems like you code against a sub-type of AlecRabbit\ConsoleColour such as AlecRabbit\Tools\Internal\Theme. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
                $this->theme->/** @scrutinizer ignore-call */ 
42
                              yellow(format_time_auto($average)),
Loading history...
42
                $this->col($relative),
43
                $function->getIndexedName(),
44
                implode(', ', $types),
45
                $this->theme->comment($function->getComment()),
0 ignored issues
show
Bug introduced by
The method comment() does not exist on AlecRabbit\ConsoleColour. It seems like you code against a sub-type of AlecRabbit\ConsoleColour such as AlecRabbit\Tools\Internal\Theme. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
                $this->theme->/** @scrutinizer ignore-call */ 
46
                              comment($function->getComment()),
Loading history...
46
                PHP_EOL
47
            );
48
            if ($this->report->isWithResults()) {
49
                $result = $function->getResult();
50
                $r .= $this->theme->dark('return: '. str_replace('double', 'float', typeOf($result)) . ' "'
0 ignored issues
show
Bug introduced by
The method dark() does not exist on AlecRabbit\ConsoleColour. It seems like you code against a sub-type of AlecRabbit\ConsoleColour such as AlecRabbit\Tools\Internal\Theme. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
                $r .= $this->theme->/** @scrutinizer ignore-call */ dark('return: '. str_replace('double', 'float', typeOf($result)) . ' "'
Loading history...
51
                        . var_export($function->getResult(), true) . '" ') . PHP_EOL;
52
            }
53
        }
54
        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;
0 ignored issues
show
Bug introduced by
The method red() does not exist on AlecRabbit\ConsoleColour. It seems like you code against a sub-type of AlecRabbit\ConsoleColour such as AlecRabbit\Tools\Internal\Theme. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
                $r .= brackets($name) . ': ' . $this->theme->/** @scrutinizer ignore-call */ red($exceptionMessage) . PHP_EOL;
Loading history...
58
            }
59
        }
60
        return
61
            $r . PHP_EOL . $profilerReport;
62
    }
63
64
    /**
65
     * @param $relative
66
     * @return string
67
     */
68
    private function col($relative): string
69
    {
70
        if ($relative > 1) {
71
            return $this->theme->red($this->percent($relative));
72
        }
73
        if ($relative >= 0.03) {
74
            return $this->theme->yellow($this->percent($relative));
75
        }
76
        return $this->theme->green($this->percent($relative));
0 ignored issues
show
Bug introduced by
The method green() does not exist on AlecRabbit\ConsoleColour. It seems like you code against a sub-type of AlecRabbit\ConsoleColour such as AlecRabbit\Tools\Internal\Theme. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
        return $this->theme->/** @scrutinizer ignore-call */ green($this->percent($relative));
Loading history...
77
    }
78
79
    /**
80
     * @param float $relative
81
     * @return string
82
     */
83
    private function percent(float $relative): string
84
    {
85
        return
86
            number_format($relative * 100, 1) . '%';
87
    }
88
89
90
}