Completed
Push — master ( 4e3380...4c0581 )
by Alec
03:34 queued 26s
created

BenchmarkFunction::getTimer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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