Skip to content

Commit 56cc4ff

Browse files
jack-wormannicolas-grekas
authored andcommitted
[Process] Improve typing for process callback
1 parent c1d73b1 commit 56cc4ff

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

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):void)|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):void)|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: 26 additions & 17 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)|null
56+
*/
5457
private ?\Closure $callback = null;
5558
private array|string $commandline;
5659
private ?string $cwd;
@@ -231,8 +234,8 @@ 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
235-
* output available on STDOUT or STDERR
237+
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is somed
238+
* output available on STDOUT or STDERR
236239
*
237240
* @return int The exit status code
238241
*
@@ -257,6 +260,9 @@ 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):void)|null $callback A PHP callback to run whenever there is some
264+
* output available on STDOUT or STDERR
265+
*
260266
* @return $this
261267
*
262268
* @throws ProcessFailedException if the process didn't terminate successfully
@@ -284,8 +290,8 @@ public function mustRun(?callable $callback = null, array $env = []): static
284290
* the output in real-time while writing the standard input to the process.
285291
* It allows to have feedback from the independent process during execution.
286292
*
287-
* @param callable|null $callback A PHP callback to run whenever there is some
288-
* output available on STDOUT or STDERR
293+
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
294+
* output available on STDOUT or STDERR
289295
*
290296
* @throws ProcessStartFailedException When process can't be launched
291297
* @throws RuntimeException When process is already running
@@ -395,8 +401,8 @@ public function start(?callable $callback = null, array $env = []): void
395401
*
396402
* Be warned that the process is cloned before being started.
397403
*
398-
* @param callable|null $callback A PHP callback to run whenever there is some
399-
* output available on STDOUT or STDERR
404+
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
405+
* output available on STDOUT or STDERR
400406
*
401407
* @throws ProcessStartFailedException When process can't be launched
402408
* @throws RuntimeException When process is already running
@@ -424,7 +430,8 @@ public function restart(?callable $callback = null, array $env = []): static
424430
* from the output in real-time while writing the standard input to the process.
425431
* It allows to have feedback from the independent process during execution.
426432
*
427-
* @param callable|null $callback A valid PHP callback
433+
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
434+
* output available on STDOUT or STDERR
428435
*
429436
* @return int The exitcode of the process
430437
*
@@ -471,6 +478,9 @@ public function wait(?callable $callback = null): int
471478
* from the output in real-time while writing the standard input to the process.
472479
* It allows to have feedback from the independent process during execution.
473480
*
481+
* @param (callable('out'|'err', string):void)|null $callback A PHP callback to run whenever there is some
482+
* output available on STDOUT or STDERR
483+
*
474484
* @throws RuntimeException When process timed out
475485
* @throws LogicException When process is not yet started
476486
* @throws ProcessTimedOutException In case the timeout was reached
@@ -1291,22 +1301,21 @@ private function getDescriptors(bool $hasCallback): array
12911301
* The callbacks adds all occurred output to the specific buffer and calls
12921302
* the user callback (if present) with the received output.
12931303
*
1294-
* @param callable|null $callback The user defined PHP callback
1304+
* @param callable('out'|'err', string)|null $callback
1305+
*
1306+
* @return \Closure('out'|'err', string)
12951307
*/
12961308
protected function buildCallback(?callable $callback = null): \Closure
12971309
{
12981310
if ($this->outputDisabled) {
1299-
return fn ($type, $data): bool => null !== $callback && $callback($type, $data);
1311+
return fn ($type, $data) => null !== $callback && $callback($type, $data);
13001312
}
13011313

1302-
$out = self::OUT;
1303-
1304-
return function ($type, $data) use ($callback, $out): bool {
1305-
if ($out == $type) {
1306-
$this->addOutput($data);
1307-
} else {
1308-
$this->addErrorOutput($data);
1309-
}
1314+
return function (string $type, string $data) use ($callback): bool {
1315+
match ($type) {
1316+
self::OUT => $this->addOutput($data),
1317+
self::ERR => $this->addErrorOutput($data),
1318+
};
13101319

13111320
return null !== $callback && $callback($type, $data);
13121321
};

0 commit comments

Comments
 (0)