Completed
Push — develop ( d2e190...d276d7 )
by Alec
10:43 queued 07:55
created

BenchmarkFunction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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