Skip to content

Commit 1999c92

Browse files
committed
[WIP][3.x] Add native types
1 parent 452f2de commit 1999c92

21 files changed

Lines changed: 91 additions & 77 deletions

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"react/dns": "^1.13",
3232
"react/event-loop": "^1.2",
3333
"react/promise": "^3.2 || ^2.6 || ^1.2.1",
34-
"react/stream": "^1.4"
34+
"react/stream": "3.x-dev as 1.666.666"
3535
},
3636
"require-dev": {
3737
"phpunit/phpunit": "^9.6 || ^7.5",

‎src/Connection.php‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,42 @@ public function __construct($resource, LoopInterface $loop)
7878
$this->input->on('close', [$this, 'close']);
7979
}
8080

81-
public function isReadable()
81+
public function isReadable(): bool
8282
{
8383
return $this->input->isReadable();
8484
}
8585

86-
public function isWritable()
86+
public function isWritable(): bool
8787
{
8888
return $this->input->isWritable();
8989
}
9090

91-
public function pause()
91+
public function pause(): void
9292
{
9393
$this->input->pause();
9494
}
9595

96-
public function resume()
96+
public function resume(): void
9797
{
9898
$this->input->resume();
9999
}
100100

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

106-
public function write($data)
106+
public function write($data): bool
107107
{
108108
return $this->input->write($data);
109109
}
110110

111-
public function end($data = null)
111+
public function end($data = null): void
112112
{
113113
$this->input->end($data);
114114
}
115115

116-
public function close()
116+
public function close(): void
117117
{
118118
$this->input->close();
119119
$this->handleClose();
@@ -132,7 +132,7 @@ public function handleClose()
132132
@\stream_socket_shutdown($this->stream, \STREAM_SHUT_RDWR);
133133
}
134134

