Completed
Push — develop ( d2e190...d276d7 )
by Alec
10:43 queued 07:55
created

BenchmarkReportFormatter::added()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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