Skip to content

Commit 31e5e9f

Browse files
xabbuhkeluniknicolas-grekas
authored
Use str_increment() when possible to increment non-numeric strings (#110)
Co-authored-by: Niklas Keller <me@kelunik.com> Co-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com>
1 parent bd97bc9 commit 31e5e9f

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

.php-cs-fixer.dist.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function getRules(): array
4040
'pcntl_signal_dispatch',
4141
'pcntl_signal',
4242
'posix_kill',
43+
'str_increment',
4344
'uv_loop_new',
4445
'uv_poll_start',
4546
'uv_poll_stop',

src/EventLoop/Internal/AbstractDriver.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function queue(\Closure $closure, mixed ...$args): void
141141

142142
public function defer(\Closure $closure): string
143143
{
144-
$deferCallback = new DeferCallback($this->nextId++, $closure);
144+
$deferCallback = new DeferCallback($this->callbackId(), $closure);
145145

146146
$this->callbacks[$deferCallback->id] = $deferCallback;
147147
$this->enableDeferQueue[$deferCallback->id] = $deferCallback;
@@ -155,7 +155,7 @@ public function delay(float $delay, \Closure $closure): string
155155
throw new \Error("Delay must be greater than or equal to zero");
156156
}
157157

158-
$timerCallback = new TimerCallback($this->nextId++, $delay, $closure, $this->now() + $delay);
158+
$timerCallback = new TimerCallback($this->callbackId(), $delay, $closure, $this->now() + $delay);
159159

160160
$this->callbacks[$timerCallback->id] = $timerCallback;
161161
$this->enableQueue[$timerCallback->id] = $timerCallback;
@@ -169,7 +169,7 @@ public function repeat(float $interval, \Closure $closure): string
169169
throw new \Error("Interval must be greater than or equal to zero");
170170
}
171171

172-
$timerCallback = new TimerCallback($this->nextId++, $interval, $closure, $this->now() + $interval, true);
172+
$timerCallback = new TimerCallback($this->callbackId(), $interval, $closure, $this->now() + $interval, true);
173173

174174
$this->callbacks[$timerCallback->id] = $timerCallback;
175175
$this->enableQueue[$timerCallback->id] = $timerCallback;
@@ -179,7 +179,7 @@ public function repeat(float $interval, \Closure $closure): string
179179

180180
public function onReadable(mixed $stream, \Closure $closure): string
181181
{
182-
$streamCallback = new StreamReadableCallback($this->nextId++, $closure, $stream);
182+
$streamCallback = new StreamReadableCallback($this->callbackId(), $closure, $stream);
183183

184184
$this->callbacks[$streamCallback->id] = $streamCallback;
185185
$this->enableQueue[$streamCallback->id] = $streamCallback;
@@ -189,7 +189,7 @@ public function onReadable(mixed $stream, \Closure $closure): string
189189

190190
public function onWritable($stream, \Closure $closure): string
191191
{
192-
$streamCallback = new StreamWritableCallback($this->nextId++, $closure, $stream);
192+
$streamCallback = new StreamWritableCallback($this->callbackId(), $closure, $stream);
193193

194194
$this->callbacks[$streamCallback->id] = $streamCallback;
195195
$this->enableQueue[$streamCallback->id] = $streamCallback;
@@ -199,7 +199,7 @@ public function onWritable($stream, \Closure $closure): string
199199

200200
public function onSignal(int $signal, \Closure $closure): string
201201
{
202-
$signalCallback = new SignalCallback($this->nextId++, $closure, $signal);
202+
$signalCallback = new SignalCallback($this->callbackId(), $closure, $signal);
203203

204204
$this->callbacks[$signalCallback->id] = $signalCallback;
205205
$this->enableQueue[$signalCallback->id] = $signalCallback;
@@ -640,6 +640,20 @@ private function createErrorCallback(): void
640640
};
641641
}
642642

643+
private function callbackId(): string
644+
{
645+
$callbackId = $this->nextId;
646+
647+
if (\PHP_VERSION_ID >= 80300) {
648+
/** @psalm-suppress UndefinedFunction */
649+
$this->nextId = \str_increment($this->nextId);
650+
} else {
651+
$this->nextId++;
652+
}
653+
654+
return $callbackId;
655+
}
656+
643657
final public function __serialize(): never
644658
{
645659
throw new \Error(__CLASS__ . ' does not support serialization');

0 commit comments

Comments
 (0)