Skip to content

Commit 3088887

Browse files
committed
More use of property promotion and other code cleanup
1 parent d23e0b8 commit 3088887

14 files changed

+94
-122
lines changed

src/Connection/ConnectionLimitingPool.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ private static function formatUri(Request $request): string
4242
return $scheme . '://' . $authority;
4343
}
4444

45-
private int $connectionLimit;
46-
4745
private ConnectionFactory $connectionFactory;
4846

4947
/** @var array<string, \ArrayObject<int, Future<Connection>>> */
@@ -67,14 +65,13 @@ private static function formatUri(Request $request): string
6765

6866
private int $openConnectionCount = 0;
6967

70-
private function __construct(int $connectionLimit, ?ConnectionFactory $connectionFactory = null)
68+
private function __construct(private readonly int $connectionLimit, ?ConnectionFactory $connectionFactory = null)
7169
{
7270
if ($connectionLimit < 1) {
7371
throw new \Error('The connection limit must be greater than 0');
7472
}
7573

76-
$this->connectionLimit = $connectionLimit;
77-
$this->connectionFactory = $connectionFactory ?? new DefaultConnectionFactory;
74+
$this->connectionFactory = $connectionFactory ?? new DefaultConnectionFactory();
7875
}
7976

8077
public function __clone()

src/Connection/DefaultConnectionFactory.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,21 @@
1818

