Completed
Push — develop ( eb616e...328010 )
by Alec
03:31
created

BenchmarkFunction::makeTimer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools\Internal;
4
5
use AlecRabbit\Tools\AbstractTimer;
6
use AlecRabbit\Tools\Formattable;
7
use AlecRabbit\Tools\HRTimer;
8
use AlecRabbit\Tools\Timer;
9
use AlecRabbit\Traits\GettableName;
10
11
/**
12
 * Class BenchmarkFunction
13
 * @package AlecRabbit
14
 * @internal
15
 */
16
class BenchmarkFunction extends Formattable
17
{
18
    use GettableName;
19
20
    /** @var bool */
21
    protected $showReturns = true;
22
    /** @var callable */
23
    protected $callable;
24
    /** @var int */
25
    protected $index;
26
    /** @var array */
27
    protected $args;
28
    /** @var mixed */
29
    protected $return;
30
    /** @var null|string */
31
    protected $comment;
32
    /** @var null|string */
33
    protected $humanReadableName;
34
    /** @var \Throwable|null */
35
    protected $exception;
36
    /** @var AbstractTimer */
37
    protected $timer;
38
    /** @var null|BenchmarkRelative */
39
    protected $benchmarkRelative;
40
    /** @var bool */
41
    protected static $forceRegularTimer = false;
42
43
    /**
44
     * BenchmarkFunction constructor.
45
     *
46
     * @param callable $func
47
     * @param string $name
48
     * @param int $index
49
     * @param array $args
50
     * @param null|string $comment
51
     * @param null|string $humanReadableName
52
     * @throws \Exception
53
     */
54 18
    public function __construct(
55
        $func,
56
        string $name,
57
        int $index,
58
        array $args,
59
        ?string $comment = null,
60
        ?string $humanReadableName = null
61
    ) {
62 18
        $this->callable = $func;
63 18
        $this->name = $name;
64 18
        $this->index = $index;
65 18
        $this->args = $args;
66 18
        $this->comment = $comment;
67 18
        $this->humanReadableName = $humanReadableName;
68 18
        $this->makeTimer();
69 18
    }
70
71
    /**
72
     * @throws \Exception
73
     */
74 18
    protected function makeTimer(): void
75
    {
76 18
        if (PHP_VERSION_ID >= 70300 && false === static::$forceRegularTimer) {
77 13
            $this->timer = new HRTimer($this->getIndexedName());
78
        } else {
79 6
            $this->timer = new Timer($this->getIndexedName());
80
        }
81 18
    }
82
83
    /**
84
     * @return string
85
     */
86 18
    public function getIndexedName(): string
87
    {
88 18
        return "⟨{$this->getIndex()}⟩ {$this->getName()}";
89
    }
90
91
    /**
92
     * @return int
93
     */
94 18
    public function getIndex(): int
95
    {
96 18
        return $this->index;
97
    }
98
99
    /**
100
     * @param bool $force
101
     */
102 1
    public static function setForceRegularTimer(bool $force): void
103
    {
104 1
        static::$forceRegularTimer = $force;
105 1
    }
106
107
    /**
108
     * @return bool
109
     */
110 1
    public static function isForceRegularTimer(): bool
111
    {
112 1
        return self::$forceRegularTimer;
113
    }
114
115
    /**
116
     * @return string
117
     */
118 12
    public function comment(): string
119
    {
120 12
        return $this->comment ?? '';
121
    }
122
123
    /**
124
     * @return array
125
     */
126 17
    public function getArgs(): array
127
    {
128 17
        return $this->args;
129
    }
130
131
    /**
132
     * @return callable
133
     */
134 15
    public function getCallable(): callable
135
    {
136 15
        return $this->callable;
137
    }
138
139 15
    public function enumeratedName(): string
140
    {
141 15
        return $this->getIndexedName();
142
    }
143
144
    /**
145
     * @return mixed
146
     */
147 13
    public function getReturn()
148
    {
149 13
        return $this->return;
150
    }
151
152
    /**
153
     * @return AbstractTimer
154
     */
155 16
    public function getTimer(): AbstractTimer
156
    {
157 16
        return $this->timer;
158
    }
159
160
    /**
161
     * @return null|string
162
     */
163 14
    public function humanReadableName(): ?string
164
    {
165 14
        return $this->humanReadableName ?? $this->getIndexedName();
166
    }
167
168
    /**
169
     * @return null|\Throwable
170
     */
171 13
    public function getException(): ?\Throwable
172
    {
173 13
        return $this->exception;
174
    }
175
176
    /**
177
     * @param \Throwable $exception
178
     * @return BenchmarkFunction
179
     */
180 6
    public function setException(\Throwable $exception): BenchmarkFunction
181
    {
182 6
        $this->exception = $exception;
183 6
        return $this;
184
    }
185
186
    /**
187
     * @return null|BenchmarkRelative
188
     */
189 14
    public function getBenchmarkRelative(): ?BenchmarkRelative
190
    {
191 14
        return $this->benchmarkRelative;
192
    }
193
194
    /**
195
     * @param null|BenchmarkRelative $benchmarkRelative
196
     * @return BenchmarkFunction
197
     */
198 15
    public function setBenchmarkRelative(?BenchmarkRelative $benchmarkRelative): BenchmarkFunction
199
    {
200 15
        $this->benchmarkRelative = $benchmarkRelative;
201 15
        return $this;
202
    }
203
204
    /**
205
     * @return bool
206
     */
207 17
    public function execute(): bool
208
    {
209
        try {
210 17
            $this->setReturn(
211 17
                ($this->callable)(...$this->args)
212
            );
213 17
            return true;
214 6
        } catch (\Throwable $e) {
215 6
            $this->setException($e);
216
        }
217 6
        return false;
218
    }
219
220
    /**
221
     * @param mixed $return
222
     */
223 17
    public function setReturn($return): void
224
    {
225 17
        $this->return = $return;
226 17
    }
227
228
    /**
229
     * @return bool
230
     */
231 4
    public function isNotShowReturns(): bool
232
    {
233 4
        return !$this->isShowReturns();
234
    }
235
236
    /**
237
     * @return bool
238
     */
239 4
    public function isShowReturns(): bool
240
    {
241 4
        return $this->showReturns;
242
    }
243
244
    /**
245
     * @param bool $showReturns
246
     */
247 15
    public function setShowReturns(bool $showReturns): void
248
    {
249 15
        $this->showReturns = $showReturns;
250 15
    }
251
}
252