Completed
Push — master ( 9d2605...9c3b37 )
by Alec
02:41
created

Factory::getBenchmarkFunctionFormatter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools;
4
5
use AlecRabbit\Tools\Reports\Formatters\BenchmarkFunctionFormatter;
6
use AlecRabbit\Tools\Reports\Formatters\BenchmarkFunctionSymfonyFormatter;
7
use AlecRabbit\Tools\Reports\Formatters\BenchmarkReportFormatter;
8
use AlecRabbit\Tools\Reports\Formatters\ExtendedCounterReportFormatter;
9
use AlecRabbit\Tools\Reports\Formatters\Formatter;
10
use AlecRabbit\Tools\Reports\Formatters\ProfilerReportFormatter;
11
use AlecRabbit\Tools\Reports\Formatters\SimpleCounterReportFormatter;
12
use AlecRabbit\Tools\Reports\Formatters\TimerReportFormatter;
13
use Symfony\Component\Console\Helper\ProgressBar;
14
use function AlecRabbit\typeOf;
15
16
class Factory
17
{
18
    /** @var null|TimerReportFormatter */
19
    protected static $timerReportFormatter;
20
21
    /** @var null|SimpleCounterReportFormatter */
22
    protected static $simpleCounterReportFormatter;
23
24
    /** @var null|ExtendedCounterReportFormatter */
25
    protected static $extendedCounterReportFormatter;
26
27
    /** @var null|ProfilerReportFormatter */
28
    protected static $profilerReportFormatter;
29
30
    /** @var null|BenchmarkReportFormatter */
31
    protected static $benchmarkReportFormatter;
32
33
    /** @var null|BenchmarkFunctionFormatter */
34
    protected static $benchmarkFunctionFormatter;
35
36
    /** @var int */
37
    protected static $defaultIterations = 1000000;
38
39
    /** @codeCoverageIgnore */
40
    private function __construct()
41
    {
42
        // Static class
43
    }
44
45
    /**
46
     * @param int $defaultIterations
47
     */
48
    public static function setDefaultIterations(int $defaultIterations): void
49
    {
50
        self::$defaultIterations = $defaultIterations;
51
    }
52
53 8
    public static function setFormatter(Formatter $formatter): Formatter
54
    {
55 8
        if ($formatter instanceof SimpleCounterReportFormatter) {
56 1
            static::setSimpleCounterReportFormatter($formatter);
57
            return
58 1
                static::getSimpleCounterReportFormatter();
59
        }
60 7
        if ($formatter instanceof ExtendedCounterReportFormatter) {
61 1
            static::setExtendedCounterReportFormatter($formatter);
62
            return
63 1
                static::getExtendedCounterReportFormatter();
64
        }
65 6
        if ($formatter instanceof TimerReportFormatter) {
66 1
            static::setTimerReportFormatter($formatter);
67
            return
68 1
                static::getTimerReportFormatter();
69
        }
70 5
        if ($formatter instanceof ProfilerReportFormatter) {
71 1
            static::setProfilerReportFormatter($formatter);
72
            return
73 1
                static::getProfilerReportFormatter();
74
        }
75 4
        if ($formatter instanceof BenchmarkReportFormatter) {
76 2
            static::setBenchmarkReportFormatter($formatter);
77
            return
78 2
                static::getBenchmarkReportFormatter();
79
        }
80 2
        if ($formatter instanceof BenchmarkFunctionFormatter) {
81 1
            static::setBenchmarkFunctionFormatter($formatter);
82
            return
83 1
                static::getBenchmarkFunctionFormatter();
84
        }
85 1
        throw new \RuntimeException('Formatter [' . typeOf($formatter) . '] is not accepted.');
86
    }
87
88
    /**
89
     * @return SimpleCounterReportFormatter
90
     */
91 6
    public static function getSimpleCounterReportFormatter(): SimpleCounterReportFormatter
92
    {
93 6
        if (null === static::$simpleCounterReportFormatter) {
94 1
            static::$simpleCounterReportFormatter = new SimpleCounterReportFormatter();
95
        }
96
        return
97 6
            static::$simpleCounterReportFormatter;
98
    }
99
100
    /**
101
     * @param null|SimpleCounterReportFormatter $simpleCounterReportFormatter
102
     */
103 2
    public static function setSimpleCounterReportFormatter(
104
        ?SimpleCounterReportFormatter $simpleCounterReportFormatter
105
    ): void {
106 2
        static::$simpleCounterReportFormatter = $simpleCounterReportFormatter;
107 2
    }
108
109
    /**
110
     * @return ExtendedCounterReportFormatter
111
     */
112 5
    public static function getExtendedCounterReportFormatter(): ExtendedCounterReportFormatter
113
    {
114 5
        if (null === static::$extendedCounterReportFormatter) {
115 1
            static::$extendedCounterReportFormatter = new ExtendedCounterReportFormatter();
116
        }
117
        return
118 5
            static::$extendedCounterReportFormatter;
119
    }
120
121
    /**
122
     * @param null|ExtendedCounterReportFormatter $extendedCounterReportFormatter
123
     */
124 2
    public static function setExtendedCounterReportFormatter(
125
        ?ExtendedCounterReportFormatter $extendedCounterReportFormatter
126
    ): void {
127 2
        static::$extendedCounterReportFormatter = $extendedCounterReportFormatter;
128 2
    }
129
130
    /**
131
     * @return TimerReportFormatter
132
     */
133 16
    public static function getTimerReportFormatter(): TimerReportFormatter
134
    {
135 16
        if (null === static::$timerReportFormatter) {
136 1
            static::$timerReportFormatter = new TimerReportFormatter();
137
        }
138
        return
139 16
            static::$timerReportFormatter;
140
    }
141
142
    /**
143
     * @param null|TimerReportFormatter $timerReportFormatter
144
     */
145 2
    public static function setTimerReportFormatter(?TimerReportFormatter $timerReportFormatter): void
146
    {
147 2
        static::$timerReportFormatter = $timerReportFormatter;
148 2
    }
149
150
    /**
151
     * @return ProfilerReportFormatter
152
     */
153 4
    public static function getProfilerReportFormatter(): ProfilerReportFormatter
154
    {
155 4
        if (null === static::$profilerReportFormatter) {
156 1
            static::$profilerReportFormatter = new ProfilerReportFormatter();
157
        }
158
        return
159 4
            static::$profilerReportFormatter;
160
    }
161
162
    /**
163
     * @param null|ProfilerReportFormatter $profilerReportFormatter
164
     */
165 2
    public static function setProfilerReportFormatter(
166
        ?ProfilerReportFormatter $profilerReportFormatter
167
    ): void {
168 2
        static::$profilerReportFormatter = $profilerReportFormatter;
169 2
    }
170
171
    /**
172
     * @return BenchmarkReportFormatter
173
     */
174 13
    public static function getBenchmarkReportFormatter(): BenchmarkReportFormatter
175
    {
176 13
        if (null === static::$benchmarkReportFormatter) {
177 1
            static::$benchmarkReportFormatter = new BenchmarkReportFormatter();
178
        }
179
        return
180 13
            static::$benchmarkReportFormatter;
181
    }
182
183
    /**
184
     * @param null|BenchmarkReportFormatter $benchmarkReportFormatter
185
     */
186 3
    public static function setBenchmarkReportFormatter(
187
        ?BenchmarkReportFormatter $benchmarkReportFormatter
188
    ): void {
189 3
        static::$benchmarkReportFormatter = $benchmarkReportFormatter;
190 3
    }
191
192
    /**
193
     * @return BenchmarkFunctionFormatter
194
     */
195 12
    public static function getBenchmarkFunctionFormatter(): BenchmarkFunctionFormatter
196
    {
197 12
        if (null === static::$benchmarkFunctionFormatter) {
198 1
            static::$benchmarkFunctionFormatter = new BenchmarkFunctionFormatter();
199
        }
200
        return
201 12
            static::$benchmarkFunctionFormatter->resetEqualReturns();
202
    }
203
204
    /**
205
     * @param null|BenchmarkFunctionFormatter $benchmarkFunctionFormatter
206
     */
207 2
    public static function setBenchmarkFunctionFormatter(
208
        ?BenchmarkFunctionFormatter $benchmarkFunctionFormatter
209
    ): void {
210 2
        static::$benchmarkFunctionFormatter = $benchmarkFunctionFormatter;
211 2
    }
212
213
    /**
214
     * @param null|int $iterations
215
     * @param bool $withProgressBar
216
     * @return Benchmark
217
     * @throws \Exception
218
     */
219
    public static function createBenchmark(?int $iterations = null, bool $withProgressBar = true): Benchmark
220
    {
221
        $iterations = $iterations ?? static::$defaultIterations;
222
        if (!$withProgressBar) {
223
            return
224
                new Benchmark($iterations);
225
        }
226
        if (\class_exists(ProgressBar::class)) {
227
            // TODO where to set colored formatters?
228
            static::setFormatter(new BenchmarkFunctionSymfonyFormatter());
229
            return
230
                new BenchmarkSymfonyProgressBar($iterations);
231
        }
232
        return
233
            new BenchmarkSimpleProgressBar($iterations);
234
    }
235
}
236