Skip to content

Commit 71469bc

Browse files
committed
Improve-typing-for-process-callback
1 parent c1d73b1 commit 71469bc

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

src/Symfony/Component/Console/Command/DumpCompletionCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ private function tailDebugLog(string $commandName, OutputInterface $output): voi
123123
touch($debugFile);
124124
}
125125
$process = new Process(['tail', '-f', $debugFile], null, null, null, 0);
126-
$process->run(function (string $type, string $line) use ($output): void {
126+
$process->run(function (string $type, string $line) use ($output): true {
127127
$output->write($line);
128+
return true;
128129
});
129130
}
130131

src/Symfony/Component/Process/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66
---
77

88
* Add `RunProcessMessage::fromShellCommandline()` to instantiate a Process via the fromShellCommandline method
9+
* Improved typing of callbacks
910

1011
7.1
1112
---

src/Symfony/Component/Process/PhpProcess.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public static function fromShellCommandline(string $command, ?string $cwd = null
5555
throw new LogicException(\sprintf('The "%s()" method cannot be called when using "%s".', __METHOD__, self::class));
5656
}
5757

58+
/**
59+
* @param (callable('out'|'err', string):bool)|null $callback
60+
*/
5861
public function start(?callable $callback = null, array $env = []): void
5962
{
6063
if (null === $this->getCommandLine()) {

src/Symfony/Component/Process/PhpSubprocess.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public static function fromShellCommandline(string $command, ?string $cwd = null
7878
throw new LogicException(\sprintf('The "%s()" method cannot be called when using "%s".', __METHOD__, self::class));
7979
}
8080

81+
/**
82+
* @param (callable('out'|'err', string):bool)|null $callback
83+
*/
8184
public function start(?callable $callback = null, array $env = []): void
8285
{
8386
if (null === $this->getCommandLine()) {

src/Symfony/Component/Process/Process.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class Process implements \IteratorAggregate
5151
public const ITER_SKIP_OUT = 4; // Use this flag to skip STDOUT while iterating
5252
public const ITER_SKIP_ERR = 8; // Use this flag to skip STDERR while iterating
5353

54+
/**
55+
* @var (\Closure('out'|'err', string):bool)|null
56+
*/
5457
private ?\Closure $callback = null;
5558
private array|string $commandline;
5659
private ?string $cwd;
@@ -231,7 +234,7 @@ public function __clone()
231234
* The STDOUT and STDERR are also available after the process is finished
232235
* via the getOutput() and getErrorOutput() methods.
233236
*
234-
* @param callable|null $callback A PHP callback to run whenever there is some
237+
* @param (callable('out'|'err', string):bool)|null $callback A PHP callback to run whenever there is some
235238
* output available on STDOUT or STDERR
236239
*
237240
* @return int The exit status code
@@ -257,6 +260,8 @@ public function run(?callable $callback = null, array $env = []): int
257260
* This is identical to run() except that an exception is thrown if the process
258261
* exits with a non-zero exit code.
259262
*
263+
* @param (callable('out'|'err', string):bool)|null $callback
264+
*
260265
* @return $this
261266
*
262267
* @throws ProcessFailedException if the process didn't terminate successfully
@@ -284,7 +289,7 @@ public function mustRun(?callable $callback = null, array $env = []): static
284289
* the output in real-time while writing the standard input to the process.
285290
* It allows to have feedback from the independent process during execution.
286291
*
287-
* @param callable|null $callback A PHP callback to run whenever there is some
292+
* @param (callable('out'|'err', string):bool)|null $callback A PHP callback to run whenever there is some
288293
* output available on STDOUT or STDERR
289294
*
290295
* @throws ProcessStartFailedException When process can't be launched
@@ -395,7 +400,7 @@ public function start(?callable $callback = null, array $env = []): void
395400
*
396401
* Be warned that the process is cloned before being started.
397402
*
398-
* @param callable|null $callback A PHP callback to run whenever there is some
403+
* @param (callable('out'|'err', string):bool)|null $callback A PHP callback to run whenever there is some
399404
* output available on STDOUT or STDERR
400405
*
401406
* @throws ProcessStartFailedException When process can't be launched
@@ -424,7 +429,7 @@ public function restart(?callable $callback = null, array $env = []): static
424429
* from the output in real-time while writing the standard input to the process.
425430
* It allows to have feedback from the independent process during execution.
426431
*
427-
* @param callable|null $callback A valid PHP callback
432+
* @param (callable('out'|'err', string):bool)|null $callback A valid PHP callback
428433
*
429434
* @return int The exitcode of the process
430435
*
@@ -471,6 +476,8 @@ public function wait(?callable $callback = null): int
471476
* from the output in real-time while writing the standard input to the process.
472477
* It allows to have feedback from the independent process during execution.
473478
*
479+
* @param callable('out'|'err', string):bool $callback
480+
*
474481
* @throws RuntimeException When process timed out
475482
* @throws LogicException When process is not yet started
476483
* @throws ProcessTimedOutException In case the timeout was reached
@@ -1291,7 +1298,8 @@ private function getDescriptors(bool $hasCallback): array
12911298
* The callbacks adds all occurred output to the specific buffer and calls
12921299
* the user callback (if present) with the received output.
12931300
*
1294-
* @param callable|null $callback The user defined PHP callback
1301+
* @param (callable('out'|'err', string):bool)|null $callback The user defined PHP callback
1302+
* @return \Closure('out'|'err', string):bool
12951303
*/
12961304
protected function buildCallback(?callable $callback = null): \Closure
12971305
{
@@ -1301,15 +1309,19 @@ protected function buildCallback(?callable $callback = null): \Closure
13011309

13021310
$out = self::OUT;
13031311

1304-
return function ($type, $data) use ($callback, $out): bool {
1305-
if ($out == $type) {
1306-
$this->addOutput($data);
1307-
} else {
1308-
$this->addErrorOutput($data);
1309-
}
1312+
return
1313+
/**
1314+
* @param 'out'|'err' $type
1315+
*/
1316+
function (string $type, string $data) use ($callback, $out): bool {
1317+
if ($out == $type) {
1318+
$this->addOutput($data);
1319+
} else {
1320+
$this->addErrorOutput($data);
1321+
}
13101322

1311-
return null !== $callback && $callback($type, $data);
1312-
};
1323+
return null !== $callback && $callback($type, $data);
1324+
};
13131325
}
13141326

13151327
/**

0 commit comments

Comments
 (0)