Test Failed
Push — develop ( f741b1...4c7784 )
by Alec
05:40
created

BenchmarkFunction   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Test Coverage

Coverage 95.12%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 171
ccs 39
cts 41
cp 0.9512
rs 10
c 0
b 0
f 0
wmc 15

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getException() 0 3 1
A enumeratedName() 0 3 1
A getResult() 0 3 1
A getIndexedName() 0 3 1
A getCallable() 0 3 1
A getTimer() 0 3 1
A getHumanReadableName() 0 3 1
A setException() 0 4 1
A setRank() 0 4 1
A getComment() 0 3 1
A getIndex() 0 3 1
A getArgs() 0 3 1
A setResult() 0 3 1
A getRank() 0 3 1
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
use function AlecRabbit\brackets;
13
use function AlecRabbit\str_decorate;
14
use const AlecRabbit\Helpers\Constants\BRACKETS_ANGLE;
15
16
/**
17
 * Class BenchmarkFunction
18
 * @package AlecRabbit
19
 * @internal
20
 */
21
class BenchmarkFunction
22
{
23
    use GettableName;
24
25
    /** @var callable */
26
    private $func;
27
28
    /** @var int */
29
    private $index;
30
31
    /** @var array */
32
    private $args;
33
34
    /** @var mixed */
35
    private $result;
36
37
    /** @var null|string */
38
    private $comment;
39
40
    /** @var null|string */
41
    private $humanReadableName;
42
43
    /** @var \Throwable|null */
44
    private $exception;
45
46
    /** @var Timer */
47
    private $timer;
48
49
    /** @var null|int */
50
    private $rank;
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 2
    public function __construct(
62
        $func,
63
        string $name,
64
        int $index,
65
        array $args,
66
        ?string $comment = null,
67
        ?string $humanReadableName = null
68
    ) {
69 2
        $this->func = $func;
70 2
        $this->name = $name;
71 2
        $this->index = $index;
72 2
        $this->args = $args;
73 2
        $this->comment = $comment;
74 2
        $this->humanReadableName = $humanReadableName;
75 2
        $this->timer = new Timer($this->getIndexedName());
76 2
    }
77
78
    /**
79
     * @return string
80
     */
81 2
    public function getIndexedName(): string
82
    {
83 2
        return "⟨{$this->getIndex()}⟩ {$this->getName()}";
84
    }
85
86
    /**
87
     * @return int
88
     */
89 2
    public function getIndex(): int
90
    {
91 2
        return $this->index;
92
    }
93
94
    /**
95
     * @return string
96
     */
97 2
    public function getComment(): string
98
    {
99 2
        return $this->comment;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->comment could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
100
//        return $this->comment ? str_decorate($this->comment, '"') : '';
101
    }
102 2
103
    /**
104
     * @return array
105 2
     */
106
    public function getArgs(): array
107
    {
108
        return $this->args;
109
    }
110
111 1
    /**
112
     * @return callable
113 1
     */
114
    public function getCallable(): callable
115
    {
116
        return $this->func;
117
    }
118
119 2
    public function enumeratedName(): string
120
    {
121 2
        return $this->getIndexedName();
122 2
//        return
123
//            brackets((string)$this->index, BRACKETS_ANGLE) . ' ' . $this->name;
124
    }
125
126
    /**
127 2
     * @return mixed
128
     */
129 2
    public function getResult()
130 2
    {
131 2
        return $this->result;
132 2
    }
133
134
    /**
135
     * @param mixed $result
136
     */
137
    public function setResult($result): void
138
    {
139 2
        $this->result = $result;
140
    }
141 2
142
    /**
143
     * @return Timer
144
     */
145
    public function getTimer(): Timer
146
    {
147 2
        return $this->timer;
148
    }
149 2
150
    /**
151
     * @return null|string
152
     */
153
    public function getHumanReadableName(): ?string
154
    {
155 2
        return $this->humanReadableName ?? $this->getIndexedName();
156
    }
157 2
158
    /**
159
     * @return null|\Throwable
160
     */
161
    public function getException(): ?\Throwable
162
    {
163 2
        return $this->exception;
164
    }
165 2
166
    /**
167
     * @param \Throwable $exception
168
     * @return BenchmarkFunction
169
     */
170
    public function setException(\Throwable $exception): BenchmarkFunction
171
    {
172 1
        $this->exception = $exception;
173
        return $this;
174 1
    }
175 1
176
    /**
177
     * @return null|int
178
     */
179
    public function getRank(): ?int
180
    {
181
        return $this->rank;
182
    }
183
184
    /**
185
     * @param null|int $rank
186
     * @return BenchmarkFunction
187
     */
188
    public function setRank(?int $rank): BenchmarkFunction
189
    {
190 2
        $this->rank = $rank;
191
        return $this;
192 2
    }
193
}
194