1919
final class DefaultConnectionFactory implements ConnectionFactory
2020
{
21-
private ?Socket\SocketConnector $connector;
21+
private readonly ConnectContext $connectContext;
2222

23-
private ?ConnectContext $connectContext;
24-
25-
public function __construct(?Socket\SocketConnector $connector = null, ?ConnectContext $connectContext = null)
26-
{
27-
$this->connector = $connector;
28-
$this->connectContext = $connectContext;
23+
public function __construct(
24+
private readonly ?Socket\SocketConnector $connector = null,
25+
?ConnectContext $connectContext = null,
26+
) {
27+
$this->connectContext = $connectContext ?? new ConnectContext();
2928
}
3029

3130
public function create(Request $request, Cancellation $cancellation): Connection
3231
{
3332
$connectStart = now();
3433

3534
$connector = $this->connector ?? Socket\socketConnector();
36-
$connectContext = $this->connectContext ?? new ConnectContext;
35+
$connectContext = $this->connectContext;
3736

3837
$uri = $request->getUri();
3938
$scheme = $uri->getScheme();

src/Connection/Http1Connection.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ final class Http1Connection implements Connection
6969
/** @var list<\Closure():void>|null */
7070
private ?array $onClose = [];
7171

72-
private float $timeoutGracePeriod;
73-
7472
private float $lastUsedAt;
7573

7674
private bool $explicitTimeout = false;
@@ -87,13 +85,12 @@ public function __construct(
8785
Socket $socket,
8886
private readonly float $connectDuration,
8987
private readonly ?float $tlsHandshakeDuration,
90-
float $timeoutGracePeriod = 2
88+
private readonly float $timeoutGracePeriod = 2,
9189
) {
9290
$this->socket = $socket;
9391
$this->localAddress = $socket->getLocalAddress();
9492
$this->remoteAddress = $socket->getRemoteAddress();
9593
$this->tlsInfo = $socket->getTlsInfo();
96-
$this->timeoutGracePeriod = $timeoutGracePeriod;
9794
$this->lastUsedAt = now();
9895
$this->watchIdleConnection();
9996
}

src/Connection/HttpStream.php

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,66 +12,73 @@
1212
use Amp\Socket\TlsInfo;
1313
use function Amp\Http\Client\processRequest;
1414

15+
/**
16+
* @psalm-type RequestCallbackType = callable(Request, Cancellation, HttpStream):Response
17+
* @psalm-type ReleaseCallbackType = callable():void
18+
*/
1519
final class HttpStream implements Stream
1620
{
1721
use ForbidSerialization;
1822
use ForbidCloning;
1923

24+
/**
25+
* @param RequestCallbackType $RequestCallbackType
26+
* @param ReleaseCallbackType $ReleaseCallbackType
27+
*/
2028
public static function fromConnection(
2129
Connection $connection,
22-
callable $requestCallback,
23-
callable $releaseCallback
30+
callable $RequestCallbackType,
31+
callable $ReleaseCallbackType
2432
): self {
2533
return new self(
2634
$connection->getLocalAddress(),
2735
$connection->getRemoteAddress(),
2836
$connection->getTlsInfo(),
29-
$requestCallback,
30-
$releaseCallback
37+
$RequestCallbackType,
38+
$ReleaseCallbackType,
3139
);
3240
}
3341

34-
public static function fromStream(Stream $stream, callable $requestCallback, callable $releaseCallback): self
42+
/**
43+
* @param RequestCallbackType $RequestCallbackType
44+
* @param ReleaseCallbackType $ReleaseCallbackType
45+
*/
46+
public static function fromStream(Stream $stream, callable $RequestCallbackType, callable $ReleaseCallbackType): self
3547
{
3648
return new self(
3749
$stream->getLocalAddress(),
3850
$stream->getRemoteAddress(),
3951
$stream->getTlsInfo(),
40-
$requestCallback,
41-
$releaseCallback
52+
$RequestCallbackType,
53+
$ReleaseCallbackType,
4254
);
4355
}
4456

45-
private SocketAddress $localAddress;
46-
47-
private SocketAddress $remoteAddress;
48-
49-
private ?TlsInfo $tlsInfo;
50-
5157
/** @var callable */
52-
private $requestCallback;
58+
private $RequestCallbackType;
5359

5460
/** @var callable|null */
55-
private $releaseCallback;
61+
private $ReleaseCallbackType;
5662

63+
/**
64+
* @param RequestCallbackType $RequestCallbackType
65+
* @param ReleaseCallbackType $ReleaseCallbackType
66+
*/
5767
private function __construct(
58-
SocketAddress $localAddress,
59-
SocketAddress $remoteAddress,
60-
?TlsInfo $tlsInfo,
61-
callable $requestCallback,
62-
callable $releaseCallback
68+
private readonly SocketAddress $localAddress,
69+
private readonly SocketAddress $remoteAddress,
70+
private readonly ?TlsInfo $tlsInfo,
71+
callable $RequestCallbackType,
72+
callable $ReleaseCallbackType,
6373
) {
64-
$this->localAddress = $localAddress;
65-
$this->remoteAddress = $remoteAddress;
66-
$this->tlsInfo = $tlsInfo;
67-
$this->requestCallback = $requestCallback;
68-
$this->releaseCallback = $releaseCallback;
74+
$this->RequestCallbackType = $RequestCallbackType;
75+
$this->ReleaseCallbackType = $ReleaseCallbackType;
6976
}
7077

7178
public function __destruct()
7279
{
73-
if ($this->releaseCallback !== null) {
74-
($this->releaseCallback)();
80+
if ($this->ReleaseCallbackType !== null) {
81+
($this->ReleaseCallbackType)();
7582
}
7683
}
7784

@@ -80,13 +87,13 @@ public function __destruct()
8087
*/
8188
public function request(Request $request, Cancellation $cancellation): Response
8289
{
83-
if ($this->releaseCallback === null) {
90+
if ($this->ReleaseCallbackType === null) {
8491
throw new \Error('A stream may only be used for a single request');
8592
}
8693

87-
$this->releaseCallback = null;
94+
$this->ReleaseCallbackType = null;
8895

89-
return processRequest($request, [], fn (): Response => ($this->requestCallback)($request, $cancellation, $this));
96+
return processRequest($request, [], fn (): Response => ($this->RequestCallbackType)($request, $cancellation, $this));
9097
}
9198

9299
public function getLocalAddress(): SocketAddress

src/Connection/InterceptedStream.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ final class InterceptedStream implements Stream
2121

2222
private static \WeakMap $requestInterceptors;
2323

24-
private Stream $stream;
25-
2624
private ?NetworkInterceptor $interceptor;
2725

28-
public function __construct(Stream $stream, NetworkInterceptor $interceptor)
26+
public function __construct(private readonly Stream $stream, NetworkInterceptor $interceptor)
2927
{
30-
$this->stream = $stream;
3128
$this->interceptor = $interceptor;
3229
}
3330

@@ -37,14 +34,14 @@ public function __construct(Stream $stream, NetworkInterceptor $interceptor)
3734
public function request(Request $request, Cancellation $cancellation): Response
3835
{
3936
return processRequest($request, [], function () use ($request, $cancellation): Response {
40-
if (!$this->interceptor) {
37+
$interceptor = $this->interceptor;
38+
$this->interceptor = null;
39+
40+
if (!$interceptor) {
4141
throw new \Error(__METHOD__ . ' may only be invoked once per instance. '
4242
. 'If you need to implement retries or otherwise issue multiple requests, register an ApplicationInterceptor to do so.');
4343
}
4444

45-
$interceptor = $this->interceptor;
46-
$this->interceptor = null;
47-
4845
/** @psalm-suppress RedundantPropertyInitializationCheck */
4946
self::$requestInterceptors ??= new \WeakMap();
5047
$requestInterceptors = self::$requestInterceptors[$request] ?? [];

src/Connection/Internal/Http1Parser.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@
1010
use Amp\Http\Client\Request;
1111
use Amp\Http\Client\Response;
1212
use Amp\Http\Http1\Rfc7230;
13+
use Amp\Http\HttpMessage;
1314
use Amp\Http\HttpStatus;
1415
use Amp\Http\InvalidHeaderException;
1516
use function Amp\Http\Client\events;
1617
use function Amp\Http\mapHeaderPairs;
1718

18-
/** @internal */
19+
/**
20+
* @internal
21+
*
22+
* @psalm-import-type HeaderMapType from HttpMessage
23+
*/
1924
final class Http1Parser
2025
{
2126
use ForbidSerialization;
@@ -34,10 +39,6 @@ final class Http1Parser
3439
public const TRAILERS_START = 4;
3540
public const TRAILERS = 5;
3641

37-
private Request $request;
38-
39-
private Stream $stream;
40-
4142
private ?Response $response = null;
4243

4344
private int $state = self::AWAITING_HEADERS;
@@ -57,26 +58,20 @@ final class Http1Parser
5758

5859
private bool $complete = false;
5960

60-
private int $maxHeaderBytes;
61-
62-
private int $maxBodyBytes;
61+
private readonly int $maxHeaderBytes;
6362

64-
/** @var callable */
65-
private $bodyDataCallback;
66-
67-
/** @var callable */
68-
private $trailersCallback;
63+
private readonly int $maxBodyBytes;
6964

65+
/**
66+
* @param \Closure(string):void $bodyDataCallback
67+
* @param \Closure(HeaderMapType):void $trailersCallback
68+
*/
7069
public function __construct(
71-
Request $request,
72-
Stream $stream,
73-
callable $bodyDataCallback,
74-
callable $trailersCallback,
70+
private readonly Request $request,
71+
private readonly Stream $stream,
72+
private readonly \Closure $bodyDataCallback,
73+
private readonly \Closure $trailersCallback,
7574
) {
76-
$this->request = $request;
77-
$this->stream = $stream;
78-
$this->bodyDataCallback = $bodyDataCallback;
79-
$this->trailersCallback = $trailersCallback;
8075
$this->maxHeaderBytes = $request->getHeaderSizeLimit();
8176
$this->maxBodyBytes = $request->getBodySizeLimit();
8277
}

src/HttpClient.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111
*/
1212
final class HttpClient implements DelegateHttpClient
1313
{
14-
private DelegateHttpClient $httpClient;
15-
16-
/** @var EventListener[] */
17-
private array $eventListeners;
18-
19-
public function __construct(DelegateHttpClient $httpClient, array $eventListeners)
20-
{
21-
$this->httpClient = $httpClient;
22-
$this->eventListeners = $eventListeners;
14+
/**
15+
* @param EventListener[] $eventListeners
16+
*/
17+
public function __construct(
18+
private readonly DelegateHttpClient $httpClient,
19+
private readonly array $eventListeners,
20+
) {
2321
}
2422

2523
/**
@@ -32,7 +30,7 @@ public function request(Request $request, ?Cancellation $cancellation = null): R
3230
return processRequest(
3331
$request,
3432
$this->eventListeners,
35-
fn () => $this->httpClient->request($request, $cancellation ?? new NullCancellation())
33+
fn () => $this->httpClient->request($request, $cancellation ?? new NullCancellation()),
3634
);
3735
}
3836
}

src/InterceptedHttpClient.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,22 @@ final class InterceptedHttpClient implements DelegateHttpClient
1313

1414
private static \WeakMap $requestInterceptors;
1515

16-
private DelegateHttpClient $httpClient;
17-
18-
private ApplicationInterceptor $interceptor;
19-
20-
/** @var EventListener[] */
21-
private array $eventListeners;
22-
16+
/**
17+
* @param EventListener[] $eventListeners
18+
*/
2319
public function __construct(
24-
DelegateHttpClient $httpClient,
25-
ApplicationInterceptor $interceptor,
26-
array $eventListeners
20+
private readonly DelegateHttpClient $httpClient,
21+
private readonly ApplicationInterceptor $interceptor,
22+
private readonly array $eventListeners,
2723
) {
28-
$this->httpClient = $httpClient;
29-
$this->interceptor = $interceptor;
30-
$this->eventListeners = $eventListeners;
3124
}
3225

3326
public function request(Request $request, Cancellation $cancellation): Response
3427
{
3528
return processRequest($request, $this->eventListeners, function () use ($request, $cancellation) {
3629
/** @psalm-suppress RedundantPropertyInitializationCheck */
3730
self::$requestInterceptors ??= new \WeakMap();
31+
3832
$requestInterceptors = self::$requestInterceptors[$request] ?? [];
3933
$requestInterceptors[] = $this->interceptor;
4034
self::$requestInterceptors[$request] = $requestInterceptors;

src/Interceptor/DecompressResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class DecompressResponse implements NetworkInterceptor
1717
use ForbidCloning;
1818
use ForbidSerialization;
1919

20-
private bool $hasZlib;
20+
private readonly bool $hasZlib;
2121

2222
public function __construct()
2323
{

src/Interceptor/FollowRedirects.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ private static function mergePaths(string $basePath, string $pathToMerge): strin
105105
return self::removeDotSegments(\implode('/', $parts));
106106
}
107107

108-
private int $maxRedirects;
108+
private readonly int $maxRedirects;
109109

110-
private bool $autoReferrer;
110+
private readonly bool $autoReferrer;
111111

112112
public function __construct(int $limit, bool $autoReferrer = true)
113113
{

0 commit comments

Comments
 (0)