From 8596d8721b023583843331312974dd8df36bdb11 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 25 Aug 2025 09:15:35 +0200 Subject: [PATCH 1/4] use str_increment() when possible to increment non-numeric strings --- src/EventLoop/Internal/AbstractDriver.php | 25 +++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/EventLoop/Internal/AbstractDriver.php b/src/EventLoop/Internal/AbstractDriver.php index 94de316..910bb27 100644 --- a/src/EventLoop/Internal/AbstractDriver.php +++ b/src/EventLoop/Internal/AbstractDriver.php @@ -141,7 +141,7 @@ public function queue(\Closure $closure, mixed ...$args): void public function defer(\Closure $closure): string { - $deferCallback = new DeferCallback($this->nextId++, $closure); + $deferCallback = new DeferCallback($this->callbackId(), $closure); $this->callbacks[$deferCallback->id] = $deferCallback; $this->enableDeferQueue[$deferCallback->id] = $deferCallback; @@ -155,7 +155,7 @@ public function delay(float $delay, \Closure $closure): string throw new \Error("Delay must be greater than or equal to zero"); } - $timerCallback = new TimerCallback($this->nextId++, $delay, $closure, $this->now() + $delay); + $timerCallback = new TimerCallback($this->callbackId(), $delay, $closure, $this->now() + $delay); $this->callbacks[$timerCallback->id] = $timerCallback; $this->enableQueue[$timerCallback->id] = $timerCallback; @@ -169,7 +169,7 @@ public function repeat(float $interval, \Closure $closure): string throw new \Error("Interval must be greater than or equal to zero"); } - $timerCallback = new TimerCallback($this->nextId++, $interval, $closure, $this->now() + $interval, true); + $timerCallback = new TimerCallback($this->callbackId(), $interval, $closure, $this->now() + $interval, true); $this->callbacks[$timerCallback->id] = $timerCallback; $this->enableQueue[$timerCallback->id] = $timerCallback; @@ -179,7 +179,7 @@ public function repeat(float $interval, \Closure $closure): string public function onReadable(mixed $stream, \Closure $closure): string { - $streamCallback = new StreamReadableCallback($this->nextId++, $closure, $stream); + $streamCallback = new StreamReadableCallback($this->callbackId(), $closure, $stream); $this->callbacks[$streamCallback->id] = $streamCallback; $this->enableQueue[$streamCallback->id] = $streamCallback; @@ -189,7 +189,7 @@ public function onReadable(mixed $stream, \Closure $closure): string public function onWritable($stream, \Closure $closure): string { - $streamCallback = new StreamWritableCallback($this->nextId++, $closure, $stream); + $streamCallback = new StreamWritableCallback($this->callbackId(), $closure, $stream); $this->callbacks[$streamCallback->id] = $streamCallback; $this->enableQueue[$streamCallback->id] = $streamCallback; @@ -199,7 +199,7 @@ public function onWritable($stream, \Closure $closure): string public function onSignal(int $signal, \Closure $closure): string { - $signalCallback = new SignalCallback($this->nextId++, $closure, $signal); + $signalCallback = new SignalCallback($this->callbackId(), $closure, $signal); $this->callbacks[$signalCallback->id] = $signalCallback; $this->enableQueue[$signalCallback->id] = $signalCallback; @@ -640,6 +640,19 @@ private function createErrorCallback(): void }; } + private function callbackId(): string + { + $callbackId = $this->nextId; + + if (\PHP_VERSION_ID >= 80300) { + $this->nextId = str_increment($this->nextId); + } else { + $this->nextId++; + } + + return $callbackId; + } + final public function __serialize(): never { throw new \Error(__CLASS__ . ' does not support serialization'); From d0d5a90866782dc2d7953af9cf87d32c99dfa5a6 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Wed, 27 Aug 2025 09:35:26 +0200 Subject: [PATCH 2/4] Fix code style --- src/EventLoop/Internal/AbstractDriver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventLoop/Internal/AbstractDriver.php b/src/EventLoop/Internal/AbstractDriver.php index 910bb27..7398ad9 100644 --- a/src/EventLoop/Internal/AbstractDriver.php +++ b/src/EventLoop/Internal/AbstractDriver.php @@ -645,7 +645,7 @@ private function callbackId(): string $callbackId = $this->nextId; if (\PHP_VERSION_ID >= 80300) { - $this->nextId = str_increment($this->nextId); + $this->nextId = \str_increment($this->nextId); } else { $this->nextId++; } From 23182c5fff62996c04fcab7695005962b67d1377 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Wed, 27 Aug 2025 09:40:37 +0200 Subject: [PATCH 3/4] Suppress psalm error for str_increment --- src/EventLoop/Internal/AbstractDriver.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/EventLoop/Internal/AbstractDriver.php b/src/EventLoop/Internal/AbstractDriver.php index 7398ad9..986071e 100644 --- a/src/EventLoop/Internal/AbstractDriver.php +++ b/src/EventLoop/Internal/AbstractDriver.php @@ -645,6 +645,7 @@ private function callbackId(): string $callbackId = $this->nextId; if (\PHP_VERSION_ID >= 80300) { + /** @psalm-suppress UndefinedFunction */ $this->nextId = \str_increment($this->nextId); } else { $this->nextId++; From 0bc51f509092962a1e85b6e482855ef9538c0d8a Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Wed, 27 Aug 2025 11:24:54 +0200 Subject: [PATCH 4/4] Fix code style settings --- .php-cs-fixer.dist.php | 1 + 1 file changed, 1 insertion(+) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 81766c9..b4432f7 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -40,6 +40,7 @@ public function getRules(): array 'pcntl_signal_dispatch', 'pcntl_signal', 'posix_kill', + 'str_increment', 'uv_loop_new', 'uv_poll_start', 'uv_poll_stop',