Completed
Push — master ( c2654a...8b49d6 )
by Alec
21:15 queued 13:44
created

BenchmarkFunction::setException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools;
4
5
use AlecRabbit\Tools\Internal\BenchmarkRelative;
6
use AlecRabbit\Traits\GettableName;
7
use function AlecRabbit\typeOf;
8
9
class BenchmarkFunction
10
{
11
    use GettableName;
12
13
    public const CLOSURE_NAME = 'λ';
14
15
    /** @var bool */
16
    protected $withReturns = false;
17
    /** @var callable */
18
    protected $callable;
19
    /** @var int */
20
    protected $index;
21
    /** @var array */
22
    protected $args;
23
    /** @var mixed */
24
    protected $return;
25
    /** @var null|string */
26
    protected $comment;
27
    /** @var null|string */
28
    protected $assignedName;
29
    /** @var \Throwable|null */
30
    protected $exception;
31
32
    /** @var null|BenchmarkResult */
33
    protected $result;
34
    /** @var null|BenchmarkRelative */
35
    protected $relative;
36
37
    /**
38
     * BenchmarkFunction constructor.
39
     * @param callable $func
40
     * @param array $args
41
     * @param int $index
42
     * @param null|string $assignName
43
     * @param null|string $comment
44
     */
45
    public function __construct(
46
        $func,
47
        array $args,
48
        int $index,
49
        ?string $assignName = null,
50
        ?string $comment = null
51
    ) {
52
//        parent::__construct();
53
        if (!\is_callable($func, false, $name)) {
54
            throw new \InvalidArgumentException(
55
                sprintf(
56
                    '\'%s\' is NOT callable. Function must be callable. Type of "%s" provided instead.',
57
                    $name,
58
                    typeOf($func)
59
                )
60
            );
61
        }
62
        $this->callable = $func;
63
        $this->name = $this->refineName($func, $name);
64
        $this->index = $index;
65
        $this->args = $args;
66
        $this->comment = $comment;
67
        $this->assignedName = $assignName;
68
    }
69
70
    /**
71
     * @param callable $func
72
     * @param string $name
73
     * @return string
74
     */
75
    protected function refineName($func, string $name): string
76
    {
77
        if ($func instanceof \Closure) {
78
            $name = self::CLOSURE_NAME;
79
        }
80
        return $name;
81
    }
82
83
    /**
84
     * @return bool
85
     */
86
    public function execute(): bool
87
    {
88
        try {
89
            $this->setReturn(
90
                ($this->callable)(...$this->args)
91
            );
92
        } catch (\Throwable $e) {
93
            $this->setException($e);
94
            return false;
95
        }
96
        return true;
97
    }
98
99
    /**
100
     * @return callable
101
     */
102
    public function getCallable(): callable
103
    {
104
        return $this->callable;
105
    }
106
107
    /**
108
     * @return array
109
     */
110
    public function getArgs(): array
111
    {
112
        return $this->args;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getAssignedName(): string
119
    {
120
        return $this->assignedName ?? $this->getIndexedName();
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getIndexedName(): string
127
    {
128
        return "⟨{$this->getIndex()}⟩ {$this->getName()}";
129
    }
130
131
    /**
132
     * @return int
133
     */
134
    public function getIndex(): int
135
    {
136
        return $this->index;
137
    }
138
139
    /**
140
     * @return null|\Throwable
141
     */
142
    public function getException(): ?\Throwable
143
    {
144
        return $this->exception;
145
    }
146
147
    /**
148
     * @param \Throwable $exception
149
     * @return BenchmarkFunction
150
     */
151
    public function setException(\Throwable $exception): self
152
    {
153
        $this->exception = $exception;
154
        return $this;
155
    }
156
157
    /**
158
     * @return null|string
159
     */
160
    public function getComment(): ?string
161
    {
162
        return $this->comment;
163
    }
164
165
    /**
166
     * @return BenchmarkResult|null
167
     */
168
    public function getResult(): ?BenchmarkResult
169
    {
170
        return $this->result;
171
    }
172
173
    /**
174
     * @param BenchmarkResult $result
175
     */
176
    public function setResult(BenchmarkResult $result): void
177
    {
178
        $this->result = $result;
179
    }
180
181
    /**
182
     * @return mixed
183
     */
184
    public function getReturn()
185
    {
186
        return $this->return;
187
    }
188
189
    /**
190
     * @param mixed $return
191
     */
192
    public function setReturn($return): void
193
    {
194
        $this->return = $return;
195
    }
196
197
    public function setBenchmarkRelative(BenchmarkRelative $relative): void
198
    {
199
        $this->relative = $relative;
200
    }
201
202
    /**
203
     * @return BenchmarkRelative|null
204
     */
205
    public function getRelative(): ?BenchmarkRelative
206
    {
207
        return $this->relative;
208
    }
209
}
210