Skip to content

Commit 78a5d4e

Browse files
authored
Merge pull request #282 from WyriHaximus-labs/1.x-php-8.5
[1.x] Improve PHP 8.5+ support by avoiding deprecated method calls
2 parents cfc7ad3 + 81bd563 commit 78a5d4e

File tree

5 files changed

+64
-27
lines changed

5 files changed

+64
-27
lines changed

.github/workflows/ci.yml

Lines changed: 3 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
@@ -49,6 +50,7 @@ jobs:
4950
strategy:
5051
matrix:
5152
php:
53+
- 8.5
5254
- 8.4
5355
- 8.3
5456
- 8.2
@@ -113,6 +115,7 @@ jobs:
113115
strategy:
114116
matrix:
115117
php:
118+
- 8.5
116119
- 8.4
117120
- 8.3
118121
- 8.2

src/ExtEvLoop.php

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

146-
if ($timers->contains($timer)) {
146+
if ($timers->offsetExists($timer)) {
147147
$that->cancelTimer($timer);
148148
}
149149
};
150150

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

154154
return $timer;
155155
}
@@ -163,7 +163,7 @@ public function addPeriodicTimer($interval, $callback)
163163
};
164164

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

168168
return $timer;
169169
}
@@ -176,7 +176,7 @@ public function cancelTimer(TimerInterface $timer)
176176

177177
$event = $this->timers[$timer];
178178
$event->stop();
179-
$this->timers->detach($timer);
179+
$this->timers->offsetUnset($timer);
180180
}
181181

182182
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 = array();
@@ -157,9 +157,9 @@ public function addPeriodicTimer($interval, $callback)
157157

