Passed
Push — master ( 1472b6...a5e0bb )
by Alec
02:55
created

BenchmarkReportFormatter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 115
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A countersStatistics() 0 21 2
A checkReturns() 0 4 1
A allReturnsAreEqual() 0 12 2
A functionsReturns() 0 8 2
A getString() 0 19 2
A countedExceptions() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Tools\Reports\Formatters;
6
7
use function AlecRabbit\is_homogeneous;
8
use AlecRabbit\Tools\Internal\BenchmarkFunction;
9
use AlecRabbit\Tools\Reports\BenchmarkReport;
10
11
class BenchmarkReportFormatter extends ReportFormatter
12
{
13
    /** @var BenchmarkReport */
14
    protected $report;
15
    /** @var mixed */
16
    protected $lastReturn;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 4
    public function getString(): string
22
    {
23 4
        $str = self::BENCHMARK . PHP_EOL;
24 4
        $equalReturns = $this->checkReturns();
25
        /** @var BenchmarkFunction $function */
26 4
        foreach ($this->report->getFunctions() as $name => $function) {
27
            $str .=
28 4
                (new BenchmarkFunctionFormatter($function))
29 4
                    ->noResultsIf($equalReturns)
30 4
                    ->getString();
31
        }
32
        return
33 4
            sprintf(
34 4
                '%s%s%s%s%s',
35 4
                $str,
36 4
                $this->allReturnsAreEqual($equalReturns),
37 4
                $this->countersStatistics(),
38 4
                $this->report->getMemoryUsageReport(),
39 4
                PHP_EOL
40
            );
41
    }
42
43
    /**
44
     * @return bool
45
     */
46 4
    protected function checkReturns(): bool
47
    {
48
        return
49 4
            is_homogeneous($this->functionsReturns());
50
    }
51
52
    /**
53
     * @return array
54
     */
55 4
    private function functionsReturns(): array
56
    {
57 4
        $returns = [];
58
        /** @var BenchmarkFunction $function */
59 4
        foreach ($this->report->getFunctions() as $name => $function) {
60 4
            $returns[] = $this->lastReturn = $function->getReturn();
61
        }
62 4
        return $returns;
63
    }
64
65 4
    private function allReturnsAreEqual(bool $equalReturns): string
66
    {
67 4
        if (!$equalReturns) {
68 3
            return '';
69
        }
70
        return
71 1
            sprintf(
72 1
                '%s %s%s %s',
73 1
                'All returns are equal:',
74 1
                PHP_EOL,
75 1
                BenchmarkFunctionFormatter::returnToString($this->lastReturn),
76 1
                PHP_EOL
77
            );
78
    }
79
80
    // TODO move to php-helpers
81
82
    /**
83
     * @return string
84
     */
85 4
    private function countersStatistics(): string
86
    {
87
        $added =
88 4
            $this->report->getProfiler()
89 4
                ->counter(static::ADDED)->getValue();
90
        $benchmarked =
91 4
            $this->report->getProfiler()
92 4
                ->counter(static::BENCHMARKED)->getValue();
93 4
        if ($added === $benchmarked) {
94 1
            return '';
95
        }
96
97
        return
98 3
            sprintf(
99 3
                '%s: %s %s: %s %s %s',
100 3
                static::ADDED,
101 3
                $added,
102 3
                static::BENCHMARKED,
103 3
                $benchmarked,
104 3
                $this->countedExceptions($added, $benchmarked),
105 3
                PHP_EOL
106
            );
107
    }
108
109
    /**
110
     * @param int $added
111
     * @param int $benchmarked
112
     * @return string
113
     */
114 3
    private function countedExceptions(int $added, int $benchmarked): string
115
    {
116 3
        if (0 !== $exceptions = $added - $benchmarked) {
117
            return
118 3
                sprintf(
119 3
                    '%s %s',
120 3
                    static::EXCEPTIONS,
121 3
                    $exceptions
122
                );
123
        }
124
        // @codeCoverageIgnoreStart
125
        return '';
126
        // @codeCoverageIgnoreEnd
127
    }
128
}
129