Passed
Push — develop ( 84aa32...d180ee )
by Alec
02:42
created

BenchmarkReportFormatter::average()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
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\Tools\Internal\BenchmarkFunction;
8
use AlecRabbit\Tools\Reports\BenchmarkReport;
9
10
class BenchmarkReportFormatter extends ReportFormatter
11
{
12
    /** @var BenchmarkReport */
13
    protected $report;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 4
    public function getString(): string
19
    {
20 4
        $str = self::BENCHMARK . PHP_EOL;
21
        /** @var BenchmarkFunction $function */
22 4
        foreach ($this->report->getFunctions() as $name => $function) {
23 4
            $str .= (new BenchmarkFunctionFormatter($function))->getString();
24
        }
25
        return
26 4
            sprintf(
27 4
                '%s%s%s%s',
28 4
                $str,
29 4
                $this->countersStatistics(),
30 4
                $this->report->getMemoryUsageReport(),
31 4
                PHP_EOL
32
            );
33
    }
34
35
    /**
36
     * @return string
37
     */
38 4
    private function countersStatistics(): string
39
    {
40
        $added =
41 4
            $this->report->getProfiler()
42 4
                ->counter(static::ADDED)->getValue();
43
        $benchmarked =
44 4
            $this->report->getProfiler()
45 4
                ->counter(static::BENCHMARKED)->getValue();
46 4
        if ($added === $benchmarked) {
47 1
            return '';
48
        }
49
50
        return
51 3
            sprintf(
52 3
                '%s: %s %s: %s %s %s',
53 3
                static::ADDED,
54 3
                $added,
55 3
                static::BENCHMARKED,
56 3
                $benchmarked,
57 3
                $this->countedExceptions($added, $benchmarked),
58 3
                PHP_EOL
59
            );
60
    }
61
62
    /**
63
     * @param int $added
64
     * @param int $benchmarked
65
     * @return string
66
     */
67 3
    private function countedExceptions(int $added, int $benchmarked): string
68
    {
69 3
        if (0 !== $exceptions = $added - $benchmarked) {
70
            return
71 3
                sprintf(
72 3
                    '%s %s',
73 3
                    static::EXCEPTIONS,
74 3
                    $exceptions
75
                );
76
        }
77
        // @codeCoverageIgnoreStart
78
        return '';
79
        // @codeCoverageIgnoreEnd
80
    }
81
}
82