Skip to content

Commit 15ac1e9

Browse files
committed
Update PHP lanuage syntax
1 parent 97e0478 commit 15ac1e9

File tree

7 files changed

+86
-101
lines changed

7 files changed

+86
-101
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ send a message to all clients currently subscribed to a given channel:
173173

174174
```php
175175
$channel = 'user';
176-
$message = json_encode(array('id' => 10));
176+
$message = json_encode(['id' => 10]);
177177
$redis->publish($channel, $message);
178178
```
179179

@@ -288,16 +288,16 @@ proxy servers etc.), you can explicitly pass a custom instance of the
288288
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
289289

290290
```php
291-
$connector = new React\Socket\Connector(array(
291+
$connector = new React\Socket\Connector([
292292
'dns' => '127.0.0.1',
293-
'tcp' => array(
293+
'tcp' => [
294294
'bindto' => '192.168.10.1:0'
295-
),
296-
'tls' => array(
295+
],
296+
'tls' => [
297297
'verify_peer' => false,
298298
'verify_peer_name' => false
299-
)
300-
));
299+
]
300+
]);
301301

302302
$factory = new Clue\React\Redis\Factory(null, $connector);
303303
```

examples/cli.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
$params = explode(' ', $line);
3131
$method = array_shift($params);
32-
$promise = call_user_func_array(array($redis, $method), $params);
32+
$promise = call_user_func_array([$redis, $method], $params);
3333

