Completed
Push — develop ( 6ca366...ef8817 )
by Alec
02:53
created

preformatFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 3
dl 0
loc 14
ccs 0
cts 9
cp 0
crap 2
rs 9.9666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools\Reports\Formatters;
4
5
use AlecRabbit\Exception\InvalidStyleException;
6
use AlecRabbit\Tools\Internal\BenchmarkFunction;
7
use AlecRabbit\Tools\Internal\BenchmarkRelative;
8
use AlecRabbit\Tools\Reports\Formatters\Colour\Theme;
9
10
class BenchmarkFunctionSymfonyFormatter extends BenchmarkFunctionFormatter
11
{
12
    /** @var Theme */
13
    protected $theme;
14
15
    /**
16
     * BenchmarkFunctionSymfonyFormatter constructor.
17
     * @throws InvalidStyleException
18
     */
19
    public function __construct()
20
    {
21
        $this->theme = new Theme(true);
22
    }
23
24
    /**
25
     * @param BenchmarkRelative $br
26
     * @param BenchmarkFunction $function
27
     * @param array $argumentsTypes
28
     *
29
     * @return string
30
     */
31
    protected function preformatFunction(
32
        BenchmarkRelative $br,
33
        BenchmarkFunction $function,
34
        array $argumentsTypes
35
    ): string {
36
        return
37
            sprintf(
38
                '%s. %s (%s) %s(%s) %s',
39
                (string)$br->getRank(),
40
                $this->average($br->getAverage()),
41
                $this->relativePercent($br->getRelative()),
42
                $function->humanReadableName(),
43
                $this->theme->dark(implode(', ', $argumentsTypes)),
44
                $this->theme->yellow($function->comment())
45
            );
46
    }
47
48
    /**
49
     * @param float $relative
50
     * @return string
51
     */
52
    protected function relativePercent(float $relative): string
53
    {
54
        $color = 'green';
55
        if ($relative > 1) {
56
            $color = 'red';
57
        }
58
        if ($relative >= 0.03) {
59
            $color = 'yellow';
60
        }
61
        return
62
            $this->theme->$color(
63
                parent::relativePercent($relative)
64
            );
65
    }
66
67
}
68