Skip to content

Commit 6261012

Browse files
Merge pull request #49 from TheDragonCode/4.x
[4.x] Remove Symfony
2 parents 2aeb4ce + 8fab709 commit 6261012

29 files changed

Lines changed: 593 additions & 398 deletions

.github/workflows/tests.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,26 @@ jobs:
99
strategy:
1010
fail-fast: true
1111
matrix:
12-
php: [ "8.2", "8.3", "8.4" ]
13-
symfony: [ "6.0", "7.0" ]
12+
php: [ "8.2", "8.3", "8.4", "8.5" ]
1413

15-
name: PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }} ${{ matrix.prefer }}
14+
name: PHP ${{ matrix.php }}
1615

1716
steps:
18-
- name: Checkout code
19-
uses: actions/checkout@v6
17+
- uses: actions/checkout@v6
2018

2119
- name: Setup PHP
2220
uses: shivammathur/setup-php@v2
2321
with:
2422
php-version: ${{ matrix.php }}
25-
extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv
2623
coverage: none
2724

2825
- name: Install dependencies
2926
run: |
30-
composer require --no-interaction \
31-
symfony/console:^${{ matrix.symfony }} \
32-
symfony/var-dumper:^${{ matrix.symfony }}
27+
composer update \
28+
--no-audit \
29+
--no-suggest \
30+
--no-interaction
31+
3332
3433
- name: Execute tests
3534
run: composer test

UPGRADING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
- Метод `prepare` переименован в `beforeEach`
77
- Метод `start` переименован в `make`
88
- Исправлена типизация входящих колбэков с супер-класса `callable` на `Closure`
9+
- Класс `ValueIsNotCallableException` теперь принимает объект вместо строки типа.

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@
3838
],
3939
"require": {
4040
"php": "^8.2",
41-
"dragon-code/support": "^6.10",
42-
"symfony/console": "^6.0 || ^7.0"
41+
"dragon-code/support": "^6.10"
4342
},
4443
"require-dev": {
4544
"pestphp/pest": "^4.3",
46-
"symfony/var-dumper": "^6.0 || ^7.0"
45+
"symfony/var-dumper": "^7.0 || ^8.0"
4746
},
4847
"minimum-stability": "stable",
4948
"prefer-stable": true,

src/Benchmark.php

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66

77
use Closure;
88
use DragonCode\Benchmark\Exceptions\ValueIsNotCallableException;
9+
use DragonCode\Benchmark\Services\Callbacks;
910
use DragonCode\Benchmark\Services\Runner;
1011
use DragonCode\Benchmark\Services\View;
1112
use DragonCode\Benchmark\Transformers\Transformer;
12-
use Symfony\Component\Console\Helper\ProgressBar as ProgressBarService;
13-
use Symfony\Component\Console\Input\ArgvInput;
14-
use Symfony\Component\Console\Output\ConsoleOutput;
15-
use Symfony\Component\Console\Style\SymfonyStyle;
13+
use DragonCode\Benchmark\View\ProgressBarView;
1614

1715
use function count;
1816
use function func_get_args;
@@ -23,30 +21,19 @@
2321

2422
class Benchmark
2523
{
26-
protected View $view;
27-
2824
protected int $iterations = 100;
2925

30-
protected ?Closure $beforeEach = null;
31-
32-
protected ?Closure $afterEach = null;
33-
3426
protected array $result = [
3527
'each' => [],
3628
'total' => [],
3729
];
3830

3931
public function __construct(
4032
protected Runner $runner = new Runner,
41-
protected Transformer $transformer = new Transformer
42-
) {
43-
$this->view = new View(
44-
new SymfonyStyle(
45-
new ArgvInput,
46-
new ConsoleOutput
47-
)
48-
);
49-
}
33+
protected Transformer $transformer = new Transformer,
34+
protected View $view = new View,
35+
protected Callbacks $callbacks = new Callbacks,
36+
) {}
5037

5138
public static function make(): static
5239
{
@@ -55,14 +42,14 @@ public static function make(): static
5542

5643
public function beforeEach(Closure $callback): self
5744
{
58-
$this->beforeEach = $callback;
45+
$this->callbacks->beforeEach = $callback;
5946

6047
return $this;
6148
}
6249

6350
public function afterEach(Closure $callback): self
6451
{
65-
$this->afterEach = $callback;
52+
$this->callbacks->afterEach = $callback;
6653

6754
return $this;
6855
}
@@ -81,14 +68,23 @@ public function round(?int $precision): self
8168
return $this;
8269
}
8370

84-
public function compare(array|Closure ...$callbacks): void
71+
public function compare(array|Closure ...$callbacks): static
8572
{
8673
$values = is_array($callbacks[0]) ? $callbacks[0] : func_get_args();
8774

8875
$this->withProgress($values, $this->stepsCount($values));
8976
$this->show();
77+
78+
return $this;
9079
}
9180

81+
/**
82+
* @return \DragonCode\Benchmark\Data\ResultData[]
83+
*/
84+
public function toData(): array {}
85+
86+
public function toConsole(): void {}
87+
9288
protected function withProgress(array $callbacks, int $count): void
9389
{
9490
$bar = $this->view->progressBar()->create($count);
@@ -104,7 +100,7 @@ protected function stepsCount(array $callbacks): int
104100
return count($callbacks) * $this->iterations;
105101
}
106102

