Skip to content

Commit 7fa1b76

Browse files
committed
Update PHP language syntax and remove legacy workarounds
1 parent ec578d4 commit 7fa1b76

File tree

9 files changed

+44
-84
lines changed

9 files changed

+44
-84
lines changed

src/ExtEvLoop.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ class ExtEvLoop implements LoopInterface
4141
/**
4242
* @var EvIo[]
4343
*/
44-
private $readStreams = array();
44+
private $readStreams = [];
4545

4646
/**
4747
* @var EvIo[]
4848
*/
49-
private $writeStreams = array();
49+
private $writeStreams = [];
5050

5151
/**
5252
* @var bool
@@ -61,7 +61,7 @@ class ExtEvLoop implements LoopInterface
6161
/**
6262
* @var \EvSignal[]
6363
*/
64-
private $signalEvents = array();
64+
private $signalEvents = [];
6565

6666
public function __construct()
6767
{
@@ -138,13 +138,12 @@ public function addTimer($interval, $callback)
138138
{
139139
$timer = new Timer($interval, $callback, false);
140140

141-
$that = $this;
142141
$timers = $this->timers;
143-
$callback = function () use ($timer, $timers, $that) {
142+
$callback = function () use ($timer, $timers) {
144143
\call_user_func($timer->getCallback(), $timer);
145144

146145
if ($timers->contains($timer)) {
147-
$that->cancelTimer($timer);
146+
$this->cancelTimer($timer);
148147
}
149148
};
150149

src/ExtEventLoop.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ final class ExtEventLoop implements LoopInterface
2727
private $timerCallback;
2828
private $timerEvents;
2929
private $streamCallback;
30-
private $readEvents = array();
31-
private $writeEvents = array();
32-
private $readListeners = array();
33-
private $writeListeners = array();
34-
private $readRefs = array();
35-
private $writeRefs = array();
30+
private $readEvents = [];
31+
private $writeEvents = [];
32+
private $readListeners = [];
33+
private $writeListeners = [];
34+
private $readRefs = [];
35+
private $writeRefs = [];
3636
private $running;
3737
private $signals;
38-
private $signalEvents = array();
38+
private $signalEvents = [];
3939

4040
public function __construct()
4141
{
@@ -67,8 +67,8 @@ public function __destruct()
6767
$this->timerEvents->detach($timer);
6868
}
6969

70-
$this->readEvents = array();
71-
$this->writeEvents = array();
70+
$this->readEvents = [];
71+
$this->writeEvents = [];
7272
}
7373

7474
public function addReadStream($stream, $listener)
@@ -85,9 +85,7 @@ public function addReadStream($stream, $listener)
8585

8686
// ext-event does not increase refcount on stream resources for PHP 7+
8787
// manually keep track of stream resource to prevent premature garbage collection
88-
if (\PHP_VERSION_ID >= 70000) {
89-
$this->readRefs[$key] = $stream;
90-
}
88+
$this->readRefs[$key] = $stream;
9189
}
9290

9391
public function addWriteStream($stream, $listener)
@@ -104,9 +102,7 @@ public function addWriteStream($stream, $listener)
104102

105103
// ext-event does not increase refcount on stream resources for PHP 7+
106104
// manually keep track of stream resource to prevent premature garbage collection
107-
if (\PHP_VERSION_ID >= 70000) {
108-
$this->writeRefs[$key] = $stream;
109-
}
105+
$this->writeRefs[$key] = $stream;
110106
}
111107

112108
public function removeReadStream($stream)
@@ -173,7 +169,7 @@ public function addSignal($signal, $listener)
173169
$this->signals->add($signal, $listener);
174170

175171
if (!isset($this->signalEvents[$signal])) {
176-
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, array($this->signals, 'call'));
172+
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, [$this->signals, 'call']);
177173
$this->signalEvents[$signal]->add();
178174
}
179175
}

src/ExtUvLoop.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ final class ExtUvLoop implements LoopInterface
2222
private $uv;
2323
private $futureTickQueue;
2424
private $timers;
25-
private $streamEvents = array();
26-
private $readStreams = array();
27-
private $writeStreams = array();
25+
private $streamEvents = [];
26+
private $readStreams = [];
27+
private $writeStreams = [];
2828
private $running;
2929
private $signals;
30-
private $signalEvents = array();
30+
private $signalEvents = [];
3131
private $streamListener;
3232

