Skip to content

Commit

Permalink
Update PHP language syntax and remove legacy workarounds
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Feb 24, 2024
1 parent ec578d4 commit 7fa1b76
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 84 deletions.
11 changes: 5 additions & 6 deletions src/ExtEvLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class ExtEvLoop implements LoopInterface
/**
* @var EvIo[]
*/
private $readStreams = array();
private $readStreams = [];

/**
* @var EvIo[]
*/
private $writeStreams = array();
private $writeStreams = [];

/**
* @var bool
Expand All @@ -61,7 +61,7 @@ class ExtEvLoop implements LoopInterface
/**
* @var \EvSignal[]
*/
private $signalEvents = array();
private $signalEvents = [];

public function __construct()
{
Expand Down Expand Up @@ -138,13 +138,12 @@ public function addTimer($interval, $callback)
{
$timer = new Timer($interval, $callback, false);

$that = $this;
$timers = $this->timers;
$callback = function () use ($timer, $timers, $that) {
$callback = function () use ($timer, $timers) {
\call_user_func($timer->getCallback(), $timer);

if ($timers->contains($timer)) {
$that->cancelTimer($timer);
$this->cancelTimer($timer);
}
};

Expand Down
28 changes: 12 additions & 16 deletions src/ExtEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ final class ExtEventLoop implements LoopInterface
private $timerCallback;
private $timerEvents;
private $streamCallback;
private $readEvents = array();
private $writeEvents = array();
private $readListeners = array();
private $writeListeners = array();
private $readRefs = array();
private $writeRefs = array();
private $readEvents = [];
private $writeEvents = [];
private $readListeners = [];
private $writeListeners = [];
private $readRefs = [];
private $writeRefs = [];
private $running;
private $signals;
private $signalEvents = array();
private $signalEvents = [];

public function __construct()
{
Expand Down Expand Up @@ -67,8 +67,8 @@ public function __destruct()
$this->timerEvents->detach($timer);
}

$this->readEvents = array();
$this->writeEvents = array();
$this->readEvents = [];
$this->writeEvents = [];
}

public function addReadStream($stream, $listener)
Expand All @@ -85,9 +85,7 @@ public function addReadStream($stream, $listener)

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

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

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

public function removeReadStream($stream)
Expand Down Expand Up @@ -173,7 +169,7 @@ public function addSignal($signal, $listener)
$this->signals->add($signal, $listener);

if (!isset($this->signalEvents[$signal])) {
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, array($this->signals, 'call'));
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, [$this->signals, 'call']);
$this->signalEvents[$signal]->add();
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/ExtUvLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ final class ExtUvLoop implements LoopInterface
private $uv;
private $futureTickQueue;
private $timers;
private $streamEvents = array();
private $readStreams = array();
private $writeStreams = array();
private $streamEvents = [];
private $readStreams = [];
private $writeStreams = [];
private $running;
private $signals;
private $signalEvents = array();
private $signalEvents = [];
private $streamListener;

public function __construct()
Expand Down Expand Up @@ -114,13 +114,12 @@ public function addTimer($interval, $callback)
{
$timer = new Timer($interval, $callback, false);

$that = $this;
$timers = $this->timers;
$callback = function () use ($timer, $timers, $that) {
$callback = function () use ($timer, $timers) {
\call_user_func($timer->getCallback(), $timer);

if ($timers->contains($timer)) {
$that->cancelTimer($timer);
$this->cancelTimer($timer);
}
};

Expand Down
45 changes: 7 additions & 38 deletions src/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ public static function set(LoopInterface $loop)
*/
public static function addReadStream($stream, $listener)
{
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
if (self::$instance === null) {
self::get();
}
self::$instance->addReadStream($stream, $listener);
(self::$instance ?? self::get())->addReadStream($stream, $listener);
}

/**
Expand All @@ -101,11 +97,7 @@ public static function addReadStream($stream, $listener)
*/
public static function addWriteStream($stream, $listener)
{
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
if (self::$instance === null) {
self::get();
}
self::$instance->addWriteStream($stream, $listener);
(self::$instance ?? self::get())->addWriteStream($stream, $listener);
}

/**
Expand Down Expand Up @@ -146,11 +138,7 @@ public static function removeWriteStream($stream)
*/
public static function addTimer($interval, $callback)
{
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
if (self::$instance === null) {
self::get();
}
return self::$instance->addTimer($interval, $callback);
return (self::$instance ?? self::get())->addTimer($interval, $callback);
}

/**
Expand All @@ -163,11 +151,7 @@ public static function addTimer($interval, $callback)
*/
public static function addPeriodicTimer($interval, $callback)
{
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
if (self::$instance === null) {
self::get();
}
return self::$instance->addPeriodicTimer($interval, $callback);
return (self::$instance ?? self::get())->addPeriodicTimer($interval, $callback);
}

/**
Expand All @@ -193,12 +177,7 @@ public static function cancelTimer(TimerInterface $timer)
*/
public static function futureTick($listener)
{
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
if (self::$instance === null) {
self::get();
}

self::$instance->futureTick($listener);
(self::$instance ?? self::get())->futureTick($listener);
}

/**
Expand All @@ -211,12 +190,7 @@ public static function futureTick($listener)
*/
public static function addSignal($signal, $listener)
{
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
if (self::$instance === null) {
self::get();
}

self::$instance->addSignal($signal, $listener);
(self::$instance ?? self::get())->addSignal($signal, $listener);
}

/**
Expand All @@ -242,12 +216,7 @@ public static function removeSignal($signal, $listener)
*/
public static function run()
{
// create loop instance on demand (legacy PHP < 7 doesn't like ternaries in method calls)
if (self::$instance === null) {
self::get();
}

self::$instance->run();
(self::$instance ?? self::get())->run();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/SignalsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
*/
final class SignalsHandler
{
private $signals = array();
private $signals = [];

public function add($signal, $listener)
{
if (!isset($this->signals[$signal])) {
$this->signals[$signal] = array();
$this->signals[$signal] = [];
}

if (\in_array($listener, $this->signals[$signal])) {
Expand Down
15 changes: 6 additions & 9 deletions src/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ final class StreamSelectLoop implements LoopInterface

private $futureTickQueue;
private $timers;
private $readStreams = array();
private $readListeners = array();
private $writeStreams = array();
private $writeListeners = array();
private $readStreams = [];
private $readListeners = [];
private $writeStreams = [];
private $writeListeners = [];
private $running;
private $pcntl = false;
private $pcntlPoll = false;
Expand Down Expand Up @@ -157,7 +157,7 @@ public function addSignal($signal, $listener)
$this->signals->add($signal, $listener);

if ($first) {
\pcntl_signal($signal, array($this->signals, 'call'));
\pcntl_signal($signal, [$this->signals, 'call']);
}
}

Expand Down Expand Up @@ -278,7 +278,7 @@ private function streamSelect(array &$read, array &$write, $timeout)
// @link https://docs.microsoft.com/de-de/windows/win32/api/winsock2/nf-winsock2-select
$except = null;
if (\DIRECTORY_SEPARATOR === '\\') {
$except = array();
$except = [];
foreach ($write as $key => $socket) {
if (!isset($read[$key]) && @\ftell($socket) === 0) {
$except[$key] = $socket;
Expand All @@ -305,9 +305,6 @@ private function streamSelect(array &$read, array &$write, $timeout)
} catch (\Throwable $e) { // @codeCoverageIgnoreStart
\restore_error_handler();
throw $e;
} catch (\Exception $e) {
\restore_error_handler();
throw $e;
} // @codeCoverageIgnoreEnd

if ($except) {
Expand Down
4 changes: 2 additions & 2 deletions src/Timer/Timers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
final class Timers
{
private $time;
private $timers = array();
private $schedule = array();
private $timers = [];
private $schedule = [];
private $sorted = true;
private $useHighResolution;

Expand Down
2 changes: 1 addition & 1 deletion tests/LoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testCallingLoopGetShouldAlwaysReturnTheSameEventLoop()
*/
public function numberOfTests()
{
return array(array(), array(), array());
return [[], [], []];
}

public function testStaticAddReadStreamCallsAddReadStreamOnLoopInstance()
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ protected function createCallableMock()
{
if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) {
// PHPUnit 9+
return $this->getMockBuilder('stdClass')->addMethods(array('__invoke'))->getMock();
return $this->getMockBuilder('stdClass')->addMethods(['__invoke'])->getMock();
} else {
// legacy PHPUnit 4 - PHPUnit 9
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
// legacy PHPUnit
return $this->getMockBuilder('stdClass')->setMethods(['__invoke'])->getMock();
}
}

Expand Down

0 comments on commit 7fa1b76

Please sign in to comment.