Test Failed
Push — master ( 44657e...85337f )
by Alec
03:46
created

BenchmarkFunction::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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