Passed
Push — develop ( 0a2c3f...8e5858 )
by Alec
03:05
created

BenchmarkReportFormatter::computeVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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