Skip to content

Commit 9e1c1b6

Browse files
author
Andrii
committed
📦 [php-http#458] Introduce property types, use PHP 8.1 features (CPP, short arrow function, readonly properties, etc)
1 parent 840e7ef commit 9e1c1b6

28 files changed

+168
-576
lines changed

src/ClientFactory/BuzzFactory.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313
*/
1414
class BuzzFactory implements ClientFactory
1515
{
16-
/**
17-
* @var ResponseFactoryInterface
18-
*/
19-
private $responseFactory;
20-
21-
public function __construct(ResponseFactoryInterface $responseFactory)
16+
public function __construct(private readonly ResponseFactoryInterface $responseFactory)
2217
{
23-
$this->responseFactory = $responseFactory;
2418
}
2519

2620
/**

src/ClientFactory/CurlFactory.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,8 @@
1313
*/
1414
class CurlFactory implements ClientFactory
1515
{
16-
/**
17-
* @var ResponseFactoryInterface
18-
*/
19-
private $responseFactory;
20-
21-
/**
22-
* @var StreamFactoryInterface
23-
*/
24-
private $streamFactory;
25-
26-
public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
16+
public function __construct(private readonly ResponseFactoryInterface $responseFactory, private readonly StreamFactoryInterface $streamFactory)
2717
{
28-
$this->responseFactory = $responseFactory;
29-
$this->streamFactory = $streamFactory;
3018
}
3119

3220
/**

src/ClientFactory/MockFactory.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
*/
1313
final class MockFactory implements ClientFactory
1414
{
15-
/**
16-
* @var ClientInterface
17-
*/
18-
private $client;
15+
private ?ClientInterface $client = null;
1916

2017
/**
2118
* Set the client instance that this factory should return.

src/ClientFactory/SymfonyFactory.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,8 @@
1414
*/
1515
class SymfonyFactory implements ClientFactory
1616
{
17-
/**
18-
* @var ResponseFactoryInterface
19-
*/
20-
private $responseFactory;
21-
22-
/**
23-
* @var StreamFactoryInterface
24-
*/
25-
private $streamFactory;
26-
27-
public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory)
17+
public function __construct(private readonly ResponseFactoryInterface $responseFactory, private readonly StreamFactoryInterface $streamFactory)
2818
{
29-
$this->responseFactory = $responseFactory;
30-
$this->streamFactory = $streamFactory;
3119
}
3220

3321
/**

src/Collector/Collector.php

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@
2121
*/
2222
class Collector extends DataCollector
2323
{
24-
/**
25-
* @var Stack|null
26-
*/
27-
private $activeStack;
28-
29-
/**
30-
* @var int|null
31-
*/
32-
private $capturedBodyLength;
24+
private ?Stack $activeStack = null;
3325

34-
public function __construct(?int $capturedBodyLength = null)
26+
public function __construct(private ?int $capturedBodyLength = null)
3527
{
3628
$this->reset();
37-
$this->capturedBodyLength = $capturedBodyLength;
29+
}
30+
31+
public function __wakeup(): void
32+
{
33+
$this->capturedBodyLength = null;
34+
35+
parent::__wakeup();
3836
}
3937

4038
/**
@@ -97,9 +95,7 @@ public function addStack(Stack $stack)
9795
*/
9896
public function getChildrenStacks(Stack $parent)
9997
{
100-
return array_filter($this->data['stacks'], function (Stack $stack) use ($parent) {
101-
return $stack->getParent() === $parent;
102-
});
98+
return array_filter($this->data['stacks'], fn(Stack $stack) => $stack->getParent() === $parent);
10399
}
104100

105101
/**
@@ -115,33 +111,25 @@ public function getStacks()
115111
*/
116112
public function getSuccessfulStacks()
117113
{
118-
return array_filter($this->data['stacks'], function (Stack $stack) {
119-
return !$stack->isFailed();
120-
});
114+
return array_filter($this->data['stacks'], fn(Stack $stack) => !$stack->isFailed());
121115
}
122116

123117
/**
124118
* @return Stack[]
125119
*/
126120
public function getFailedStacks()
127121
{
128-
return array_filter($this->data['stacks'], function (Stack $stack) {
129-
return $stack->isFailed();
130-
});
122+
return array_filter($this->data['stacks'], fn(Stack $stack) => $stack->isFailed());
131123
}
132124

133125
/**
134126
* @return array
135127
*/
136128
public function getClients()
137129
{
138-
$stacks = array_filter($this->data['stacks'], function (Stack $stack) {
139-
return null === $stack->getParent();
140-
});
130+
$stacks = array_filter($this->data['stacks'], fn(Stack $stack) => null === $stack->getParent());
141131

142-
return array_unique(array_map(function (Stack $stack) {
143-
return $stack->getClient();
144-
}, $stacks));
132+
return array_unique(array_map(fn(Stack $stack) => $stack->getClient(), $stacks));
145133
}
146134

147135
/**
@@ -151,9 +139,7 @@ public function getClients()
151139
*/
152140
public function getClientRootStacks($client)
153141
{
154-
return array_filter($this->data['stacks'], function (Stack $stack) use ($client) {
155-
return $stack->getClient() == $client && null == $stack->getParent();
156-
});
142+
return array_filter($this->data['stacks'], fn(Stack $stack) => $stack->getClient() == $client && null == $stack->getParent());
157143
}
158144

159145
/**
@@ -165,9 +151,7 @@ public function getClientRootStacks($client)
165151
*/
166152
public function countClientMessages($client)
167153
{
168-
return array_sum(array_map(function (Stack $stack) {
169-
return $this->countStackMessages($stack);
170-
}, $this->getClientRootStacks($client)));
154+
return array_sum(array_map(fn(Stack $stack) => $this->countStackMessages($stack), $this->getClientRootStacks($client)));
171155
}
172156

173157
/**
@@ -177,19 +161,15 @@ public function countClientMessages($client)
177161
*/
178162
private function countStackMessages(Stack $stack)
179163
{
180-
return 1 + array_sum(array_map(function (Stack $child) {
181-
return $this->countStackMessages($child);
182-
}, $this->getChildrenStacks($stack)));
164+
return 1 + array_sum(array_map(fn(Stack $child) => $this->countStackMessages($child), $this->getChildrenStacks($stack)));
183165
}
184166

185167
/**
186168
* @return int
187169
*/
188170
public function getTotalDuration()
189171
{
190-
return array_reduce($this->data['stacks'], function ($carry, Stack $stack) {
191-
return $carry + $stack->getDuration();
192-
}, 0);
172+
return array_reduce($this->data['stacks'], fn($carry, Stack $stack) => $carry + $stack->getDuration(), 0);
193173
}
194174

195175
/**

src/Collector/Formatter.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,8 @@
2323
*/
2424
class Formatter implements MessageFormatter
2525
{
26-
/**
27-
* @var MessageFormatter
28-
*/
29-
private $formatter;
30-
31-
/**
32-
* @var CurlCommandFormatter
33-
*/
34-
private $curlFormatter;
35-
36-
public function __construct(MessageFormatter $formatter, MessageFormatter $curlFormatter)
26+
public function __construct(private readonly MessageFormatter $formatter, private readonly MessageFormatter $curlFormatter)
3727
{
38-
$this->formatter = $formatter;
39-
$this->curlFormatter = $curlFormatter;
4028
}
4129

4230
/**
@@ -54,7 +42,7 @@ public function formatException(\Throwable $exception)
5442
return sprintf('Transfer error: %s', $exception->getMessage());
5543
}
5644

57-
return sprintf('Unexpected exception of type "%s": %s', get_class($exception), $exception->getMessage());
45+
return sprintf('Unexpected exception of type "%s": %s', $exception::class, $exception->getMessage());
5846
}
5947

6048
/**

src/Collector/PluginClientFactory.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,8 @@
2020
*/
2121
final class PluginClientFactory
2222
{
23-
/**
24-
* @var Collector
25-
*/
26-
private $collector;
27-
28-
/**
29-
* @var Formatter
30-
*/
31-
private $formatter;
32-
33-
/**
34-
* @var Stopwatch
35-
*/
36-
private $stopwatch;
37-
38-
public function __construct(Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
23+
public function __construct(private readonly Collector $collector, private readonly Formatter $formatter, private readonly Stopwatch $stopwatch)
3924
{
40-
$this->collector = $collector;
41-
$this->formatter = $formatter;
42-
$this->stopwatch = $stopwatch;
4325
}
4426

4527
/**
@@ -56,9 +38,7 @@ public function __construct(Collector $collector, Formatter $formatter, Stopwatc
5638
*/
5739
public function createClient($client, array $plugins = [], array $options = [])
5840
{
59-
$plugins = array_map(function (Plugin $plugin) {
60-
return new ProfilePlugin($plugin, $this->collector, $this->formatter);
61-
}, $plugins);
41+
$plugins = array_map(fn(Plugin $plugin) => new ProfilePlugin($plugin, $this->collector, $this->formatter), $plugins);
6242

6343
$clientName = $options['client_name'] ?? 'Default';
6444
array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName));

src/Collector/PluginClientFactoryListener.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,16 @@
1919
*/
2020
final class PluginClientFactoryListener implements EventSubscriberInterface
2121
{
22-
/**
23-
* @var PluginClientFactory
24-
*/
25-
private $factory;
26-
27-
public function __construct(PluginClientFactory $factory)
22+
public function __construct(private readonly PluginClientFactory $factory)
2823
{
29-
$this->factory = $factory;
3024
}
3125

3226
/**
3327
* Make sure to profile clients created using PluginClientFactory.
3428
*/
3529
public function onEvent(Event $e)
3630
{
37-
DefaultPluginClientFactory::setFactory([$this->factory, 'createClient']);
31+
DefaultPluginClientFactory::setFactory($this->factory->createClient(...));
3832
}
3933

4034
/**

src/Collector/Profile.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,14 @@
1313
*/
1414
final class Profile
1515
{
16-
/**
17-
* @var string
18-
*/
19-
private $plugin;
16+
private string $request;
2017

21-
/**
22-
* @var string
23-
*/
24-
private $request;
18+
private string $response;
2519

26-
/**
27-
* @var string
28-
*/
29-
private $response;
20+
private bool $failed = false;
3021

31-
/**
32-
* @var bool
33-
*/
34-
private $failed = false;
35-
36-
/**
37-
* @param string $plugin
38-
*/
39-
public function __construct($plugin)
22+
public function __construct(private readonly string $plugin)
4023
{
41-
$this->plugin = $plugin;
4224
}
4325

4426
/**

src/Collector/ProfileClient.php

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,23 @@ class ProfileClient implements ClientInterface, HttpAsyncClient
2626
{
2727
use VersionBridgeClient;
2828

29-
/**
30-
* @var ClientInterface&HttpAsyncClient
31-
*/
32-
private $client;
29+
private ClientInterface&HttpAsyncClient $client;
3330

34-
/**
35-
* @var Collector
36-
*/
37-
private $collector;
38-
39-
/**
40-
* @var Formatter
41-
*/
42-
private $formatter;
43-
44-
/**
45-
* @var Stopwatch
46-
*/
47-
private $stopwatch;
48-
49-
/**
50-
* @var array
51-
*/
52-
private $eventNames = [];
31+
private array $eventNames = [];
5332

5433
private const STOPWATCH_CATEGORY = 'httplug';
5534

5635
/**
5736
* @param ClientInterface|HttpAsyncClient $client The client to profile. Client must implement HttpClient or
5837
* HttpAsyncClient interface.
5938
*/
60-
public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch)
39+
public function __construct($client, private readonly Collector $collector, private readonly Formatter $formatter, private readonly Stopwatch $stopwatch)
6140
{
6241
if (!($client instanceof ClientInterface && $client instanceof HttpAsyncClient)) {
6342
$client = new FlexibleHttpClient($client);
6443
}
6544

6645
$this->client = $client;
67-
$this->collector = $collector;
68-
$this->formatter = $formatter;
69-
$this->stopwatch = $stopwatch;
7046
}
7147

7248
public function sendAsyncRequest(RequestInterface $request)
@@ -92,7 +68,7 @@ public function sendAsyncRequest(RequestInterface $request)
9268
return $response;
9369
};
9470

95-
$onRejected = function (\Exception $exception) use ($event, $stack) {
71+
$onRejected = function (\Exception $exception) use ($event, $stack): void {
9672
$this->collectExceptionInformations($exception, $event, $stack);
9773
$event->stop();
9874

0 commit comments

Comments
 (0)