From a996b9bf6f573bc8b6f4a933e0fdd34f0fb5775b Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Thu, 5 Sep 2024 08:15:14 +0200 Subject: [PATCH] Declare parameter and return types --- CHANGELOG.md | 1 + src/ClientFactory/AutoDiscoveryFactory.php | 3 +- src/ClientFactory/BuzzFactory.php | 5 +- src/ClientFactory/ClientFactory.php | 4 +- src/ClientFactory/CurlFactory.php | 3 +- src/ClientFactory/Guzzle6Factory.php | 3 +- src/ClientFactory/Guzzle7Factory.php | 3 +- src/ClientFactory/MockFactory.php | 4 +- src/ClientFactory/ReactFactory.php | 3 +- src/ClientFactory/SocketFactory.php | 3 +- src/ClientFactory/SymfonyFactory.php | 3 +- src/Collector/Collector.php | 52 +++--- src/Collector/Formatter.php | 19 +-- src/Collector/PluginClientFactory.php | 13 +- src/Collector/PluginClientFactoryListener.php | 2 +- src/Collector/Profile.php | 35 +---- src/Collector/ProfileClient.php | 31 ++-- src/Collector/ProfileClientFactory.php | 2 +- src/Collector/Stack.php | 148 ++++-------------- .../Twig/HttpMessageMarkupExtension.php | 6 +- src/DependencyInjection/Configuration.php | 21 +-- src/Discovery/ConfiguredClientsStrategy.php | 2 +- .../ConfiguredClientsStrategyListener.php | 2 +- tests/Unit/Collector/FormatterTest.php | 9 +- tests/Unit/Collector/ProfileClientTest.php | 16 +- tests/Unit/Collector/ProfilePluginTest.php | 22 +-- 26 files changed, 137 insertions(+), 278 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6240893c..cd418619 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/ClientFactory/AutoDiscoveryFactory.php b/src/ClientFactory/AutoDiscoveryFactory.php index 8a675696..860d6a5f 100644 --- a/src/ClientFactory/AutoDiscoveryFactory.php +++ b/src/ClientFactory/AutoDiscoveryFactory.php @@ -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. @@ -13,7 +14,7 @@ */ final class AutoDiscoveryFactory implements ClientFactory { - public function createClient(array $config = []) + public function createClient(array $config = []): ClientInterface { return Psr18ClientDiscovery::find(); } diff --git a/src/ClientFactory/BuzzFactory.php b/src/ClientFactory/BuzzFactory.php index 02eadff1..39d12d10 100644 --- a/src/ClientFactory/BuzzFactory.php +++ b/src/ClientFactory/BuzzFactory.php @@ -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; @@ -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.'); @@ -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(); diff --git a/src/ClientFactory/ClientFactory.php b/src/ClientFactory/ClientFactory.php index 9009c5c4..1f6b5700 100644 --- a/src/ClientFactory/ClientFactory.php +++ b/src/ClientFactory/ClientFactory.php @@ -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; } diff --git a/src/ClientFactory/CurlFactory.php b/src/ClientFactory/CurlFactory.php index 054a5c36..ea4ba289 100644 --- a/src/ClientFactory/CurlFactory.php +++ b/src/ClientFactory/CurlFactory.php @@ -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; @@ -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.'); diff --git a/src/ClientFactory/Guzzle6Factory.php b/src/ClientFactory/Guzzle6Factory.php index a1930ed5..65dd8fae 100644 --- a/src/ClientFactory/Guzzle6Factory.php +++ b/src/ClientFactory/Guzzle6Factory.php @@ -5,13 +5,14 @@ namespace Http\HttplugBundle\ClientFactory; use Http\Adapter\Guzzle6\Client; +use Psr\Http\Client\ClientInterface; /** * @author Tobias Nyholm */ 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.'); diff --git a/src/ClientFactory/Guzzle7Factory.php b/src/ClientFactory/Guzzle7Factory.php index 8a5cbbe0..dd5656b6 100644 --- a/src/ClientFactory/Guzzle7Factory.php +++ b/src/ClientFactory/Guzzle7Factory.php @@ -5,13 +5,14 @@ namespace Http\HttplugBundle\ClientFactory; use Http\Adapter\Guzzle7\Client; +use Psr\Http\Client\ClientInterface; /** * @author Tobias Nyholm */ 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.'); diff --git a/src/ClientFactory/MockFactory.php b/src/ClientFactory/MockFactory.php index 1d994812..68807187 100644 --- a/src/ClientFactory/MockFactory.php +++ b/src/ClientFactory/MockFactory.php @@ -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.'); diff --git a/src/ClientFactory/ReactFactory.php b/src/ClientFactory/ReactFactory.php index f98c9ae5..4995dd0b 100644 --- a/src/ClientFactory/ReactFactory.php +++ b/src/ClientFactory/ReactFactory.php @@ -5,13 +5,14 @@ namespace Http\HttplugBundle\ClientFactory; use Http\Adapter\React\Client; +use Psr\Http\Client\ClientInterface; /** * @author Tobias Nyholm */ 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.'); diff --git a/src/ClientFactory/SocketFactory.php b/src/ClientFactory/SocketFactory.php index b70f8eaa..59636276 100644 --- a/src/ClientFactory/SocketFactory.php +++ b/src/ClientFactory/SocketFactory.php @@ -5,13 +5,14 @@ namespace Http\HttplugBundle\ClientFactory; use Http\Client\Socket\Client; +use Psr\Http\Client\ClientInterface; /** * @author Tobias Nyholm */ 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.'); diff --git a/src/ClientFactory/SymfonyFactory.php b/src/ClientFactory/SymfonyFactory.php index c52568c6..0aaa6275 100644 --- a/src/ClientFactory/SymfonyFactory.php +++ b/src/ClientFactory/SymfonyFactory.php @@ -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; @@ -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.'); diff --git a/src/Collector/Collector.php b/src/Collector/Collector.php index 50a4aa8c..89a23d04 100644 --- a/src/Collector/Collector.php +++ b/src/Collector/Collector.php @@ -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); @@ -66,20 +66,17 @@ 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; } @@ -87,15 +84,15 @@ public function addStack(Stack $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']; } @@ -103,63 +100,56 @@ public function getStacks() /** * @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 diff --git a/src/Collector/Formatter.php b/src/Collector/Formatter.php index f2032f24..a39a003a 100644 --- a/src/Collector/Formatter.php +++ b/src/Collector/Formatter.php @@ -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()); @@ -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); @@ -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); } diff --git a/src/Collector/PluginClientFactory.php b/src/Collector/PluginClientFactory.php index 9402514d..ed79c13b 100644 --- a/src/Collector/PluginClientFactory.php +++ b/src/Collector/PluginClientFactory.php @@ -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); diff --git a/src/Collector/PluginClientFactoryListener.php b/src/Collector/PluginClientFactoryListener.php index f1135fa1..256d88ab 100644 --- a/src/Collector/PluginClientFactoryListener.php +++ b/src/Collector/PluginClientFactoryListener.php @@ -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(...)); } diff --git a/src/Collector/Profile.php b/src/Collector/Profile.php index 9288e8bc..9f608411 100644 --- a/src/Collector/Profile.php +++ b/src/Collector/Profile.php @@ -23,58 +23,37 @@ public function __construct(private readonly string $plugin) { } - /** - * @return string - */ - public function getPlugin() + public function getPlugin(): string { return $this->plugin; } - /** - * @return string|null - */ - public function getRequest() + public function getRequest(): ?string { return $this->request; } - /** - * @param string $request - */ - public function setRequest($request) + public function setRequest(string $request): void { $this->request = $request; } - /** - * @return string|null - */ - public function getResponse() + public function getResponse(): ?string { return $this->response; } - /** - * @param string $response - */ - public function setResponse($response) + public function setResponse(string $response): void { $this->response = $response; } - /** - * @return bool - */ - public function isFailed() + public function isFailed(): bool { return $this->failed; } - /** - * @param bool $failed - */ - public function setFailed($failed) + public function setFailed(bool $failed): void { $this->failed = $failed; } diff --git a/src/Collector/ProfileClient.php b/src/Collector/ProfileClient.php index 8ea52812..9b1bf67d 100644 --- a/src/Collector/ProfileClient.php +++ b/src/Collector/ProfileClient.php @@ -8,6 +8,7 @@ use Http\Client\Common\VersionBridgeClient; use Http\Client\Exception\HttpException; use Http\Client\HttpAsyncClient; +use Http\Promise\Promise; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -32,12 +33,8 @@ final class ProfileClient implements ClientInterface, HttpAsyncClient private const STOPWATCH_CATEGORY = 'httplug'; - /** - * @param ClientInterface|HttpAsyncClient $client The client to profile. Client must implement HttpClient or - * HttpAsyncClient interface. - */ public function __construct( - $client, + HttpAsyncClient|ClientInterface $client, private readonly Collector $collector, private readonly Formatter $formatter, private readonly Stopwatch $stopwatch, @@ -49,7 +46,7 @@ public function __construct( $this->client = $client; } - public function sendAsyncRequest(RequestInterface $request) + public function sendAsyncRequest(RequestInterface $request): Promise { $activateStack = true; $stack = $this->collector->getActiveStack(); @@ -62,18 +59,18 @@ public function sendAsyncRequest(RequestInterface $request) $activateStack = false; } - $this->collectRequestInformations($request, $stack); + $this->collectRequestInformation($request, $stack); $event = $this->stopwatch->start($this->getStopwatchEventName($request), self::STOPWATCH_CATEGORY); $onFulfilled = function (ResponseInterface $response) use ($request, $event, $stack) { - $this->collectResponseInformations($request, $response, $event, $stack); + $this->collectResponseInformation($request, $response, $event, $stack); $event->stop(); return $response; }; $onRejected = function (\Exception $exception) use ($event, $stack): void { - $this->collectExceptionInformations($exception, $event, $stack); + $this->collectExceptionInformation($exception, $event, $stack); $event->stop(); throw $exception; @@ -95,7 +92,7 @@ public function sendAsyncRequest(RequestInterface $request) } } - protected function doSendRequest(RequestInterface $request) + protected function doSendRequest(RequestInterface $request): ResponseInterface { $stack = $this->collector->getActiveStack(); if (null === $stack) { @@ -105,16 +102,16 @@ protected function doSendRequest(RequestInterface $request) $this->collector->addStack($stack); } - $this->collectRequestInformations($request, $stack); + $this->collectRequestInformation($request, $stack); $event = $this->stopwatch->start($this->getStopwatchEventName($request), self::STOPWATCH_CATEGORY); try { $response = $this->client->sendRequest($request); - $this->collectResponseInformations($request, $response, $event, $stack); + $this->collectResponseInformation($request, $response, $event, $stack); return $response; } catch (\Throwable $e) { - $this->collectExceptionInformations($e, $event, $stack); + $this->collectExceptionInformation($e, $event, $stack); throw $e; } finally { @@ -122,7 +119,7 @@ protected function doSendRequest(RequestInterface $request) } } - private function collectRequestInformations(RequestInterface $request, Stack $stack): void + private function collectRequestInformation(RequestInterface $request, Stack $stack): void { $uri = $request->getUri(); $stack->setRequestTarget($request->getRequestTarget()); @@ -134,17 +131,17 @@ private function collectRequestInformations(RequestInterface $request, Stack $st $stack->setCurlCommand($this->formatter->formatAsCurlCommand($request)); } - private function collectResponseInformations(RequestInterface $request, ResponseInterface $response, StopwatchEvent $event, Stack $stack): void + private function collectResponseInformation(RequestInterface $request, ResponseInterface $response, StopwatchEvent $event, Stack $stack): void { $stack->setDuration((int) $event->getDuration()); $stack->setResponseCode($response->getStatusCode()); $stack->setClientResponse($this->formatter->formatResponseForRequest($response, $request)); } - private function collectExceptionInformations(\Throwable $exception, StopwatchEvent $event, Stack $stack): void + private function collectExceptionInformation(\Throwable $exception, StopwatchEvent $event, Stack $stack): void { if ($exception instanceof HttpException) { - $this->collectResponseInformations($exception->getRequest(), $exception->getResponse(), $event, $stack); + $this->collectResponseInformation($exception->getRequest(), $exception->getResponse(), $event, $stack); } $stack->setDuration((int) $event->getDuration()); diff --git a/src/Collector/ProfileClientFactory.php b/src/Collector/ProfileClientFactory.php index 3715fd4d..17e60f0e 100644 --- a/src/Collector/ProfileClientFactory.php +++ b/src/Collector/ProfileClientFactory.php @@ -33,7 +33,7 @@ public function __construct( $this->factory = $factory; } - public function createClient(array $config = []) + public function createClient(array $config = []): ClientInterface { $client = is_callable($this->factory) ? call_user_func($this->factory, $config) : $this->factory->createClient($config); diff --git a/src/Collector/Stack.php b/src/Collector/Stack.php index c48b7808..fc2a370e 100644 --- a/src/Collector/Stack.php +++ b/src/Collector/Stack.php @@ -52,28 +52,22 @@ public function __construct( ) { } - /** - * @return string - */ - public function getClient() + public function getClient(): string { return $this->client; } - /** - * @return Stack|null - */ - public function getParent() + public function getParent(): ?Stack { return $this->parent; } - public function setParent(self $parent) + public function setParent(self $parent): void { $this->parent = $parent; } - public function addProfile(Profile $profile) + public function addProfile(Profile $profile): void { $this->profiles[] = $profile; } @@ -81,175 +75,112 @@ public function addProfile(Profile $profile) /** * @return Profile[] */ - public function getProfiles() + public function getProfiles(): array { return $this->profiles; } - /** - * @return string - */ - public function getRequest() + public function getRequest(): string { return $this->request; } - /** - * @return string|null - */ - public function getResponse() + public function getResponse(): ?string { return $this->response; } - /** - * @param string $response - */ - public function setResponse($response) + public function setResponse(string $response): void { $this->response = $response; } - /** - * @return bool - */ - public function isFailed() + public function isFailed(): bool { return $this->failed; } - /** - * @param bool $failed - */ - public function setFailed($failed) + public function setFailed(bool $failed): void { $this->failed = $failed; } - /** - * @return string|null - */ - public function getRequestTarget() + public function getRequestTarget(): ?string { return $this->requestTarget; } - /** - * @param string $requestTarget - */ - public function setRequestTarget($requestTarget) + public function setRequestTarget(string $requestTarget): void { $this->requestTarget = $requestTarget; } - /** - * @return string|null - */ - public function getRequestMethod() + public function getRequestMethod(): ?string { return $this->requestMethod; } - /** - * @param string $requestMethod - */ - public function setRequestMethod($requestMethod) + public function setRequestMethod(string $requestMethod): void { $this->requestMethod = $requestMethod; } - /** - * @return string|null - */ - public function getClientRequest() + public function getClientRequest(): ?string { return $this->clientRequest; } - /** - * @param string $clientRequest - */ - public function setClientRequest($clientRequest) + public function setClientRequest(string $clientRequest): void { $this->clientRequest = $clientRequest; } - /** - * @return string|null - */ - public function getClientResponse() + public function getClientResponse(): ?string { return $this->clientResponse; } - /** - * @param string $clientResponse - */ - public function setClientResponse($clientResponse) + public function setClientResponse(string $clientResponse): void { $this->clientResponse = $clientResponse; } - /** - * @return string|null - */ - public function getClientException() + public function getClientException(): ?string { return $this->clientException; } - /** - * @param string $clientException - */ - public function setClientException($clientException) + public function setClientException(string $clientException): void { $this->clientException = $clientException; } - /** - * @return int|null - */ - public function getResponseCode() + public function getResponseCode(): ?int { return $this->responseCode; } - /** - * @param int $responseCode - */ - public function setResponseCode($responseCode) + public function setResponseCode(int $responseCode): void { $this->responseCode = $responseCode; } - /** - * @return string|null - */ - public function getRequestHost() + public function getRequestHost(): ?string { return $this->requestHost; } - /** - * @param string $requestHost - */ - public function setRequestHost($requestHost) + public function setRequestHost(string $requestHost): void { $this->requestHost = $requestHost; } - /** - * @return string|null - */ - public function getRequestScheme() + public function getRequestScheme(): ?string { return $this->requestScheme; } - /** - * @param string $requestScheme - */ - public function setRequestScheme($requestScheme) + public function setRequestScheme(string $requestScheme): void { $this->requestScheme = $requestScheme; } @@ -259,47 +190,32 @@ public function getRequestPort(): ?int return $this->requestPort; } - public function setRequestPort(?int $port) + public function setRequestPort(?int $port): void { $this->requestPort = $port; } - /** - * @return int - */ - public function getDuration() + public function getDuration(): int { return $this->duration; } - /** - * @param int $duration - */ - public function setDuration($duration) + public function setDuration(int $duration): void { $this->duration = $duration; } - /** - * @return string|null - */ - public function getCurlCommand() + public function getCurlCommand(): ?string { return $this->curlCommand; } - /** - * @param string $curlCommand - */ - public function setCurlCommand($curlCommand) + public function setCurlCommand(string $curlCommand): void { $this->curlCommand = $curlCommand; } - /** - * @return string - */ - public function getClientSlug() + public function getClientSlug(): string { return preg_replace('/[^a-zA-Z0-9_-]/u', '_', $this->client); } diff --git a/src/Collector/Twig/HttpMessageMarkupExtension.php b/src/Collector/Twig/HttpMessageMarkupExtension.php index 5876ba74..10bcf417 100644 --- a/src/Collector/Twig/HttpMessageMarkupExtension.php +++ b/src/Collector/Twig/HttpMessageMarkupExtension.php @@ -27,9 +27,9 @@ public function __construct(?ClonerInterface $cloner = null, ?DataDumperInterfac } /** - * @return array + * @return TwigFilter[] */ - public function getFilters() + public function getFilters(): array { return [ new TwigFilter('httplug_markup', $this->markup(...), ['is_safe' => ['html']]), @@ -74,7 +74,7 @@ public function markupBody(string $body): ?string return $this->dumper->dump($this->cloner->cloneVar($body)); } - public function getName() + public function getName(): string { return 'httplug.message_markup'; } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 133bee8b..a27de5a1 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -34,19 +34,14 @@ */ final class Configuration implements ConfigurationInterface { - /** - * Whether to use the debug mode. - * - * @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41 - */ - private bool $debug; - - /** - * @param bool $debug - */ - public function __construct($debug) - { - $this->debug = (bool) $debug; + public function __construct( + /** + * Whether to use the debug mode. + * + * @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41 + */ + private bool $debug, + ) { } public function getConfigTreeBuilder(): TreeBuilder diff --git a/src/Discovery/ConfiguredClientsStrategy.php b/src/Discovery/ConfiguredClientsStrategy.php index 50c21b16..df02f3e2 100644 --- a/src/Discovery/ConfiguredClientsStrategy.php +++ b/src/Discovery/ConfiguredClientsStrategy.php @@ -28,7 +28,7 @@ public function __construct(?ClientInterface $httpClient = null, ?HttpAsyncClien Psr18ClientDiscovery::clearCache(); } - public static function getCandidates($type) + public static function getCandidates($type): array { if (ClientInterface::class === $type && null !== self::$client) { return [['class' => fn () => self::$client]]; diff --git a/src/Discovery/ConfiguredClientsStrategyListener.php b/src/Discovery/ConfiguredClientsStrategyListener.php index d57cfc35..4a82753a 100644 --- a/src/Discovery/ConfiguredClientsStrategyListener.php +++ b/src/Discovery/ConfiguredClientsStrategyListener.php @@ -15,7 +15,7 @@ final class ConfiguredClientsStrategyListener implements EventSubscriberInterfac /** * Make sure to use the custom strategy. */ - public function onEvent() + public function onEvent(): void { Psr18ClientDiscovery::prependStrategy(ConfiguredClientsStrategy::class); } diff --git a/tests/Unit/Collector/FormatterTest.php b/tests/Unit/Collector/FormatterTest.php index 09bdb06d..3b2aaddc 100644 --- a/tests/Unit/Collector/FormatterTest.php +++ b/tests/Unit/Collector/FormatterTest.php @@ -39,9 +39,10 @@ public function testFormatRequest(): void ->expects($this->once()) ->method('formatRequest') ->with($this->identicalTo($request)) + ->willReturn('foo') ; - $this->subject->formatRequest($request); + $this->assertSame('foo', $this->subject->formatRequest($request)); } public function testFormatResponseForRequest(): void @@ -56,9 +57,10 @@ public function testFormatResponseForRequest(): void ->expects($this->once()) ->method('formatResponseForRequest') ->with($this->identicalTo($response), $this->identicalTo($request)) + ->willReturn('foo') ; - $subject->formatResponseForRequest($response, $request); + $this->assertSame('foo', $subject->formatResponseForRequest($response, $request)); } /** @@ -72,9 +74,10 @@ public function testFormatResponse(): void ->expects($this->once()) ->method('formatResponse') ->with($this->identicalTo($response)) + ->willReturn('foo') ; - $this->subject->formatResponse($response); + $this->assertSame('foo', $this->subject->formatResponse($response)); } public function testFormatHttpException(): void diff --git a/tests/Unit/Collector/ProfileClientTest.php b/tests/Unit/Collector/ProfileClientTest.php index 2188fac0..bd078a41 100644 --- a/tests/Unit/Collector/ProfileClientTest.php +++ b/tests/Unit/Collector/ProfileClientTest.php @@ -12,7 +12,7 @@ use Http\HttplugBundle\Collector\Formatter; use Http\HttplugBundle\Collector\ProfileClient; use Http\HttplugBundle\Collector\Stack; -use Http\Message\Formatter as MessageFormatter; +use Http\Message\Formatter\CurlCommandFormatter; use Http\Message\Formatter\SimpleFormatter; use Http\Promise\FulfilledPromise; use Http\Promise\Promise; @@ -45,8 +45,8 @@ final class ProfileClientTest extends TestCase public function setUp(): void { - $messageFormatter = $this->createMock(SimpleFormatter::class); - $formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class)); + $messageFormatter = new SimpleFormatter(); + $formatter = new Formatter($messageFormatter, new CurlCommandFormatter()); $this->collector = new Collector(); $stopwatch = $this->createMock(Stopwatch::class); @@ -62,12 +62,6 @@ public function setUp(): void $this->fulfilledPromise = new FulfilledPromise($this->response); $this->rejectedPromise = new RejectedPromise($exception); - $messageFormatter - ->method('formatResponseForRequest') - ->with($this->response, $this->request) - ->willReturn('FormattedResponse') - ; - $stopwatch ->method('start') ->willReturn($this->stopwatchEvent) @@ -99,7 +93,7 @@ public function testSendRequest(): void $this->assertEquals('https', $activeStack->getRequestScheme()); } - public function testSendRequestTypeError() + public function testSendRequestTypeError(): void { $this->client ->expects($this->once()) @@ -151,7 +145,7 @@ public function testOnFulfilled(): void $this->assertInstanceOf(Stack::class, $activeStack); $this->assertEquals(42, $activeStack->getDuration()); $this->assertEquals(200, $activeStack->getResponseCode()); - $this->assertEquals('FormattedResponse', $activeStack->getClientResponse()); + $this->assertEquals('200 OK 1.1', $activeStack->getClientResponse()); } public function testOnRejected(): void diff --git a/tests/Unit/Collector/ProfilePluginTest.php b/tests/Unit/Collector/ProfilePluginTest.php index e5dd268a..bce10a2f 100644 --- a/tests/Unit/Collector/ProfilePluginTest.php +++ b/tests/Unit/Collector/ProfilePluginTest.php @@ -12,7 +12,7 @@ use Http\HttplugBundle\Collector\Formatter; use Http\HttplugBundle\Collector\ProfilePlugin; use Http\HttplugBundle\Collector\Stack; -use Http\Message\Formatter as MessageFormatter; +use Http\Message\Formatter\CurlCommandFormatter; use Http\Message\Formatter\SimpleFormatter; use Http\Promise\FulfilledPromise; use Http\Promise\Promise; @@ -43,8 +43,8 @@ final class ProfilePluginTest extends TestCase public function setUp(): void { $this->collector = new Collector(); - $messageFormatter = $this->createMock(SimpleFormatter::class); - $formatter = new Formatter($messageFormatter, $this->createMock(MessageFormatter::class)); + $messageFormatter = new SimpleFormatter(); + $formatter = new Formatter($messageFormatter, new CurlCommandFormatter()); $this->plugin = $this->createMock(Plugin::class); $this->request = new Request('GET', '/'); @@ -60,18 +60,6 @@ public function setUp(): void ->willReturnCallback(fn ($request, $next, $first) => $next($request)) ; - $messageFormatter - ->method('formatRequest') - ->with($this->identicalTo($this->request)) - ->willReturn('FormattedRequest') - ; - - $messageFormatter - ->method('formatResponseForRequest') - ->with($this->identicalTo($this->response), $this->identicalTo($this->request)) - ->willReturn('FormattedResponse') - ; - $this->subject = new ProfilePlugin( $this->plugin, $this->collector, @@ -110,7 +98,7 @@ public function testCollectRequestInformations(): void $activeStack = $this->collector->getActiveStack(); $this->assertCount(1, $activeStack->getProfiles()); $profile = $activeStack->getProfiles()[0]; - $this->assertEquals('FormattedRequest', $profile->getRequest()); + $this->assertEquals('GET / 1.1', $profile->getRequest()); } public function testOnFulfilled(): void @@ -123,7 +111,7 @@ public function testOnFulfilled(): void $activeStack = $this->collector->getActiveStack(); $this->assertCount(1, $activeStack->getProfiles()); $profile = $activeStack->getProfiles()[0]; - $this->assertEquals('FormattedResponse', $profile->getResponse()); + $this->assertEquals('200 OK 1.1', $profile->getResponse()); } public function testOnRejected(): void