Skip to content

Commit

Permalink
Declare parameter and return types
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Sep 5, 2024
1 parent 4d1ca68 commit a2720a4
Show file tree
Hide file tree
Showing 26 changed files with 135 additions and 283 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
- Changed the type of `httplug.client.default` to `ClientInterface` instead of `HttpClient`
- Removed the `DummyClient` interface
- Removed the `Http\Client\HttpClient` alias use the `Psr\Http\Client\ClientInterface` typehint in your services for autowiring.
- 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 @@ -15,7 +16,7 @@
*/
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 @@ -19,7 +20,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 @@ -31,7 +32,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 @@ -21,7 +22,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,6 +5,7 @@
namespace Http\HttplugBundle\ClientFactory;

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

/**
* @author Tobias Nyholm <[email protected]>
Expand All @@ -13,7 +14,7 @@
*/
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,6 +5,7 @@
namespace Http\HttplugBundle\ClientFactory;

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

/**
* @author Tobias Nyholm <[email protected]>
Expand All @@ -13,7 +14,7 @@
*/
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,6 +5,7 @@
namespace Http\HttplugBundle\ClientFactory;

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

/**
* @author Tobias Nyholm <[email protected]>
Expand All @@ -13,7 +14,7 @@
*/
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,6 +5,7 @@
namespace Http\HttplugBundle\ClientFactory;

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

/**
* @author Tobias Nyholm <[email protected]>
Expand All @@ -13,7 +14,7 @@
*/
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 @@ -22,7 +23,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 @@ -56,7 +56,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 @@ -68,100 +68,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
20 changes: 6 additions & 14 deletions src/Collector/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Http\HttplugBundle\Collector;

use Exception;
use Http\Client\Exception\HttpException;
use Http\Client\Exception\TransferException;
use Http\Message\Formatter as MessageFormatter;
Expand All @@ -30,12 +29,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 @@ -48,12 +42,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 @@ -62,17 +56,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

0 comments on commit a2720a4

Please sign in to comment.