Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update PHP language syntax and remove legacy workarounds
  • Loading branch information
WyriHaximus authored and clue committed May 16, 2024
commit e95a017499aad09ebabb71d75269728ef90ffc74
2 changes: 1 addition & 1 deletion examples/12-generate-yes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require __DIR__ . '/../vendor/autoload.php';

// data can be given as first argument or defaults to "y"
$data = (isset($argv[1]) ? $argv[1] : 'y') . "\n";
$data = ($argv[1] ?? 'y') . "\n";

// repeat data X times in order to fill around 200 KB
$data = str_repeat($data, round(200000 / strlen($data)));
Expand Down
2 changes: 1 addition & 1 deletion examples/91-benchmark-ticks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require __DIR__ . '/../vendor/autoload.php';

$n = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
$n = (int) ($argv[1] ?? 1000 * 100);

for ($i = 0; $i < $n; ++$i) {
Loop::futureTick(function () { });
Expand Down
2 changes: 1 addition & 1 deletion examples/92-benchmark-timers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require __DIR__ . '/../vendor/autoload.php';

$n = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
$n = (int) ($argv[1] ?? 1000 * 100);

for ($i = 0; $i < $n; ++$i) {
Loop::addTimer(0, function () { });
Expand Down
2 changes: 1 addition & 1 deletion examples/93-benchmark-ticks-delay.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require __DIR__ . '/../vendor/autoload.php';

$ticks = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
$ticks = (int) ($argv[1] ?? 1000 * 100);
$tick = function () use (&$tick, &$ticks) {
if ($ticks > 0) {
--$ticks;
Expand Down
2 changes: 1 addition & 1 deletion examples/94-benchmark-timers-delay.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require __DIR__ . '/../vendor/autoload.php';

$ticks = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
$ticks = (int) ($argv[1] ?? 1000 * 100);
$tick = function () use (&$tick, &$ticks) {
if ($ticks > 0) {
--$ticks;
Expand Down
4 changes: 2 additions & 2 deletions examples/95-benchmark-memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
require __DIR__ . '/../vendor/autoload.php';

$args = getopt('t:l:r:');
$t = isset($args['t']) ? (int)$args['t'] : 0;
$t = (int) ($args['t'] ?? 0);
$loop = isset($args['l']) && class_exists('React\EventLoop\\' . $args['l'] . 'Loop') ? 'React\EventLoop\\' . $args['l'] . 'Loop' : Loop::get();

if (!($loop instanceof LoopInterface)) {
Loop::set(new $loop());
}

$r = isset($args['r']) ? (int)$args['r'] : 2;
$r = (int) ($args['r'] ?? 2);

$runs = 0;

Expand Down
14 changes: 6 additions & 8 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,11 @@ 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) {
\call_user_func($timer->getCallback(), $timer);

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

Expand Down
37 changes: 17 additions & 20 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 Down Expand Up @@ -169,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 Expand Up @@ -235,11 +235,10 @@ private function scheduleTimer(TimerInterface $timer)
*/
private function createTimerCallback()
{
$timers = $this->timerEvents;
$this->timerCallback = function ($_, $__, $timer) use ($timers) {
$this->timerCallback = function ($_, $__, $timer) {
\call_user_func($timer->getCallback(), $timer);

if (!$timer->isPeriodic() && $timers->contains($timer)) {
if (!$timer->isPeriodic() && $this->timerEvents->contains($timer)) {
$this->cancelTimer($timer);
}
};
Expand All @@ -254,17 +253,15 @@ private function createTimerCallback()
*/
private function createStreamCallback()
{
$read =& $this->readListeners;
$write =& $this->writeListeners;
$this->streamCallback = function ($stream, $flags) use (&$read, &$write) {
$this->streamCallback = function ($stream, $flags) {
$key = (int) $stream;

if (Event::READ === (Event::READ & $flags) && isset($read[$key])) {
\call_user_func($read[$key], $stream);
if (Event::READ === (Event::READ & $flags) && isset($this->readListeners[$key])) {
\call_user_func($this->readListeners[$key], $stream);
}

if (Event::WRITE === (Event::WRITE & $flags) && isset($write[$key])) {
\call_user_func($write[$key], $stream);
if (Event::WRITE === (Event::WRITE & $flags) && isset($this->writeListeners[$key])) {
\call_user_func($this->writeListeners[$key], $stream);
}
};
}
Expand Down
21 changes: 9 additions & 12 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,11 @@ 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) {
\call_user_func($timer->getCallback(), $timer);

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

Expand Down Expand Up @@ -184,10 +182,9 @@ public function addSignal($signal, $listener)
$this->signals->add($signal, $listener);

if (!isset($this->signalEvents[$signal])) {
$signals = $this->signals;
$this->signalEvents[$signal] = \uv_signal_init($this->uv);
\uv_signal_start($this->signalEvents[$signal], function () use ($signals, $signal) {
$signals->call($signal);
\uv_signal_start($this->signalEvents[$signal], function () use ($signal) {
$this->signals->call($signal);
}, $signal);
}
}
Expand Down
52 changes: 10 additions & 42 deletions src/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ public static function get()
$hasRun = true;
});

$stopped =& self::$stopped;
register_shutdown_function(function () use ($loop, &$hasRun, &$stopped) {
register_shutdown_function(function () use ($loop, &$hasRun) {
// Don't run if we're coming from a fatal error (uncaught exception).
$error = error_get_last();
if ((isset($error['type']) ? $error['type'] : 0) & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR)) {
if (($error['type'] ?? 0) & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR)) {
return;
}

if (!$hasRun && !$stopped) {
if (!$hasRun && !self::$stopped) {
$loop->run();
}
});
Expand Down Expand Up @@ -83,11 +82,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 +96,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 +137,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 +150,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 +176,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 +189,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 +215,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
12 changes: 6 additions & 6 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 Down
Loading