107-
protected function chunks(array $callbacks, ProgressBarService $progressBar): void
103+
protected function chunks(array $callbacks, ProgressBarView $progressBar): void
108104
{
109105
foreach ($callbacks as $name => $callback) {
110106
$this->validate($callback);
@@ -113,37 +109,28 @@ protected function chunks(array $callbacks, ProgressBarService $progressBar): vo
113109
}
114110
}
115111

116-
protected function each(mixed $name, Closure $callback, ProgressBarService $progressBar): void
112+
protected function each(mixed $name, Closure $callback, ProgressBarView $progressBar): void
117113
{
118114
$this->result['total'][$name] = $this->call(
119115
fn () => $this->run($name, $callback, $progressBar)
120116
);
121117
}
122118

123-
protected function run(mixed $name, Closure $callback, ProgressBarService $progressBar): void
119+
protected function run(mixed $name, Closure $callback, ProgressBarView $progressBar): void
124120
{
125121
for ($i = 1; $i <= $this->iterations; ++$i) {
126-
$result = $this->runCallback($this->beforeEach, $name, $i);
122+
$result = $this->callbacks->performBeforeEach($name, $i);
127123

128124
[$time, $ram] = $this->call($callback, [$i, $result]);
129125

130-
$this->runCallback($this->afterEach, $name, $i, $time, $ram);
126+
$this->callbacks->performAfterEach($name, $i, $time, $ram);
131127

132128
$this->push($name, $i, $time, $ram);
133129

134130
$progressBar->advance();
135131
}
136132
}
137133

138-
protected function runCallback(?Closure $callback, mixed ...$arguments): mixed
139-
{
140-
if (! $callback) {
141-
return null;
142-
}
143-
144-
return $callback(...$arguments);
145-
}
146-
147134
protected function call(Closure $callback, array $parameters = []): array
148135
{
149136
return $this->runner->call($callback, $parameters);

src/Exceptions/ValueIsNotCallableException.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use TypeError;
88

9+
use function gettype;
10+
911
class ValueIsNotCallableException extends TypeError
1012
{
11-
public function __construct(string $actualType)
13+
public function __construct(mixed $value)
1214
{
13-
parent::__construct(sprintf('The array value must be of type callable, %s given.', $actualType), 500);
15+
parent::__construct(sprintf('The array value must be of type Closure, %s given.', gettype($value)), 500);
1416
}
1517
}

src/Services/Callbacks.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Benchmark\Services;
6+
7+
use Closure;
8+
9+
class Callbacks
10+
{
11+
public ?Closure $before = null;
12+
13+
public ?Closure $beforeEach = null;
14+
15+
public ?Closure $after = null;
16+
17+
public ?Closure $afterEach = null;
18+
19+
public function performBefore(string|int $name): mixed
20+
{
21+
return $this->perform($this->before, $name);
22+
}
23+
24+
public function performBeforeEach(string|int $name, int $iteration): mixed
25+
{
26+
return $this->perform($this->beforeEach, $name, $iteration);
27+
}
28+
29+
public function performAfter(string|int $name): mixed
30+
{
31+
return $this->perform($this->after, $name);
32+
}
33+
34+
public function performAfterEach(string|int $name, int $iteration, float $time, float $ram): mixed
35+
{
36+
return $this->perform($this->afterEach, $name, $iteration, $time, $ram);
37+
}
38+
39+
protected function perform(?Closure $callback, mixed ...$args): mixed
40+
{
41+
if ($callback === null) {
42+
return null;
43+
}
44+
45+
return $callback(...$args);
46+
}
47+
}

src/Services/View.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace DragonCode\Benchmark\Services;
66

7-
use DragonCode\Benchmark\View\ProgressBar;
8-
use DragonCode\Benchmark\View\Table;
9-
use Symfony\Component\Console\Style\SymfonyStyle;
7+
use DragonCode\Benchmark\View\ProgressBarView;
8+
use DragonCode\Benchmark\View\TableView;
109

1110
use function is_array;
1211
use function is_numeric;
@@ -15,19 +14,13 @@
1514

1615
class View
1716
{
18-
protected Table $table;
19-
20-
protected ProgressBar $progressBar;
21-
2217
protected ?int $roundPrecision = null;
2318

2419
public function __construct(
25-
protected SymfonyStyle $io,
26-
protected Memory $memory = new Memory
27-
) {
28-
$this->table = new Table($this->io);
29-
$this->progressBar = new ProgressBar($this->io);
30-
}
20+
protected Memory $memory = new Memory,
21+
protected TableView $table = new TableView,
22+
protected ProgressBarView $progressBar = new ProgressBarView,
23+
) {}
3124

3225
public function setRound(?int $precision): void
3326
{
@@ -41,14 +34,14 @@ public function table(array $data): void
4134
);
4235
}
4336

44-
public function progressBar(): ProgressBar
37+
public function progressBar(): ProgressBarView
4538
{
4639
return $this->progressBar;
4740
}
4841

4942
public function emptyLine(int $count = 1): void
5043
{
51-
$this->io->newLine($count);
44+
echo "\r";
5245
}
5346

5447
protected function appendMs(array $data): array

src/Transformers/Separator.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace DragonCode\Benchmark\Transformers;
66

7-
use Symfony\Component\Console\Helper\TableSeparator;
8-
97
class Separator extends Base
108
{
119
public function transform(array $data): array

src/View/ProgressBar.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)