Completed
Push — develop ( a0bdf0...500e55 )
by Alec
02:54
created

BenchmarkReportFormatter::added()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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