Skip to content

Commit bdb1d93

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 bdb1d93

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@
394394
"rule-doc-generator:rector-validate": "@rule-doc-generator validate src/Support/Rectors/",
395395
"sk": "@php vendor/bin/swiss-knife --ansi -vv",
396396
"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",
397+
"sk:check-commented-code": "@sk check-commented-code examples/ src/ --line-limit=6 --skip-file=src/Soar.php",
398398
"sk:check-conflicts": "@sk check-conflicts examples/ src/",
399399
"sk:dump-editorconfig": "@sk dump-editorconfig",
400400
"sk:finalize-classes": "@sk finalize-classes examples/ src/",

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)