135-
public function getRemoteAddress()
135+
public function getRemoteAddress(): ?string
136136
{
137137
if (!\is_resource($this->stream)) {
138138
return null;
@@ -141,7 +141,7 @@ public function getRemoteAddress()
141141
return $this->parseAddress(\stream_socket_get_name($this->stream, true));
142142
}
143143

144-
public function getLocalAddress()
144+
public function getLocalAddress(): ?string
145145
{
146146
if (!\is_resource($this->stream)) {
147147
return null;
@@ -150,7 +150,7 @@ public function getLocalAddress()
150150
return $this->parseAddress(\stream_socket_get_name($this->stream, false));
151151
}
152152

153-
private function parseAddress($address)
153+
private function parseAddress($address): ?string
154154
{
155155
if ($address === false) {
156156
return null;

‎src/ConnectionInterface.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ interface ConnectionInterface extends DuplexStreamInterface
8282
*
8383
* @return ?string remote address (URI) or null if unknown
8484
*/
85-
public function getRemoteAddress();
85+
public function getRemoteAddress(): ?string;
8686

8787
/**
8888
* Returns the full local address (full URI with scheme, IP and port) where this connection has been established with
@@ -115,5 +115,5 @@ public function getRemoteAddress();
115115
* @return ?string local address (URI) or null if unknown
116116
* @see self::getRemoteAddress()
117117
*/
118-
public function getLocalAddress();
118+
public function getLocalAddress(): ?string;
119119
}

‎src/Connector.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use React\Dns\Resolver\Factory as DnsFactory;
77
use React\Dns\Resolver\ResolverInterface;
88
use React\EventLoop\LoopInterface;
9+
use React\Promise\PromiseInterface;
910
use function React\Promise\reject;
1011

1112
/**
@@ -146,7 +147,7 @@ public function __construct(array $context = [], ?LoopInterface $loop = null)
146147
}
147148
}
148149

149-
public function connect($uri)
150+
public function connect($uri): PromiseInterface
150151
{
151152
$scheme = 'tcp';
152153
if (\strpos($uri, '://') !== false) {
@@ -173,7 +174,7 @@ public function connect($uri)
173174
* @return string
174175
* @internal
175176
*/
176-
public static function uri(array $parts, $host, $ip)
177+
public static function uri(array $parts, $host, $ip): string
177178
{
178179
$uri = '';
179180

‎src/ConnectorInterface.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace React\Socket;
44

5+
use React\Promise\PromiseInterface;
6+
57
/**
68
* The `ConnectorInterface` is responsible for providing an interface for
79
* establishing streaming connections, such as a normal TCP/IP connection.
@@ -55,5 +57,5 @@ interface ConnectorInterface
5557
* Resolves with a `ConnectionInterface` on success or rejects with an `Exception` on error.
5658
* @see ConnectionInterface
5759
*/
58-
public function connect($uri);
60+
public function connect($uri): PromiseInterface;
5961
}

‎src/DnsConnector.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function __construct(ConnectorInterface $connector, ResolverInterface $re
1818
$this->resolver = $resolver;
1919
}
2020

21-
public function connect($uri)
21+
public function connect($uri): PromiseInterface
2222
{
2323
$original = $uri;
2424
if (\strpos($uri, '://') === false) {

‎src/FdServer.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function __construct($fd, ?LoopInterface $loop = null)
146146
$this->resume();
147147
}
148148

149-
public function getAddress()
149+
public function getAddress(): ?string
150150
{
151151
if (!\is_resource($this->master)) {
152152
return null;
@@ -167,7 +167,7 @@ public function getAddress()
167167
return 'tcp://' . $address;
168168
}
169169

170-
public function pause()
170+
public function pause(): void
171171
{
172172
if (!$this->listening) {
173173
return;
@@ -177,7 +177,7 @@ public function pause()
177177
$this->listening = false;
178178
}
179179

180-
public function resume()
180+
public function resume(): void
181181
{
182182
if ($this->listening || !\is_resource($this->master)) {
183183
return;
@@ -195,7 +195,7 @@ public function resume()
195195
$this->listening = true;
196196
}
197197

198-
public function close()
198+
public function close(): void
199199
{
200200
if (!\is_resource($this->master)) {
201201
return;
@@ -207,7 +207,7 @@ public function close()
207207
}
208208

209209
/** @internal */
210-
public function handleConnection($socket)
210+
public function handleConnection($socket): void
211211
{
212212
$connection = new Connection($socket, $this->loop);
213213
$connection->unix = $this->unix;

‎src/FixedUriConnector.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace React\Socket;
44

5+
use React\Promise\PromiseInterface;
6+
57
/**
68
* Decorates an existing Connector to always use a fixed, preconfigured URI
79
*
@@ -34,7 +36,7 @@ public function __construct($uri, ConnectorInterface $connector)
3436
$this->connector = $connector;
3537
}
3638

37-
public function connect($_)
39+
public function connect($_): PromiseInterface
3840
{
3941
return $this->connector->connect($this->uri);
4042
}

‎src/HappyEyeBallsConnectionBuilder.php‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ final class HappyEyeBallsConnectionBuilder
5454
public $lastError6;
5555
public $lastError4;
5656

57-
public function __construct(LoopInterface $loop, ConnectorInterface $connector, ResolverInterface $resolver, $uri, $host, $parts)
57+
public function __construct(LoopInterface $loop, ConnectorInterface $connector, ResolverInterface $resolver, string $uri, string $host, array $parts)
5858
{
5959
$this->loop = $loop;
6060
$this->connector = $connector;
@@ -64,7 +64,7 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector,
6464
$this->parts = $parts;
6565
}
6666

67-
public function connect()
67+
public function connect(): PromiseInterface
6868
{
6969
return new Promise(function ($resolve, $reject) {
7070
$lookupResolve = function ($type) use ($resolve, $reject) {
@@ -123,7 +123,7 @@ public function connect()
123123
* always resolves with a list of IP addresses on success or an empty
124124
* list on error.
125125
*/
126-
public function resolve($type, $reject)
126+
public function resolve(int $type, callable $reject): PromiseInterface
127127
{
128128
return $this->resolver->resolveAll($this->host, $type)->then(null, function (\Exception $e) use ($type, $reject) {
129129
unset($this->resolverPromises[$type]);
@@ -159,7 +159,7 @@ public function resolve($type, $reject)
159159
/**
160160
* @internal
161161
*/
162-
public function check($resolve, $reject)
162+
public function check(callable $resolve, callable $reject)
163163
{
164164
$ip = \array_shift($this->connectQueue);
165165

@@ -168,13 +168,13 @@ public function check($resolve, $reject)
168168
\end($this->connectionPromises);
169169
$index = \key($this->connectionPromises);
170170

171-
$this->connectionPromises[$index]->then(function ($connection) use ($index, $resolve) {
171+
$this->connectionPromises[$index]->then(function ($connection) use ($index, $resolve): void {
172172
unset($this->connectionPromises[$index]);
173173

174174
$this->cleanUp();
175175

176176
$resolve($connection);
177-
}, function (\Exception $e) use ($index, $ip, $resolve, $reject) {
177+
}, function (\Exception $e) use ($index, $ip, $resolve, $reject): void {
178178
unset($this->connectionPromises[$index]);
179179

180180
$this->failureCount++;
@@ -228,8 +228,9 @@ public function check($resolve, $reject)
228228

229229
/**
230230
* @internal
231+
* @return PromiseInterface<ConnectionInterface>
231232
*/
232-
public function attemptConnection($ip)
233+
public function attemptConnection($ip): PromiseInterface
233234
{
234235
$uri = Connector::uri($this->parts, $this->host, $ip);
235236

@@ -239,7 +240,7 @@ public function attemptConnection($ip)
239240
/**
240241
* @internal
241242
*/
242-
public function cleanUp()
243+
public function cleanUp(): void
243244
{
244245
// clear list of outstanding IPs to avoid creating new connections
245246
$this->connectQueue = [];
@@ -267,7 +268,7 @@ public function cleanUp()
267268
/**
268269
* @internal
269270
*/
270-
public function hasBeenResolved()
271+
public function hasBeenResolved(): bool
271272
{
272273
foreach ($this->resolved as $typeHasBeenResolved) {
273274
if ($typeHasBeenResolved === false) {
@@ -286,6 +287,8 @@ public function hasBeenResolved()
286287
* @link https://tools.ietf.org/html/rfc8305#section-4
287288
*
288289
* @internal
290+
*
291+
* @param array<string> $ips
289292
*/
290293
public function mixIpsIntoConnectQueue(array $ips)
291294
{
@@ -307,7 +310,7 @@ public function mixIpsIntoConnectQueue(array $ips)
307310
* @internal
308311
* @return string
309312
*/
310-
public function error()
313+
public function error(): string
311314
{
312315
if ($this->lastError4 === $this->lastError6) {
313316
$message = $this->lastError6;

‎src/HappyEyeBallsConnector.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use React\Dns\Resolver\ResolverInterface;
66
use React\EventLoop\Loop;
77
use React\EventLoop\LoopInterface;
8+
use React\Promise\PromiseInterface;
89
use function React\Promise\reject;
910

1011
final class HappyEyeBallsConnector implements ConnectorInterface
@@ -20,7 +21,7 @@ public function __construct(?LoopInterface $loop, ConnectorInterface $connector,
2021
$this->resolver = $resolver;
2122
}
2223

23-
public function connect($uri)
24+
public function connect($uri): PromiseInterface
2425
{
2526
$original = $uri;
2627
if (\strpos($uri, '://') === false) {

0 commit comments

Comments
 (0)