Skip to content

Commit a06b914

Browse files
WyriHaximusclue
authored andcommitted
Update PHP language syntax and remove legacy workarounds
1 parent 9bde4fd commit a06b914

21 files changed

+65
-68
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ By default, this will call `end()` on the destination stream once the
353353
source stream emits an `end` event. This can be disabled like this:
354354

355355
```php
356-
$source->pipe($dest, array('end' => false));
356+
$source->pipe($dest, ['end' => false]);
357357
```
358358

359359
Note that this only applies to the `end` event.
@@ -1126,7 +1126,7 @@ $through = new ThroughStream(function ($data) {
11261126
});
11271127
$through->on('data', $this->expectCallableOnceWith("[2, true]\n"));
11281128

1129-
$through->write(array(2, true));
1129+
$through->write([2, true]);
11301130
```
11311131

11321132
The callback function is allowed to throw an `Exception`. In this case,

examples/01-http.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

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

18-
$host = isset($argv[1]) ? $argv[1] : 'www.google.com';
18+
$host = $argv[1] ?? 'www.google.com';
1919

2020
// connect to tcp://www.google.com:80 (blocking call!)
2121
// for illustration purposes only, should use react/http-client or react/socket instead!

examples/02-https.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

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

18-
$host = isset($argv[1]) ? $argv[1] : 'www.google.com';
18+
$host = $argv[1] ?? 'www.google.com';
1919

2020
// connect to tls://www.google.com:443 (blocking call!)
2121
// for illustration purposes only, should use react/http-client or react/socket instead!

examples/91-benchmark-throughput.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
}
2222

2323
$args = getopt('i:o:t:');
24-
$if = isset($args['i']) ? $args['i'] : '/dev/zero';
25-
$of = isset($args['o']) ? $args['o'] : '/dev/null';
26-
$t = isset($args['t']) ? $args['t'] : 1;
24+
$if = $args['i'] ?? '/dev/zero';
25+
$of = $args['o'] ?? '/dev/null';
26+
$t = $args['t'] ?? 1;
2727

2828
// passing file descriptors requires mapping paths (https://bugs.php.net/bug.php?id=53465)
2929
$if = str_replace('/dev/fd/', 'php://fd/', $if);

src/CompositeStream.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public function __construct(ReadableStreamInterface $readable, WritableStreamInt
2020
return;
2121
}
2222

23-
Util::forwardEvents($this->readable, $this, array('data', 'end', 'error'));
24-
Util::forwardEvents($this->writable, $this, array('drain', 'error', 'pipe'));
23+
Util::forwardEvents($this->readable, $this, ['data', 'end', 'error']);
24+
Util::forwardEvents($this->writable, $this, ['drain', 'error', 'pipe']);
2525

26-
$this->readable->on('close', array($this, 'close'));
27-
$this->writable->on('close', array($this, 'close'));
26+
$this->readable->on('close', [$this, 'close']);
27+
$this->writable->on('close', [$this, 'close']);
2828
}
2929

3030
public function isReadable()
@@ -46,7 +46,7 @@ public function resume()
4646
$this->readable->resume();
4747
}
4848

49-
public function pipe(WritableStreamInterface $dest, array $options = array())
49+
public function pipe(WritableStreamInterface $dest, array $options = [])
5050
{
5151
return Util::pipe($this, $dest, $options);
5252
}

src/DuplexResourceStream.php

+9-11
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,14 @@ public function __construct($stream, LoopInterface $loop = null, $readChunkSize
7272
$this->bufferSize = ($readChunkSize === null) ? 65536 : (int)$readChunkSize;
7373
$this->buffer = $buffer;
7474

75-
$that = $this;
76-
77-
$this->buffer->on('error', function ($error) use ($that) {
78-
$that->emit('error', array($error));
75+
$this->buffer->on('error', function ($error) {
76+
$this->emit('error', [$error]);
7977
});
8078

81-
$this->buffer->on('close', array($this, 'close'));
79+
$this->buffer->on('close', [$this, 'close']);
8280

83-
$this->buffer->on('drain', function () use ($that) {
84-
$that->emit('drain');
81+
$this->buffer->on('drain', function () {
82+
$this->emit('drain');
8583
});
8684

8785
$this->resume();
@@ -108,7 +106,7 @@ public function pause()
108106
public function resume()
109107
{
110108
if (!$this->listening && $this->readable) {
111-
$this->loop->addReadStream($this->stream, array($this, 'handleData'));
109+
$this->loop->addReadStream($this->stream, [$this, 'handleData']);
112110
$this->listening = true;
113111
}
114112
}
@@ -158,7 +156,7 @@ public function end($data = null)
158156
$this->buffer->end($data);
159157
}
160158

161-
public function pipe(WritableStreamInterface $dest, array $options = array())
159+
public function pipe(WritableStreamInterface $dest, array $options = [])
162160
{
163161
return Util::pipe($this, $dest, $options);
164162
}
@@ -182,13 +180,13 @@ public function handleData($stream)
182180
\restore_error_handler();
183181

184182
if ($error !== null) {
185-
$this->emit('error', array(new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error)));
183+
$this->emit('error', [new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error)]);
186184
$this->close();
187185
return;
188186
}
189187

190188
if ($data !== '') {
191-
$this->emit('data', array($data));
189+
$this->emit('data', [$data]);
192190
} elseif (\feof($this->stream)) {
193191
// no data read => we reached the end and close the stream
194192
$this->emit('end');

src/ReadableResourceStream.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ public function pause()
8888
public function resume()
8989
{
9090
if (!$this->listening && !$this->closed) {
91-
$this->loop->addReadStream($this->stream, array($this, 'handleData'));
91+
$this->loop->addReadStream($this->stream, [$this, 'handleData']);
9292
$this->listening = true;
9393
}
9494
}
9595

96-
public function pipe(WritableStreamInterface $dest, array $options = array())
96+
public function pipe(WritableStreamInterface $dest, array $options = [])
9797
{
9898
return Util::pipe($this, $dest, $options);
9999
}
@@ -134,13 +134,13 @@ public function handleData()
134134
\restore_error_handler();
135135

136136
if ($error !== null) {
137-
$this->emit('error', array(new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error)));
137+
$this->emit('error', [new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error)]);
138138
$this->close();
139139
return;
140140
}
141141

142142
if ($data !== '') {
143-
$this->emit('data', array($data));
143+
$this->emit('data', [$data]);
144144
} elseif (\feof($this->stream)) {
145145
// no data read => we reached the end and close the stream
146146
$this->emit('end');

src/ReadableStreamInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public function resume();
278278
* source stream emits an `end` event. This can be disabled like this:
279279
*
280280
* ```php
281-
* $source->pipe($dest, array('end' => false));
281+
* $source->pipe($dest, ['end' => false]);
282282
* ```
283283
*
284284
* Note that this only applies to the `end` event.
@@ -322,7 +322,7 @@ public function resume();
322322
* @param array $options
323323
* @return WritableStreamInterface $dest stream as-is
324324
*/
325-
public function pipe(WritableStreamInterface $dest, array $options = array());
325+
public function pipe(WritableStreamInterface $dest, array $options = []);
326326

327327
/**
328328
* Closes the stream (forcefully).

src/ThroughStream.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
* });
4949
* $through->on('data', $this->expectCallableOnceWith("[2, true]\n"));
5050
*
51-
* $through->write(array(2, true));
51+
* $through->write([2, true]);
5252
* ```
5353
*
5454
* The callback function is allowed to throw an `Exception`. In this case,
@@ -108,7 +108,7 @@ public function resume()
108108
}
109109
}
110110

