Skip to content

Commit 91a3dc0

Browse files
committed
Declare parameter and return types
1 parent 4d1ca68 commit 91a3dc0

26 files changed

+137
-279
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
1818
- Changed the type of `httplug.client.default` to `ClientInterface` instead of `HttpClient`
1919
- Removed the `DummyClient` interface
2020
- Removed the `Http\Client\HttpClient` alias use the `Psr\Http\Client\ClientInterface` typehint in your services for autowiring.
21+
- Added return type declaration to `Http\HttplugBundle\ClientFactory\ClientFactory::createClient`
2122

2223
# Version 1
2324

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.
@@ -15,7 +16,7 @@
1516
*/
1617
class AutoDiscoveryFactory implements ClientFactory
1718
{
18-
public function createClient(array $config = [])
19+
public function createClient(array $config = []): ClientInterface
1920
{
2021
return Psr18ClientDiscovery::find();
2122
}

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

@@ -19,7 +20,7 @@ public function __construct(private readonly ResponseFactoryInterface $responseF
1920
{
2021
}
2122

22-
public function createClient(array $config = [])
23+
public function createClient(array $config = []): ClientInterface
2324
{
2425
if (!class_exists('Buzz\Client\FileGetContents')) {
2526
throw new \LogicException('To use the Buzz you need to install the "kriswallsmith/buzz" package.');
@@ -31,7 +32,7 @@ public function createClient(array $config = [])
3132
/**
3233
* Get options to configure the Buzz client.
3334
*/
34-
private function getOptions(array $config = [])
35+
private function getOptions(array $config = []): array
3536
{
3637
$resolver = new OptionsResolver();
3738

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

@@ -21,7 +22,7 @@ public function __construct(
2122
) {
2223
}
2324

24-
public function createClient(array $config = [])
25+
public function createClient(array $config = []): ClientInterface
2526
{
2627
if (!class_exists('Http\Client\Curl\Client')) {
2728
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,6 +5,7 @@
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]>
@@ -13,7 +14,7 @@
1314
*/
1415
class Guzzle6Factory implements ClientFactory
1516
{
16-
public function createClient(array $config = [])
17+
public function createClient(array $config = []): ClientInterface
1718
{
1819
if (!class_exists('Http\Adapter\Guzzle6\Client')) {
1920
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,6 +5,7 @@
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]>
@@ -13,7 +14,7 @@
1314
*/
1415
class Guzzle7Factory implements ClientFactory
1516
{
16-
public function createClient(array $config = [])
17+
public function createClient(array $config = []): ClientInterface
1718
{
1819
if (!class_exists('Http\Adapter\Guzzle7\Client')) {
1920
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,6 +5,7 @@
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]>
@@ -13,7 +14,7 @@
1314
*/
1415
class ReactFactory implements ClientFactory
1516
{
16-
public function createClient(array $config = [])
17+
public function createClient(array $config = []): ClientInterface
1718
{
1819
if (!class_exists('Http\Adapter\React\Client')) {
1920
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,6 +5,7 @@
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]>
@@ -13,7 +14,7 @@
1314
*/
1415
class SocketFactory implements ClientFactory
1516
{
16-
public function createClient(array $config = [])
17+
public function createClient(array $config = []): ClientInterface
1718
{
1819
if (!class_exists('Http\Client\Socket\Client')) {
1920
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;
@@ -22,7 +23,7 @@ public function __construct(
2223
) {
2324
}
2425

25-
public function createClient(array $config = [])
26+
public function createClient(array $config = []): ClientInterface
2627
{
2728
if (!class_exists(HttplugClient::class)) {
2829
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
@@ -56,7 +56,7 @@ public function getCapturedBodyLength(): ?int
5656
/**
5757
* Mark the stack as active. If a stack was already active, use it as parent for our stack.
5858
*/
59-
public function activateStack(Stack $stack)
59+
public function activateStack(Stack $stack): void
6060
{
6161
if (null !== $this->activeStack) {
6262
$stack->setParent($this->activeStack);
@@ -68,100 +68,90 @@ public function activateStack(Stack $stack)
6868
/**
6969
* Mark the stack as inactive.
7070
*/
71-
public function deactivateStack(Stack $stack)
71+
public function deactivateStack(Stack $stack): void
7272
{
7373
$this->activeStack = $stack->getParent();
7474
}
7575

76-
/**
77-
* @return Stack|null
78-
*/
79-
public function getActiveStack()
76+
public function getActiveStack(): ?Stack
8077
{
8178
return $this->activeStack;
8279
}
8380

84-
public function addStack(Stack $stack)
81+
public function addStack(Stack $stack): void
8582
{
8683
$this->data['stacks'][] = $stack;
8784
}
8885

8986
/**
9087
* @return Stack[]
9188
*/
92-
public function getChildrenStacks(Stack $parent)
89+
public function getChildrenStacks(Stack $parent): array
9390
{
94-
return array_filter($this->data['stacks'], fn (Stack $stack) => $stack->getParent() === $parent);
91+
return array_filter($this->data['stacks'], static fn (Stack $stack) => $stack->getParent() === $parent);
9592
}
9693

9794
/**
9895
* @return Stack[]
9996
*/
100-
public function getStacks()
97+
public function getStacks(): array
10198
{
10299
return $this->data['stacks'];
103100
}
104101

105102
/**
106103
* @return Stack[]
107104
*/
108-
public function getSuccessfulStacks()
105+
public function getSuccessfulStacks(): array
109106
{
110-
return array_filter($this->data['stacks'], fn (Stack $stack) => !$stack->isFailed());
107+
return array_filter($this->data['stacks'], static fn (Stack $stack) => !$stack->isFailed());
111108
}
112109

113110
/**
114111
* @return Stack[]
115112
*/
116-
public function getFailedStacks()
113+
public function getFailedStacks(): array
117114
{
118-
return array_filter($this->data['stacks'], fn (Stack $stack) => $stack->isFailed());
115+
return array_filter($this->data['stacks'], static fn (Stack $stack) => $stack->isFailed());
119116
}
120117

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

128-
return array_unique(array_map(fn (Stack $stack) => $stack->getClient(), $stacks));
125+
return array_unique(array_map(static fn (Stack $stack) => $stack->getClient(), $stacks));
129126
}
130127

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

139136
/**
140137
* Count all messages for a client.
141-
*
142-
* @return int
143138
*/
144-
public function countClientMessages($client)
139+
public function countClientMessages(string $client): int
145140
{
146141
return array_sum(array_map(fn (Stack $stack) => $this->countStackMessages($stack), $this->getClientRootStacks($client)));
147142
}
148143

149144
/**
150145
* Recursively count message in stack.
151-
*
152-
* @return int
153146
*/
154-
private function countStackMessages(Stack $stack)
147+
private function countStackMessages(Stack $stack): int
155148
{
156149
return 1 + array_sum(array_map(fn (Stack $child) => $this->countStackMessages($child), $this->getChildrenStacks($stack)));
157150
}
158151

159-
/**
160-
* @return int
161-
*/
162-
public function getTotalDuration()
152+
public function getTotalDuration(): int
163153
{
164-
return array_reduce($this->data['stacks'], fn ($carry, Stack $stack) => $carry + $stack->getDuration(), 0);
154+
return array_reduce($this->data['stacks'], static fn ($carry, Stack $stack) => $carry + $stack->getDuration(), 0);
165155
}
166156

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

src/Collector/Formatter.php

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

55
namespace Http\HttplugBundle\Collector;
66

7-
use Exception;
87
use Http\Client\Exception\HttpException;
98
use Http\Client\Exception\TransferException;
109
use Http\Message\Formatter as MessageFormatter;
@@ -30,12 +29,7 @@ public function __construct(
3029
) {
3130
}
3231

33-
/**
34-
* Formats an exception.
35-
*
36-
* @return string
37-
*/
38-
public function formatException(\Throwable $exception)
32+
public function formatException(\Throwable $exception): string
3933
{
4034
if ($exception instanceof HttpException) {
4135
return $this->formatter->formatResponseForRequest($exception->getResponse(), $exception->getRequest());
@@ -48,12 +42,12 @@ public function formatException(\Throwable $exception)
4842
return sprintf('Unexpected exception of type "%s": %s', $exception::class, $exception->getMessage());
4943
}
5044

51-
public function formatRequest(RequestInterface $request)
45+
public function formatRequest(RequestInterface $request): string
5246
{
5347
return $this->formatter->formatRequest($request);
5448
}
5549

56-
public function formatResponseForRequest(ResponseInterface $response, RequestInterface $request)
50+
public function formatResponseForRequest(ResponseInterface $response, RequestInterface $request): string
5751
{
5852
if (method_exists($this->formatter, 'formatResponseForRequest')) {
5953
return $this->formatter->formatResponseForRequest($response, $request);
@@ -62,17 +56,15 @@ public function formatResponseForRequest(ResponseInterface $response, RequestInt
6256
return $this->formatter->formatResponse($response);
6357
}
6458

65-
public function formatResponse(ResponseInterface $response)
59+
public function formatResponse(ResponseInterface $response): string
6660
{
6761
return $this->formatter->formatResponse($response);
6862
}
6963

7064
/**
71-
* Format a RequestInterface as a cURL command.
72-
*
73-
* @return string
65+
* Format the RequestInterface as a cURL command that can be copied to the command line.
7466
*/
75-
public function formatAsCurlCommand(RequestInterface $request)
67+
public function formatAsCurlCommand(RequestInterface $request): string
7668
{
7769
return $this->curlFormatter->formatRequest($request);
7870
}

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)