Completed
Push — develop ( 5a5dff...7e0445 )
by Alec
07:58 queued 03:29
created

BenchmarkReportFormatter::countersStatistics()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 2
dl 0
loc 20
ccs 15
cts 15
cp 1
crap 2
rs 9.7666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools\Reports\Formatters;
4
5
use AlecRabbit\Tools\Internal\BenchmarkFunction;
6
use AlecRabbit\Tools\Reports\BenchmarkReport;
7
use AlecRabbit\Tools\Reports\Factory;
8
use function AlecRabbit\array_is_homogeneous;
9
10
class BenchmarkReportFormatter extends ReportFormatter
11
{
12
    /** @var BenchmarkReport */
13
    protected $report;
14
    /** @var mixed */
15
    protected $lastReturn;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20 4
    public function process(): string
21
    {
22 4
        $str = 'Results:' . PHP_EOL;
23 4
        $added = $this->added();
24 4
        $benchmarked = $this->benchmarked();
25 4
        $benchmarkedAny = $this->benchmarkedAny($added, $benchmarked);
26 4
        if ($benchmarkedAny) {
27 4
            $str .= self::BENCHMARK . PHP_EOL;
28
        }
29 4
        $equalReturns = $this->checkReturns();
30
        /** @var BenchmarkFunction $function */
31 4
        foreach ($this->report->getFunctions() as $name => $function) {
32
            $str .=
33 4
                Factory::getBenchmarkFunctionFormatter($function)
34 4
                    ->noResultsIf($equalReturns)
35 4
                    ->process();
36
        }
37
        return
38 4
            sprintf(
39 4
                '%s%s%s%s%s',
40 4
                $str,
41 4
                $benchmarkedAny ? $this->allReturnsAreEqual($equalReturns) : '',
42 4
                $this->countersStatistics($added, $benchmarked),
43 4
                $this->report->getMemoryUsageReport(),
44 4
                PHP_EOL
45
            );
46
    }
47
48
    /**
49
     * @return int
50
     */
51 4
    private function added(): int
52
    {
53
        return
54 4
            $this->report->getProfiler()
55 4
                ->counter(static::ADDED)->getValue();
56
    }
57
58
    /**
59
     * @return int
60
     */
61 4
    private function benchmarked(): int
62
    {
63
        return
64 4
            $this->report->getProfiler()
65 4
                ->counter(static::BENCHMARKED)->getValue();
66
    }
67
68
    /**
69
     * @param int $added
70
     * @param int $benchmarked
71
     * @return bool
72
     */
73 4
    private function benchmarkedAny(int $added, int $benchmarked): bool
74
    {
75 4
        return $added !== $added - $benchmarked;
76
    }
77
78
    /**
79
     * @return bool
80
     */
81 4
    protected function checkReturns(): bool
82
    {
83
        return
84 4
            array_is_homogeneous($this->functionsReturns());
85
    }
86
87
    /**
88
     * @return array
89
     */
90 4
    private function functionsReturns(): array
91
    {
92 4
        $returns = [];
93
        /** @var BenchmarkFunction $function */
94 4
        foreach ($this->report->getFunctions() as $name => $function) {
95 4
            $returns[] = $this->lastReturn = $function->getReturn();
96
        }
97 4
        return $returns;
98
    }
99
100 4
    private function allReturnsAreEqual(bool $equalReturns): string
101
    {
102 4
        if ($equalReturns) {
103
            return
104 1
                sprintf(
105 1
                    '%s %s%s %s',
106 1
                    'All returns are equal:',
107 1
                    PHP_EOL,
108 1
                    BenchmarkFunctionFormatter::returnToString($this->lastReturn),
109 1
                    PHP_EOL
110
                );
111
        }
112 3
        return '';
113
    }
114
115
    /**
116
     * @param int $added
117
     * @param int $benchmarked
118
     * @return string
119
     */
120 4
    private function countersStatistics(int $added, int $benchmarked): string
121
    {
122 4
        if ($added === $benchmarked) {
123 1
            return sprintf(
124 1
                '%s: %s %s',
125 1
                static::BENCHMARKED,
126 1
                $benchmarked,
127 1
                PHP_EOL
128
            );
129
        }
130
131
        return
132 3
            sprintf(
133 3
                '%s: %s %s: %s %s %s',
134 3
                static::ADDED,
135 3
                $added,
136 3
                static::BENCHMARKED,
137 3
                $benchmarked,
138 3
                $this->countedExceptions($added, $benchmarked),
139 3
                PHP_EOL
140
            );
141
    }
142
143
    /**
144
     * @param int $added
145
     * @param int $benchmarked
146
     * @return string
147
     */
148 3
    private function countedExceptions(int $added, int $benchmarked): string
149
    {
150 3
        if (0 !== $exceptions = $added - $benchmarked) {
151
            return
152 3
                sprintf(
153 3
                    '%s %s',
154 3
                    static::EXCEPTIONS,
155 3
                    $exceptions
156
                );
157
        }
158
        // @codeCoverageIgnoreStart
159
        return '';
160
        // @codeCoverageIgnoreEnd
161
    }
162
}
163