3434
// special method such as end() / close() called
3535
if (!$promise instanceof React\Promise\PromiseInterface) {

examples/publish.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
$factory = new Clue\React\Redis\Factory();
99
$redis = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
1010

11-
$channel = isset($argv[1]) ? $argv[1] : 'channel';
12-
$message = isset($argv[2]) ? $argv[2] : 'message';
11+
$channel = $argv[1] ?? 'channel';
12+
$message = $argv[2] ?? 'message';
1313

1414
$redis->publish($channel, $message)->then(function ($received) {
1515
echo 'Successfully published. Received by ' . $received . PHP_EOL;

examples/subscribe.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
$factory = new Clue\React\Redis\Factory();
1111
$redis = $factory->createLazyClient(getenv('REDIS_URI') ?: 'localhost:6379');
1212

13-
$channel = isset($argv[1]) ? $argv[1] : 'channel';
13+
$channel = $argv[1] ?? 'channel';
1414

1515
$redis->subscribe($channel)->then(function () {
1616
echo 'Now subscribed to channel ' . PHP_EOL;

src/Factory.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use React\Socket\ConnectionInterface;
1111
use React\Socket\Connector;
1212
use React\Socket\ConnectorInterface;
13+
use function React\Promise\reject;
14+
use function React\Promise\Timer\timeout;
1315

1416
class Factory
1517
{
@@ -30,7 +32,7 @@ class Factory
3032
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null, ProtocolFactory $protocol = null)
3133
{
3234
$this->loop = $loop ?: Loop::get();
33-
$this->connector = $connector ?: new Connector(array(), $this->loop);
35+
$this->connector = $connector ?: new Connector([], $this->loop);
3436
$this->protocol = $protocol ?: new ProtocolFactory();
3537
}
3638

@@ -54,18 +56,18 @@ public function createClient($uri)
5456
$parts = parse_url($uri);
5557
}
5658

57-
$uri = preg_replace(array('/(:)[^:\/]*(@)/', '/([?&]password=).*?($|&)/'), '$1***$2', $uri);
58-
if ($parts === false || !isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('redis', 'rediss', 'redis+unix'))) {
59-
return \React\Promise\reject(new \InvalidArgumentException(
59+
$uri = preg_replace(['/(:)[^:\/]*(@)/', '/([?&]password=).*?($|&)/'], '$1***$2', $uri);
60+
if ($parts === false || !isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], ['redis', 'rediss', 'redis+unix'])) {
61+
return reject(new \InvalidArgumentException(
6062
'Invalid Redis URI given (EINVAL)',
6163
defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22
6264
));
6365
}
6466

65-
$args = array();
66-
parse_str(isset($parts['query']) ? $parts['query'] : '', $args);
67+
$args = [];
68+
parse_str($parts['query'] ?? '', $args);
6769

68-
$authority = $parts['host'] . ':' . (isset($parts['port']) ? $parts['port'] : 6379);
70+
$authority = $parts['host'] . ':' . ($parts['port'] ?? 6379);
6971
if ($parts['scheme'] === 'rediss') {
7072
$authority = 'tls://' . $authority;
7173
} elseif ($parts['scheme'] === 'redis+unix') {
@@ -88,9 +90,8 @@ public function createClient($uri)
8890
$connecting->cancel();
8991
});
9092

91-
$protocol = $this->protocol;
92-
$promise = $connecting->then(function (ConnectionInterface $stream) use ($protocol) {
93-
return new StreamingClient($stream, $protocol->createResponseParser(), $protocol->createSerializer());
93+
$promise = $connecting->then(function (ConnectionInterface $stream) {
94+
return new StreamingClient($stream, $this->protocol->createResponseParser(), $this->protocol->createSerializer());
9495
}, function (\Exception $e) use ($uri) {
9596
throw new \RuntimeException(
9697
'Connection to ' . $uri . ' failed: ' . $e->getMessage(),
@@ -100,9 +101,8 @@ public function createClient($uri)
100101
});
101102

102103
// use `?password=secret` query or `user:secret@host` password form URL
103-
$pass = isset($args['password']) ? $args['password'] : (isset($parts['pass']) ? rawurldecode($parts['pass']) : null);
104104
if (isset($args['password']) || isset($parts['pass'])) {
105-
$pass = isset($args['password']) ? $args['password'] : rawurldecode($parts['pass']);
105+
$pass = $args['password'] ?? rawurldecode($parts['pass']);
106106
$promise = $promise->then(function (StreamingClient $redis) use ($pass, $uri) {
107107
return $redis->auth($pass)->then(
108108
function () use ($redis) {
@@ -130,7 +130,7 @@ function (\Exception $e) use ($redis, $uri) {
130130

131131
// use `?db=1` query or `/1` path (skip first slash)
132132
if (isset($args['db']) || (isset($parts['path']) && $parts['path'] !== '/')) {
133-
$db = isset($args['db']) ? $args['db'] : substr($parts['path'], 1);
133+
$db = $args['db'] ?? substr($parts['path'], 1);
134134
$promise = $promise->then(function (StreamingClient $redis) use ($db, $uri) {
135135
return $redis->select($db)->then(
136136
function () use ($redis) {
@@ -159,15 +159,15 @@ function (\Exception $e) use ($redis, $uri) {
159159
});
160160
}
161161

162-
$promise->then(array($deferred, 'resolve'), array($deferred, 'reject'));
162+
$promise->then([$deferred, 'resolve'], [$deferred, 'reject']);
163163

164164
// use timeout from explicit ?timeout=x parameter or default to PHP's default_socket_timeout (60)
165165
$timeout = isset($args['timeout']) ? (float) $args['timeout'] : (int) ini_get("default_socket_timeout");
166166
if ($timeout < 0) {
167167
return $deferred->promise();
168168
}
169169

170-
return \React\Promise\Timer\timeout($deferred->promise(), $timeout, $this->loop)->then(null, function ($e) use ($uri) {
170+
return timeout($deferred->promise(), $timeout, $this->loop)->then(null, function ($e) use ($uri) {
171171
if ($e instanceof TimeoutException) {
172172
throw new \RuntimeException(
173173
'Connection to ' . $uri . ' timed out after ' . $e->getTimeout() . ' seconds (ETIMEDOUT)',

src/LazyClient.php

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Evenement\EventEmitter;
66
use React\Stream\Util;
77
use React\EventLoop\LoopInterface;
8+
use function React\Promise\reject;
89

910
/**
1011
* @internal
@@ -22,15 +23,15 @@ class LazyClient extends EventEmitter implements Client
2223
private $idleTimer;
2324
private $pending = 0;
2425

25-
private $subscribed = array();
26-
private $psubscribed = array();
26+
private $subscribed = [];
27+
private $psubscribed = [];
2728

2829
/**
2930
* @param $target
3031
*/
3132
public function __construct($target, Factory $factory, LoopInterface $loop)
3233
{
33-
$args = array();
34+
$args = [];
3435
\parse_str((string) \parse_url($target, \PHP_URL_QUERY), $args);
3536
if (isset($args['idle'])) {
3637
$this->idlePeriod = (float)$args['idle'];
@@ -47,66 +48,59 @@ private function client()
4748
return $this->promise;
4849
}
4950

50-
$self = $this;
51-
$pending =& $this->promise;
52-
$idleTimer=& $this->idleTimer;
53-
$subscribed =& $this->subscribed;
54-
$psubscribed =& $this->psubscribed;
55-
$loop = $this->loop;
56-
return $pending = $this->factory->createClient($this->target)->then(function (Client $redis) use ($self, &$pending, &$idleTimer, &$subscribed, &$psubscribed, $loop) {
51+
return $this->promise = $this->factory->createClient($this->target)->then(function (Client $redis) {
5752
// connection completed => remember only until closed
58-
$redis->on('close', function () use (&$pending, $self, &$subscribed, &$psubscribed, &$idleTimer, $loop) {
59-
$pending = null;
53+
$redis->on('close', function () {
54+
$this->promise = null;
6055

6156
// foward unsubscribe/punsubscribe events when underlying connection closes
62-
$n = count($subscribed);
63-
foreach ($subscribed as $channel => $_) {
64-
$self->emit('unsubscribe', array($channel, --$n));
57+
$n = count($this->subscribed);
58+
foreach ($this->subscribed as $channel => $_) {
59+
$this->emit('unsubscribe', [$channel, --$n]);
6560
}
66-
$n = count($psubscribed);
67-
foreach ($psubscribed as $pattern => $_) {
68-
$self->emit('punsubscribe', array($pattern, --$n));
61+
$n = count($this->psubscribed);
62+
foreach ($this->psubscribed as $pattern => $_) {
63+
$this->emit('punsubscribe', [$pattern, --$n]);
6964
}
70-
$subscribed = array();
71-
$psubscribed = array();
65+
$this->subscribed = $this->psubscribed = [];
7266

73-
if ($idleTimer !== null) {
74-
$loop->cancelTimer($idleTimer);
75-
$idleTimer = null;
67+
if ($this->idleTimer !== null) {
68+
$this->loop->cancelTimer($this->idleTimer);
69+
$this->idleTimer = null;
7670
}
7771
});
7872

7973
// keep track of all channels and patterns this connection is subscribed to
80-
$redis->on('subscribe', function ($channel) use (&$subscribed) {
81-
$subscribed[$channel] = true;
74+
$redis->on('subscribe', function ($channel) {
75+
$this->subscribed[$channel] = true;
8276
});
83-
$redis->on('psubscribe', function ($pattern) use (&$psubscribed) {
84-
$psubscribed[$pattern] = true;
77+
$redis->on('psubscribe', function ($pattern) {
78+
$this->psubscribed[$pattern] = true;
8579
});
86-
$redis->on('unsubscribe', function ($channel) use (&$subscribed) {
87-
unset($subscribed[$channel]);
80+
$redis->on('unsubscribe', function ($channel) {
81+
unset($this->subscribed[$channel]);
8882
});
89-
$redis->on('punsubscribe', function ($pattern) use (&$psubscribed) {
90-
unset($psubscribed[$pattern]);
83+
$redis->on('punsubscribe', function ($pattern) {
84+
unset($this->psubscribed[$pattern]);
9185
});
9286

9387
Util::forwardEvents(
9488
$redis,
95-
$self,
96-
array(
89+
$this,
90+
[
9791
'message',
9892
'subscribe',
9993
'unsubscribe',
10094
'pmessage',
10195
'psubscribe',
10296
'punsubscribe',
103-
)
97+
]
10498
);
10599

106100
return $redis;
107-
}, function (\Exception $e) use (&$pending) {
101+
}, function (\Exception $e) {
108102
// connection failed => discard connection attempt
109-
$pending = null;
103+
$this->promise = null;
110104

111105
throw $e;
112106
});
@@ -115,22 +109,21 @@ private function client()
115109
public function __call($name, $args)
116110
{
117111
if ($this->closed) {
118-
return \React\Promise\reject(new \RuntimeException(
112+
return reject(new \RuntimeException(
119113
'Connection closed (ENOTCONN)',
120114
defined('SOCKET_ENOTCONN') ? SOCKET_ENOTCONN : 107
121115
));
122116
}
123117

124-
$that = $this;
125-
return $this->client()->then(function (Client $redis) use ($name, $args, $that) {
126-
$that->awake();
127-
return \call_user_func_array(array($redis, $name), $args)->then(
128-
function ($result) use ($that) {
129-
$that->idle();
118+
return $this->client()->then(function (Client $redis) use ($name, $args) {
119+
$this->awake();
120+
return \call_user_func_array([$redis, $name], $args)->then(
121+
function ($result) {
122+
$this->idle();
130123
return $result;
131124
},
132-
function ($error) use ($that) {
133-
$that->idle();
125+
function ($error) {
126+
$this->idle();
134127
throw $error;
135128
}
136129
);
@@ -147,10 +140,9 @@ public function end()
147140
return;
148141
}
149142

150-
$that = $this;
151-
return $this->client()->then(function (Client $redis) use ($that) {
152-
$redis->on('close', function () use ($that) {
153-
$that->close();
143+
return $this->client()->then(function (Client $redis) {
144+
$redis->on('close', function () {
145+
$this->close();
154146
});
155147
$redis->end();
156148
});
@@ -205,14 +197,12 @@ public function idle()
205197
--$this->pending;
206198

207199
if ($this->pending < 1 && $this->idlePeriod >= 0 && !$this->subscribed && !$this->psubscribed && $this->promise !== null) {
208-
$idleTimer =& $this->idleTimer;
209-
$promise =& $this->promise;
210-
$idleTimer = $this->loop->addTimer($this->idlePeriod, function () use (&$idleTimer, &$promise) {
211-
$promise->then(function (Client $redis) {
200+
$this->idleTimer = $this->loop->addTimer($this->idlePeriod, function () {
201+
$this->promise->then(function (Client $redis) {
212202
$redis->close();
213203
});
214-
$promise = null;
215-
$idleTimer = null;
204+
$this->promise = null;
205+
$this->idleTimer = null;
216206
});
217207
}
218208
}

0 commit comments

Comments
 (0)