Skip to content

Commit 1043242

Browse files
committed
Update PHP language syntax, documentation, and tests
1 parent b013836 commit 1043242

16 files changed

+77
-79
lines changed

README.md

Lines changed: 2 additions & 2 deletions
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,

src/CompositeStream.php

Lines changed: 5 additions & 5 deletions
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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ public function __construct($stream, LoopInterface $loop = null, $readChunkSize
8080
$that = $this;
8181

8282
$this->buffer->on('error', function ($error) use ($that) {
83-
$that->emit('error', array($error));
83+
$that->emit('error', [$error]);
8484
});
8585

86-
$this->buffer->on('close', array($this, 'close'));
86+
$this->buffer->on('close', [$this, 'close']);
8787

8888
$this->buffer->on('drain', function () use ($that) {
8989
$that->emit('drain');
@@ -113,7 +113,7 @@ public function pause()
113113
public function resume()
114114
{
115115
if (!$this->listening && $this->readable) {
116-
$this->loop->addReadStream($this->stream, array($this, 'handleData'));
116+
$this->loop->addReadStream($this->stream, [$this, 'handleData']);
117117
$this->listening = true;
118118
}
119119
}
@@ -163,7 +163,7 @@ public function end($data = null)
163163
$this->buffer->end($data);
164164
}
165165

166-
public function pipe(WritableStreamInterface $dest, array $options = array())
166+
public function pipe(WritableStreamInterface $dest, array $options = [])
167167
{
168168
return Util::pipe($this, $dest, $options);
169169
}
@@ -187,13 +187,13 @@ public function handleData($stream)
187187
\restore_error_handler();
188188

189189
if ($error !== null) {
190-
$this->emit('error', array(new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error)));
190+
$this->emit('error', [new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error)]);
191191
$this->close();
192192
return;
193193
}
194194

195195
if ($data !== '') {
196-
$this->emit('data', array($data));
196+
$this->emit('data', [$data]);
197197
} elseif (\feof($this->stream)) {
198198
// no data read => we reached the end and close the stream
199199
$this->emit('end');

src/ReadableResourceStream.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ public function pause()
9393
public function resume()
9494
{
9595
if (!$this->listening && !$this->closed) {
96-
$this->loop->addReadStream($this->stream, array($this, 'handleData'));
96+
$this->loop->addReadStream($this->stream, [$this, 'handleData']);
9797
$this->listening = true;
9898
}
9999
}
100100

101-
public function pipe(WritableStreamInterface $dest, array $options = array())
101+
public function pipe(WritableStreamInterface $dest, array $options = [])
102102
{
103103
return Util::pipe($this, $dest, $options);
104104
}
@@ -139,13 +139,13 @@ public function handleData()
139139
\restore_error_handler();
140140

141141
if ($error !== null) {
142-
$this->emit('error', array(new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error)));
142+
$this->emit('error', [new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error)]);
143143
$this->close();
144144
return;
145145
}
146146

147147
if ($data !== '') {
148-
$this->emit('data', array($data));
148+
$this->emit('data', [$data]);
149149
} elseif (\feof($this->stream)) {
150150
// no data read => we reached the end and close the stream
151151
$this->emit('end');

src/ReadableStreamInterface.php

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,38 @@ class DuplexResourceStreamIntegrationTest extends TestCase
1717
{
1818
public function loopProvider()
1919
{
20-
return array(
21-
array(
22-
function() {
23-
return true;
24-
},
25-
function () {
26-
return new StreamSelectLoop();
27-
}
28-
),
29-
array(
30-
function () {
31-
return function_exists('event_base_new');
32-
},
33-
function () {
34-
return class_exists('React\EventLoop\ExtLibeventLoop') ? new ExtLibeventLoop() : new LibEventLoop();
35-
}
36-
),
37-
array(
38-
function () {
39-
return class_exists('libev\EventLoop');
40-
},
41-
function () {
42-
return class_exists('React\EventLoop\ExtLibevLoop') ? new ExtLibevLoop() : new LibEvLoop();
43-
}
44-
),
45-
array(
46-
function () {
47-
return class_exists('EventBase') && class_exists('React\EventLoop\ExtEventLoop');
48-
},
49-
function () {
50-
return new ExtEventLoop();
51-
}
52-
)
53-
);
20+
yield [
21+
function() {
22+
return true;
23+
},
24+
function () {
25+
return new StreamSelectLoop();
26+
}
27+
];
28+
yield [
29+
function () {
30+
return function_exists('event_base_new');
31+
},
32+
function () {
33+
return class_exists('React\EventLoop\ExtLibeventLoop') ? new ExtLibeventLoop() : new LibEventLoop();
34+
}
35+
];
36+
yield [
37+
function () {
38+
return class_exists('libev\EventLoop');
39+
},
40+
function () {
41+
return class_exists('React\EventLoop\ExtLibevLoop') ? new ExtLibevLoop() : new LibEvLoop();
42+
}
43+
];
44+
yield [
45+
function () {
46+
return class_exists('EventBase') && class_exists('React\EventLoop\ExtEventLoop');
47+
},
48+
function () {
49+
return new ExtEventLoop();
50+
}
51+
];
5452
}
5553

5654
/**

tests/DuplexResourceStreamTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public function testBufferEventsShouldBubbleUp()
427427
$conn->on('error', $this->expectCallableOnce());
428428

429429
$buffer->emit('drain');
430-
$buffer->emit('error', array(new \RuntimeException('Whoops')));
430+
$buffer->emit('error', [new \RuntimeException('Whoops')]);
431431
}
432432

433433
/**

tests/Stub/ReadableStreamStub.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ public function isReadable()
2020
// trigger data event
2121
public function write($data)
2222
{
23-
$this->emit('data', array($data));
23+
$this->emit('data', [$data]);
2424
}
2525

2626
// trigger error event
2727
public function error($error)
2828
{
29-
$this->emit('error', array($error));
29+
$this->emit('error', [$error]);
3030
}
3131

3232
// trigger end event
3333
public function end()
3434
{
35-
$this->emit('end', array());
35+
$this->emit('end', []);
3636
}
3737

3838
public function pause()
@@ -52,7 +52,7 @@ public function close()
5252
$this->emit('close');
5353
}
5454

55-
public function pipe(WritableStreamInterface $dest, array $options = array())
55+
public function pipe(WritableStreamInterface $dest, array $options = [])
5656
{
5757
Util::pipe($this, $dest, $options);
5858

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

tests/ThroughStreamTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public function pipingStuffIntoItShouldWork()
185185
$through->on('data', $this->expectCallableOnceWith('foo'));
186186

187187
$readable->pipe($through);
188-
$readable->emit('data', array('foo'));
188+
$readable->emit('data', ['foo']);
189189
}
190190

191191
/** @test */
@@ -243,7 +243,7 @@ public function writeAfterEndShouldReturnFalse()
243243
public function writeDataWillCloseStreamShouldReturnFalse()
244244
{
245245
$through = new ThroughStream();
246-
$through->on('data', array($through, 'close'));
246+
$through->on('data', [$through, 'close']);
247247

248248
$this->assertFalse($through->write('foo'));
249249
}

0 commit comments

Comments
 (0)