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