3333
public function __construct()
@@ -114,13 +114,12 @@ public function addTimer($interval, $callback)
114114
{
115115
$timer = new Timer($interval, $callback, false);
116116

117-
$that = $this;
118117
$timers = $this->timers;
119-
$callback = function () use ($timer, $timers, $that) {
118+
$callback = function () use ($timer, $timers) {
120119
\call_user_func($timer->getCallback(), $timer);
121120

122121
if ($timers->contains($timer)) {
123-
$that->cancelTimer($timer);
122+
$this->cancelTimer($timer);
124123
}
125124
};
126125

src/Loop.php

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ public static function set(LoopInterface $loop)
8383
*/
8484
public static function addReadStream($stream, $listener)
8585
{
86-
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
87-
if (self::$instance === null) {
88-
self::get();
89-
}
90-
self::$instance->addReadStream($stream, $listener);
86+
(self::$instance ?? self::get())->addReadStream($stream, $listener);
9187
}
9288

9389
/**
@@ -101,11 +97,7 @@ public static function addReadStream($stream, $listener)
10197
*/
10298
public static function addWriteStream($stream, $listener)
10399
{
104-
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
105-
if (self::$instance === null) {
106-
self::get();
107-
}
108-
self::$instance->addWriteStream($stream, $listener);
100+
(self::$instance ?? self::get())->addWriteStream($stream, $listener);
109101
}
110102

111103
/**
@@ -146,11 +138,7 @@ public static function removeWriteStream($stream)
146138
*/
147139
public static function addTimer($interval, $callback)
148140
{
149-
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
150-
if (self::$instance === null) {
151-
self::get();
152-
}
153-
return self::$instance->addTimer($interval, $callback);
141+
return (self::$instance ?? self::get())->addTimer($interval, $callback);
154142
}
155143

156144
/**
@@ -163,11 +151,7 @@ public static function addTimer($interval, $callback)
163151
*/
164152
public static function addPeriodicTimer($interval, $callback)
165153
{
166-
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
167-
if (self::$instance === null) {
168-
self::get();
169-
}
170-
return self::$instance->addPeriodicTimer($interval, $callback);
154+
return (self::$instance ?? self::get())->addPeriodicTimer($interval, $callback);
171155
}
172156

173157
/**
@@ -193,12 +177,7 @@ public static function cancelTimer(TimerInterface $timer)
193177
*/
194178
public static function futureTick($listener)
195179
{
196-
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
197-
if (self::$instance === null) {
198-
self::get();
199-
}
200-
201-
self::$instance->futureTick($listener);
180+
(self::$instance ?? self::get())->futureTick($listener);
202181
}
203182

204183
/**
@@ -211,12 +190,7 @@ public static function futureTick($listener)
211190
*/
212191
public static function addSignal($signal, $listener)
213192
{
214-
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
215-
if (self::$instance === null) {
216-
self::get();
217-
}
218-
219-
self::$instance->addSignal($signal, $listener);
193+
(self::$instance ?? self::get())->addSignal($signal, $listener);
220194
}
221195

222196
/**
@@ -242,12 +216,7 @@ public static function removeSignal($signal, $listener)
242216
*/
243217
public static function run()
244218
{
245-
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
246-
if (self::$instance === null) {
247-
self::get();
248-
}
249-
250-
self::$instance->run();
219+
(self::$instance ?? self::get())->run();
251220
}
252221

253222
/**

src/SignalsHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
*/
88
final class SignalsHandler
99
{
10-
private $signals = array();
10+
private $signals = [];
1111

1212
public function add($signal, $listener)
1313
{
1414
if (!isset($this->signals[$signal])) {
15-
$this->signals[$signal] = array();
15+
$this->signals[$signal] = [];
1616
}
1717

1818
if (\in_array($listener, $this->signals[$signal])) {

src/StreamSelectLoop.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ final class StreamSelectLoop implements LoopInterface
5656

5757
private $futureTickQueue;
5858
private $timers;
59-
private $readStreams = array();
60-
private $readListeners = array();
61-
private $writeStreams = array();
62-
private $writeListeners = array();
59+
private $readStreams = [];
60+
private $readListeners = [];
61+
private $writeStreams = [];
62+
private $writeListeners = [];
6363
private $running;
6464
private $pcntl = false;
6565
private $pcntlPoll = false;
@@ -157,7 +157,7 @@ public function addSignal($signal, $listener)
157157
$this->signals->add($signal, $listener);
158158

159159
if ($first) {
160-
\pcntl_signal($signal, array($this->signals, 'call'));
160+
\pcntl_signal($signal, [$this->signals, 'call']);
161161
}
162162
}
163163

@@ -278,7 +278,7 @@ private function streamSelect(array &$read, array &$write, $timeout)
278278
// @link https://docs.microsoft.com/de-de/windows/win32/api/winsock2/nf-winsock2-select
279279
$except = null;
280280
if (\DIRECTORY_SEPARATOR === '\\') {
281-
$except = array();
281+
$except = [];
282282
foreach ($write as $key => $socket) {
283283
if (!isset($read[$key]) && @\ftell($socket) === 0) {
284284
$except[$key] = $socket;
@@ -305,9 +305,6 @@ private function streamSelect(array &$read, array &$write, $timeout)
305305
} catch (\Throwable $e) { // @codeCoverageIgnoreStart
306306
\restore_error_handler();
307307
throw $e;
308-
} catch (\Exception $e) {
309-
\restore_error_handler();
310-
throw $e;
311308
} // @codeCoverageIgnoreEnd
312309

313310
if ($except) {

src/Timer/Timers.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
final class Timers
1616
{
1717
private $time;
18-
private $timers = array();
19-
private $schedule = array();
18+
private $timers = [];
19+
private $schedule = [];
2020
private $sorted = true;
2121
private $useHighResolution;
2222

tests/LoopTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testCallingLoopGetShouldAlwaysReturnTheSameEventLoop()
2121
*/
2222
public function numberOfTests()
2323
{
24-
return array(array(), array(), array());
24+
return [[], [], []];
2525
}
2626

2727
public function testStaticAddReadStreamCallsAddReadStreamOnLoopInstance()

tests/TestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ protected function createCallableMock()
4141
{
4242
if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) {
4343
// PHPUnit 9+
44-
return $this->getMockBuilder('stdClass')->addMethods(array('__invoke'))->getMock();
44+
return $this->getMockBuilder('stdClass')->addMethods(['__invoke'])->getMock();
4545
} else {
46-
// legacy PHPUnit 4 - PHPUnit 9
47-
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
46+
// legacy PHPUnit
47+
return $this->getMockBuilder('stdClass')->setMethods(['__invoke'])->getMock();
4848
}
4949
}
5050

0 commit comments

Comments
 (0)