Skip to content

Commit f4c53d9

Browse files
authored
Merge pull request #281 from WyriHaximus-labs/3.x-PHP8.5
[3.x] Improve PHP 8.5+ support by avoiding deprecated method calls
2 parents 2a20bf3 + 143445e commit f4c53d9

File tree

5 files changed

+65
-27
lines changed

5 files changed

+65
-27
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
php:
14+
- 8.5
1415
- 8.4
1516
- 8.3
1617
- 8.2
@@ -44,6 +45,8 @@ jobs:
4445
strategy:
4546
matrix:
4647
php:
48+
- 8.5
49+
- 8.4
4750
- 8.3
4851
- 8.2
4952
- 8.1
@@ -78,6 +81,7 @@ jobs:
7881
strategy:
7982
matrix:
8083
php:
84+
- 8.5
8185
- 8.4
8286
- 8.3
8387
- 8.2

src/ExtEvLoop.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ public function addTimer($interval, $callback)
141141
$callback = function () use ($timer) {
142142
\call_user_func($timer->getCallback(), $timer);
143143

144-
if ($this->timers->contains($timer)) {
144+
if ($this->timers->offsetExists($timer)) {
145145
$this->cancelTimer($timer);
146146
}
147147
};
148148

149149
$event = $this->loop->timer($timer->getInterval(), 0.0, $callback);
150-
$this->timers->attach($timer, $event);
150+
$this->timers->offsetSet($timer, $event);
151151

152152
return $timer;
153153
}
@@ -161,7 +161,7 @@ public function addPeriodicTimer($interval, $callback)
161161
};
162162

163163
$event = $this->loop->timer($timer->getInterval(), $timer->getInterval(), $callback);
164-
$this->timers->attach($timer, $event);
164+
$this->timers->offsetSet($timer, $event);
165165

166166
return $timer;
167167
}
@@ -174,7 +174,7 @@ public function cancelTimer(TimerInterface $timer)
174174

175175
$event = $this->timers[$timer];
176176
$event->stop();
177-
$this->timers->detach($timer);
177+
$this->timers->offsetUnset($timer);
178178
}
179179

180180
public function futureTick($listener)