111-
public function pipe(WritableStreamInterface $dest, array $options = array())
111+
public function pipe(WritableStreamInterface $dest, array $options = [])
112112
{
113113
return Util::pipe($this, $dest, $options);
114114
}
@@ -133,14 +133,14 @@ public function write($data)
133133
try {
134134
$data = \call_user_func($this->callback, $data);
135135
} catch (\Exception $e) {
136-
$this->emit('error', array($e));
136+
$this->emit('error', [$e]);
137137
$this->close();
138138

139139
return false;
140140
}
141141
}
142142

143-
$this->emit('data', array($data));
143+
$this->emit('data', [$data]);
144144

145145
// emit drain event on next resume if currently paused (throttled)
146146
if ($this->paused) {

src/Util.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class Util
1313
* @return WritableStreamInterface $dest stream as-is
1414
* @see ReadableStreamInterface::pipe() for more details
1515
*/
16-
public static function pipe(ReadableStreamInterface $source, WritableStreamInterface $dest, array $options = array())
16+
public static function pipe(ReadableStreamInterface $source, WritableStreamInterface $dest, array $options = [])
1717
{
1818
// source not readable => NO-OP
1919
if (!$source->isReadable()) {
@@ -27,7 +27,7 @@ public static function pipe(ReadableStreamInterface $source, WritableStreamInter
2727
return $dest;
2828
}
2929

30-
$dest->emit('pipe', array($source));
30+
$dest->emit('pipe', [$source]);
3131

3232
// forward all source data events as $dest->write()
3333
$source->on('data', $dataer = function ($data) use ($source, $dest) {

src/WritableResourceStream.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function write($data)
6868
if (!$this->listening && $this->data !== '') {
6969
$this->listening = true;
7070

71-
$this->loop->addWriteStream($this->stream, array($this, 'handleWrite'));
71+
$this->loop->addWriteStream($this->stream, [$this, 'handleWrite']);
7272
}
7373

7474
return !isset($this->data[$this->softLimit - 1]);
@@ -137,7 +137,7 @@ public function handleWrite()
137137
// Should this turn out to be a permanent error later, it will eventually
138138
// send *nothing* and we can detect this.
139139
if (($sent === 0 || $sent === false) && $error !== null) {
140-
$this->emit('error', array(new \RuntimeException('Unable to write to stream: ' . $error)));
140+
$this->emit('error', [new \RuntimeException('Unable to write to stream: ' . $error)]);
141141
$this->close();
142142

143143
return;

tests/CompositeStreamTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function itShouldReceiveForwardedEvents()
217217
$composite->on('data', $this->expectCallableOnce());
218218
$composite->on('drain', $this->expectCallableOnce());
219219

220-
$readable->emit('data', array('foo'));
220+
$readable->emit('data', ['foo']);
221221
$writable->emit('drain');
222222
}
223223

@@ -241,7 +241,7 @@ public function itShouldHandlePipingCorrectly()
241241

242242
$input = new ThroughStream();
243243
$input->pipe($composite);
244-
$input->emit('data', array('foo'));
244+
$input->emit('data', ['foo']);
245245
}
246246

247247
/** @test */
@@ -262,6 +262,6 @@ public function itShouldForwardPipeCallsToReadableStream()
262262
->with('foo');
263263

264264
$composite->pipe($output);
265-
$readable->emit('data', array('foo'));
265+
$readable->emit('data', ['foo']);
266266
}
267267
}

tests/DuplexResourceStreamIntegrationTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace React\Tests\Stream;
44

5-
use Clue\StreamFilter as Filter;
65
use React\Stream\DuplexResourceStream;
76
use React\Stream\ReadableResourceStream;
87
use React\EventLoop\ExtEventLoop;
98
use React\EventLoop\LoopInterface;
109
use React\EventLoop\StreamSelectLoop;
10+
use function Clue\StreamFilter\append as filter_append;
1111

1212
class DuplexResourceStreamIntegrationTest extends TestCase
1313
{
@@ -340,7 +340,7 @@ public function testEmptyReadShouldntFcloseStream($condition, $loopFactory)
340340

341341

342342
// add a filter which returns an error when encountering an 'a' when reading
343-
Filter\append($stream, function ($chunk) {
343+
filter_append($stream, function ($chunk) {
344344
return '';
345345
}, STREAM_FILTER_READ);
346346

tests/DuplexResourceStreamTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace React\Tests\Stream;
44

55
use React\Stream\DuplexResourceStream;
6-
use Clue\StreamFilter as Filter;
76
use React\Stream\WritableResourceStream;
7+
use function Clue\StreamFilter\append as filter_append;
88

99
class DuplexResourceStreamTest extends TestCase
1010
{
@@ -423,7 +423,7 @@ public function testBufferEventsShouldBubbleUp()
423423
$conn->on('error', $this->expectCallableOnce());
424424

425425
$buffer->emit('drain');
426-
$buffer->emit('error', array(new \RuntimeException('Whoops')));
426+
$buffer->emit('error', [new \RuntimeException('Whoops')]);
427427
}
428428

429429
/**
@@ -454,7 +454,7 @@ public function testDataFiltered()
454454
$stream = fopen('php://temp', 'r+');
455455

456456
// add a filter which removes every 'a' when reading
457-
Filter\append($stream, function ($chunk) {
457+
filter_append($stream, function ($chunk) {
458458
return str_replace('a', '', $chunk);
459459
}, STREAM_FILTER_READ);
460460

@@ -482,7 +482,7 @@ public function testDataErrorShouldEmitErrorAndClose()
482482
$stream = fopen('php://temp', 'r+');
483483

484484
// add a filter which returns an error when encountering an 'a' when reading
485-
Filter\append($stream, function ($chunk) {
485+
filter_append($stream, function ($chunk) {
486486
if (strpos($chunk, 'a') !== false) {
487487
throw new \Exception('Invalid');
488488
}

tests/FunctionalInternetTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,9 @@ private function awaitStreamClose(DuplexResourceStream $stream, LoopInterface $l
120120
$loop->stop();
121121
});
122122

123-
$that = $this;
124-
$loop->addTimer($timeout, function () use ($loop, $that) {
123+
$loop->addTimer($timeout, function () use ($loop) {
125124
$loop->stop();
126-
$that->fail('Timed out while waiting for stream to close');
125+
$this->fail('Timed out while waiting for stream to close');
127126
});
128127

129128
$loop->run();

tests/ReadableResourceStreamTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace React\Tests\Stream;
44

55
use React\Stream\ReadableResourceStream;
6-
use Clue\StreamFilter as Filter;
6+
use function Clue\StreamFilter\append as filter_append;
77

88
class ReadableResourceStreamTest extends TestCase
99
{
@@ -326,7 +326,7 @@ public function testDataFiltered()
326326
$stream = fopen('php://temp', 'r+');
327327

328328
// add a filter which removes every 'a' when reading
329-
Filter\append($stream, function ($chunk) {
329+
filter_append($stream, function ($chunk) {
330330
return str_replace('a', '', $chunk);
331331
}, STREAM_FILTER_READ);
332332

@@ -354,7 +354,7 @@ public function testDataErrorShouldEmitErrorAndClose()
354354
$stream = fopen('php://temp', 'r+');
355355

356356
// add a filter which returns an error when encountering an 'a' when reading
357-
Filter\append($stream, function ($chunk) {
357+
filter_append($stream, function ($chunk) {
358358
if (strpos($chunk, 'a') !== false) {
359359
throw new \Exception('Invalid');
360360
}

0 commit comments

Comments
 (0)