Skip to content

Commit f0225db

Browse files
committed
refactor(WithRunable): Update callable types to Closure for better type safety
- Change callable type hints to Closure for $pipe and $tap properties. - Update method signatures for withPipe and withTap to accept Closure. - Ensure type checks use instanceof for Closure instead of is_callable. Signed-off-by: guanguans <ityaozm@gmail.com>
1 parent 38433b9 commit f0225db

File tree

6 files changed

+29
-20
lines changed

6 files changed

+29
-20
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ $soar->withSudoPassword('your sudo password'); // With a sudo password to run th
514514

515515
### Or configure sudoers
516516

517-
> On higher versions of macOS, it is possible that the fingerprint authentication window will pop up. You can configure sudoers to run `soar` commands without a password.
517+
> On higher versions of macOS, it is possible that the fingerprint authentication window will pop up. You can configure sudoers to run `soar` command without password.
518518
519519
1. Edit Configuration file of sudoers:
520520

@@ -534,8 +534,8 @@ guanguans ALL=(ALL) NOPASSWD: /Users/guanguans/Documents/develop/soar-php/bin/so
534534
```shell
535535
composer benchmark
536536
composer checks:required
537-
composer soar:example-run
538-
composer soar:example-serve
537+
composer soar:usage-example-run
538+
composer soar:usage-example-serve
539539
composer test
540540
```
541541

composer-bump

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class ComposerBump
213213
},
214214
];
215215

216-
sort($dependencyVersions);
216+
sort($dependencyVersions, \SORT_NUMERIC);
217217

218218
$package['dependency_version'] = implode(' || ', $dependencyVersions);
219219
$carry[$package['name']] = $package;

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
"@php-cs-fixer:fix-dry-run-stop-on-violation",
215215
"@soar:check-binary",
216216
"@soar:dump-config",
217+
"@soar:usage-example-run",
217218
"@pest:bail",
218219
"@rector:process-dry-run",
219220
"@phpstan:analyse"
@@ -394,7 +395,7 @@
394395
"rule-doc-generator:rector-validate": "@rule-doc-generator validate src/Support/Rectors/",
395396
"sk": "@php vendor/bin/swiss-knife --ansi -vv",
396397
"sk:alice-yaml-fixtures-to-php": "@sk alice-yaml-fixtures-to-php --help",
397-
"sk:check-commented-code": "@sk check-commented-code examples/ src/ --line-limit=5 --skip-file=src/Soar.php",
398+
"sk:check-commented-code": "@sk check-commented-code examples/ src/ --line-limit=6 --skip-file=src/Soar.php",
398399
"sk:check-conflicts": "@sk check-conflicts examples/ src/",
399400
"sk:dump-editorconfig": "@sk dump-editorconfig",
400401
"sk:finalize-classes": "@sk finalize-classes examples/ src/",
@@ -428,13 +429,13 @@
428429
],
429430
"soar:dump-php-config-prototype": "Guanguans\\SoarPHP\\Support\\ComposerScripts::dumpSoarPHPConfig",
430431
"soar:dump-yaml-config": "Guanguans\\SoarPHP\\Support\\ComposerScripts::dumpSoarYamlConfig",
431-
"soar:example-run": [
432+
"soar:usage-example-run": [
432433
"echo 'examples/example.php:\n'",
433-
"@php examples/example.php"
434+
"@php examples/usage-example.php"
434435
],
435-
"soar:example-serve": [
436+
"soar:usage-example-serve": [
436437
"@composer-config:disable-process-timeout",
437-
"@php -S localhost:8123 examples/example.php"
438+
"@php -S localhost:8123 examples/usage-example.php"
438439
],
439440
"testbench": "@php vendor/bin/testbench --ansi",
440441
"testbench:build": "@testbench workbench:build",

src/Concerns/WithRunable.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@
2020
*/
2121
trait WithRunable
2222
{
23-
/** @var null|callable(Process): Process */
24-
protected $pipe;
23+
/** @var null|\Closure(Process): Process */
24+
protected ?\Closure $pipe = null;
2525

26-
/** @var null|callable(Process): void */
27-
protected $tap;
26+
/** @var null|\Closure(Process): void */
27+
protected ?\Closure $tap = null;
2828

29-
public function withPipe(?callable $pipe): self
29+
public function withPipe(?\Closure $pipe): self
3030
{
3131
$this->pipe = $pipe;
3232

3333
return $this;
3434
}
3535

36-
public function withTap(?callable $tap): self
36+
public function withTap(?\Closure $tap): self
3737
{
3838
$this->tap = $tap;
3939

@@ -59,10 +59,10 @@ protected function toProcess(): Process
5959
? new Process(command: ['sudo', '-S', ...$command], input: $this->getSudoPassword())
6060
: new Process($command);
6161

62-
if (\is_callable($this->tap)) {
63-
($this->tap)($process);
62+
if ($this->tap instanceof \Closure) {
63+
$this->tap->call($process, $process);
6464
}
6565

66-
return \is_callable($this->pipe) ? ($this->pipe)($process) : $process;
66+
return $this->pipe instanceof \Closure ? ($this->pipe)->call($process, $process) : $process;
6767
}
6868
}

tests/Concerns/WithRunableTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ protected function shouldApplySudoPassword(): bool
6262
it('can run soar process with pipe', function (): void {
6363
expect(Soar::make())
6464
->withVersion(true)
65-
->withPipe(static fn (Process $process): Process => $process->setTimeout(3))
65+
->withPipe(function (Process $process): Process {
66+
expect($this)->toBeInstanceOf(Process::class);
67+
expect($this->commandline)->toBeArray();
68+
69+
return $process->setTimeout(3);
70+
})
6671
->run(static function (string $type, string $line): void {
6772
// dump($type, $line);
6873
})
@@ -72,7 +77,10 @@ protected function shouldApplySudoPassword(): bool
7277
it('can run soar process with tap', function (): void {
7378
expect(Soar::make())
7479
->withVersion(true)
75-
->withTap(static function (Process $process): void {
80+
->withTap(function (Process $process): void {
81+
expect($this)->toBeInstanceOf(Process::class);
82+
expect($this->commandline)->toBeArray();
83+
7684
$process->setTimeout(3);
7785
})
7886
->run(static function (string $type, string $line): void {

0 commit comments

Comments
 (0)