Skip to content

Commit ce2654d

Browse files
authored
Merge pull request #108 from clue-labs/nullable
Improve PHP 8.4+ support by avoiding implicitly nullable types
2 parents bdc37c7 + 6c640a4 commit ce2654d

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
"php": ">=5.3.0",
3030
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
3131
"react/event-loop": "^1.2",
32-
"react/stream": "^1.2"
32+
"react/stream": "^1.4"
3333
},
3434
"require-dev": {
3535
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36",
36-
"react/socket": "^1.8",
36+
"react/socket": "^1.16",
3737
"sebastian/environment": "^5.0 || ^3.0 || ^2.0 || ^1.0"
3838
},
3939
"autoload": {

src/Process.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,14 @@ class Process extends EventEmitter
103103
* @param null|array $fds File descriptors to allocate for this process (or null = default STDIO streams)
104104
* @throws \LogicException On windows or when proc_open() is not installed
105105
*/
106-
public function __construct($cmd, $cwd = null, array $env = null, array $fds = null)
106+
public function __construct($cmd, $cwd = null, $env = null, $fds = null)
107107
{
108+
if ($env !== null && !\is_array($env)) { // manual type check to support legacy PHP < 7.1
109+
throw new \InvalidArgumentException('Argument #3 ($env) expected null|array');
110+
}
111+
if ($fds !== null && !\is_array($fds)) { // manual type check to support legacy PHP < 7.1
112+
throw new \InvalidArgumentException('Argument #4 ($fds) expected null|array');
113+
}
108114
if (!\function_exists('proc_open')) {
109115
throw new \LogicException('The Process class relies on proc_open(), which is not available on your PHP installation.');
110116
}
@@ -154,8 +160,11 @@ public function __construct($cmd, $cwd = null, array $env = null, array $fds = n
154160
* @param float $interval Interval to periodically monitor process state (seconds)
155161
* @throws \RuntimeException If the process is already running or fails to start
156162
*/
157-
public function start(LoopInterface $loop = null, $interval = 0.1)
163+
public function start($loop = null, $interval = 0.1)
158164
{
165+
if ($loop !== null && !$loop instanceof LoopInterface) { // manual type check to support legacy PHP < 7.1
166+
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
167+
}
159168
if ($this->isRunning()) {
160169
throw new \RuntimeException('Process is already running');
161170
}

tests/AbstractProcessTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ abstract class AbstractProcessTest extends TestCase
1111
{
1212
abstract public function createLoop();
1313

14+
public function testCtorThrowsForInvalidEnv()
15+
{
16+
$this->setExpectedException('InvalidArgumentException', 'Argument #3 ($env) expected null|array');
17+
new Process('exit 0', null, 'env');
18+
}
19+
20+
public function testCtorThrowsForInvalidFds()
21+
{
22+
$this->setExpectedException('InvalidArgumentException', 'Argument #4 ($fds) expected null|array');
23+
new Process('exit 0', null, null, 'fds');
24+
}
25+
1426
public function testGetCommand()
1527
{
1628
$process = new Process('echo foo', null, null, array());
@@ -126,6 +138,14 @@ public function testStartWithCustomPipesWillAssignPipes()
126138
$this->assertInstanceOf('React\Stream\WritableStreamInterface', $process->pipes[3]);
127139
}
128140

141+
public function testStartWithInvalidLoopWillThrow()
142+
{
143+
$process = new Process('exit 0', null, null, array());
144+
145+
$this->setExpectedException('InvalidArgumentException', 'Argument #1 ($loop) expected null|React\EventLoop\LoopInterface');
146+
$process->start('loop');
147+
}
148+
129149
public function testStartWithInvalidFileDescriptorPathWillThrowWithoutCallingCustomErrorHandler()
130150
{
131151
if (defined('HHVM_VERSION')) {

0 commit comments

Comments
 (0)