From e07c964fbbaf0515c8025204e18400dcd664ba8f Mon Sep 17 00:00:00 2001 From: Eser DENIZ Date: Fri, 22 Nov 2024 22:26:33 +0100 Subject: [PATCH] fix: throw an exception if $cmd is not an indexed array --- src/ChildProcess.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/ChildProcess.php b/src/ChildProcess.php index 32cedbc..9293a1d 100644 --- a/src/ChildProcess.php +++ b/src/ChildProcess.php @@ -58,7 +58,7 @@ public function start( ?array $env = null, bool $persistent = false ): static { - $cmd = is_array($cmd) ? array_values($cmd) : [$cmd]; + $cmd = $this->ensureCmdIsAnIndexedArray($cmd); $process = $this->client->post('child-process/start', [ 'alias' => $alias, @@ -73,8 +73,8 @@ public function start( public function php(string|array $cmd, string $alias, ?array $env = null, ?bool $persistent = false): self { - $cmd = is_array($cmd) ? array_values($cmd) : [$cmd]; - + $cmd = $this->ensureCmdIsAnIndexedArray($cmd); + $process = $this->client->post('child-process/start-php', [ 'alias' => $alias, 'cmd' => $cmd, @@ -135,4 +135,15 @@ protected function fromRuntimeProcess($process): static return $this; } + + protected function ensureCmdIsAnIndexedArray(string|array $cmd): array + { + if (is_string($cmd)) { + return [$cmd]; + } + + if (array_keys($cmd) !== range(0, count($cmd) - 1)) { + throw new \InvalidArgumentException('Only indexed arrays are supported for the cmd: argument.'); + } + } }