158158
public function cancelTimer(TimerInterface $timer)
159159
{
160-
if ($this->timerEvents->contains($timer)) {
160+
if ($this->timerEvents->offsetExists($timer)) {
161161
$this->timerEvents[$timer]->free();
162-
$this->timerEvents->detach($timer);
162+
$this->timerEvents->offsetUnset($timer);
163163
}
164164
}
165165

@@ -243,7 +243,7 @@ private function createTimerCallback()
243243
$this->timerCallback = function ($_, $__, $timer) use ($timers) {
244244
\call_user_func($timer->getCallback(), $timer);
245245

246-
if (!$timer->isPeriodic() && $timers->contains($timer)) {
246+
if (!$timer->isPeriodic() && $timers->offsetExists($timer)) {
247247
$this->cancelTimer($timer);
248248
}
249249
};

src/ExtUvLoop.php

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

122-
if ($timers->contains($timer)) {
122+
if ($timers->offsetExists($timer)) {
123123
$that->cancelTimer($timer);
124124
}
125125
};
126126

127127
$event = \uv_timer_init($this->uv);
128-
$this->timers->attach($timer, $event);
128+
$this->timers->offsetSet($timer, $event);
129129
\uv_timer_start(
130130
$event,
131131
$this->convertFloatSecondsToMilliseconds($interval),
@@ -149,7 +149,7 @@ public function addPeriodicTimer($interval, $callback)
149149

150150
$interval = $this->convertFloatSecondsToMilliseconds($interval);
151151
$event = \uv_timer_init($this->uv);
152-
$this->timers->attach($timer, $event);
152+
$this->timers->offsetSet($timer, $event);
153153
\uv_timer_start(
154154
$event,
155155
$interval,
@@ -167,7 +167,7 @@ public function cancelTimer(TimerInterface $timer)
167167
{
168168
if (isset($this->timers[$timer])) {
169169
@\uv_timer_stop($this->timers[$timer]);
170-
$this->timers->detach($timer);
170+
$this->timers->offsetUnset($timer);
171171
}
172172
}
173173

@@ -329,9 +329,17 @@ private function convertFloatSecondsToMilliseconds($interval)
329329
}
330330

331331
$maxValue = (int) (\PHP_INT_MAX / 1000);
332-
$intInterval = (int) $interval;
332+
$intervalOverflow = false;
333+
if (PHP_VERSION_ID > 80499 && $interval >= \PHP_INT_MAX + 1) {
334+
$intervalOverflow = true;
335+
} else {
336+
$intInterval = (int) $interval;
337+
if (($intInterval <= 0 && $interval > 1) || $intInterval >= $maxValue) {
338+
$intervalOverflow = true;
339+
}
340+
}
333341

334-
if (($intInterval <= 0 && $interval > 1) || $intInterval >= $maxValue) {
342+
if ($intervalOverflow) {
335343
throw new \InvalidArgumentException(
336344
"Interval overflow, value must be lower than '{$maxValue}', but '{$interval}' passed."
337345
);

tests/LoopTest.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public function testStaticAddReadStreamCallsAddReadStreamOnLoopInstance()
6464
public function testStaticAddReadStreamWithNoDefaultLoopCallsAddReadStreamOnNewLoopInstance()
6565
{
6666
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
67-
$ref->setAccessible(true);
67+
if (PHP_VERSION_ID < 80100) {
68+
$ref->setAccessible(true);
69+
}
6870
$ref->setValue(null, null);
6971

7072
$stream = stream_socket_server('127.0.0.1:0');
@@ -90,7 +92,9 @@ public function testStaticAddWriteStreamCallsAddWriteStreamOnLoopInstance()
9092
public function testStaticAddWriteStreamWithNoDefaultLoopCallsAddWriteStreamOnNewLoopInstance()
9193
{
9294
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
93-
$ref->setAccessible(true);
95+
if (PHP_VERSION_ID < 80100) {
96+
$ref->setAccessible(true);
97+
}
9498
$ref->setValue(null, null);
9599

96100
$stream = stream_socket_server('127.0.0.1:0');
@@ -115,7 +119,9 @@ public function testStaticRemoveReadStreamCallsRemoveReadStreamOnLoopInstance()
115119
public function testStaticRemoveReadStreamWithNoDefaultLoopIsNoOp()
116120
{
117121
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
118-
$ref->setAccessible(true);
122+
if (PHP_VERSION_ID < 80100) {
123+
$ref->setAccessible(true);
124+
}
119125
$ref->setValue(null, null);
120126

121127
$stream = tmpfile();
@@ -139,7 +145,9 @@ public function testStaticRemoveWriteStreamCallsRemoveWriteStreamOnLoopInstance(
139145
public function testStaticRemoveWriteStreamWithNoDefaultLoopIsNoOp()
140146
{
141147
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
142-
$ref->setAccessible(true);
148+
if (PHP_VERSION_ID < 80100) {
149+
$ref->setAccessible(true);
150+
}
143151
$ref->setValue(null, null);
144152

145153
$stream = tmpfile();
@@ -167,7 +175,9 @@ public function testStaticAddTimerCallsAddTimerOnLoopInstanceAndReturnsTimerInst
167175
public function testStaticAddTimerWithNoDefaultLoopCallsAddTimerOnNewLoopInstance()
168176
{
169177
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
170-
$ref->setAccessible(true);
178+
if (PHP_VERSION_ID < 80100) {
179+
$ref->setAccessible(true);
180+
}
171181
$ref->setValue(null, null);
172182

173183
$interval = 1.0;
@@ -197,7 +207,9 @@ public function testStaticAddPeriodicTimerCallsAddPeriodicTimerOnLoopInstanceAnd
197207
public function testStaticAddPeriodicTimerWithNoDefaultLoopCallsAddPeriodicTimerOnNewLoopInstance()
198208
{
199209
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
200-
$ref->setAccessible(true);
210+
if (PHP_VERSION_ID < 80100) {
211+
$ref->setAccessible(true);
212+
}
201213
$ref->setValue(null, null);
202214

203215
$interval = 1.0;
@@ -224,7 +236,9 @@ public function testStaticCancelTimerCallsCancelTimerOnLoopInstance()
224236
public function testStaticCancelTimerWithNoDefaultLoopIsNoOp()
225237
{
226238
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
227-
$ref->setAccessible(true);
239+
if (PHP_VERSION_ID < 80100) {
240+
$ref->setAccessible(true);
241+
}
228242
$ref->setValue(null, null);
229243

230244
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
@@ -248,7 +262,9 @@ public function testStaticFutureTickCallsFutureTickOnLoopInstance()
248262
public function testStaticFutureTickWithNoDefaultLoopCallsFutureTickOnNewLoopInstance()
249263
{
250264
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
251-
$ref->setAccessible(true);
265+
if (PHP_VERSION_ID < 80100) {
266+
$ref->setAccessible(true);
267+
}
252268
$ref->setValue(null, null);
253269

254270
$listener = function () { };
@@ -277,7 +293,9 @@ public function testStaticAddSignalWithNoDefaultLoopCallsAddSignalOnNewLoopInsta
277293
}
278294

279295
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
280-
$ref->setAccessible(true);
296+
if (PHP_VERSION_ID < 80100) {
297+
$ref->setAccessible(true);
298+
}
281299
$ref->setValue(null, null);
282300

283301
$signal = 1;
@@ -307,7 +325,9 @@ public function testStaticRemoveSignalCallsRemoveSignalOnLoopInstance()
307325
public function testStaticRemoveSignalWithNoDefaultLoopIsNoOp()
308326
{
309327
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
310-
$ref->setAccessible(true);
328+
if (PHP_VERSION_ID < 80100) {
329+
$ref->setAccessible(true);
330+
}
311331
$ref->setValue(null, null);
312332

313333
$signal = 1;
@@ -330,7 +350,9 @@ public function testStaticRunCallsRunOnLoopInstance()
330350
public function testStaticRunWithNoDefaultLoopCallsRunsOnNewLoopInstance()
331351
{
332352
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
333-
$ref->setAccessible(true);
353+
if (PHP_VERSION_ID < 80100) {
354+
$ref->setAccessible(true);
355+
}
334356
$ref->setValue(null, null);
335357

336358
Loop::run();
@@ -351,7 +373,9 @@ public function testStaticStopCallsStopOnLoopInstance()
351373
public function testStaticStopCallWithNoDefaultLoopIsNoOp()
352374
{
353375
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
354-
$ref->setAccessible(true);
376+
if (PHP_VERSION_ID < 80100) {
377+
$ref->setAccessible(true);
378+
}
355379
$ref->setValue(null, null);
356380

357381
Loop::stop();
@@ -366,7 +390,9 @@ public function testStaticStopCallWithNoDefaultLoopIsNoOp()
366390
public function unsetLoopFromLoopAccessor()
367391
{
368392
$ref = new \ReflectionProperty('React\EventLoop\Loop', 'instance');
369-
$ref->setAccessible(true);
393+
if (PHP_VERSION_ID < 80100) {
394+
$ref->setAccessible(true);
395+
}
370396
$ref->setValue(null, null);
371397
}
372398
}

0 commit comments

Comments
 (0)