Passed
Push — develop ( 84aa32...d180ee )
by Alec
02:42
created

BenchmarkFunction::getIndex()   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
    /**
50
     * BenchmarkFunction constructor.
51
     * @param callable $func
52
     * @param string $name
53
     * @param int $index
54
     * @param array $args
55
     * @param null|string $comment
56
     * @param null|string $humanReadableName
57
     */
58 6
    public function __construct(
59
        $func,
60
        string $name,
61
        int $index,
62
        array $args,
63
        ?string $comment = null,
64
        ?string $humanReadableName = null
65
    ) {
66 6
        $this->callable = $func;
67 6
        $this->name = $name;
68 6
        $this->index = $index;
69 6
        $this->args = $args;
70 6
        $this->comment = $comment;
71 6
        $this->humanReadableName = $humanReadableName;
72 6
        $this->timer = new Timer($this->getIndexedName());
73 6
    }
74
75
    /**
76
     * @return string
77
     */
78 6
    public function getIndexedName(): string
79
    {
80 6
        return "⟨{$this->getIndex()}⟩ {$this->getName()}";
81
    }
82
83
    /**
84
     * @return int
85
     */
86 6
    public function getIndex(): int
87
    {
88 6
        return $this->index;
89
    }
90
91
    /**
92
     * @return string
93
     */
94 4
    public function comment(): string
95
    {
96 4
        return $this->comment ?? '';
97
//        return $this->comment ? str_decorate($this->comment, '"') : '';
98
    }
99
100
    /**
101
     * @return array
102
     */
103 6
    public function getArgs(): array
104
    {
105 6
        return $this->args;
106
    }
107
108
    /**
109
     * @return callable
110
     */
111 6
    public function getCallable(): callable
112
    {
113 6
        return $this->callable;
114
    }
115
116 6
    public function enumeratedName(): string
117
    {
118 6
        return $this->getIndexedName();
119
//        return
120
//            brackets((string)$this->index, BRACKETS_ANGLE) . ' ' . $this->name;
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126 4
    public function getReturn()
127
    {
128 4
        return $this->result;
129
    }
130
131
    /**
132
     * @param mixed $result
133
     */
134 6
    public function setResult($result): void
135
    {
136 6
        $this->result = $result;
137 6
    }
138
139
    /**
140
     * @return Timer
141
     */
142 6
    public function getTimer(): Timer
143
    {
144 6
        return $this->timer;
145
    }
146
147
    /**
148
     * @return null|string
149
     */
150 4
    public function humanReadableName(): ?string
151
    {
152 4
        return $this->humanReadableName ?? $this->getIndexedName();
153
    }
154
155
    /**
156
     * @return null|\Throwable
157
     */
158 4
    public function getException(): ?\Throwable
159
    {
160 4
        return $this->exception;
161
    }
162
163
    /**
164
     * @param \Throwable $exception
165
     * @return BenchmarkFunction
166
     */
167 3
    public function setException(\Throwable $exception): BenchmarkFunction
168
    {
169 3
        $this->exception = $exception;
170 3
        return $this;
171
    }
172
173
    /**
174
     * @return null|BenchmarkRelative
175
     */
176 4
    public function getBenchmarkRelative(): ?BenchmarkRelative
177
    {
178 4
        return $this->benchmarkRelative;
179
    }
180
181
    /**
182
     * @param null|BenchmarkRelative $benchmarkRelative
183
     * @return BenchmarkFunction
184
     */
185 4
    public function setBenchmarkRelative(?BenchmarkRelative $benchmarkRelative): BenchmarkFunction
186
    {
187 4
        $this->benchmarkRelative = $benchmarkRelative;
188 4
        return $this;
189
    }
190
191
    /**
192
     * @return bool
193
     */
194 6
    public function execute(): bool
195
    {
196
        try {
197 6
            $this->setResult(
198 6
                ($this->callable)(...$this->args)
199
            );
200 6
            return true;
201 3
        } catch (\Throwable $e) {
202 3
            $this->setException($e);
203
        }
204 3
        return false;
205
    }
206
}
207