Passed
Push — master ( 428dea...f32a2d )
by Alec
02:52
created

BenchmarkReportFormatter::countersStatistics()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 0
dl 0
loc 17
ccs 13
cts 13
cp 1
crap 2
rs 9.8333
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 4
    private function countersStatistics(): string
36
    {
37 4
        $added = $this->report->getProfiler()->counter(static::ADDED)->getValue();
38 4
        $benchmarked = $this->report->getProfiler()->counter(static::BENCHMARKED)->getValue();
39 4
        if ($added === $benchmarked) {
40 1
            return '';
41
        }
42
43
        return
44 3
            sprintf(
45 3
                '%s: %s %s: %s %s %s',
46 3
                static::ADDED,
47 3
                $added,
48 3
                static::BENCHMARKED,
49 3
                $benchmarked,
50 3
                $this->countedExceptions($added, $benchmarked),
51 3
                PHP_EOL
52
            );
53
    }
54
55 3
    private function countedExceptions($added, $benchmarked): string
56
    {
57 3
        if (0 !== $exceptions = $added - $benchmarked) {
58
            return
59 3
                sprintf(
60 3
                    '%s %s',
61 3
                    static::EXCEPTIONS,
62 3
                    $exceptions
63
                );
64
        }
65
        return '';
66
    }
67
}
68