Passed
Push — master ( 13c7b8...1472b6 )
by Alec
07:12 queued 03:14
created

BenchmarkReportFormatter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 130
ccs 57
cts 57
cp 1
rs 10
c 0
b 0
f 0
wmc 14

7 Methods

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