Skip to content

Commit 9973636

Browse files
committed
bug #51478 [Process] Fix bug where $this->callback is never null, resulting in bad argument (weaverryan)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Process] Fix bug where $this->callback is never null, resulting in bad argument | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | None | License | MIT | Doc PR | Not needed Hi! Caused by #51068 - by the time `$this->getDescriptors()` is called, `$this->callback` is ALWAYS a callable thanks to `$this->callback = $this->buildCallback($callback)`. Previously, there was a separate `hasCallback` property that handled this logic. I chose to NOT re-add that because it was only needed in `getDescriptors()` and that is only called from this one place: easier to pass `$hasCallback` as an argument. Caught by the symfony/ux test suite ❤️ Cheers! Commits ------- 95aa23699f2 [Process] Fix bug where $this->callback is never null, resulting in bad argument
2 parents 7a98cdf + b8f2424 commit 9973636

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Process.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public function start(callable $callback = null, array $env = [])
304304
$this->resetProcessData();
305305
$this->starttime = $this->lastOutputTime = microtime(true);
306306
$this->callback = $this->buildCallback($callback);
307-
$descriptors = $this->getDescriptors();
307+
$descriptors = $this->getDescriptors(null !== $callback);
308308

309309
if ($this->env) {
310310
$env += '\\' === \DIRECTORY_SEPARATOR ? array_diff_ukey($this->env, $env, 'strcasecmp') : $this->env;
@@ -1240,15 +1240,15 @@ public static function isPtySupported(): bool
12401240
/**
12411241
* Creates the descriptors needed by the proc_open.
12421242
*/
1243-
private function getDescriptors(): array
1243+
private function getDescriptors(bool $hasCallback): array
12441244
{
12451245
if ($this->input instanceof \Iterator) {
12461246
$this->input->rewind();
12471247
}
12481248
if ('\\' === \DIRECTORY_SEPARATOR) {
1249-
$this->processPipes = new WindowsPipes($this->input, !$this->outputDisabled || $this->callback);
1249+
$this->processPipes = new WindowsPipes($this->input, !$this->outputDisabled || $hasCallback);
12501250
} else {
1251-
$this->processPipes = new UnixPipes($this->isTty(), $this->isPty(), $this->input, !$this->outputDisabled || $this->callback);
1251+
$this->processPipes = new UnixPipes($this->isTty(), $this->isPty(), $this->input, !$this->outputDisabled || $hasCallback);
12521252
}
12531253

12541254
return $this->processPipes->getDescriptors();

Tests/ProcessTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ public function testCallbacksAreExecutedWithStart()
200200
$this->assertSame('foo'.\PHP_EOL, $data);
201201
}
202202

203+
public function testReadSupportIsDisabledWithoutCallback()
204+
{
205+
$this->expectException(LogicException::class);
206+
$this->expectExceptionMessage('Pass the callback to the "Process::start" method or call enableOutput to use a callback with "Process::wait".');
207+
208+
$process = $this->getProcess('echo foo');
209+
// disabling output + not passing a callback to start() => read support disabled
210+
$process->disableOutput();
211+
$process->start();
212+
$process->wait(function ($type, $buffer) use (&$data) {
213+
$data .= $buffer;
214+
});
215+
}
216+
203217
/**
204218
* tests results from sub processes.
205219
*

0 commit comments

Comments
 (0)