Completed
Push — develop ( ca99c7...b953bd )
by Alec
03:03
created

BenchmarkFunction   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 206
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0
wmc 19

18 Methods

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