Skip to content

Commit b621fbd

Browse files
authored
Merge pull request #473 from php-http/strict-typing
Declare parameter and return types
2 parents ffeb988 + a996b9b commit b621fbd

26 files changed

+137
-278
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
2020
- Removed the `DummyClient` interface
2121
- Removed the `Http\Client\HttpClient` alias use the `Psr\Http\Client\ClientInterface` typehint in your services for autowiring.
2222
- Changed classes marked as `@final` to be actually `final`. If you extended any of those, instead implement interfaces or decorate the class rather than extending it. Open an issue if you think a class needs to be made non-final to discuss what we should do.
23+
- Added return type declaration to `Http\HttplugBundle\ClientFactory\ClientFactory::createClient`
2324

2425
# Version 1
2526

src/ClientFactory/AutoDiscoveryFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Http\HttplugBundle\ClientFactory;
66

77
use Http\Discovery\Psr18ClientDiscovery;
8+
use Psr\Http\Client\ClientInterface;
89

910
/**
1011
* Use auto discovery to find a HTTP client.
@@ -13,7 +14,7 @@
1314
*/
1415
final class AutoDiscoveryFactory implements ClientFactory
1516
{
16-
public function createClient(array $config = [])
17+
public function createClient(array $config = []): ClientInterface
1718
{
1819
return Psr18ClientDiscovery::find();
1920
}

src/ClientFactory/BuzzFactory.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Http\HttplugBundle\ClientFactory;
66

77
use Buzz\Client\FileGetContents;
8+
use Psr\Http\Client\ClientInterface;
89
use Psr\Http\Message\ResponseFactoryInterface;
910
use Symfony\Component\OptionsResolver\OptionsResolver;
1011

@@ -17,7 +18,7 @@ public function __construct(private readonly ResponseFactoryInterface $responseF
1718
{
1819
}
1920

20-
public function createClient(array $config = [])
21+
public function createClient(array $config = []): ClientInterface
2122
{
2223
if (!class_exists('Buzz\Client\FileGetContents')) {
2324
throw new \LogicException('To use the Buzz you need to install the "kriswallsmith/buzz" package.');
@@ -29,7 +30,7 @@ public function createClient(array $config = [])
2930
/**
3031
* Get options to configure the Buzz client.
3132
*/
32-
private function getOptions(array $config = [])
33+
private function getOptions(array $config = []): array
3334
{
3435
$resolver = new OptionsResolver();
3536

src/ClientFactory/ClientFactory.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ interface ClientFactory
1313
{
1414
/**
1515
* Input an array of configuration to be able to create a ClientInterface.
16-
*
17-
* @return ClientInterface
1816
*/
19-
public function createClient(array $config = []);
17+
public function createClient(array $config = []): ClientInterface;
2018
}

src/ClientFactory/CurlFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Http\HttplugBundle\ClientFactory;
66

77
use Http\Client\Curl\Client;
8+
use Psr\Http\Client\ClientInterface;
89
use Psr\Http\Message\ResponseFactoryInterface;
910
use Psr\Http\Message\StreamFactoryInterface;
1011

@@ -19,7 +20,7 @@ public function __construct(
1920
) {
2021
}
2122

22-
public function createClient(array $config = [])
23+
public function createClient(array $config = []): ClientInterface
2324
{
2425
if (!class_exists('Http\Client\Curl\Client')) {
2526
throw new \LogicException('To use the Curl client you need to install the "php-http/curl-client" package.');

src/ClientFactory/Guzzle6Factory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
namespace Http\HttplugBundle\ClientFactory;
66

77
use Http\Adapter\Guzzle6\Client;
8+
use Psr\Http\Client\ClientInterface;
89

910
/**
1011
* @author Tobias Nyholm <[email protected]>
1112
*/
1213
final class Guzzle6Factory implements ClientFactory
1314
{
14-
public function createClient(array $config = [])
15+
public function createClient(array $config = []): ClientInterface
1516
{
1617
if (!class_exists('Http\Adapter\Guzzle6\Client')) {
1718
throw new \LogicException('To use the Guzzle6 adapter you need to install the "php-http/guzzle6-adapter" package.');

src/ClientFactory/Guzzle7Factory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
namespace Http\HttplugBundle\ClientFactory;
66

77
use Http\Adapter\Guzzle7\Client;
8+
use Psr\Http\Client\ClientInterface;
89

910
/**
1011
* @author Tobias Nyholm <[email protected]>
1112
*/
1213
final class Guzzle7Factory implements ClientFactory
1314
{
14-
public function createClient(array $config = [])
15+
public function createClient(array $config = []): ClientInterface
1516
{
1617
if (!class_exists('Http\Adapter\Guzzle7\Client')) {
1718
throw new \LogicException('To use the Guzzle7 adapter you need to install the "php-http/guzzle7-adapter" package.');

src/ClientFactory/MockFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class MockFactory implements ClientFactory
1919
*
2020
* Note that this can be any client, not only a mock client.
2121
*/
22-
public function setClient(ClientInterface $client)
22+
public function setClient(ClientInterface $client): void
2323
{
2424
$this->client = $client;
2525
}
2626

27-
public function createClient(array $config = [])
27+
public function createClient(array $config = []): ClientInterface
2828
{
2929
if (!class_exists(Client::class)) {
3030
throw new \LogicException('To use the mock adapter you need to install the "php-http/mock-client" package.');

src/ClientFactory/ReactFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
namespace Http\HttplugBundle\ClientFactory;
66

77
use Http\Adapter\React\Client;
8+
use Psr\Http\Client\ClientInterface;
89

910
/**
1011
* @author Tobias Nyholm <[email protected]>
1112
*/
1213
final class ReactFactory implements ClientFactory
1314
{
14-
public function createClient(array $config = [])
15+
public function createClient(array $config = []): ClientInterface
1516
{
1617
if (!class_exists('Http\Adapter\React\Client')) {
1718
throw new \LogicException('To use the React adapter you need to install the "php-http/react-adapter" package.');

src/ClientFactory/SocketFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
namespace Http\HttplugBundle\ClientFactory;
66

77
use Http\Client\Socket\Client;
8+
use Psr\Http\Client\ClientInterface;
89

910
/**
1011
* @author Tobias Nyholm <[email protected]>
1112
*/
1213
final class SocketFactory implements ClientFactory
1314
{
14-
public function createClient(array $config = [])
15+
public function createClient(array $config = []): ClientInterface
1516
{
1617
if (!class_exists('Http\Client\Socket\Client')) {
1718
throw new \LogicException('To use the Socket client you need to install the "php-http/socket-client" package.');

src/ClientFactory/SymfonyFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Http\HttplugBundle\ClientFactory;
66

7+
use Psr\Http\Client\ClientInterface;
78
use Psr\Http\Message\ResponseFactoryInterface;
89
use Psr\Http\Message\StreamFactoryInterface;
910
use Symfony\Component\HttpClient\HttpClient;
@@ -20,7 +21,7 @@ public function __construct(
2021
) {
2122
}
2223

23-
public function createClient(array $config = [])
24+
public function createClient(array $config = []): ClientInterface
2425
{
2526
if (!class_exists(HttplugClient::class)) {
2627
throw new \LogicException('To use the Symfony client you need to install the "symfony/http-client" package.');

src/Collector/Collector.php

+21-31
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function getCapturedBodyLength(): ?int
5454
/**
5555
* Mark the stack as active. If a stack was already active, use it as parent for our stack.
5656
*/
57-
public function activateStack(Stack $stack)
57+
public function activateStack(Stack $stack): void
5858
{
5959
if (null !== $this->activeStack) {
6060
$stack->setParent($this->activeStack);
@@ -66,100 +66,90 @@ public function activateStack(Stack $stack)
6666
/**
6767
* Mark the stack as inactive.
6868
*/
69-
public function deactivateStack(Stack $stack)
69+
public function deactivateStack(Stack $stack): void
7070
{
7171
$this->activeStack = $stack->getParent();
7272
}
7373

74-
/**
75-
* @return Stack|null
76-
*/
77-
public function getActiveStack()
74+
public function getActiveStack(): ?Stack
7875
{
7976
return $this->activeStack;
8077
}
8178

82-
public function addStack(Stack $stack)
79+
public function addStack(Stack $stack): void
8380
{
8481
$this->data['stacks'][] = $stack;
8582
}
8683

8784
/**
8885
* @return Stack[]
8986
*/
90-
public function getChildrenStacks(Stack $parent)
87+
public function getChildrenStacks(Stack $parent): array
9188
{
92-
return array_filter($this->data['stacks'], fn (Stack $stack) => $stack->getParent() === $parent);
89+
return array_filter($this->data['stacks'], static fn (Stack $stack) => $stack->getParent() === $parent);
9390
}
9491

9592
/**
9693
* @return Stack[]
9794
*/
98-
public function getStacks()
95+
public function getStacks(): array
9996
{
10097
return $this->data['stacks'];
10198
}
10299

103100
/**
104101
* @return Stack[]
105102
*/
106-
public function getSuccessfulStacks()
103+
public function getSuccessfulStacks(): array
107104
{
108-
return array_filter($this->data['stacks'], fn (Stack $stack) => !$stack->isFailed());
105+
return array_filter($this->data['stacks'], static fn (Stack $stack) => !$stack->isFailed());
109106
}
110107

111108
/**
112109
* @return Stack[]
113110
*/
114-
public function getFailedStacks()
111+
public function getFailedStacks(): array
115112
{
116-
return array_filter($this->data['stacks'], fn (Stack $stack) => $stack->isFailed());
113+
return array_filter($this->data['stacks'], static fn (Stack $stack) => $stack->isFailed());
117114
}
118115

119116
/**
120-
* @return array
117+
* @return string[]
121118
*/
122-
public function getClients()
119+
public function getClients(): array
123120
{
124-
$stacks = array_filter($this->data['stacks'], fn (Stack $stack) => null === $stack->getParent());
121+
$stacks = array_filter($this->data['stacks'], static fn (Stack $stack) => null === $stack->getParent());
125122

126-
return array_unique(array_map(fn (Stack $stack) => $stack->getClient(), $stacks));
123+
return array_unique(array_map(static fn (Stack $stack) => $stack->getClient(), $stacks));
127124
}
128125

129126
/**
130127
* @return Stack[]
131128
*/
132-
public function getClientRootStacks($client)
129+
public function getClientRootStacks(string $client): array
133130
{
134-
return array_filter($this->data['stacks'], fn (Stack $stack) => $stack->getClient() == $client && null == $stack->getParent());
131+
return array_filter($this->data['stacks'], static fn (Stack $stack) => $stack->getClient() == $client && null == $stack->getParent());
135132
}
136133

137134
/**
138135
* Count all messages for a client.
139-
*
140-
* @return int
141136
*/
142-
public function countClientMessages($client)
137+
public function countClientMessages(string $client): int
143138
{
144139
return array_sum(array_map(fn (Stack $stack) => $this->countStackMessages($stack), $this->getClientRootStacks($client)));
145140
}
146141

147142
/**
148143
* Recursively count message in stack.
149-
*
150-
* @return int
151144
*/
152-
private function countStackMessages(Stack $stack)
145+
private function countStackMessages(Stack $stack): int
153146
{
154147
return 1 + array_sum(array_map(fn (Stack $child) => $this->countStackMessages($child), $this->getChildrenStacks($stack)));
155148
}
156149

157-
/**
158-
* @return int
159-
*/
160-
public function getTotalDuration()
150+
public function getTotalDuration(): int
161151
{
162-
return array_reduce($this->data['stacks'], fn ($carry, Stack $stack) => $carry + $stack->getDuration(), 0);
152+
return array_reduce($this->data['stacks'], static fn ($carry, Stack $stack) => $carry + $stack->getDuration(), 0);
163153
}
164154

165155
public function collect(Request $request, Response $response, $exception = null): void

src/Collector/Formatter.php

+6-13
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@ public function __construct(
2727
) {
2828
}
2929

30-
/**
31-
* Formats an exception.
32-
*
33-
* @return string
34-
*/
35-
public function formatException(\Throwable $exception)
30+
public function formatException(\Throwable $exception): string
3631
{
3732
if ($exception instanceof HttpException) {
3833
return $this->formatter->formatResponseForRequest($exception->getResponse(), $exception->getRequest());
@@ -45,12 +40,12 @@ public function formatException(\Throwable $exception)
4540
return sprintf('Unexpected exception of type "%s": %s', $exception::class, $exception->getMessage());
4641
}
4742

48-
public function formatRequest(RequestInterface $request)
43+
public function formatRequest(RequestInterface $request): string
4944
{
5045
return $this->formatter->formatRequest($request);
5146
}
5247

53-
public function formatResponseForRequest(ResponseInterface $response, RequestInterface $request)
48+
public function formatResponseForRequest(ResponseInterface $response, RequestInterface $request): string
5449
{
5550
if (method_exists($this->formatter, 'formatResponseForRequest')) {
5651
return $this->formatter->formatResponseForRequest($response, $request);
@@ -59,17 +54,15 @@ public function formatResponseForRequest(ResponseInterface $response, RequestInt
5954
return $this->formatter->formatResponse($response);
6055
}
6156

62-
public function formatResponse(ResponseInterface $response)
57+
public function formatResponse(ResponseInterface $response): string
6358
{
6459
return $this->formatter->formatResponse($response);
6560
}
6661

6762
/**
68-
* Format a RequestInterface as a cURL command.
69-
*
70-
* @return string
63+
* Format the RequestInterface as a cURL command that can be copied to the command line.
7164
*/
72-
public function formatAsCurlCommand(RequestInterface $request)
65+
public function formatAsCurlCommand(RequestInterface $request): string
7366
{
7467
return $this->curlFormatter->formatRequest($request);
7568
}

src/Collector/PluginClientFactory.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,15 @@ public function __construct(
2828
}
2929

3030
/**
31-
* @param ClientInterface|HttpAsyncClient $client
32-
* @param Plugin[] $plugins
33-
* @param array{client_name?: string} $options
31+
* @param Plugin[] $plugins
32+
* @param array{client_name?: string} $options
3433
*
35-
* - client_name: to give client a name which may be used when displaying client information like in
36-
* the HTTPlugBundle profiler
34+
* Options:
35+
* - client_name: to give client a name which may be used when displaying client information like in the HTTPlugBundle profiler
3736
*
3837
* @see PluginClient constructor for PluginClient specific $options.
39-
*
40-
* @return PluginClient
4138
*/
42-
public function createClient($client, array $plugins = [], array $options = [])
39+
public function createClient(HttpAsyncClient|ClientInterface $client, array $plugins = [], array $options = []): PluginClient
4340
{
4441
$plugins = array_map(fn (Plugin $plugin) => new ProfilePlugin($plugin, $this->collector, $this->formatter), $plugins);
4542

src/Collector/PluginClientFactoryListener.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(private readonly PluginClientFactory $factory)
2626
/**
2727
* Make sure to profile clients created using PluginClientFactory.
2828
*/
29-
public function onEvent(Event $e)
29+
public function onEvent(Event $e): void
3030
{
3131
DefaultPluginClientFactory::setFactory($this->factory->createClient(...));
3232
}

0 commit comments

Comments
 (0)