Skip to content

Commit cf107f5

Browse files
authored
Merge pull request #178 from clue-labs/types
Add native types to public API
2 parents 212e923 + 74dd763 commit cf107f5

11 files changed

+64
-71
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ See also `pause()`.
324324

325325
#### pipe()
326326

327-
The `pipe(WritableStreamInterface $dest, array $options = [])` method can be used to
327+
The `pipe(WritableStreamInterface $dest, array $options = []): WritableStreamInterface` method can be used to
328328
pipe all the data from this readable source into the given writable destination.
329329

330330
Automatically sends all incoming data to the destination.

src/CompositeStream.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ public function __construct(ReadableStreamInterface $readable, WritableStreamInt
2727
$this->writable->on('close', [$this, 'close']);
2828
}
2929

30-
public function isReadable()
30+
public function isReadable(): bool
3131
{
3232
return $this->readable->isReadable();
3333
}
3434

35-
public function pause()
35+
public function pause(): void
3636
{
3737
$this->readable->pause();
3838
}
3939

40-
public function resume()
40+
public function resume(): void
4141
{
4242
if (!$this->writable->isWritable()) {
4343
return;
@@ -46,28 +46,28 @@ public function resume()
4646
$this->readable->resume();
4747
}
4848

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

54-
public function isWritable()
54+
public function isWritable(): bool
5555
{
5656
return $this->writable->isWritable();
5757
}
5858

59-
public function write($data)
59+
public function write($data): bool
6060
{
6161
return $this->writable->write($data);
6262
}
6363

64-
public function end($data = null)
64+
public function end($data = null): void
6565
{
6666
$this->readable->pause();
6767
$this->writable->end($data);
6868
}
6969

70-
public function close()
70+
public function close(): void
7171
{
7272
if ($this->closed) {
7373
return;

src/DuplexResourceStream.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class DuplexResourceStream extends EventEmitter implements DuplexStreamInt
4444
* @param ?int $readChunkSize
4545
* @param ?WritableStreamInterface $buffer
4646
*/
47-
public function __construct($stream, ?LoopInterface $loop = null, $readChunkSize = null, ?WritableStreamInterface $buffer = null)
47+
public function __construct($stream, ?LoopInterface $loop = null, ?int $readChunkSize = null, ?WritableStreamInterface $buffer = null)
4848
{
4949
if (!\is_resource($stream) || \get_resource_type($stream) !== "stream") {
5050
throw new InvalidArgumentException('First parameter must be a valid stream resource');
@@ -75,7 +75,7 @@ public function __construct($stream, ?LoopInterface $loop = null, $readChunkSize
7575

7676
$this->stream = $stream;
7777
$this->loop = $loop ?: Loop::get();
78-
$this->bufferSize = ($readChunkSize === null) ? 65536 : (int)$readChunkSize;
78+
$this->bufferSize = $readChunkSize ?? 65536;
7979
$this->buffer = $buffer;
8080

8181
$this->buffer->on('error', function ($error) {
@@ -91,33 +91,33 @@ public function __construct($stream, ?LoopInterface $loop = null, $readChunkSize
9191
$this->resume();
9292
}
9393

94-
public function isReadable()
94+
public function isReadable(): bool
9595
{
9696
return $this->readable;
9797
}
9898

99-
public function isWritable()
99+
public function isWritable(): bool
100100
{
101101
return $this->writable;
102102
}
103103

104-
public function pause()
104+
public function pause(): void
105105
{
106106
if ($this->listening) {
107107
$this->loop->removeReadStream($this->stream);
108108
$this->listening = false;
109109
}
110110
}
111111

112-
public function resume()
112+
public function resume(): void
113113
{
114114
if (!$this->listening && $this->readable) {
115115
$this->loop->addReadStream($this->stream, [$this, 'handleData']);
116116
$this->listening = true;
117117
}
118118
}
119119

120-
public function write($data)
120+
public function write($data): bool
121121
{
122122
if (!$this->writable) {
123123
return false;
@@ -126,7 +126,7 @@ public function write($data)
126126
return $this->buffer->write($data);
127127
}
128128

129-
public function close()
129+
public function close(): void
130130
{
131131
if (!$this->writable && !$this->closing) {
132132
return;
@@ -147,7 +147,7 @@ public function close()
147147
}
148148
}
149149

150-
public function end($data = null)
150+
public function end($data = null): void
151151
{
152152
if (!$this->writable) {
153153
return;
@@ -162,7 +162,7 @@ public function end($data = null)
162162
$this->buffer->end($data);
163163
}
164164

165-
public function pipe(WritableStreamInterface $dest, array $options = [])
165+
public function pipe(WritableStreamInterface $dest, array $options = []): WritableStreamInterface
166166
{
167167
return Util::pipe($this, $dest, $options);
168168
}

src/ReadableResourceStream.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class ReadableResourceStream extends EventEmitter implements ReadableStrea
4545
* @param ?LoopInterface $loop
4646
* @param ?int $readChunkSize
4747
*/
48-
public function __construct($stream, ?LoopInterface $loop = null, $readChunkSize = null)
48+
public function __construct($stream, ?LoopInterface $loop = null, ?int $readChunkSize = null)
4949
{
5050
if (!\is_resource($stream) || \get_resource_type($stream) !== "stream") {
5151
throw new InvalidArgumentException('First parameter must be a valid stream resource');
@@ -72,38 +72,38 @@ public function __construct($stream, ?LoopInterface $loop = null, $readChunkSize
7272

7373
$this->stream = $stream;
7474
$this->loop = $loop ?: Loop::get();
75-
$this->bufferSize = ($readChunkSize === null) ? 65536 : (int)$readChunkSize;
75+
$this->bufferSize = $readChunkSize ?? 65536;
7676

7777
$this->resume();
7878
}
7979

80-
public function isReadable()
80+
public function isReadable(): bool
8181
{
8282
return !$this->closed;
8383
}
8484

85-
public function pause()
85+
public function pause(): void
8686
{
8787
if ($this->listening) {
8888
$this->loop->removeReadStream($this->stream);
8989
$this->listening = false;
9090
}
9191
}
9292

93-
public function resume()
93+
public function resume(): void
9494
{
9595
if (!$this->listening && !$this->closed) {
9696
$this->loop->addReadStream($this->stream, [$this, 'handleData']);
9797
$this->listening = true;
9898
}
9999
}
100100

101-
public function pipe(WritableStreamInterface $dest, array $options = [])
101+
public function pipe(WritableStreamInterface $dest, array $options = []): WritableStreamInterface
102102
{
103103
return Util::pipe($this, $dest, $options);
104104
}
105105

106-
public function close()
106+
public function close(): void
107107
{
108108
if ($this->closed) {
109109
return;

src/ReadableStreamInterface.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ interface ReadableStreamInterface extends EventEmitterInterface
194194
*
195195
* @return bool
196196
*/
197-
public function isReadable();
197+
public function isReadable(): bool;
198198

199199
/**
200200
* Pauses reading incoming data events.
@@ -226,7 +226,7 @@ public function isReadable();
226226
* @see self::resume()
227227
* @return void
228228
*/
229-
public function pause();
229+
public function pause(): void;
230230

231231
/**
232232
* Resumes reading incoming data events.
@@ -247,7 +247,7 @@ public function pause();
247247
* @see self::pause()
248248
* @return void
249249
*/
250-
public function resume();
250+
public function resume(): void;
251251

252252
/**
253253
* Pipes all the data from this readable source into the given writable destination.
@@ -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 = []);
325+
public function pipe(WritableStreamInterface $dest, array $options = []): WritableStreamInterface;
326326

327327
/**
328328
* Closes the stream (forcefully).
@@ -358,5 +358,5 @@ public function pipe(WritableStreamInterface $dest, array $options = []);
358358
* @return void
359359
* @see WritableStreamInterface::close()
360360
*/
361-
public function close();
361+
public function close(): void;
362362
}

src/ThroughStream.php

+9-13
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,18 @@ final class ThroughStream extends EventEmitter implements DuplexStreamInterface
8282
private $drain = false;
8383
private $callback;
8484

85-
public function __construct($callback = null)
85+
public function __construct(?callable $callback = null)
8686
{
87-
if ($callback !== null && !\is_callable($callback)) {
88-
throw new InvalidArgumentException('Invalid transformation callback given');
89-
}
90-
9187
$this->callback = $callback;
9288
}
9389

94-
public function pause()
90+
public function pause(): void
9591
{
9692
// only allow pause if still readable, false otherwise
9793
$this->paused = $this->readable;
9894
}
9995

100-
public function resume()
96+
public function resume(): void
10197
{
10298
$this->paused = false;
10399

@@ -108,22 +104,22 @@ public function resume()
108104
}
109105
}
110106

111-
public function pipe(WritableStreamInterface $dest, array $options = [])
107+
public function pipe(WritableStreamInterface $dest, array $options = []): WritableStreamInterface
112108
{
113109
return Util::pipe($this, $dest, $options);
114110
}
115111

116-
public function isReadable()
112+
public function isReadable(): bool
117113
{
118114
return $this->readable;
119115
}
120116

121-
public function isWritable()
117+
public function isWritable(): bool
122118
{
123119
return $this->writable;
124120
}
125121

126-
public function write($data)
122+
public function write($data): bool
127123
{
128124
if (!$this->writable) {
129125
return false;
@@ -151,7 +147,7 @@ public function write($data)
151147
return $this->writable && !$this->paused;
152148
}
153149

154-
public function end($data = null)
150+
public function end($data = null): void
155151
{
156152
if (!$this->writable) {
157153
return;
@@ -175,7 +171,7 @@ public function end($data = null)
175171
$this->close();
176172
}
177173

178-
public function close()
174+
public function close(): void
179175
{
180176
if ($this->closed) {
181177
return;

src/Util.php

+8-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 = [])
16+
public static function pipe(ReadableStreamInterface $source, WritableStreamInterface $dest, array $options = []): WritableStreamInterface
1717
{
1818
// source not readable => NO-OP
1919
if (!$source->isReadable()) {
@@ -64,7 +64,13 @@ public static function pipe(ReadableStreamInterface $source, WritableStreamInter
6464
return $dest;
6565
}
6666

67-
public static function forwardEvents($source, $target, array $events)
67+
/**
68+
* @param ReadableStreamInterface|WritableStreamInterface $source
69+
* @param ReadableStreamInterface|WritableStreamInterface $target
70+
* @param string[] $events
71+
* @return void
72+
*/
73+
public static function forwardEvents($source, $target, array $events): void
6874
{
6975
foreach ($events as $event) {
7076
$source->on($event, function () use ($event, $target) {

src/WritableResourceStream.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class WritableResourceStream extends EventEmitter implements WritableStrea
3434
* @param ?int $writeBufferSoftLimit
3535
* @param ?int $writeChunkSize
3636
*/
37-
public function __construct($stream, ?LoopInterface $loop = null, $writeBufferSoftLimit = null, $writeChunkSize = null)
37+
public function __construct($stream, ?LoopInterface $loop = null, ?int $writeBufferSoftLimit = null, ?int $writeChunkSize = null)
3838
{
3939
if (!\is_resource($stream) || \get_resource_type($stream) !== "stream") {
4040
throw new \InvalidArgumentException('First parameter must be a valid stream resource');
@@ -54,16 +54,16 @@ public function __construct($stream, ?LoopInterface $loop = null, $writeBufferSo
5454

5555
$this->stream = $stream;
5656
$this->loop = $loop ?: Loop::get();
57-
$this->softLimit = ($writeBufferSoftLimit === null) ? 65536 : (int)$writeBufferSoftLimit;
58-
$this->writeChunkSize = ($writeChunkSize === null) ? -1 : (int)$writeChunkSize;
57+
$this->softLimit = $writeBufferSoftLimit ?? 65536;
58+
$this->writeChunkSize = $writeChunkSize ?? -1;
5959
}
6060

61-
public function isWritable()
61+
public function isWritable(): bool
6262
{
6363
return $this->writable;
6464
}
6565

66-
public function write($data)
66+
public function write($data): bool
6767
{
6868
if (!$this->writable) {
6969
return false;
@@ -80,7 +80,7 @@ public function write($data)
8080
return !isset($this->data[$this->softLimit - 1]);
8181
}
8282

83-
public function end($data = null)
83+
public function end($data = null): void
8484
{
8585
if (null !== $data) {
8686
$this->write($data);
@@ -95,7 +95,7 @@ public function end($data = null)
9595
}
9696
}
9797

98-
public function close()
98+
public function close(): void
9999
{
100100
if ($this->closed) {
101101
return;

0 commit comments

Comments
 (0)