Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare parameter and return types #473

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
- Removed the `DummyClient` interface
- Removed the `Http\Client\HttpClient` alias use the `Psr\Http\Client\ClientInterface` typehint in your services for autowiring.
- 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.
- Added return type declaration to `Http\HttplugBundle\ClientFactory\ClientFactory::createClient`

# Version 1

Expand Down
3 changes: 2 additions & 1 deletion src/ClientFactory/AutoDiscoveryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Http\HttplugBundle\ClientFactory;

use Http\Discovery\Psr18ClientDiscovery;
use Psr\Http\Client\ClientInterface;

/**
* Use auto discovery to find a HTTP client.
Expand All @@ -13,7 +14,7 @@
*/
final class AutoDiscoveryFactory implements ClientFactory
{
public function createClient(array $config = [])
public function createClient(array $config = []): ClientInterface
{
return Psr18ClientDiscovery::find();
}
Expand Down
5 changes: 3 additions & 2 deletions src/ClientFactory/BuzzFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Http\HttplugBundle\ClientFactory;

use Buzz\Client\FileGetContents;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -17,7 +18,7 @@ public function __construct(private readonly ResponseFactoryInterface $responseF
{
}

public function createClient(array $config = [])
public function createClient(array $config = []): ClientInterface
{
if (!class_exists('Buzz\Client\FileGetContents')) {
throw new \LogicException('To use the Buzz you need to install the "kriswallsmith/buzz" package.');
Expand All @@ -29,7 +30,7 @@ public function createClient(array $config = [])
/**
* Get options to configure the Buzz client.
*/
private function getOptions(array $config = [])
private function getOptions(array $config = []): array
{
$resolver = new OptionsResolver();

Expand Down
4 changes: 1 addition & 3 deletions src/ClientFactory/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ interface ClientFactory
{
/**
* Input an array of configuration to be able to create a ClientInterface.
*
* @return ClientInterface
*/
public function createClient(array $config = []);
public function createClient(array $config = []): ClientInterface;
}
3 changes: 2 additions & 1 deletion src/ClientFactory/CurlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Http\HttplugBundle\ClientFactory;

use Http\Client\Curl\Client;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

Expand All @@ -19,7 +20,7 @@ public function __construct(
) {
}

public function createClient(array $config = [])
public function createClient(array $config = []): ClientInterface
{
if (!class_exists('Http\Client\Curl\Client')) {
throw new \LogicException('To use the Curl client you need to install the "php-http/curl-client" package.');
Expand Down
3 changes: 2 additions & 1 deletion src/ClientFactory/Guzzle6Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Http\HttplugBundle\ClientFactory;

use Http\Adapter\Guzzle6\Client;
use Psr\Http\Client\ClientInterface;

/**
* @author Tobias Nyholm <[email protected]>
*/
final class Guzzle6Factory implements ClientFactory
{
public function createClient(array $config = [])
public function createClient(array $config = []): ClientInterface
{
if (!class_exists('Http\Adapter\Guzzle6\Client')) {
throw new \LogicException('To use the Guzzle6 adapter you need to install the "php-http/guzzle6-adapter" package.');
Expand Down
3 changes: 2 additions & 1 deletion src/ClientFactory/Guzzle7Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Http\HttplugBundle\ClientFactory;

use Http\Adapter\Guzzle7\Client;
use Psr\Http\Client\ClientInterface;

/**
* @author Tobias Nyholm <[email protected]>
*/
final class Guzzle7Factory implements ClientFactory
{
public function createClient(array $config = [])
public function createClient(array $config = []): ClientInterface
{
if (!class_exists('Http\Adapter\Guzzle7\Client')) {
throw new \LogicException('To use the Guzzle7 adapter you need to install the "php-http/guzzle7-adapter" package.');
Expand Down
4 changes: 2 additions & 2 deletions src/ClientFactory/MockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class MockFactory implements ClientFactory
*
* Note that this can be any client, not only a mock client.
*/
public function setClient(ClientInterface $client)
public function setClient(ClientInterface $client): void
{
$this->client = $client;
}

public function createClient(array $config = [])
public function createClient(array $config = []): ClientInterface
{
if (!class_exists(Client::class)) {
throw new \LogicException('To use the mock adapter you need to install the "php-http/mock-client" package.');
Expand Down
3 changes: 2 additions & 1 deletion src/ClientFactory/ReactFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Http\HttplugBundle\ClientFactory;

use Http\Adapter\React\Client;
use Psr\Http\Client\ClientInterface;

/**
* @author Tobias Nyholm <[email protected]>
*/
final class ReactFactory implements ClientFactory
{
public function createClient(array $config = [])
public function createClient(array $config = []): ClientInterface
{
if (!class_exists('Http\Adapter\React\Client')) {
throw new \LogicException('To use the React adapter you need to install the "php-http/react-adapter" package.');
Expand Down
3 changes: 2 additions & 1 deletion src/ClientFactory/SocketFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Http\HttplugBundle\ClientFactory;

use Http\Client\Socket\Client;
use Psr\Http\Client\ClientInterface;

/**
* @author Tobias Nyholm <[email protected]>
*/
final class SocketFactory implements ClientFactory
{
public function createClient(array $config = [])
public function createClient(array $config = []): ClientInterface
{
if (!class_exists('Http\Client\Socket\Client')) {
throw new \LogicException('To use the Socket client you need to install the "php-http/socket-client" package.');
Expand Down
3 changes: 2 additions & 1 deletion src/ClientFactory/SymfonyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Http\HttplugBundle\ClientFactory;

use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Symfony\Component\HttpClient\HttpClient;
Expand All @@ -20,7 +21,7 @@ public function __construct(
) {
}

public function createClient(array $config = [])
public function createClient(array $config = []): ClientInterface
{
if (!class_exists(HttplugClient::class)) {
throw new \LogicException('To use the Symfony client you need to install the "symfony/http-client" package.');
Expand Down
52 changes: 21 additions & 31 deletions src/Collector/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getCapturedBodyLength(): ?int
/**
* Mark the stack as active. If a stack was already active, use it as parent for our stack.
*/
public function activateStack(Stack $stack)
public function activateStack(Stack $stack): void
{
if (null !== $this->activeStack) {
$stack->setParent($this->activeStack);
Expand All @@ -66,100 +66,90 @@ public function activateStack(Stack $stack)
/**
* Mark the stack as inactive.
*/
public function deactivateStack(Stack $stack)
public function deactivateStack(Stack $stack): void
{
$this->activeStack = $stack->getParent();
}

/**
* @return Stack|null
*/
public function getActiveStack()
public function getActiveStack(): ?Stack
{
return $this->activeStack;
}

public function addStack(Stack $stack)
public function addStack(Stack $stack): void
{
$this->data['stacks'][] = $stack;
}

/**
* @return Stack[]
*/
public function getChildrenStacks(Stack $parent)
public function getChildrenStacks(Stack $parent): array
{
return array_filter($this->data['stacks'], fn (Stack $stack) => $stack->getParent() === $parent);
return array_filter($this->data['stacks'], static fn (Stack $stack) => $stack->getParent() === $parent);
}

/**
* @return Stack[]
*/
public function getStacks()
public function getStacks(): array
{
return $this->data['stacks'];
}

/**
* @return Stack[]
*/
public function getSuccessfulStacks()
public function getSuccessfulStacks(): array
{
return array_filter($this->data['stacks'], fn (Stack $stack) => !$stack->isFailed());
return array_filter($this->data['stacks'], static fn (Stack $stack) => !$stack->isFailed());
}

/**
* @return Stack[]
*/
public function getFailedStacks()
public function getFailedStacks(): array
{
return array_filter($this->data['stacks'], fn (Stack $stack) => $stack->isFailed());
return array_filter($this->data['stacks'], static fn (Stack $stack) => $stack->isFailed());
}

/**
* @return array
* @return string[]
*/
public function getClients()
public function getClients(): array
{
$stacks = array_filter($this->data['stacks'], fn (Stack $stack) => null === $stack->getParent());
$stacks = array_filter($this->data['stacks'], static fn (Stack $stack) => null === $stack->getParent());

return array_unique(array_map(fn (Stack $stack) => $stack->getClient(), $stacks));
return array_unique(array_map(static fn (Stack $stack) => $stack->getClient(), $stacks));
}

/**
* @return Stack[]
*/
public function getClientRootStacks($client)
public function getClientRootStacks(string $client): array
{
return array_filter($this->data['stacks'], fn (Stack $stack) => $stack->getClient() == $client && null == $stack->getParent());
return array_filter($this->data['stacks'], static fn (Stack $stack) => $stack->getClient() == $client && null == $stack->getParent());
}

/**
* Count all messages for a client.
*
* @return int
*/
public function countClientMessages($client)
public function countClientMessages(string $client): int
{
return array_sum(array_map(fn (Stack $stack) => $this->countStackMessages($stack), $this->getClientRootStacks($client)));
}

/**
* Recursively count message in stack.
*
* @return int
*/
private function countStackMessages(Stack $stack)
private function countStackMessages(Stack $stack): int
{
return 1 + array_sum(array_map(fn (Stack $child) => $this->countStackMessages($child), $this->getChildrenStacks($stack)));
}

/**
* @return int
*/
public function getTotalDuration()
public function getTotalDuration(): int
{
return array_reduce($this->data['stacks'], fn ($carry, Stack $stack) => $carry + $stack->getDuration(), 0);
return array_reduce($this->data['stacks'], static fn ($carry, Stack $stack) => $carry + $stack->getDuration(), 0);
}

public function collect(Request $request, Response $response, $exception = null): void
Expand Down
19 changes: 6 additions & 13 deletions src/Collector/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ public function __construct(
) {
}

/**
* Formats an exception.
*
* @return string
*/
public function formatException(\Throwable $exception)
public function formatException(\Throwable $exception): string
{
if ($exception instanceof HttpException) {
return $this->formatter->formatResponseForRequest($exception->getResponse(), $exception->getRequest());
Expand All @@ -45,12 +40,12 @@ public function formatException(\Throwable $exception)
return sprintf('Unexpected exception of type "%s": %s', $exception::class, $exception->getMessage());
}

public function formatRequest(RequestInterface $request)
public function formatRequest(RequestInterface $request): string
{
return $this->formatter->formatRequest($request);
}

public function formatResponseForRequest(ResponseInterface $response, RequestInterface $request)
public function formatResponseForRequest(ResponseInterface $response, RequestInterface $request): string
{
if (method_exists($this->formatter, 'formatResponseForRequest')) {
return $this->formatter->formatResponseForRequest($response, $request);
Expand All @@ -59,17 +54,15 @@ public function formatResponseForRequest(ResponseInterface $response, RequestInt
return $this->formatter->formatResponse($response);
}

public function formatResponse(ResponseInterface $response)
public function formatResponse(ResponseInterface $response): string
{
return $this->formatter->formatResponse($response);
}

/**
* Format a RequestInterface as a cURL command.
*
* @return string
* Format the RequestInterface as a cURL command that can be copied to the command line.
*/
public function formatAsCurlCommand(RequestInterface $request)
public function formatAsCurlCommand(RequestInterface $request): string
{
return $this->curlFormatter->formatRequest($request);
}
Expand Down
13 changes: 5 additions & 8 deletions src/Collector/PluginClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@ public function __construct(
}

/**
* @param ClientInterface|HttpAsyncClient $client
* @param Plugin[] $plugins
* @param array{client_name?: string} $options
* @param Plugin[] $plugins
* @param array{client_name?: string} $options
*
* - client_name: to give client a name which may be used when displaying client information like in
* the HTTPlugBundle profiler
* Options:
* - client_name: to give client a name which may be used when displaying client information like in the HTTPlugBundle profiler
*
* @see PluginClient constructor for PluginClient specific $options.
*
* @return PluginClient
*/
public function createClient($client, array $plugins = [], array $options = [])
public function createClient(HttpAsyncClient|ClientInterface $client, array $plugins = [], array $options = []): PluginClient
{
$plugins = array_map(fn (Plugin $plugin) => new ProfilePlugin($plugin, $this->collector, $this->formatter), $plugins);

Expand Down
2 changes: 1 addition & 1 deletion src/Collector/PluginClientFactoryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(private readonly PluginClientFactory $factory)
/**
* Make sure to profile clients created using PluginClientFactory.
*/
public function onEvent(Event $e)
public function onEvent(Event $e): void
{
DefaultPluginClientFactory::setFactory($this->factory->createClient(...));
}
Expand Down
Loading