src/ExtEventLoop.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function __destruct()
6464
{
6565
// explicitly clear all references to Event objects to prevent SEGFAULTs on Windows
6666
foreach ($this->timerEvents as $timer) {
67-
$this->timerEvents->detach($timer);
67+
$this->timerEvents->offsetUnset($timer);
6868
}
6969

7070
$this->readEvents = [];
@@ -153,9 +153,9 @@ public function addPeriodicTimer($interval, $callback)
153153

154154
public function cancelTimer(TimerInterface $timer)
155155
{
156-
if ($this->timerEvents->contains($timer)) {
156+
if ($this->timerEvents->offsetExists($timer)) {
157157
$this->timerEvents[$timer]->free();
158-
$this->timerEvents->detach($timer);
158+
$this->timerEvents->offsetUnset($timer);
159159
}
160160
}
161161

@@ -238,7 +238,7 @@ private function createTimerCallback()
238238
$this->timerCallback = function ($_, $__, $timer) {
239239
\call_user_func($timer->getCallback(), $timer);
240240

241-
if (!$timer->isPeriodic() && $this->timerEvents->contains($timer)) {
241+
if (!$timer->isPeriodic() && $this->timerEvents->offsetExists($timer)) {
242242
$this->cancelTimer($timer);
243243
}
244244
};

src/ExtUvLoop.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ public function addTimer($interval, $callback)
117117
$callback = function () use ($timer) {
118118
\call_user_func($timer->getCallback(), $timer);
119119

120-
if ($this->timers->contains($timer)) {
120+
if ($this->timers->offsetExists($timer)) {
121121
$this->cancelTimer($timer);
122122
}
123123
};
124124

125125
$event = \uv_timer_init($this->uv);
126-
$this->timers->attach($timer, $event);
126+
$this->timers->offsetSet($timer, $event);
127127
\uv_timer_start(
128128
$event,
129129
$this->convertFloatSecondsToMilliseconds($interval),
@@ -147,7 +147,7 @@ public function addPeriodicTimer($interval, $callback)
147147

148148
$interval = $this->convertFloatSecondsToMilliseconds($interval);
149149
$event = \uv_timer_init($this->uv);
150-
$this->timers->attach($timer, $event);
150+
$this->timers->offsetSet($timer, $event);
151151
\uv_timer_start(
152152
$event,
153153
$interval,
@@ -165,7 +165,7 @@ public function cancelTimer(TimerInterface $timer)
165165
{
166166
if (isset($this->timers[$timer])) {
167167
@\uv_timer_stop($this->timers[$timer]);
168-
$this->timers->detach($timer);
168+
$this->timers->offsetUnset($timer);
169169
}
170170
}
171171

@@ -326,9 +326,17 @@ private function convertFloatSecondsToMilliseconds($interval)
326326
}
327327

328328
$maxValue = (int) (\PHP_INT_MAX / 1000);
329-
$intInterval = (int) $interval;
329+
$intervalOverflow = false;
330+
if (PHP_VERSION_ID > 80499 && $interval >= \PHP_INT_MAX + 1) {
331+
$intervalOverflow = true;
332+
} else {
333+
$intInterval = (int) $interval;
334+
if (($intInterval <= 0 && $interval > 1) || $intInterval >= $maxValue) {
335+
$intervalOverflow = true;
336+
}
337+
}
330338

331-
if (($intInterval <= 0 && $interval > 1) || $intInterval >= $maxValue) {
339+
if ($intervalOverflow) {
332340
throw new \InvalidArgumentException(
333341
"Interval overflow, value must be lower than '{$maxValue}', but '{$interval}' passed."
334342
);

tests/LoopTest.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public function testStaticAddReadStreamCallsAddReadStreamOnLoopInstance()
4242
public function testStaticAddReadStreamWithNoDefaultLoopCallsAddReadStreamOnNewLoopInstance()
4343
{
4444
$ref = new \ReflectionProperty(Loop::class, 'instance');
45-
$ref->setAccessible(true);
45+
if (PHP_VERSION_ID < 80100) {
46+
$ref->setAccessible(true);
47+
}
4648
$ref->setValue(null, null);
4749

4850
$stream = stream_socket_server('127.0.0.1:0');
@@ -68,7 +70,9 @@ public function testStaticAddWriteStreamCallsAddWriteStreamOnLoopInstance()
6870
public function testStaticAddWriteStreamWithNoDefaultLoopCallsAddWriteStreamOnNewLoopInstance()
6971
{
7072
$ref = new \ReflectionProperty(Loop::class, 'instance');
71-
$ref->setAccessible(true);
73+
if (PHP_VERSION_ID < 80100) {
74+
$ref->setAccessible(true);
75+
}
7276
$ref->setValue(null, null);
7377

7478
$stream = stream_socket_server('127.0.0.1:0');
@@ -93,7 +97,9 @@ public function testStaticRemoveReadStreamCallsRemoveReadStreamOnLoopInstance()
9397
public function testStaticRemoveReadStreamWithNoDefaultLoopIsNoOp()
9498
{
9599
$ref = new \ReflectionProperty(Loop::class, 'instance');
96-
$ref->setAccessible(true);
100+
if (PHP_VERSION_ID < 80100) {
101+
$ref->setAccessible(true);
102+
}
97103
$ref->setValue(null, null);
98104

99105
$stream = tmpfile();
@@ -117,7 +123,9 @@ public function testStaticRemoveWriteStreamCallsRemoveWriteStreamOnLoopInstance(
117123
public function testStaticRemoveWriteStreamWithNoDefaultLoopIsNoOp()
118124
{
119125
$ref = new \ReflectionProperty(Loop::class, 'instance');
120-
$ref->setAccessible(true);
126+
if (PHP_VERSION_ID < 80100) {
127+
$ref->setAccessible(true);
128+
}
121129
$ref->setValue(null, null);
122130

123131
$stream = tmpfile();
@@ -145,7 +153,9 @@ public function testStaticAddTimerCallsAddTimerOnLoopInstanceAndReturnsTimerInst
145153
public function testStaticAddTimerWithNoDefaultLoopCallsAddTimerOnNewLoopInstance()
146154
{
147155
$ref = new \ReflectionProperty(Loop::class, 'instance');
148-
$ref->setAccessible(true);
156+
if (PHP_VERSION_ID < 80100) {
157+
$ref->setAccessible(true);
158+
}
149159
$ref->setValue(null, null);
150160

151161
$interval = 1.0;
@@ -175,7 +185,9 @@ public function testStaticAddPeriodicTimerCallsAddPeriodicTimerOnLoopInstanceAnd
175185
public function testStaticAddPeriodicTimerWithNoDefaultLoopCallsAddPeriodicTimerOnNewLoopInstance()
176186
{
177187
$ref = new \ReflectionProperty(Loop::class, 'instance');
178-
$ref->setAccessible(true);
188+
if (PHP_VERSION_ID < 80100) {
189+
$ref->setAccessible(true);
190+
}
179191
$ref->setValue(null, null);
180192

181193
$interval = 1.0;
@@ -202,7 +214,9 @@ public function testStaticCancelTimerCallsCancelTimerOnLoopInstance()
202214
public function testStaticCancelTimerWithNoDefaultLoopIsNoOp()
203215
{
204216
$ref = new \ReflectionProperty(Loop::class, 'instance');
205-
$ref->setAccessible(true);
217+
if (PHP_VERSION_ID < 80100) {
218+
$ref->setAccessible(true);
219+
}
206220
$ref->setValue(null, null);
207221

208222
$timer = $this->createMock(TimerInterface::class);
@@ -226,7 +240,9 @@ public function testStaticFutureTickCallsFutureTickOnLoopInstance()
226240
public function testStaticFutureTickWithNoDefaultLoopCallsFutureTickOnNewLoopInstance()
227241
{
228242
$ref = new \ReflectionProperty(Loop::class, 'instance');
229-
$ref->setAccessible(true);
243+
if (PHP_VERSION_ID < 80100) {
244+
$ref->setAccessible(true);
245+
}
230246
$ref->setValue(null, null);
231247

232248
$listener = function () { };
@@ -255,7 +271,9 @@ public function testStaticAddSignalWithNoDefaultLoopCallsAddSignalOnNewLoopInsta
255271
}
256272

257273
$ref = new \ReflectionProperty(Loop::class, 'instance');
258-
$ref->setAccessible(true);
274+
if (PHP_VERSION_ID < 80100) {
275+
$ref->setAccessible(true);
276+
}
259277
$ref->setValue(null, null);
260278

261279
$signal = 1;
@@ -285,7 +303,9 @@ public function testStaticRemoveSignalCallsRemoveSignalOnLoopInstance()
285303
public function testStaticRemoveSignalWithNoDefaultLoopIsNoOp()
286304
{
287305
$ref = new \ReflectionProperty(Loop::class, 'instance');
288-
$ref->setAccessible(true);
306+
if (PHP_VERSION_ID < 80100) {
307+
$ref->setAccessible(true);
308+
}
289309
$ref->setValue(null, null);
290310

291311
$signal = 1;
@@ -308,7 +328,9 @@ public function testStaticRunCallsRunOnLoopInstance()
308328
public function testStaticRunWithNoDefaultLoopCallsRunsOnNewLoopInstance()
309329
{
310330
$ref = new \ReflectionProperty(Loop::class, 'instance');
311-
$ref->setAccessible(true);
331+
if (PHP_VERSION_ID < 80100) {
332+
$ref->setAccessible(true);
333+
}
312334
$ref->setValue(null, null);
313335

314336
Loop::run();
@@ -329,7 +351,9 @@ public function testStaticStopCallsStopOnLoopInstance()
329351
public function testStaticStopCallWithNoDefaultLoopIsNoOp()
330352
{
331353
$ref = new \ReflectionProperty(Loop::class, 'instance');
332-
$ref->setAccessible(true);
354+
if (PHP_VERSION_ID < 80100) {
355+
$ref->setAccessible(true);
356+
}
333357
$ref->setValue(null, null);
334358

335359
Loop::stop();
@@ -344,7 +368,9 @@ public function testStaticStopCallWithNoDefaultLoopIsNoOp()
344368
public function unsetLoopFromLoopAccessor()
345369
{
346370
$ref = new \ReflectionProperty(Loop::class, 'instance');
347-
$ref->setAccessible(true);
371+
if (PHP_VERSION_ID < 80100) {
372+
$ref->setAccessible(true);
373+
}
348374
$ref->setValue(null, null);
349375
}
350376
}

0 commit comments

Comments
 (0)