Skip to content

Commit cb32e4d

Browse files
Merge pull request #12 from TheDragonCode/2.x
Added static call method
2 parents 5a96e27 + cd1d694 commit cb32e4d

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ Or manually update `require-dev` block of `composer.json` and run `composer upda
3636
```php
3737
use DragonCode\Benchmark\Benchmark;
3838

39-
(new Benchmark())->compare(
39+
Benchmark::start()->compare(
4040
fn () => /* some code */,
4141
fn () => /* some code */,
4242
);
4343

44-
(new Benchmark())->compare([
44+
Benchmark::start()->compare([
4545
fn () => /* some code */,
4646
fn () => /* some code */,
4747
]);
4848

49-
(new Benchmark())->compare([
49+
Benchmark::start()->compare([
5050
'foo' => fn () => /* some code */,
5151
'bar' => fn () => /* some code */,
5252
]);
@@ -92,7 +92,7 @@ the `iterations` method:
9292
```php
9393
use DragonCode\Benchmark\Benchmark;
9494

95-
(new Benchmark())
95+
Benchmark::start()
9696
->iterations(5)
9797
->compare(
9898
fn () => /* some code */,
@@ -126,7 +126,7 @@ You can also get the number of the current execution iteration from the input pa
126126
```php
127127
use DragonCode\Benchmark\Benchmark;
128128

129-
(new Benchmark())
129+
Benchmark::start()
130130
->iterations(5)
131131
->compare(
132132
fn (int $iteration) => /* some code */,
@@ -143,7 +143,7 @@ summary information:
143143
```php
144144
use DragonCode\Benchmark\Benchmark;
145145

146-
(new Benchmark())
146+
Benchmark::start()
147147
->withoutData()
148148
->compare([
149149
'foo' => fn () => /* some code */,
@@ -183,7 +183,7 @@ For example:
183183
```php
184184
use DragonCode\Benchmark\Benchmark;
185185

186-
(new Benchmark())
186+
Benchmark::start()
187187
->iterations(5)
188188
->round(2)
189189
->compare(
@@ -222,7 +222,7 @@ There is a `prepare` method for this:
222222
```php
223223
use DragonCode\Benchmark\Benchmark;
224224

225-
(new Benchmark())
225+
Benchmark::start()
226226
->prepare(fn () => /* some code */)
227227
->compare(
228228
fn () => /* some code */,
@@ -236,7 +236,7 @@ inside the callback function.
236236
```php
237237
use DragonCode\Benchmark\Benchmark;
238238

239-
(new Benchmark())
239+
Benchmark::start()
240240
->prepare(fn (mixed $name, int $iteration) => /* some code */)
241241
->compare(
242242
fn () => /* some code */,
@@ -250,7 +250,7 @@ the input parameter:
250250
```php
251251
use DragonCode\Benchmark\Benchmark;
252252

253-
(new Benchmark())
253+
Benchmark::start()
254254
->prepare(fn (mixed $name, int $iteration) => /* some code */)
255255
->compare(
256256
fn (int $iteration, mixed $prepareResult) => /* some code */,

src/Benchmark.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function __construct(
4141
);
4242
}
4343

44+
public static function start(): static
45+
{
46+
return new static();
47+
}
48+
4449
public function prepare(callable $callback): self
4550
{
4651
$this->prepare = $callback;

tests/Benchmark/CreateTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Benchmark;
6+
7+
use DragonCode\Benchmark\Benchmark;
8+
use Tests\TestCase;
9+
10+
class CreateTest extends TestCase
11+
{
12+
public function testAsDynamic(): void
13+
{
14+
$this->assertInstanceOf(Benchmark::class, new Benchmark());
15+
}
16+
17+
public function testAsStatic(): void
18+
{
19+
$this->assertInstanceOf(Benchmark::class, Benchmark::start());
20+
}
21+
}

0 commit comments

Comments
 (0)