From 60d78692df6e57259ea8dab089b45a65028547ad Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:14:22 +0300 Subject: [PATCH 01/19] :arrow_up: [#458] Update min PHP version to 8.1 --- .github/workflows/continuous-integration.yml | 29 +++++++------------- CHANGELOG.md | 1 + composer.json | 2 +- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 88e0a071..dc803879 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -22,21 +22,12 @@ jobs: include: # Minimum supported Symfony version and lowest PHP version - dependencies: "php-http/guzzle7-adapter php-http/discovery:^1.12" - php-version: "7.3" + php-version: "8.1" dependency-versions: "lowest" symfony-deprecations-helper: "weak" stability: "stable" # Test the latest stable release - - dependencies: "php-http/guzzle7-adapter php-http/vcr-plugin:^1.0@dev" - php-version: "7.3" - symfony-deprecations-helper: "weak" - - dependencies: "php-http/guzzle6-adapter" - php-version: "7.4" - symfony-deprecations-helper: "weak" - - dependencies: "php-http/guzzle7-adapter" - php-version: "8.0" - symfony-deprecations-helper: "weak" - dependencies: "php-http/guzzle7-adapter" php-version: "8.1" symfony-deprecations-helper: "weak" @@ -49,7 +40,7 @@ jobs: # Test with httplug 2.x clients - dependencies: "php-http/guzzle7-adapter php-http/curl-client:^2.0.0 php-http/vcr-plugin:^1.0@dev php-http/socket-client:^2.0" - php-version: "7.3" + php-version: "8.1" symfony-deprecations-helper: "weak" # Latest commit to master @@ -63,17 +54,17 @@ jobs: symfony-deprecations-helper: "weak" # Test maintained versions of Symfony - - dependencies: "php-http/guzzle7-adapter symfony/http-client:^5.4" - symfony-require: "5.4.*" - php-version: "7.3" + - dependencies: "php-http/guzzle7-adapter symfony/http-client:^6.4" + symfony-require: "6.4.*" + php-version: "8.1" symfony-deprecations-helper: "weak" - - dependencies: "php-http/guzzle7-adapter symfony/http-client:^6.0" - symfony-require: "6.3.*" + - dependencies: "php-http/guzzle7-adapter symfony/http-client:^6.4" + symfony-require: "6.4.*" php-version: "8.2" symfony-deprecations-helper: "weak" - - dependencies: "php-http/guzzle7-adapter symfony/http-client:^6.0" - symfony-require: "6.4.*" - php-version: "8.1" + - dependencies: "php-http/guzzle7-adapter symfony/http-client:^7.1" + symfony-require: "7.1.*" + php-version: "8.2" symfony-deprecations-helper: "weak" steps: diff --git a/CHANGELOG.md b/CHANGELOG.md index e241c748..08176801 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee # Version 2 # 2.0.0 - (unreleased) +- Increased min PHP version to 8.1 - Fixed a deprecation when creating a `HttpMethodsClient` via `http_methods_client: true`. Only PSR-17 factories are now passed as constructor arguments. - Changed the default stream factory argument for the cache plugin. This now requires a PSR-17 StreamFactoryInterface instance. - Creating a client using the `BuzzFactory` no longer accepts `verify_peer` and `verify_host` config options. Only a boolean `verify` flag is accepted, covering both previous options. diff --git a/composer.json b/composer.json index bab75c8f..aa667b3b 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ } ], "require": { - "php": "^7.3 || ^8.0", + "php": "^8.1", "php-http/client-common": "^2.0", "php-http/client-implementation": "^1.0", "php-http/discovery": "^1.14", From f896933744b0997ce3a791c4b0cadaa62f42932a Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:14:22 +0300 Subject: [PATCH 02/19] :package: Add typehint to parameter in private method --- src/DependencyInjection/Configuration.php | 4 ++-- src/DependencyInjection/HttplugExtension.php | 16 +++++----------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 217a825e..ca1b219e 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -520,7 +520,7 @@ private function createClientPluginNode(): ArrayNodeDefinition * @param ArrayNodeDefinition $pluginNode the node to add to * @param bool $disableAll Some shared plugins are enabled by default. On the client, all are disabled by default. */ - private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableAll = false): void + private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, bool $disableAll = false): void { $children = $pluginNode->children(); @@ -696,7 +696,7 @@ private function createAuthenticationPluginNode(): NodeDefinition * * @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type') */ - private function validateAuthenticationType(array $expected, array $actual, $authName): void + private function validateAuthenticationType(array $expected, array $actual, string $authName): void { unset($actual['type']); // Empty array is always provided, even if the config is not filled. diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index 3807f95b..4135bb8b 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -195,7 +195,7 @@ private function configurePlugins(ContainerBuilder $container, array $config): v * @param ContainerBuilder $container In case we need to add additional services for this plugin * @param string $serviceId service id of the plugin, in case we need to add additional services for this plugin */ - private function configurePluginByName($name, Definition $definition, array $config, ContainerBuilder $container, $serviceId): void + private function configurePluginByName(string $name, Definition $definition, array $config, ContainerBuilder $container, string $serviceId): void { switch ($name) { case 'cache': @@ -398,10 +398,7 @@ private function configureAuthentication(ContainerBuilder $container, array $con return $pluginServices; } - /** - * @param string $clientName - */ - private function configureClient(ContainerBuilder $container, $clientName, array $arguments): void + private function configureClient(ContainerBuilder $container, string $clientName, array $arguments): void { $serviceId = 'httplug.client.'.$clientName; @@ -499,7 +496,7 @@ function ($id) { * @param string $serviceId Name of the private service to create * @param string $uri String representation of the URI */ - private function createUri(ContainerBuilder $container, $serviceId, $uri): void + private function createUri(ContainerBuilder $container, string $serviceId, string $uri): void { $container ->register($serviceId, UriInterface::class) @@ -555,12 +552,9 @@ public function getConfiguration(array $config, ContainerBuilder $container): ?C /** * Configure a plugin using the parent definition from plugins.xml. * - * @param string $serviceId - * @param string $pluginName - * * @return string configured service id */ - private function configurePlugin(ContainerBuilder $container, $serviceId, $pluginName, array $pluginConfig): string + private function configurePlugin(ContainerBuilder $container, string $serviceId, string $pluginName, array $pluginConfig): string { $pluginServiceId = $serviceId.'.plugin.'.$pluginName; @@ -572,7 +566,7 @@ private function configurePlugin(ContainerBuilder $container, $serviceId, $plugi return $pluginServiceId; } - private function configureVcrPlugin(ContainerBuilder $container, array $config, $prefix): array + private function configureVcrPlugin(ContainerBuilder $container, array $config, string $prefix): array { $recorder = $config['recorder']; $recorderId = in_array($recorder, ['filesystem', 'in_memory']) ? 'httplug.plugin.vcr.recorder.'.$recorder : $recorder; From 3c37a869b74507475e5d5ba48390e537bc05be7f Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:14:22 +0300 Subject: [PATCH 03/19] :package: [#458] Update min Symfony version to `^6.4 || ^7.1` --- composer.json | 28 ++++++++++---------- src/DependencyInjection/HttplugExtension.php | 4 +-- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/composer.json b/composer.json index aa667b3b..107ae049 100644 --- a/composer.json +++ b/composer.json @@ -35,11 +35,11 @@ "php-http/stopwatch-plugin": "^1.2", "psr/http-factory-implementation": "^1.0", "psr/http-message": "^1.0 || ^2.0", - "symfony/config": "^5.4 || ^6.0 || ^7.0", - "symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0", - "symfony/event-dispatcher": "^5.4 || ^6.0 || ^7.0", - "symfony/http-kernel": "^5.4 || ^6.0 || ^7.0", - "symfony/options-resolver": "^5.4 || ^6.0 || ^7.0" + "symfony/config": "^6.4 || ^7.1", + "symfony/dependency-injection": "^6.4 || ^7.1", + "symfony/event-dispatcher": "^6.4 || ^7.1", + "symfony/http-kernel": "^6.4 || ^7.1", + "symfony/options-resolver": "^6.4 || ^7.1" }, "conflict": { "php-http/guzzle6-adapter": "<1.1", @@ -57,15 +57,15 @@ "php-http/cache-plugin": "^1.7", "php-http/mock-client": "^1.2", "php-http/promise": "^1.0", - "symfony/browser-kit": "^5.4 || ^6.0 || ^7.0", - "symfony/cache": "^5.4 || ^6.0 || ^7.0", - "symfony/dom-crawler": "^5.4 || ^6.0 || ^7.0", - "symfony/framework-bundle": "^5.4 || ^6.0 || ^7.0", - "symfony/http-foundation": "^5.4 || ^6.0 || ^7.0", - "symfony/phpunit-bridge": "^7.0.3", - "symfony/stopwatch": "^5.4 || ^6.0 || ^7.0", - "symfony/twig-bundle": "^5.4 || ^6.0 || ^7.0", - "symfony/web-profiler-bundle": "^5.4 || ^6.0 || ^7.0", + "symfony/browser-kit": "^6.4 || ^7.1", + "symfony/cache": "^6.4 || ^7.1", + "symfony/dom-crawler": "^6.4 || ^7.1", + "symfony/framework-bundle": "^6.4 || ^7.1", + "symfony/http-foundation": "^6.4 || ^7.1", + "symfony/phpunit-bridge": "^7.1", + "symfony/stopwatch": "^6.4 || ^7.1", + "symfony/twig-bundle": "^6.4 || ^7.1", + "symfony/web-profiler-bundle": "^6.4 || ^7.1", "twig/twig": "^1.41 || ^2.10 || ^3.0" }, "suggest": { diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index 4135bb8b..891e9802 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -45,10 +45,8 @@ class HttplugExtension extends Extension /** * Used to check is the VCR plugin is installed. - * - * @var bool */ - private $useVcrPlugin = false; + private bool $useVcrPlugin = false; public function load(array $configs, ContainerBuilder $container): void { From 606257f721d3a61811499ca15d43da6adb1ea9f6 Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:14:23 +0300 Subject: [PATCH 04/19] :package: Hint about always non-null return type --- src/DependencyInjection/HttplugExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index 891e9802..db135fb3 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -542,7 +542,7 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra /** * {@inheritdoc} */ - public function getConfiguration(array $config, ContainerBuilder $container): ?ConfigurationInterface + public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface { return new Configuration($container->getParameter('kernel.debug')); } From 053c8c5fb8bb754dbae6c32f0feaf27e5adc0307 Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:14:23 +0300 Subject: [PATCH 05/19] :package: Avoid deprecated DI extension class --- src/DependencyInjection/HttplugExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index db135fb3..d37b3ee7 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -30,9 +30,9 @@ use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Twig\Environment as TwigEnvironment; /** From 7ff923b70cb6f61976722387db370b6e04f9162b Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:14:23 +0300 Subject: [PATCH 06/19] :package: Fix property type in phpdoc --- src/Collector/Twig/HttpMessageMarkupExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Collector/Twig/HttpMessageMarkupExtension.php b/src/Collector/Twig/HttpMessageMarkupExtension.php index 562ebece..ed344a76 100644 --- a/src/Collector/Twig/HttpMessageMarkupExtension.php +++ b/src/Collector/Twig/HttpMessageMarkupExtension.php @@ -22,7 +22,7 @@ class HttpMessageMarkupExtension extends AbstractExtension private $cloner; /** - * @var HtmlDumper + * @var DataDumperInterface */ private $dumper; From 8a11bd04a634916ada6bf0dd0279b51b85a463ce Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:14:23 +0300 Subject: [PATCH 07/19] :package: Avoid invalid argument - cast possible float to the integer --- src/Collector/ProfileClient.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Collector/ProfileClient.php b/src/Collector/ProfileClient.php index 1b3fb812..0a312b57 100644 --- a/src/Collector/ProfileClient.php +++ b/src/Collector/ProfileClient.php @@ -156,7 +156,7 @@ private function collectRequestInformations(RequestInterface $request, Stack $st private function collectResponseInformations(RequestInterface $request, ResponseInterface $response, StopwatchEvent $event, Stack $stack): void { - $stack->setDuration($event->getDuration()); + $stack->setDuration((int) $event->getDuration()); $stack->setResponseCode($response->getStatusCode()); $stack->setClientResponse($this->formatter->formatResponseForRequest($response, $request)); } @@ -167,7 +167,7 @@ private function collectExceptionInformations(\Throwable $exception, StopwatchEv $this->collectResponseInformations($exception->getRequest(), $exception->getResponse(), $event, $stack); } - $stack->setDuration($event->getDuration()); + $stack->setDuration((int) $event->getDuration()); $stack->setClientException($this->formatter->formatException($exception)); } From c51791af5e4c682cae44bbc0b3f866b996601370 Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:14:23 +0300 Subject: [PATCH 08/19] :package: Fix property type in phpdoc - there's should be an intersection type --- src/Collector/ProfileClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Collector/ProfileClient.php b/src/Collector/ProfileClient.php index 0a312b57..45f1cf3f 100644 --- a/src/Collector/ProfileClient.php +++ b/src/Collector/ProfileClient.php @@ -27,7 +27,7 @@ class ProfileClient implements ClientInterface, HttpAsyncClient use VersionBridgeClient; /** - * @var ClientInterface|HttpAsyncClient + * @var ClientInterface&HttpAsyncClient */ private $client; From 82f8822a13734f8c547a904d4cd9209778ed1871 Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:14:23 +0300 Subject: [PATCH 09/19] :bug: Fix property type and return type for parent - can be nullable --- src/Collector/Stack.php | 46 ++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Collector/Stack.php b/src/Collector/Stack.php index 4fec0cd0..9046718b 100644 --- a/src/Collector/Stack.php +++ b/src/Collector/Stack.php @@ -19,7 +19,7 @@ final class Stack private $client; /** - * @var Stack + * @var Stack|null */ private $parent; @@ -34,7 +34,7 @@ final class Stack private $request; /** - * @var string + * @var string|null */ private $response; @@ -44,22 +44,22 @@ final class Stack private $failed = false; /** - * @var string + * @var string|null */ private $requestTarget; /** - * @var string + * @var string|null */ private $requestMethod; /** - * @var string + * @var string|null */ private $requestHost; /** - * @var string + * @var string|null */ private $requestScheme; @@ -69,22 +69,22 @@ final class Stack private $requestPort; /** - * @var string + * @var string|null */ private $clientRequest; /** - * @var string + * @var string|null */ private $clientResponse; /** - * @var string + * @var string|null */ private $clientException; /** - * @var int + * @var int|null */ private $responseCode; @@ -94,7 +94,7 @@ final class Stack private $duration = 0; /** - * @var string + * @var string|null */ private $curlCommand; @@ -117,7 +117,7 @@ public function getClient() } /** - * @return Stack + * @return Stack|null */ public function getParent() { @@ -154,7 +154,7 @@ public function getRequest() } /** - * @return string + * @return string|null */ public function getResponse() { @@ -186,7 +186,7 @@ public function setFailed($failed) } /** - * @return string + * @return string|null */ public function getRequestTarget() { @@ -202,7 +202,7 @@ public function setRequestTarget($requestTarget) } /** - * @return string + * @return string|null */ public function getRequestMethod() { @@ -218,7 +218,7 @@ public function setRequestMethod($requestMethod) } /** - * @return string + * @return string|null */ public function getClientRequest() { @@ -234,7 +234,7 @@ public function setClientRequest($clientRequest) } /** - * @return mixed + * @return string|null */ public function getClientResponse() { @@ -242,7 +242,7 @@ public function getClientResponse() } /** - * @param mixed $clientResponse + * @param string $clientResponse */ public function setClientResponse($clientResponse) { @@ -250,7 +250,7 @@ public function setClientResponse($clientResponse) } /** - * @return string + * @return string|null */ public function getClientException() { @@ -266,7 +266,7 @@ public function setClientException($clientException) } /** - * @return int + * @return int|null */ public function getResponseCode() { @@ -282,7 +282,7 @@ public function setResponseCode($responseCode) } /** - * @return string + * @return string|null */ public function getRequestHost() { @@ -298,7 +298,7 @@ public function setRequestHost($requestHost) } /** - * @return string + * @return string|null */ public function getRequestScheme() { @@ -340,7 +340,7 @@ public function setDuration($duration) } /** - * @return string + * @return string|null */ public function getCurlCommand() { From 7d826646ce12ed95f6a0d2b4f2a518fcd6e5d50b Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:14:23 +0300 Subject: [PATCH 10/19] :package: [#458] Introduce property types, use PHP 8.1 features (CPP, short arrow function, readonly properties, etc) --- src/ClientFactory/BuzzFactory.php | 8 +- src/ClientFactory/CurlFactory.php | 14 +-- src/ClientFactory/MockFactory.php | 5 +- src/ClientFactory/SymfonyFactory.php | 14 +-- src/Collector/Collector.php | 56 ++++------ src/Collector/Formatter.php | 16 +-- src/Collector/PluginClientFactory.php | 24 +---- src/Collector/PluginClientFactoryListener.php | 10 +- src/Collector/Profile.php | 26 +---- src/Collector/ProfileClient.php | 32 +----- src/Collector/ProfileClientFactory.php | 20 +--- src/Collector/ProfilePlugin.php | 24 +---- src/Collector/Stack.php | 88 ++++----------- src/Collector/StackPlugin.php | 22 +--- .../Twig/HttpMessageMarkupExtension.php | 14 +-- src/DependencyInjection/Configuration.php | 102 ++++++------------ src/DependencyInjection/HttplugExtension.php | 8 +- src/Discovery/ConfiguredClientsStrategy.php | 18 +--- tests/Functional/ProfilingTest.php | 19 +--- tests/Resources/Fixtures/config/full.php | 16 +-- tests/Resources/app/AppKernel.php | 7 +- tests/Unit/Collector/FormatterTest.php | 15 +-- .../Collector/ProfileClientFactoryTest.php | 24 +---- tests/Unit/Collector/ProfileClientTest.php | 56 +++------- tests/Unit/Collector/ProfilePluginTest.php | 76 ++++--------- tests/Unit/Collector/StackPluginTest.php | 16 +-- .../DependencyInjection/ConfigurationTest.php | 10 +- .../HttplugExtensionTest.php | 4 +- 28 files changed, 168 insertions(+), 576 deletions(-) diff --git a/src/ClientFactory/BuzzFactory.php b/src/ClientFactory/BuzzFactory.php index 2b964536..869db632 100644 --- a/src/ClientFactory/BuzzFactory.php +++ b/src/ClientFactory/BuzzFactory.php @@ -13,14 +13,8 @@ */ class BuzzFactory implements ClientFactory { - /** - * @var ResponseFactoryInterface - */ - private $responseFactory; - - public function __construct(ResponseFactoryInterface $responseFactory) + public function __construct(private readonly ResponseFactoryInterface $responseFactory) { - $this->responseFactory = $responseFactory; } /** diff --git a/src/ClientFactory/CurlFactory.php b/src/ClientFactory/CurlFactory.php index 1d663cb7..c9e0bb43 100644 --- a/src/ClientFactory/CurlFactory.php +++ b/src/ClientFactory/CurlFactory.php @@ -13,20 +13,8 @@ */ class CurlFactory implements ClientFactory { - /** - * @var ResponseFactoryInterface - */ - private $responseFactory; - - /** - * @var StreamFactoryInterface - */ - private $streamFactory; - - public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory) + public function __construct(private readonly ResponseFactoryInterface $responseFactory, private readonly StreamFactoryInterface $streamFactory) { - $this->responseFactory = $responseFactory; - $this->streamFactory = $streamFactory; } /** diff --git a/src/ClientFactory/MockFactory.php b/src/ClientFactory/MockFactory.php index 3c598c35..6ce1d637 100644 --- a/src/ClientFactory/MockFactory.php +++ b/src/ClientFactory/MockFactory.php @@ -12,10 +12,7 @@ */ final class MockFactory implements ClientFactory { - /** - * @var ClientInterface - */ - private $client; + private ?ClientInterface $client = null; /** * Set the client instance that this factory should return. diff --git a/src/ClientFactory/SymfonyFactory.php b/src/ClientFactory/SymfonyFactory.php index 31d89fa1..1ce4c3d1 100644 --- a/src/ClientFactory/SymfonyFactory.php +++ b/src/ClientFactory/SymfonyFactory.php @@ -14,20 +14,8 @@ */ class SymfonyFactory implements ClientFactory { - /** - * @var ResponseFactoryInterface - */ - private $responseFactory; - - /** - * @var StreamFactoryInterface - */ - private $streamFactory; - - public function __construct(ResponseFactoryInterface $responseFactory, StreamFactoryInterface $streamFactory) + public function __construct(private readonly ResponseFactoryInterface $responseFactory, private readonly StreamFactoryInterface $streamFactory) { - $this->responseFactory = $responseFactory; - $this->streamFactory = $streamFactory; } /** diff --git a/src/Collector/Collector.php b/src/Collector/Collector.php index 5551d804..f9c9fd2c 100644 --- a/src/Collector/Collector.php +++ b/src/Collector/Collector.php @@ -21,20 +21,18 @@ */ class Collector extends DataCollector { - /** - * @var Stack|null - */ - private $activeStack; - - /** - * @var int|null - */ - private $capturedBodyLength; + private ?Stack $activeStack = null; - public function __construct(?int $capturedBodyLength = null) + public function __construct(private ?int $capturedBodyLength = null) { $this->reset(); - $this->capturedBodyLength = $capturedBodyLength; + } + + public function __wakeup(): void + { + $this->capturedBodyLength = null; + + parent::__wakeup(); } /** @@ -97,9 +95,7 @@ public function addStack(Stack $stack) */ public function getChildrenStacks(Stack $parent) { - return array_filter($this->data['stacks'], function (Stack $stack) use ($parent) { - return $stack->getParent() === $parent; - }); + return array_filter($this->data['stacks'], fn(Stack $stack) => $stack->getParent() === $parent); } /** @@ -115,9 +111,7 @@ public function getStacks() */ public function getSuccessfulStacks() { - return array_filter($this->data['stacks'], function (Stack $stack) { - return !$stack->isFailed(); - }); + return array_filter($this->data['stacks'], fn(Stack $stack) => !$stack->isFailed()); } /** @@ -125,9 +119,7 @@ public function getSuccessfulStacks() */ public function getFailedStacks() { - return array_filter($this->data['stacks'], function (Stack $stack) { - return $stack->isFailed(); - }); + return array_filter($this->data['stacks'], fn(Stack $stack) => $stack->isFailed()); } /** @@ -135,13 +127,9 @@ public function getFailedStacks() */ public function getClients() { - $stacks = array_filter($this->data['stacks'], function (Stack $stack) { - return null === $stack->getParent(); - }); + $stacks = array_filter($this->data['stacks'], fn(Stack $stack) => null === $stack->getParent()); - return array_unique(array_map(function (Stack $stack) { - return $stack->getClient(); - }, $stacks)); + return array_unique(array_map(fn(Stack $stack) => $stack->getClient(), $stacks)); } /** @@ -151,9 +139,7 @@ public function getClients() */ public function getClientRootStacks($client) { - return array_filter($this->data['stacks'], function (Stack $stack) use ($client) { - return $stack->getClient() == $client && null == $stack->getParent(); - }); + return array_filter($this->data['stacks'], fn(Stack $stack) => $stack->getClient() == $client && null == $stack->getParent()); } /** @@ -165,9 +151,7 @@ public function getClientRootStacks($client) */ public function countClientMessages($client) { - return array_sum(array_map(function (Stack $stack) { - return $this->countStackMessages($stack); - }, $this->getClientRootStacks($client))); + return array_sum(array_map(fn(Stack $stack) => $this->countStackMessages($stack), $this->getClientRootStacks($client))); } /** @@ -177,9 +161,7 @@ public function countClientMessages($client) */ private function countStackMessages(Stack $stack) { - return 1 + array_sum(array_map(function (Stack $child) { - return $this->countStackMessages($child); - }, $this->getChildrenStacks($stack))); + return 1 + array_sum(array_map(fn(Stack $child) => $this->countStackMessages($child), $this->getChildrenStacks($stack))); } /** @@ -187,9 +169,7 @@ private function countStackMessages(Stack $stack) */ public function getTotalDuration() { - return array_reduce($this->data['stacks'], function ($carry, Stack $stack) { - return $carry + $stack->getDuration(); - }, 0); + return array_reduce($this->data['stacks'], fn($carry, Stack $stack) => $carry + $stack->getDuration(), 0); } /** diff --git a/src/Collector/Formatter.php b/src/Collector/Formatter.php index 29be2d96..4a02961d 100644 --- a/src/Collector/Formatter.php +++ b/src/Collector/Formatter.php @@ -23,20 +23,8 @@ */ class Formatter implements MessageFormatter { - /** - * @var MessageFormatter - */ - private $formatter; - - /** - * @var CurlCommandFormatter - */ - private $curlFormatter; - - public function __construct(MessageFormatter $formatter, MessageFormatter $curlFormatter) + public function __construct(private readonly MessageFormatter $formatter, private readonly MessageFormatter $curlFormatter) { - $this->formatter = $formatter; - $this->curlFormatter = $curlFormatter; } /** @@ -54,7 +42,7 @@ public function formatException(\Throwable $exception) return sprintf('Transfer error: %s', $exception->getMessage()); } - return sprintf('Unexpected exception of type "%s": %s', get_class($exception), $exception->getMessage()); + return sprintf('Unexpected exception of type "%s": %s', $exception::class, $exception->getMessage()); } /** diff --git a/src/Collector/PluginClientFactory.php b/src/Collector/PluginClientFactory.php index d519d00d..4788d634 100644 --- a/src/Collector/PluginClientFactory.php +++ b/src/Collector/PluginClientFactory.php @@ -20,26 +20,8 @@ */ final class PluginClientFactory { - /** - * @var Collector - */ - private $collector; - - /** - * @var Formatter - */ - private $formatter; - - /** - * @var Stopwatch - */ - private $stopwatch; - - public function __construct(Collector $collector, Formatter $formatter, Stopwatch $stopwatch) + public function __construct(private readonly Collector $collector, private readonly Formatter $formatter, private readonly Stopwatch $stopwatch) { - $this->collector = $collector; - $this->formatter = $formatter; - $this->stopwatch = $stopwatch; } /** @@ -56,9 +38,7 @@ public function __construct(Collector $collector, Formatter $formatter, Stopwatc */ public function createClient($client, array $plugins = [], array $options = []) { - $plugins = array_map(function (Plugin $plugin) { - return new ProfilePlugin($plugin, $this->collector, $this->formatter); - }, $plugins); + $plugins = array_map(fn(Plugin $plugin) => new ProfilePlugin($plugin, $this->collector, $this->formatter), $plugins); $clientName = $options['client_name'] ?? 'Default'; array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName)); diff --git a/src/Collector/PluginClientFactoryListener.php b/src/Collector/PluginClientFactoryListener.php index 91be241b..7d46973e 100644 --- a/src/Collector/PluginClientFactoryListener.php +++ b/src/Collector/PluginClientFactoryListener.php @@ -19,14 +19,8 @@ */ final class PluginClientFactoryListener implements EventSubscriberInterface { - /** - * @var PluginClientFactory - */ - private $factory; - - public function __construct(PluginClientFactory $factory) + public function __construct(private readonly PluginClientFactory $factory) { - $this->factory = $factory; } /** @@ -34,7 +28,7 @@ public function __construct(PluginClientFactory $factory) */ public function onEvent(Event $e) { - DefaultPluginClientFactory::setFactory([$this->factory, 'createClient']); + DefaultPluginClientFactory::setFactory($this->factory->createClient(...)); } /** diff --git a/src/Collector/Profile.php b/src/Collector/Profile.php index 1240effa..ac044530 100644 --- a/src/Collector/Profile.php +++ b/src/Collector/Profile.php @@ -13,32 +13,14 @@ */ final class Profile { - /** - * @var string - */ - private $plugin; + private string $request; - /** - * @var string - */ - private $request; + private string $response; - /** - * @var string - */ - private $response; + private bool $failed = false; - /** - * @var bool - */ - private $failed = false; - - /** - * @param string $plugin - */ - public function __construct($plugin) + public function __construct(private readonly string $plugin) { - $this->plugin = $plugin; } /** diff --git a/src/Collector/ProfileClient.php b/src/Collector/ProfileClient.php index 45f1cf3f..e36d80ff 100644 --- a/src/Collector/ProfileClient.php +++ b/src/Collector/ProfileClient.php @@ -26,30 +26,9 @@ class ProfileClient implements ClientInterface, HttpAsyncClient { use VersionBridgeClient; - /** - * @var ClientInterface&HttpAsyncClient - */ - private $client; + private ClientInterface&HttpAsyncClient $client; - /** - * @var Collector - */ - private $collector; - - /** - * @var Formatter - */ - private $formatter; - - /** - * @var Stopwatch - */ - private $stopwatch; - - /** - * @var array - */ - private $eventNames = []; + private array $eventNames = []; private const STOPWATCH_CATEGORY = 'httplug'; @@ -57,16 +36,13 @@ class ProfileClient implements ClientInterface, HttpAsyncClient * @param ClientInterface|HttpAsyncClient $client The client to profile. Client must implement HttpClient or * HttpAsyncClient interface. */ - public function __construct($client, Collector $collector, Formatter $formatter, Stopwatch $stopwatch) + public function __construct($client, private readonly Collector $collector, private readonly Formatter $formatter, private readonly Stopwatch $stopwatch) { if (!($client instanceof ClientInterface && $client instanceof HttpAsyncClient)) { $client = new FlexibleHttpClient($client); } $this->client = $client; - $this->collector = $collector; - $this->formatter = $formatter; - $this->stopwatch = $stopwatch; } public function sendAsyncRequest(RequestInterface $request) @@ -92,7 +68,7 @@ public function sendAsyncRequest(RequestInterface $request) return $response; }; - $onRejected = function (\Exception $exception) use ($event, $stack) { + $onRejected = function (\Exception $exception) use ($event, $stack): void { $this->collectExceptionInformations($exception, $event, $stack); $event->stop(); diff --git a/src/Collector/ProfileClientFactory.php b/src/Collector/ProfileClientFactory.php index 8b68700c..b0cc6f0c 100644 --- a/src/Collector/ProfileClientFactory.php +++ b/src/Collector/ProfileClientFactory.php @@ -24,33 +24,15 @@ class ProfileClientFactory implements ClientFactory */ private $factory; - /** - * @var Collector - */ - private $collector; - - /** - * @var Formatter - */ - private $formatter; - - /** - * @var Stopwatch - */ - private $stopwatch; - /** * @param ClientFactory|callable $factory */ - public function __construct($factory, Collector $collector, Formatter $formatter, Stopwatch $stopwatch) + public function __construct($factory, private readonly Collector $collector, private readonly Formatter $formatter, private readonly Stopwatch $stopwatch) { if (!$factory instanceof ClientFactory && !is_callable($factory)) { throw new \RuntimeException(sprintf('First argument to ProfileClientFactory::__construct must be a "%s" or a callable.', ClientFactory::class)); } $this->factory = $factory; - $this->collector = $collector; - $this->formatter = $formatter; - $this->stopwatch = $stopwatch; } /** diff --git a/src/Collector/ProfilePlugin.php b/src/Collector/ProfilePlugin.php index 3f362b32..60b21b8d 100644 --- a/src/Collector/ProfilePlugin.php +++ b/src/Collector/ProfilePlugin.php @@ -21,31 +21,13 @@ class ProfilePlugin implements Plugin { use Plugin\VersionBridgePlugin; - /** - * @var Plugin - */ - private $plugin; - - /** - * @var Collector - */ - private $collector; - - /** - * @var Formatter - */ - private $formatter; - - public function __construct(Plugin $plugin, Collector $collector, Formatter $formatter) + public function __construct(private Plugin $plugin, private Collector $collector, private Formatter $formatter) { - $this->plugin = $plugin; - $this->collector = $collector; - $this->formatter = $formatter; } protected function doHandleRequest(RequestInterface $request, callable $next, callable $first) { - $profile = new Profile(get_class($this->plugin)); + $profile = new Profile($this->plugin::class); $stack = $this->collector->getActiveStack(); if (null === $stack) { @@ -79,7 +61,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca $this->onOutgoingResponse($response, $profile, $request, $stack); return $response; - }, function (Exception $exception) use ($profile, $request, $stack) { + }, function (Exception $exception) use ($profile, $request, $stack): void { $this->onException($request, $profile, $exception, $stack); throw $exception; diff --git a/src/Collector/Stack.php b/src/Collector/Stack.php index 9046718b..5be42b6e 100644 --- a/src/Collector/Stack.php +++ b/src/Collector/Stack.php @@ -13,96 +13,44 @@ */ final class Stack { - /** - * @var string - */ - private $client; + private string $client; - /** - * @var Stack|null - */ - private $parent; + private ?Stack $parent = null; /** * @var Profile[] */ - private $profiles = []; + private array $profiles = []; - /** - * @var string - */ - private $request; + private string $request; - /** - * @var string|null - */ - private $response; + private ?string $response = null; - /** - * @var bool - */ - private $failed = false; + private bool $failed = false; - /** - * @var string|null - */ - private $requestTarget; + private ?string $requestTarget = null; - /** - * @var string|null - */ - private $requestMethod; + private ?string $requestMethod = null; - /** - * @var string|null - */ - private $requestHost; + private ?string $requestHost = null; - /** - * @var string|null - */ - private $requestScheme; + private ?string $requestScheme = null; - /** - * @var int|null - */ - private $requestPort; + private ?int $requestPort = null; - /** - * @var string|null - */ - private $clientRequest; + private ?string $clientRequest = null; - /** - * @var string|null - */ - private $clientResponse; + private ?string $clientResponse = null; - /** - * @var string|null - */ - private $clientException; + private ?string $clientException = null; - /** - * @var int|null - */ - private $responseCode; + private ?int $responseCode = null; - /** - * @var int - */ - private $duration = 0; + private int $duration = 0; - /** - * @var string|null - */ - private $curlCommand; + private ?string $curlCommand = null; - /** - * @param string $client - * @param string $request - */ - public function __construct($client, $request) + public function __construct(string $client, string $request) { $this->client = $client; $this->request = $request; diff --git a/src/Collector/StackPlugin.php b/src/Collector/StackPlugin.php index a71e2f0b..2f12c640 100644 --- a/src/Collector/StackPlugin.php +++ b/src/Collector/StackPlugin.php @@ -21,29 +21,11 @@ class StackPlugin implements Plugin { use Plugin\VersionBridgePlugin; - /** - * @var Collector - */ - private $collector; - - /** - * @var string - */ - private $client; - - /** - * @var Formatter - */ - private $formatter; - /** * @param string $client */ - public function __construct(Collector $collector, Formatter $formatter, $client) + public function __construct(private Collector $collector, private Formatter $formatter, private $client) { - $this->collector = $collector; - $this->formatter = $formatter; - $this->client = $client; } protected function doHandleRequest(RequestInterface $request, callable $next, callable $first) @@ -59,7 +41,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca return $response; }; - $onRejected = function (Exception $exception) use ($stack) { + $onRejected = function (Exception $exception) use ($stack): void { $stack->setResponse($this->formatter->formatException($exception)); $stack->setFailed(true); diff --git a/src/Collector/Twig/HttpMessageMarkupExtension.php b/src/Collector/Twig/HttpMessageMarkupExtension.php index ed344a76..1ffa5afc 100644 --- a/src/Collector/Twig/HttpMessageMarkupExtension.php +++ b/src/Collector/Twig/HttpMessageMarkupExtension.php @@ -16,15 +16,9 @@ */ class HttpMessageMarkupExtension extends AbstractExtension { - /** - * @var ClonerInterface - */ - private $cloner; + private ClonerInterface $cloner; - /** - * @var DataDumperInterface - */ - private $dumper; + private DataDumperInterface $dumper; public function __construct(?ClonerInterface $cloner = null, ?DataDumperInterface $dumper = null) { @@ -40,8 +34,8 @@ public function __construct(?ClonerInterface $cloner = null, ?DataDumperInterfac public function getFilters() { return [ - new TwigFilter('httplug_markup', [$this, 'markup'], ['is_safe' => ['html']]), - new TwigFilter('httplug_markup_body', [$this, 'markupBody'], ['is_safe' => ['html']]), + new TwigFilter('httplug_markup', $this->markup(...), ['is_safe' => ['html']]), + new TwigFilter('httplug_markup_body', $this->markupBody(...), ['is_safe' => ['html']]), ]; } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index ca1b219e..ed2000e9 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -38,10 +38,8 @@ 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 - * - * @var bool */ - private $debug; + private bool $debug; /** * @param bool $debug @@ -64,13 +62,11 @@ public function getConfigTreeBuilder(): TreeBuilder $rootNode ->validate() - ->ifTrue(function ($v) { - return !empty($v['classes']['client']) - || !empty($v['classes']['psr17_request_factory']) - || !empty($v['classes']['psr17_response_factory']) - || !empty($v['classes']['psr17_uri_factory']) - || !empty($v['classes']['psr17_stream_factory']); - }) + ->ifTrue(fn($v) => !empty($v['classes']['client']) + || !empty($v['classes']['psr17_request_factory']) + || !empty($v['classes']['psr17_response_factory']) + || !empty($v['classes']['psr17_uri_factory']) + || !empty($v['classes']['psr17_stream_factory'])) ->then(function ($v) { foreach ($v['classes'] as $key => $class) { if (null !== $class && !class_exists($class)) { @@ -86,9 +82,7 @@ public function getConfigTreeBuilder(): TreeBuilder }) ->end() ->beforeNormalization() - ->ifTrue(function ($v) { - return is_array($v) && array_key_exists('toolbar', $v) && is_array($v['toolbar']); - }) + ->ifTrue(fn($v) => is_array($v) && array_key_exists('toolbar', $v) && is_array($v['toolbar'])) ->then(function ($v) { if (array_key_exists('profiling', $v)) { throw new InvalidConfigurationException('Can\'t configure both "toolbar" and "profiling" section. The "toolbar" config is deprecated as of version 1.3.0, please only use "profiling".'); @@ -156,9 +150,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->scalarNode('formatter')->defaultNull()->end() ->scalarNode('captured_body_length') ->validate() - ->ifTrue(function ($v) { - return null !== $v && !is_int($v); - }) + ->ifTrue(fn($v) => null !== $v && !is_int($v)) ->thenInvalid('The child node "captured_body_length" at path "httplug.profiling" must be an integer or null ("%s" given).') ->end() ->defaultValue(0) @@ -193,22 +185,17 @@ private function configureClients(ArrayNodeDefinition $root): void ->prototype('array') ->fixXmlConfig('plugin') ->validate() - ->ifTrue(function ($config) { + ->ifTrue(fn($config) => // Make sure we only allow one of these to be true - return (bool) $config['flexible_client'] + (bool) $config['http_methods_client'] + (bool) $config['batch_client'] >= 2; - }) + (bool) $config['flexible_client'] + (bool) $config['http_methods_client'] + (bool) $config['batch_client'] >= 2) ->thenInvalid('A http client can\'t be decorated with several of FlexibleHttpClient, HttpMethodsClient and BatchClient. Only one of the following options can be true. ("flexible_client", "http_methods_client", "batch_client")') ->end() ->validate() - ->ifTrue(function ($config) { - return 'httplug.factory.auto' === $config['factory'] && !empty($config['config']); - }) + ->ifTrue(fn($config) => 'httplug.factory.auto' === $config['factory'] && !empty($config['config'])) ->thenInvalid('If you want to use the "config" key you must also specify a valid "factory".') ->end() ->validate() - ->ifTrue(function ($config) { - return !empty($config['service']) && ('httplug.factory.auto' !== $config['factory'] || !empty($config['config'])); - }) + ->ifTrue(fn($config) => !empty($config['service']) && ('httplug.factory.auto' !== $config['factory'] || !empty($config['config']))) ->thenInvalid('If you want to use the "service" key you cannot specify "factory" or "config".') ->end() ->children() @@ -469,9 +456,7 @@ private function createClientPluginNode(): ArrayNodeDefinition ->addDefaultsIfNotSet() ->info('Record response to be replayed during tests or development cycle.') ->validate() - ->ifTrue(function ($config) { - return 'filesystem' === $config['recorder'] && empty($config['fixtures_directory']); - }) + ->ifTrue(fn($config) => 'filesystem' === $config['recorder'] && empty($config['fixtures_directory'])) ->thenInvalid('If you want to use the "filesystem" recorder you must also specify a "fixtures_directory".') ->end() ->children() @@ -637,32 +622,15 @@ private function createAuthenticationPluginNode(): NodeDefinition ->validate() ->always() ->then(function ($config) { - switch ($config['type']) { - case 'basic': - $this->validateAuthenticationType(['username', 'password'], $config, 'basic'); - - break; - case 'bearer': - $this->validateAuthenticationType(['token'], $config, 'bearer'); - - break; - case 'service': - $this->validateAuthenticationType(['service'], $config, 'service'); - - break; - case 'wsse': - $this->validateAuthenticationType(['username', 'password'], $config, 'wsse'); - - break; - case 'query_param': - $this->validateAuthenticationType(['params'], $config, 'query_param'); - - break; - case 'header': - $this->validateAuthenticationType(['header_name', 'header_value'], $config, 'header'); - - break; - } + match ($config['type']) { + 'basic' => $this->validateAuthenticationType(['username', 'password'], $config, 'basic'), + 'bearer' => $this->validateAuthenticationType(['token'], $config, 'bearer'), + 'service' => $this->validateAuthenticationType(['service'], $config, 'service'), + 'wsse' => $this->validateAuthenticationType(['username', 'password'], $config, 'wsse'), + 'query_param' => $this->validateAuthenticationType(['params'], $config, 'query_param'), + 'header' => $this->validateAuthenticationType(['header_name', 'header_value'], $config, 'header'), + default => $config, + }; return $config; }) @@ -735,10 +703,9 @@ private function createCachePluginNode(): NodeDefinition ->fixXmlConfig('cache_listener') ->addDefaultsIfNotSet() ->validate() - ->ifTrue(function ($config) { + ->ifTrue(fn($config) => // Cannot set both respect_cache_headers and respect_response_cache_directives - return isset($config['respect_cache_headers'], $config['respect_response_cache_directives']); - }) + isset($config['respect_cache_headers'], $config['respect_response_cache_directives'])) ->thenInvalid('You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives" simultaneously. Use "respect_response_cache_directives" instead.') ->end() ->children() @@ -748,18 +715,14 @@ private function createCachePluginNode(): NodeDefinition ->scalarNode('cache_lifetime') ->info('The minimum time we should store a cache item') ->validate() - ->ifTrue(function ($v) { - return null !== $v && !is_int($v); - }) + ->ifTrue(fn($v) => null !== $v && !is_int($v)) ->thenInvalid('cache_lifetime must be an integer or null, got %s') ->end() ->end() ->scalarNode('default_ttl') ->info('The default max age of a Response') ->validate() - ->ifTrue(function ($v) { - return null !== $v && !is_int($v); - }) + ->ifTrue(fn($v) => null !== $v && !is_int($v)) ->thenInvalid('default_ttl must be an integer or null, got %s') ->end() ->end() @@ -771,9 +734,7 @@ private function createCachePluginNode(): NodeDefinition ->end() ->prototype('scalar') ->validate() - ->ifTrue(function ($v) { - return false === @preg_match($v, ''); - }) + ->ifTrue(fn($v) => false === @preg_match($v, '')) ->thenInvalid('Invalid regular expression for a blacklisted path: %s') ->end() ->end() @@ -788,10 +749,9 @@ private function createCachePluginNode(): NodeDefinition ->defaultValue(['GET', 'HEAD']) ->prototype('scalar') ->validate() - ->ifTrue(function ($v) { + ->ifTrue(fn($v) => /* RFC7230 sections 3.1.1 and 3.2.6 except limited to uppercase characters. */ - return preg_match('/[^A-Z0-9!#$%&\'*+\-.^_`|~]+/', $v); - }) + preg_match('/[^A-Z0-9!#$%&\'*+\-.^_`|~]+/', (string) $v)) ->thenInvalid('Invalid method: %s') ->end() ->end() @@ -839,9 +799,7 @@ private function createCachePluginNode(): NodeDefinition ->info('Configure HTTP caching, requires the php-http/cache-plugin package') ->addDefaultsIfNotSet() ->validate() - ->ifTrue(function ($v) { - return !empty($v['enabled']) && !class_exists(CachePlugin::class); - }) + ->ifTrue(fn($v) => !empty($v['enabled']) && !class_exists(CachePlugin::class)) ->thenInvalid('To use the cache plugin, you need to require php-http/cache-plugin in your project') ->end() ->children() diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index d37b3ee7..337ed0a0 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -209,9 +209,7 @@ private function configurePluginByName(string $name, Definition $definition, arr unset($options['blacklisted_paths']); } - $options['cache_listeners'] = array_map(function (string $serviceName): Reference { - return new Reference($serviceName); - }, $options['cache_listeners']); + $options['cache_listeners'] = array_map(fn(string $serviceName): Reference => new Reference($serviceName), $options['cache_listeners']); if (0 === count($options['cache_listeners'])) { unset($options['cache_listeners']); @@ -441,9 +439,7 @@ private function configureClient(ContainerBuilder $container, string $clientName ->addArgument(new Reference($serviceId.'.client')) ->addArgument( array_map( - function ($id) { - return new Reference($id); - }, + fn($id) => new Reference($id), $plugins ) ) diff --git a/src/Discovery/ConfiguredClientsStrategy.php b/src/Discovery/ConfiguredClientsStrategy.php index 24b0ebc4..e45e62ad 100644 --- a/src/Discovery/ConfiguredClientsStrategy.php +++ b/src/Discovery/ConfiguredClientsStrategy.php @@ -17,15 +17,9 @@ */ class ConfiguredClientsStrategy implements DiscoveryStrategy { - /** - * @var ClientInterface - */ - private static $client; + private static ?ClientInterface $client = null; - /** - * @var HttpAsyncClient - */ - private static $asyncClient; + private static ?HttpAsyncClient $asyncClient = null; /** * @param ClientInterface $httpClient @@ -44,15 +38,11 @@ public function __construct(ClientInterface $httpClient = null, HttpAsyncClient public static function getCandidates($type) { if (ClientInterface::class === $type && null !== self::$client) { - return [['class' => function () { - return self::$client; - }]]; + return [['class' => fn() => self::$client]]; } if (HttpAsyncClient::class === $type && null !== self::$asyncClient) { - return [['class' => function () { - return self::$asyncClient; - }]]; + return [['class' => fn() => self::$asyncClient]]; } return []; diff --git a/tests/Functional/ProfilingTest.php b/tests/Functional/ProfilingTest.php index 1f7ababa..9335a44a 100644 --- a/tests/Functional/ProfilingTest.php +++ b/tests/Functional/ProfilingTest.php @@ -24,20 +24,11 @@ class ProfilingTest extends TestCase { - /** - * @var Collector - */ - private $collector; + private Collector $collector; - /** - * @var Formatter - */ - private $formatter; + private Formatter $formatter; - /** - * @var Stopwatch - */ - private $stopwatch; + private Stopwatch $stopwatch; public function setUp(): void { @@ -106,9 +97,7 @@ public function testProfiling(): void private function createClient(array $plugins, $clientName = 'Acme', array $clientOptions = []) { - $plugins = array_map(function (Plugin $plugin) { - return new ProfilePlugin($plugin, $this->collector, $this->formatter, get_class($plugin)); - }, $plugins); + $plugins = array_map(fn(Plugin $plugin) => new ProfilePlugin($plugin, $this->collector, $this->formatter, $plugin::class), $plugins); array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName)); diff --git a/tests/Resources/Fixtures/config/full.php b/tests/Resources/Fixtures/config/full.php index 78b2c6d3..f767f759 100644 --- a/tests/Resources/Fixtures/config/full.php +++ b/tests/Resources/Fixtures/config/full.php @@ -12,14 +12,14 @@ 'psr17_stream_factory' => 'my_psr17_stream_factory', ], 'classes' => [ - 'client' => 'Http\Adapter\Guzzle7\Client', - 'psr18_client' => 'Http\Adapter\Guzzle7\Client', - 'psr17_request_factory' => 'Nyholm\Psr7\Factory\Psr17Factory', - 'psr17_response_factory' => 'Nyholm\Psr7\Factory\Psr17Factory', - 'psr17_stream_factory' => 'Nyholm\Psr7\Factory\Psr17Factory', - 'psr17_uri_factory' => 'Nyholm\Psr7\Factory\Psr17Factory', - 'psr17_uploaded_file_factory' => 'Nyholm\Psr7\Factory\Psr17Factory', - 'psr17_server_request_factory' => 'Nyholm\Psr7\Factory\Psr17Factory', + 'client' => \Http\Adapter\Guzzle7\Client::class, + 'psr18_client' => \Http\Adapter\Guzzle7\Client::class, + 'psr17_request_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, + 'psr17_response_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, + 'psr17_stream_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, + 'psr17_uri_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, + 'psr17_uploaded_file_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, + 'psr17_server_request_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, ], 'clients' => [ 'test' => [ diff --git a/tests/Resources/app/AppKernel.php b/tests/Resources/app/AppKernel.php index 1ccf56e7..caa3c5d6 100644 --- a/tests/Resources/app/AppKernel.php +++ b/tests/Resources/app/AppKernel.php @@ -14,10 +14,7 @@ class AppKernel extends Kernel { - /** - * @var string - */ - private static $cacheDir; + private static ?string $cacheDir = null; public function registerBundles(): iterable { @@ -31,7 +28,7 @@ public function registerBundles(): iterable public function registerContainerConfiguration(LoaderInterface $loader): void { - $loader->load(function (ContainerBuilder $container) use ($loader) { + $loader->load(function (ContainerBuilder $container) use ($loader): void { $container->loadFromExtension('framework', [ 'router' => [ 'resource' => 'kernel::loadRoutes', diff --git a/tests/Unit/Collector/FormatterTest.php b/tests/Unit/Collector/FormatterTest.php index 4150756c..0f66b9e6 100644 --- a/tests/Unit/Collector/FormatterTest.php +++ b/tests/Unit/Collector/FormatterTest.php @@ -17,20 +17,11 @@ class FormatterTest extends TestCase { - /** - * @var MessageFormatter&MockObject - */ - private $formatter; + private MessageFormatter&MockObject $formatter; - /** - * @var CurlCommandFormatter&MockObject - */ - private $curlFormatter; + private CurlCommandFormatter&MockObject $curlFormatter; - /** - * @var Formatter - */ - private $subject; + private Formatter $subject; public function setUp(): void { diff --git a/tests/Unit/Collector/ProfileClientFactoryTest.php b/tests/Unit/Collector/ProfileClientFactoryTest.php index ea2eb052..5d3e137c 100644 --- a/tests/Unit/Collector/ProfileClientFactoryTest.php +++ b/tests/Unit/Collector/ProfileClientFactoryTest.php @@ -17,25 +17,13 @@ class ProfileClientFactoryTest extends TestCase { - /** - * @var Collector - */ - private $collector; + private Collector $collector; - /** - * @var Formatter - */ - private $formatter; + private Formatter $formatter; - /** - * @var Stopwatch&MockObject - */ - private $stopwatch; + private Stopwatch&MockObject $stopwatch; - /** - * @var ClientInterface&MockObject - */ - private $client; + private ClientInterface&MockObject $client; public function setUp(): void { @@ -57,9 +45,7 @@ public function testCreateClientFromClientFactory(): void public function testCreateClientFromCallable(): void { - $factory = function ($config) { - return $this->client; - }; + $factory = fn($config) => $this->client; $subject = new ProfileClientFactory($factory, $this->collector, $this->formatter, $this->stopwatch); diff --git a/tests/Unit/Collector/ProfileClientTest.php b/tests/Unit/Collector/ProfileClientTest.php index 7b2a3cdb..bf7e4ac1 100644 --- a/tests/Unit/Collector/ProfileClientTest.php +++ b/tests/Unit/Collector/ProfileClientTest.php @@ -27,45 +27,21 @@ class ProfileClientTest extends TestCase { - /** - * @var Collector - */ - private $collector; - - /** - * @var CombinedClientInterface&MockObject - */ - private $client; - - /** - * @var RequestInterface - */ - private $request; - - /** - * @var StopwatchEvent - */ - private $stopwatchEvent; - - /** - * @var ProfileClient - */ - private $subject; - - /** - * @var ResponseInterface - */ - private $response; - - /** - * @var Promise - */ - private $fulfilledPromise; - - /** - * @var RejectedPromise - */ - private $rejectedPromise; + private Collector $collector; + + private CombinedClientInterface&MockObject $client; + + private RequestInterface $request; + + private StopwatchEvent $stopwatchEvent; + + private ProfileClient $subject; + + private ResponseInterface $response; + + private Promise $fulfilledPromise; + + private RejectedPromise $rejectedPromise; public function setUp(): void { @@ -128,7 +104,7 @@ public function testSendRequestTypeError() $this->client ->expects($this->once()) ->method('sendRequest') - ->willReturnCallback(function () { + ->willReturnCallback(function (): void { throw new \Error('You set string to int prop'); }); diff --git a/tests/Unit/Collector/ProfilePluginTest.php b/tests/Unit/Collector/ProfilePluginTest.php index 8b2d2f54..76eda574 100644 --- a/tests/Unit/Collector/ProfilePluginTest.php +++ b/tests/Unit/Collector/ProfilePluginTest.php @@ -23,41 +23,21 @@ class ProfilePluginTest extends TestCase { - /** - * @var Plugin - */ - private $plugin; - - /** - * @var RequestInterface - */ - private $request; - - /** - * @var ResponseInterface - */ - private $response; - - /** - * @var Promise - */ - private $fulfilledPromise; - - /** - * @var TransferException - */ - private $exception; - - /** - * @var Promise - */ - private $rejectedPromise; - - /** - * @var ProfilePlugin - */ - private $subject; - private $collector; + private Plugin $plugin; + + private RequestInterface $request; + + private ResponseInterface $response; + + private Promise $fulfilledPromise; + + private TransferException $exception; + + private Promise $rejectedPromise; + + private ProfilePlugin $subject; + + private Collector $collector; public function setUp(): void { @@ -76,9 +56,7 @@ public function setUp(): void $this->plugin ->method('handleRequest') - ->willReturnCallback(function ($request, $next, $first) { - return $next($request); - }) + ->willReturnCallback(fn($request, $next, $first) => $next($request)) ; $messageFormatter @@ -108,30 +86,24 @@ public function testCallDecoratedPlugin(): void ->with($this->request) ; - $this->subject->handleRequest($this->request, function () { - return $this->fulfilledPromise; - }, function (): void { + $this->subject->handleRequest($this->request, fn() => $this->fulfilledPromise, function (): void { }); } public function testProfileIsInitialized(): void { - $this->subject->handleRequest($this->request, function () { - return $this->fulfilledPromise; - }, function (): void { + $this->subject->handleRequest($this->request, fn() => $this->fulfilledPromise, function (): void { }); $activeStack = $this->collector->getActiveStack(); $this->assertCount(1, $activeStack->getProfiles()); $profile = $activeStack->getProfiles()[0]; - $this->assertEquals(get_class($this->plugin), $profile->getPlugin()); + $this->assertEquals($this->plugin::class, $profile->getPlugin()); } public function testCollectRequestInformations(): void { - $this->subject->handleRequest($this->request, function () { - return $this->fulfilledPromise; - }, function (): void { + $this->subject->handleRequest($this->request, fn() => $this->fulfilledPromise, function (): void { }); $activeStack = $this->collector->getActiveStack(); @@ -142,9 +114,7 @@ public function testCollectRequestInformations(): void public function testOnFulfilled(): void { - $promise = $this->subject->handleRequest($this->request, function () { - return $this->fulfilledPromise; - }, function (): void { + $promise = $this->subject->handleRequest($this->request, fn() => $this->fulfilledPromise, function (): void { }); $this->assertEquals($this->response, $promise->wait()); @@ -157,9 +127,7 @@ public function testOnFulfilled(): void public function testOnRejected(): void { - $promise = $this->subject->handleRequest($this->request, function () { - return $this->rejectedPromise; - }, function (): void { + $promise = $this->subject->handleRequest($this->request, fn() => $this->rejectedPromise, function (): void { }); $activeStack = $this->collector->getActiveStack(); diff --git a/tests/Unit/Collector/StackPluginTest.php b/tests/Unit/Collector/StackPluginTest.php index 3202e082..e7e54c61 100644 --- a/tests/Unit/Collector/StackPluginTest.php +++ b/tests/Unit/Collector/StackPluginTest.php @@ -72,9 +72,7 @@ public function setUp(): void public function testStackIsInitialized(): void { - $next = function () { - return new FulfilledPromise($this->response); - }; + $next = fn() => new FulfilledPromise($this->response); $this->subject->handleRequest($this->request, $next, function (): void { }); @@ -88,9 +86,7 @@ public function testStackIsInitialized(): void public function testOnFulfilled(): void { - $next = function () { - return new FulfilledPromise($this->response); - }; + $next = fn() => new FulfilledPromise($this->response); $promise = $this->subject->handleRequest($this->request, $next, function (): void { }); @@ -104,9 +100,7 @@ public function testOnFulfilled(): void public function testOnRejected(): void { - $next = function () { - return new RejectedPromise($this->exception); - }; + $next = fn() => new RejectedPromise($this->exception); $promise = $this->subject->handleRequest($this->request, $next, function (): void { }); @@ -140,9 +134,7 @@ public function testOnError(): void $this->expectException(Warning::class); } - $next = function () { - return 2 / 0; - }; + $next = fn() => 2 / 0; $this->subject->handleRequest($this->request, $next, function (): void { }); diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index e566bfdb..7c004822 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -18,7 +18,7 @@ */ class ConfigurationTest extends AbstractExtensionConfigurationTestCase { - private $emptyConfig = [ + private array $emptyConfig = [ 'default_client_autowiring' => true, 'main_alias' => [ 'client' => 'httplug.client.default', @@ -300,9 +300,7 @@ public function testSupportsAllConfigFormats(): void ], ]; - $formats = array_map(function ($path) { - return __DIR__.'/../../Resources/Fixtures/'.$path; - }, [ + $formats = array_map(fn($path) => __DIR__.'/../../Resources/Fixtures/'.$path, [ 'config/full.yml', 'config/full.xml', 'config/full.php', @@ -357,9 +355,7 @@ public function testInvalidCacheConfig(): void */ public function testBackwardCompatibility(): void { - $formats = array_map(function ($path) { - return __DIR__.'/../../Resources/Fixtures/'.$path; - }, [ + $formats = array_map(fn($path) => __DIR__.'/../../Resources/Fixtures/'.$path, [ 'config/bc/toolbar.yml', 'config/bc/toolbar_auto.yml', ]); diff --git a/tests/Unit/DependencyInjection/HttplugExtensionTest.php b/tests/Unit/DependencyInjection/HttplugExtensionTest.php index b2895062..0754974e 100644 --- a/tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -186,9 +186,7 @@ public function testClientPlugins(): void 'httplug.client.acme.plugin.cache', 'httplug.client.acme.plugin.error', ]; - $pluginReferences = array_map(function ($id) { - return new Reference($id); - }, $plugins); + $pluginReferences = array_map(fn($id) => new Reference($id), $plugins); $this->assertContainerBuilderHasService('httplug.client.acme'); foreach ($plugins as $id) { From 6548642e78cf13bff7171994972d95228916e74d Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:18:18 +0300 Subject: [PATCH 11/19] :package: [#458] Switch to `php-cs-fixer-ga` 3.26 and run it --- .github/workflows/php-cs-fixer.yml | 2 +- .php_cs.dist => .php-cs-fixer.dist.php | 4 +-- src/ClientFactory/AutoDiscoveryFactory.php | 3 -- src/ClientFactory/BuzzFactory.php | 9 ++---- src/ClientFactory/CurlFactory.php | 3 -- src/ClientFactory/Guzzle6Factory.php | 3 -- src/ClientFactory/Guzzle7Factory.php | 3 -- src/ClientFactory/MockFactory.php | 3 -- src/ClientFactory/ReactFactory.php | 3 -- src/ClientFactory/SocketFactory.php | 3 -- src/ClientFactory/SymfonyFactory.php | 3 -- src/Collector/Collector.php | 31 ++++++------------- src/Collector/Formatter.php | 4 --- src/Collector/PluginClientFactory.php | 2 +- src/Collector/PluginClientFactoryListener.php | 3 -- src/Collector/ProfileClient.php | 10 +++--- src/Collector/ProfileClientFactory.php | 3 -- src/Collector/ProfilePlugin.php | 7 ++--- src/Collector/Stack.php | 3 -- src/Collector/StackPlugin.php | 3 +- .../Twig/HttpMessageMarkupExtension.php | 4 +-- src/DependencyInjection/Configuration.php | 29 ++++++++--------- src/DependencyInjection/HttplugExtension.php | 10 ++---- src/Discovery/ConfiguredClientsStrategy.php | 11 ++----- tests/Functional/DiscoveryTest.php | 3 -- tests/Functional/ProfilerTest.php | 2 +- tests/Functional/ProfilingTest.php | 2 +- tests/Functional/ServiceInstantiationTest.php | 3 -- tests/Resources/Fixtures/config/full.php | 16 +++++----- tests/Resources/app/AppKernel.php | 8 ++--- .../Collector/ProfileClientFactoryTest.php | 2 +- tests/Unit/Collector/ProfilePluginTest.php | 12 +++---- tests/Unit/Collector/StackPluginTest.php | 8 ++--- .../DependencyInjection/ConfigurationTest.php | 4 +-- .../HttplugExtensionTest.php | 3 +- 35 files changed, 73 insertions(+), 149 deletions(-) rename .php_cs.dist => .php-cs-fixer.dist.php (86%) diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml index d4c0b0be..289c83c3 100644 --- a/.github/workflows/php-cs-fixer.yml +++ b/.github/workflows/php-cs-fixer.yml @@ -17,6 +17,6 @@ jobs: - name: "Checkout" uses: "actions/checkout@v3" - name: "PHP-CS-Fixer" - uses: "docker://oskarstark/php-cs-fixer-ga:2.19.0" + uses: "docker://oskarstark/php-cs-fixer-ga:3.26.0" with: args: --diff --dry-run diff --git a/.php_cs.dist b/.php-cs-fixer.dist.php similarity index 86% rename from .php_cs.dist rename to .php-cs-fixer.dist.php index fa2e2520..0b3fc4c0 100644 --- a/.php_cs.dist +++ b/.php-cs-fixer.dist.php @@ -1,12 +1,12 @@ exclude('src/Resources') ->exclude('vendor') ->in(__DIR__) ; -return PhpCsFixer\Config::create() +return (new PhpCsFixer\Config()) ->setRiskyAllowed(true) ->setRules([ '@Symfony' => true, diff --git a/src/ClientFactory/AutoDiscoveryFactory.php b/src/ClientFactory/AutoDiscoveryFactory.php index ccad13e0..bdda63a1 100644 --- a/src/ClientFactory/AutoDiscoveryFactory.php +++ b/src/ClientFactory/AutoDiscoveryFactory.php @@ -13,9 +13,6 @@ */ class AutoDiscoveryFactory implements ClientFactory { - /** - * {@inheritdoc} - */ public function createClient(array $config = []) { return Psr18ClientDiscovery::find(); diff --git a/src/ClientFactory/BuzzFactory.php b/src/ClientFactory/BuzzFactory.php index 869db632..904bcf0c 100644 --- a/src/ClientFactory/BuzzFactory.php +++ b/src/ClientFactory/BuzzFactory.php @@ -17,9 +17,6 @@ public function __construct(private readonly ResponseFactoryInterface $responseF { } - /** - * {@inheritdoc} - */ public function createClient(array $config = []) { if (!class_exists('Buzz\Client\FileGetContents')) { @@ -37,9 +34,9 @@ private function getOptions(array $config = []) $resolver = new OptionsResolver(); $resolver->setDefaults([ - 'timeout' => 5, - 'verify' => true, - 'proxy' => null, + 'timeout' => 5, + 'verify' => true, + 'proxy' => null, ]); $resolver->setAllowedTypes('timeout', 'int'); diff --git a/src/ClientFactory/CurlFactory.php b/src/ClientFactory/CurlFactory.php index c9e0bb43..62134c81 100644 --- a/src/ClientFactory/CurlFactory.php +++ b/src/ClientFactory/CurlFactory.php @@ -17,9 +17,6 @@ public function __construct(private readonly ResponseFactoryInterface $responseF { } - /** - * {@inheritdoc} - */ public function createClient(array $config = []) { if (!class_exists('Http\Client\Curl\Client')) { diff --git a/src/ClientFactory/Guzzle6Factory.php b/src/ClientFactory/Guzzle6Factory.php index 92d9da46..590eaaef 100644 --- a/src/ClientFactory/Guzzle6Factory.php +++ b/src/ClientFactory/Guzzle6Factory.php @@ -11,9 +11,6 @@ */ class Guzzle6Factory implements ClientFactory { - /** - * {@inheritdoc} - */ public function createClient(array $config = []) { if (!class_exists('Http\Adapter\Guzzle6\Client')) { diff --git a/src/ClientFactory/Guzzle7Factory.php b/src/ClientFactory/Guzzle7Factory.php index 6045ab03..431900ef 100644 --- a/src/ClientFactory/Guzzle7Factory.php +++ b/src/ClientFactory/Guzzle7Factory.php @@ -11,9 +11,6 @@ */ class Guzzle7Factory implements ClientFactory { - /** - * {@inheritdoc} - */ public function createClient(array $config = []) { if (!class_exists('Http\Adapter\Guzzle7\Client')) { diff --git a/src/ClientFactory/MockFactory.php b/src/ClientFactory/MockFactory.php index 6ce1d637..1d994812 100644 --- a/src/ClientFactory/MockFactory.php +++ b/src/ClientFactory/MockFactory.php @@ -24,9 +24,6 @@ public function setClient(ClientInterface $client) $this->client = $client; } - /** - * {@inheritdoc} - */ public function createClient(array $config = []) { if (!class_exists(Client::class)) { diff --git a/src/ClientFactory/ReactFactory.php b/src/ClientFactory/ReactFactory.php index b0d34718..61b48147 100644 --- a/src/ClientFactory/ReactFactory.php +++ b/src/ClientFactory/ReactFactory.php @@ -11,9 +11,6 @@ */ class ReactFactory implements ClientFactory { - /** - * {@inheritdoc} - */ public function createClient(array $config = []) { if (!class_exists('Http\Adapter\React\Client')) { diff --git a/src/ClientFactory/SocketFactory.php b/src/ClientFactory/SocketFactory.php index 610399f1..c77e7d59 100644 --- a/src/ClientFactory/SocketFactory.php +++ b/src/ClientFactory/SocketFactory.php @@ -11,9 +11,6 @@ */ class SocketFactory implements ClientFactory { - /** - * {@inheritdoc} - */ public function createClient(array $config = []) { if (!class_exists('Http\Client\Socket\Client')) { diff --git a/src/ClientFactory/SymfonyFactory.php b/src/ClientFactory/SymfonyFactory.php index 1ce4c3d1..a531756c 100644 --- a/src/ClientFactory/SymfonyFactory.php +++ b/src/ClientFactory/SymfonyFactory.php @@ -18,9 +18,6 @@ public function __construct(private readonly ResponseFactoryInterface $responseF { } - /** - * {@inheritdoc} - */ public function createClient(array $config = []) { if (!class_exists(HttplugClient::class)) { diff --git a/src/Collector/Collector.php b/src/Collector/Collector.php index f9c9fd2c..c02575f4 100644 --- a/src/Collector/Collector.php +++ b/src/Collector/Collector.php @@ -35,18 +35,12 @@ public function __wakeup(): void parent::__wakeup(); } - /** - * {@inheritdoc} - */ public function reset(): void { $this->data['stacks'] = []; $this->activeStack = null; } - /** - * {@inheritdoc} - */ public function getName(): string { return 'httplug'; @@ -95,7 +89,7 @@ public function addStack(Stack $stack) */ public function getChildrenStacks(Stack $parent) { - return array_filter($this->data['stacks'], fn(Stack $stack) => $stack->getParent() === $parent); + return array_filter($this->data['stacks'], fn (Stack $stack) => $stack->getParent() === $parent); } /** @@ -111,7 +105,7 @@ public function getStacks() */ public function getSuccessfulStacks() { - return array_filter($this->data['stacks'], fn(Stack $stack) => !$stack->isFailed()); + return array_filter($this->data['stacks'], fn (Stack $stack) => !$stack->isFailed()); } /** @@ -119,7 +113,7 @@ public function getSuccessfulStacks() */ public function getFailedStacks() { - return array_filter($this->data['stacks'], fn(Stack $stack) => $stack->isFailed()); + return array_filter($this->data['stacks'], fn (Stack $stack) => $stack->isFailed()); } /** @@ -127,31 +121,27 @@ public function getFailedStacks() */ public function getClients() { - $stacks = array_filter($this->data['stacks'], fn(Stack $stack) => null === $stack->getParent()); + $stacks = array_filter($this->data['stacks'], fn (Stack $stack) => null === $stack->getParent()); - return array_unique(array_map(fn(Stack $stack) => $stack->getClient(), $stacks)); + return array_unique(array_map(fn (Stack $stack) => $stack->getClient(), $stacks)); } /** - * @param $client - * * @return Stack[] */ public function getClientRootStacks($client) { - return array_filter($this->data['stacks'], fn(Stack $stack) => $stack->getClient() == $client && null == $stack->getParent()); + return array_filter($this->data['stacks'], fn (Stack $stack) => $stack->getClient() == $client && null == $stack->getParent()); } /** * Count all messages for a client. * - * @param $client - * * @return int */ public function countClientMessages($client) { - return array_sum(array_map(fn(Stack $stack) => $this->countStackMessages($stack), $this->getClientRootStacks($client))); + return array_sum(array_map(fn (Stack $stack) => $this->countStackMessages($stack), $this->getClientRootStacks($client))); } /** @@ -161,7 +151,7 @@ public function countClientMessages($client) */ private function countStackMessages(Stack $stack) { - return 1 + array_sum(array_map(fn(Stack $child) => $this->countStackMessages($child), $this->getChildrenStacks($stack))); + return 1 + array_sum(array_map(fn (Stack $child) => $this->countStackMessages($child), $this->getChildrenStacks($stack))); } /** @@ -169,12 +159,9 @@ private function countStackMessages(Stack $stack) */ public function getTotalDuration() { - return array_reduce($this->data['stacks'], fn($carry, Stack $stack) => $carry + $stack->getDuration(), 0); + return array_reduce($this->data['stacks'], fn ($carry, Stack $stack) => $carry + $stack->getDuration(), 0); } - /** - * {@inheritdoc} - */ public function collect(Request $request, Response $response, $exception = null): void { // We do not need to collect any data from the Symfony Request and Response diff --git a/src/Collector/Formatter.php b/src/Collector/Formatter.php index 4a02961d..009103bf 100644 --- a/src/Collector/Formatter.php +++ b/src/Collector/Formatter.php @@ -8,7 +8,6 @@ use Http\Client\Exception\HttpException; use Http\Client\Exception\TransferException; use Http\Message\Formatter as MessageFormatter; -use Http\Message\Formatter\CurlCommandFormatter; use Psr\Http\Client\NetworkExceptionInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -45,9 +44,6 @@ public function formatException(\Throwable $exception) return sprintf('Unexpected exception of type "%s": %s', $exception::class, $exception->getMessage()); } - /** - * {@inheritdoc} - */ public function formatRequest(RequestInterface $request) { return $this->formatter->formatRequest($request); diff --git a/src/Collector/PluginClientFactory.php b/src/Collector/PluginClientFactory.php index 4788d634..8e356e56 100644 --- a/src/Collector/PluginClientFactory.php +++ b/src/Collector/PluginClientFactory.php @@ -38,7 +38,7 @@ public function __construct(private readonly Collector $collector, private reado */ public function createClient($client, array $plugins = [], array $options = []) { - $plugins = array_map(fn(Plugin $plugin) => new ProfilePlugin($plugin, $this->collector, $this->formatter), $plugins); + $plugins = array_map(fn (Plugin $plugin) => new ProfilePlugin($plugin, $this->collector, $this->formatter), $plugins); $clientName = $options['client_name'] ?? 'Default'; array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName)); diff --git a/src/Collector/PluginClientFactoryListener.php b/src/Collector/PluginClientFactoryListener.php index 7d46973e..f1135fa1 100644 --- a/src/Collector/PluginClientFactoryListener.php +++ b/src/Collector/PluginClientFactoryListener.php @@ -31,9 +31,6 @@ public function onEvent(Event $e) DefaultPluginClientFactory::setFactory($this->factory->createClient(...)); } - /** - * {@inheritdoc} - */ public static function getSubscribedEvents(): array { return [ diff --git a/src/Collector/ProfileClient.php b/src/Collector/ProfileClient.php index e36d80ff..81bf0d98 100644 --- a/src/Collector/ProfileClient.php +++ b/src/Collector/ProfileClient.php @@ -50,8 +50,8 @@ public function sendAsyncRequest(RequestInterface $request) $activateStack = true; $stack = $this->collector->getActiveStack(); if (null === $stack) { - //When using a discovered client not wrapped in a PluginClient, we don't have a stack from StackPlugin. So - //we create our own stack and activate it! + // When using a discovered client not wrapped in a PluginClient, we don't have a stack from StackPlugin. So + // we create our own stack and activate it! $stack = new Stack('Default', $this->formatter->formatRequest($request)); $this->collector->addStack($stack); $this->collector->activateStack($stack); @@ -85,7 +85,7 @@ public function sendAsyncRequest(RequestInterface $request) throw $e; } finally { if ($activateStack) { - //We only activate the stack when created by the StackPlugin. + // We only activate the stack when created by the StackPlugin. $this->collector->activateStack($stack); } } @@ -95,8 +95,8 @@ protected function doSendRequest(RequestInterface $request) { $stack = $this->collector->getActiveStack(); if (null === $stack) { - //When using a discovered client not wrapped in a PluginClient, we don't have a stack from StackPlugin. So - //we create our own stack but don't activate it. + // When using a discovered client not wrapped in a PluginClient, we don't have a stack from StackPlugin. So + // we create our own stack but don't activate it. $stack = new Stack('Default', $this->formatter->formatRequest($request)); $this->collector->addStack($stack); } diff --git a/src/Collector/ProfileClientFactory.php b/src/Collector/ProfileClientFactory.php index b0cc6f0c..ddf0aee7 100644 --- a/src/Collector/ProfileClientFactory.php +++ b/src/Collector/ProfileClientFactory.php @@ -35,9 +35,6 @@ public function __construct($factory, private readonly Collector $collector, pri $this->factory = $factory; } - /** - * {@inheritdoc} - */ public function createClient(array $config = []) { $client = is_callable($this->factory) ? call_user_func($this->factory, $config) : $this->factory->createClient($config); diff --git a/src/Collector/ProfilePlugin.php b/src/Collector/ProfilePlugin.php index 60b21b8d..5032a741 100644 --- a/src/Collector/ProfilePlugin.php +++ b/src/Collector/ProfilePlugin.php @@ -4,7 +4,6 @@ namespace Http\HttplugBundle\Collector; -use Exception; use Http\Client\Common\Plugin; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -51,7 +50,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca try { $promise = $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst); - } catch (Exception $e) { + } catch (\Exception $e) { $this->onException($request, $profile, $e, $stack); throw $e; @@ -61,7 +60,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca $this->onOutgoingResponse($response, $profile, $request, $stack); return $response; - }, function (Exception $exception) use ($profile, $request, $stack): void { + }, function (\Exception $exception) use ($profile, $request, $stack): void { $this->onException($request, $profile, $exception, $stack); throw $exception; @@ -71,7 +70,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca private function onException( RequestInterface $request, Profile $profile, - Exception $exception, + \Exception $exception, Stack $stack ): void { $profile->setFailed(true); diff --git a/src/Collector/Stack.php b/src/Collector/Stack.php index 5be42b6e..110fc47e 100644 --- a/src/Collector/Stack.php +++ b/src/Collector/Stack.php @@ -72,9 +72,6 @@ public function getParent() return $this->parent; } - /** - * @param Stack $parent - */ public function setParent(self $parent) { $this->parent = $parent; diff --git a/src/Collector/StackPlugin.php b/src/Collector/StackPlugin.php index 2f12c640..cd5c1e78 100644 --- a/src/Collector/StackPlugin.php +++ b/src/Collector/StackPlugin.php @@ -4,7 +4,6 @@ namespace Http\HttplugBundle\Collector; -use Exception; use Http\Client\Common\Plugin; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -41,7 +40,7 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca return $response; }; - $onRejected = function (Exception $exception) use ($stack): void { + $onRejected = function (\Exception $exception) use ($stack): void { $stack->setResponse($this->formatter->formatException($exception)); $stack->setFailed(true); diff --git a/src/Collector/Twig/HttpMessageMarkupExtension.php b/src/Collector/Twig/HttpMessageMarkupExtension.php index 1ffa5afc..1e1fb637 100644 --- a/src/Collector/Twig/HttpMessageMarkupExtension.php +++ b/src/Collector/Twig/HttpMessageMarkupExtension.php @@ -20,15 +20,13 @@ class HttpMessageMarkupExtension extends AbstractExtension private DataDumperInterface $dumper; - public function __construct(?ClonerInterface $cloner = null, ?DataDumperInterface $dumper = null) + public function __construct(ClonerInterface $cloner = null, DataDumperInterface $dumper = null) { $this->cloner = $cloner ?: new VarCloner(); $this->dumper = $dumper ?: new HtmlDumper(); } /** - * {@inheritdoc} - * * @return array */ public function getFilters() diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index ed2000e9..327a0e91 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -49,9 +49,6 @@ public function __construct($debug) $this->debug = (bool) $debug; } - /** - * {@inheritdoc} - */ public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('httplug'); @@ -62,7 +59,7 @@ public function getConfigTreeBuilder(): TreeBuilder $rootNode ->validate() - ->ifTrue(fn($v) => !empty($v['classes']['client']) + ->ifTrue(fn ($v) => !empty($v['classes']['client']) || !empty($v['classes']['psr17_request_factory']) || !empty($v['classes']['psr17_response_factory']) || !empty($v['classes']['psr17_uri_factory']) @@ -82,7 +79,7 @@ public function getConfigTreeBuilder(): TreeBuilder }) ->end() ->beforeNormalization() - ->ifTrue(fn($v) => is_array($v) && array_key_exists('toolbar', $v) && is_array($v['toolbar'])) + ->ifTrue(fn ($v) => is_array($v) && array_key_exists('toolbar', $v) && is_array($v['toolbar'])) ->then(function ($v) { if (array_key_exists('profiling', $v)) { throw new InvalidConfigurationException('Can\'t configure both "toolbar" and "profiling" section. The "toolbar" config is deprecated as of version 1.3.0, please only use "profiling".'); @@ -150,7 +147,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->scalarNode('formatter')->defaultNull()->end() ->scalarNode('captured_body_length') ->validate() - ->ifTrue(fn($v) => null !== $v && !is_int($v)) + ->ifTrue(fn ($v) => null !== $v && !is_int($v)) ->thenInvalid('The child node "captured_body_length" at path "httplug.profiling" must be an integer or null ("%s" given).') ->end() ->defaultValue(0) @@ -185,17 +182,17 @@ private function configureClients(ArrayNodeDefinition $root): void ->prototype('array') ->fixXmlConfig('plugin') ->validate() - ->ifTrue(fn($config) => + ->ifTrue(fn ($config) => // Make sure we only allow one of these to be true (bool) $config['flexible_client'] + (bool) $config['http_methods_client'] + (bool) $config['batch_client'] >= 2) ->thenInvalid('A http client can\'t be decorated with several of FlexibleHttpClient, HttpMethodsClient and BatchClient. Only one of the following options can be true. ("flexible_client", "http_methods_client", "batch_client")') ->end() ->validate() - ->ifTrue(fn($config) => 'httplug.factory.auto' === $config['factory'] && !empty($config['config'])) + ->ifTrue(fn ($config) => 'httplug.factory.auto' === $config['factory'] && !empty($config['config'])) ->thenInvalid('If you want to use the "config" key you must also specify a valid "factory".') ->end() ->validate() - ->ifTrue(fn($config) => !empty($config['service']) && ('httplug.factory.auto' !== $config['factory'] || !empty($config['config']))) + ->ifTrue(fn ($config) => !empty($config['service']) && ('httplug.factory.auto' !== $config['factory'] || !empty($config['config']))) ->thenInvalid('If you want to use the "service" key you cannot specify "factory" or "config".') ->end() ->children() @@ -456,7 +453,7 @@ private function createClientPluginNode(): ArrayNodeDefinition ->addDefaultsIfNotSet() ->info('Record response to be replayed during tests or development cycle.') ->validate() - ->ifTrue(fn($config) => 'filesystem' === $config['recorder'] && empty($config['fixtures_directory'])) + ->ifTrue(fn ($config) => 'filesystem' === $config['recorder'] && empty($config['fixtures_directory'])) ->thenInvalid('If you want to use the "filesystem" recorder you must also specify a "fixtures_directory".') ->end() ->children() @@ -703,7 +700,7 @@ private function createCachePluginNode(): NodeDefinition ->fixXmlConfig('cache_listener') ->addDefaultsIfNotSet() ->validate() - ->ifTrue(fn($config) => + ->ifTrue(fn ($config) => // Cannot set both respect_cache_headers and respect_response_cache_directives isset($config['respect_cache_headers'], $config['respect_response_cache_directives'])) ->thenInvalid('You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives" simultaneously. Use "respect_response_cache_directives" instead.') @@ -715,14 +712,14 @@ private function createCachePluginNode(): NodeDefinition ->scalarNode('cache_lifetime') ->info('The minimum time we should store a cache item') ->validate() - ->ifTrue(fn($v) => null !== $v && !is_int($v)) + ->ifTrue(fn ($v) => null !== $v && !is_int($v)) ->thenInvalid('cache_lifetime must be an integer or null, got %s') ->end() ->end() ->scalarNode('default_ttl') ->info('The default max age of a Response') ->validate() - ->ifTrue(fn($v) => null !== $v && !is_int($v)) + ->ifTrue(fn ($v) => null !== $v && !is_int($v)) ->thenInvalid('default_ttl must be an integer or null, got %s') ->end() ->end() @@ -734,7 +731,7 @@ private function createCachePluginNode(): NodeDefinition ->end() ->prototype('scalar') ->validate() - ->ifTrue(fn($v) => false === @preg_match($v, '')) + ->ifTrue(fn ($v) => false === @preg_match($v, '')) ->thenInvalid('Invalid regular expression for a blacklisted path: %s') ->end() ->end() @@ -749,7 +746,7 @@ private function createCachePluginNode(): NodeDefinition ->defaultValue(['GET', 'HEAD']) ->prototype('scalar') ->validate() - ->ifTrue(fn($v) => + ->ifTrue(fn ($v) => /* RFC7230 sections 3.1.1 and 3.2.6 except limited to uppercase characters. */ preg_match('/[^A-Z0-9!#$%&\'*+\-.^_`|~]+/', (string) $v)) ->thenInvalid('Invalid method: %s') @@ -799,7 +796,7 @@ private function createCachePluginNode(): NodeDefinition ->info('Configure HTTP caching, requires the php-http/cache-plugin package') ->addDefaultsIfNotSet() ->validate() - ->ifTrue(fn($v) => !empty($v['enabled']) && !class_exists(CachePlugin::class)) + ->ifTrue(fn ($v) => !empty($v['enabled']) && !class_exists(CachePlugin::class)) ->thenInvalid('To use the cache plugin, you need to require php-http/cache-plugin in your project') ->end() ->children() diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index 337ed0a0..d22d5e5a 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -189,7 +189,6 @@ private function configurePlugins(ContainerBuilder $container, array $config): v } /** - * @param string $name * @param ContainerBuilder $container In case we need to add additional services for this plugin * @param string $serviceId service id of the plugin, in case we need to add additional services for this plugin */ @@ -209,7 +208,7 @@ private function configurePluginByName(string $name, Definition $definition, arr unset($options['blacklisted_paths']); } - $options['cache_listeners'] = array_map(fn(string $serviceName): Reference => new Reference($serviceName), $options['cache_listeners']); + $options['cache_listeners'] = array_map(fn (string $serviceName): Reference => new Reference($serviceName), $options['cache_listeners']); if (0 === count($options['cache_listeners'])) { unset($options['cache_listeners']); @@ -280,7 +279,7 @@ private function configurePluginByName(string $name, Definition $definition, arr break; - /* client specific plugins */ + /* client specific plugins */ case 'add_host': $hostUriService = $serviceId.'.host_uri'; @@ -439,7 +438,7 @@ private function configureClient(ContainerBuilder $container, string $clientName ->addArgument(new Reference($serviceId.'.client')) ->addArgument( array_map( - fn($id) => new Reference($id), + fn ($id) => new Reference($id), $plugins ) ) @@ -535,9 +534,6 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra } } - /** - * {@inheritdoc} - */ public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface { return new Configuration($container->getParameter('kernel.debug')); diff --git a/src/Discovery/ConfiguredClientsStrategy.php b/src/Discovery/ConfiguredClientsStrategy.php index e45e62ad..e19faa87 100644 --- a/src/Discovery/ConfiguredClientsStrategy.php +++ b/src/Discovery/ConfiguredClientsStrategy.php @@ -21,10 +21,6 @@ class ConfiguredClientsStrategy implements DiscoveryStrategy private static ?HttpAsyncClient $asyncClient = null; - /** - * @param ClientInterface $httpClient - * @param HttpAsyncClient $asyncClient - */ public function __construct(ClientInterface $httpClient = null, HttpAsyncClient $asyncClient = null) { self::$client = $httpClient; @@ -32,17 +28,14 @@ public function __construct(ClientInterface $httpClient = null, HttpAsyncClient Psr18ClientDiscovery::clearCache(); } - /** - * {@inheritdoc} - */ public static function getCandidates($type) { if (ClientInterface::class === $type && null !== self::$client) { - return [['class' => fn() => self::$client]]; + return [['class' => fn () => self::$client]]; } if (HttpAsyncClient::class === $type && null !== self::$asyncClient) { - return [['class' => fn() => self::$asyncClient]]; + return [['class' => fn () => self::$asyncClient]]; } return []; diff --git a/tests/Functional/DiscoveryTest.php b/tests/Functional/DiscoveryTest.php index 0e7c824e..de569821 100644 --- a/tests/Functional/DiscoveryTest.php +++ b/tests/Functional/DiscoveryTest.php @@ -30,9 +30,6 @@ protected function setUp(): void $this->setParameter('kernel.debug', true); } - /** - * {@inheritdoc} - */ protected function getContainerExtensions(): array { return [ diff --git a/tests/Functional/ProfilerTest.php b/tests/Functional/ProfilerTest.php index 99975eb3..0768a167 100644 --- a/tests/Functional/ProfilerTest.php +++ b/tests/Functional/ProfilerTest.php @@ -22,7 +22,7 @@ public function testShowProfiler(): void $httpClient->sendRequest(new Request('GET', '/posts/1')); - //Browse any page to get a profile + // Browse any page to get a profile $client->request('GET', '/'); $client->request('GET', '/_profiler/latest?panel=httplug'); diff --git a/tests/Functional/ProfilingTest.php b/tests/Functional/ProfilingTest.php index 9335a44a..3a227b5a 100644 --- a/tests/Functional/ProfilingTest.php +++ b/tests/Functional/ProfilingTest.php @@ -97,7 +97,7 @@ public function testProfiling(): void private function createClient(array $plugins, $clientName = 'Acme', array $clientOptions = []) { - $plugins = array_map(fn(Plugin $plugin) => new ProfilePlugin($plugin, $this->collector, $this->formatter, $plugin::class), $plugins); + $plugins = array_map(fn (Plugin $plugin) => new ProfilePlugin($plugin, $this->collector, $this->formatter, $plugin::class), $plugins); array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName)); diff --git a/tests/Functional/ServiceInstantiationTest.php b/tests/Functional/ServiceInstantiationTest.php index 1f215ced..243fcc7d 100644 --- a/tests/Functional/ServiceInstantiationTest.php +++ b/tests/Functional/ServiceInstantiationTest.php @@ -120,9 +120,6 @@ public function testProfilingPsr18Decoration(): void $this->assertInstanceOf(ResponseInterface::class, $response); } - /** - * {@inheritdoc} - */ protected static function bootKernel(array $options = []): KernelInterface { parent::bootKernel($options); diff --git a/tests/Resources/Fixtures/config/full.php b/tests/Resources/Fixtures/config/full.php index f767f759..b468f177 100644 --- a/tests/Resources/Fixtures/config/full.php +++ b/tests/Resources/Fixtures/config/full.php @@ -12,14 +12,14 @@ 'psr17_stream_factory' => 'my_psr17_stream_factory', ], 'classes' => [ - 'client' => \Http\Adapter\Guzzle7\Client::class, - 'psr18_client' => \Http\Adapter\Guzzle7\Client::class, - 'psr17_request_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, - 'psr17_response_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, - 'psr17_stream_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, - 'psr17_uri_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, - 'psr17_uploaded_file_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, - 'psr17_server_request_factory' => \Nyholm\Psr7\Factory\Psr17Factory::class, + 'client' => Http\Adapter\Guzzle7\Client::class, + 'psr18_client' => Http\Adapter\Guzzle7\Client::class, + 'psr17_request_factory' => Nyholm\Psr7\Factory\Psr17Factory::class, + 'psr17_response_factory' => Nyholm\Psr7\Factory\Psr17Factory::class, + 'psr17_stream_factory' => Nyholm\Psr7\Factory\Psr17Factory::class, + 'psr17_uri_factory' => Nyholm\Psr7\Factory\Psr17Factory::class, + 'psr17_uploaded_file_factory' => Nyholm\Psr7\Factory\Psr17Factory::class, + 'psr17_server_request_factory' => Nyholm\Psr7\Factory\Psr17Factory::class, ], 'clients' => [ 'test' => [ diff --git a/tests/Resources/app/AppKernel.php b/tests/Resources/app/AppKernel.php index caa3c5d6..d277b5d2 100644 --- a/tests/Resources/app/AppKernel.php +++ b/tests/Resources/app/AppKernel.php @@ -19,9 +19,9 @@ class AppKernel extends Kernel public function registerBundles(): iterable { return [ - new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(), - new \Symfony\Bundle\TwigBundle\TwigBundle(), - new \Http\HttplugBundle\HttplugBundle(), + new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), + new Symfony\Bundle\TwigBundle\TwigBundle(), + new Http\HttplugBundle\HttplugBundle(), new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(), ]; } @@ -57,7 +57,7 @@ public function registerContainerConfiguration(LoaderInterface $loader): void public function loadRoutes(LoaderInterface $loader): RouteCollection { - $file = (new \ReflectionObject($this))->getFileName(); + $file = (new ReflectionObject($this))->getFileName(); /* @var RoutingPhpFileLoader $kernelLoader */ $kernelLoader = $loader->getResolver()->resolve($file, 'php'); $kernelLoader->setCurrentDir(\dirname($file)); diff --git a/tests/Unit/Collector/ProfileClientFactoryTest.php b/tests/Unit/Collector/ProfileClientFactoryTest.php index 5d3e137c..46b1517c 100644 --- a/tests/Unit/Collector/ProfileClientFactoryTest.php +++ b/tests/Unit/Collector/ProfileClientFactoryTest.php @@ -45,7 +45,7 @@ public function testCreateClientFromClientFactory(): void public function testCreateClientFromCallable(): void { - $factory = fn($config) => $this->client; + $factory = fn ($config) => $this->client; $subject = new ProfileClientFactory($factory, $this->collector, $this->formatter, $this->stopwatch); diff --git a/tests/Unit/Collector/ProfilePluginTest.php b/tests/Unit/Collector/ProfilePluginTest.php index 76eda574..ac6dfdc6 100644 --- a/tests/Unit/Collector/ProfilePluginTest.php +++ b/tests/Unit/Collector/ProfilePluginTest.php @@ -56,7 +56,7 @@ public function setUp(): void $this->plugin ->method('handleRequest') - ->willReturnCallback(fn($request, $next, $first) => $next($request)) + ->willReturnCallback(fn ($request, $next, $first) => $next($request)) ; $messageFormatter @@ -86,13 +86,13 @@ public function testCallDecoratedPlugin(): void ->with($this->request) ; - $this->subject->handleRequest($this->request, fn() => $this->fulfilledPromise, function (): void { + $this->subject->handleRequest($this->request, fn () => $this->fulfilledPromise, function (): void { }); } public function testProfileIsInitialized(): void { - $this->subject->handleRequest($this->request, fn() => $this->fulfilledPromise, function (): void { + $this->subject->handleRequest($this->request, fn () => $this->fulfilledPromise, function (): void { }); $activeStack = $this->collector->getActiveStack(); @@ -103,7 +103,7 @@ public function testProfileIsInitialized(): void public function testCollectRequestInformations(): void { - $this->subject->handleRequest($this->request, fn() => $this->fulfilledPromise, function (): void { + $this->subject->handleRequest($this->request, fn () => $this->fulfilledPromise, function (): void { }); $activeStack = $this->collector->getActiveStack(); @@ -114,7 +114,7 @@ public function testCollectRequestInformations(): void public function testOnFulfilled(): void { - $promise = $this->subject->handleRequest($this->request, fn() => $this->fulfilledPromise, function (): void { + $promise = $this->subject->handleRequest($this->request, fn () => $this->fulfilledPromise, function (): void { }); $this->assertEquals($this->response, $promise->wait()); @@ -127,7 +127,7 @@ public function testOnFulfilled(): void public function testOnRejected(): void { - $promise = $this->subject->handleRequest($this->request, fn() => $this->rejectedPromise, function (): void { + $promise = $this->subject->handleRequest($this->request, fn () => $this->rejectedPromise, function (): void { }); $activeStack = $this->collector->getActiveStack(); diff --git a/tests/Unit/Collector/StackPluginTest.php b/tests/Unit/Collector/StackPluginTest.php index e7e54c61..92f2817e 100644 --- a/tests/Unit/Collector/StackPluginTest.php +++ b/tests/Unit/Collector/StackPluginTest.php @@ -72,7 +72,7 @@ public function setUp(): void public function testStackIsInitialized(): void { - $next = fn() => new FulfilledPromise($this->response); + $next = fn () => new FulfilledPromise($this->response); $this->subject->handleRequest($this->request, $next, function (): void { }); @@ -86,7 +86,7 @@ public function testStackIsInitialized(): void public function testOnFulfilled(): void { - $next = fn() => new FulfilledPromise($this->response); + $next = fn () => new FulfilledPromise($this->response); $promise = $this->subject->handleRequest($this->request, $next, function (): void { }); @@ -100,7 +100,7 @@ public function testOnFulfilled(): void public function testOnRejected(): void { - $next = fn() => new RejectedPromise($this->exception); + $next = fn () => new RejectedPromise($this->exception); $promise = $this->subject->handleRequest($this->request, $next, function (): void { }); @@ -134,7 +134,7 @@ public function testOnError(): void $this->expectException(Warning::class); } - $next = fn() => 2 / 0; + $next = fn () => 2 / 0; $this->subject->handleRequest($this->request, $next, function (): void { }); diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index 7c004822..83b2e557 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -300,7 +300,7 @@ public function testSupportsAllConfigFormats(): void ], ]; - $formats = array_map(fn($path) => __DIR__.'/../../Resources/Fixtures/'.$path, [ + $formats = array_map(fn ($path) => __DIR__.'/../../Resources/Fixtures/'.$path, [ 'config/full.yml', 'config/full.xml', 'config/full.php', @@ -355,7 +355,7 @@ public function testInvalidCacheConfig(): void */ public function testBackwardCompatibility(): void { - $formats = array_map(fn($path) => __DIR__.'/../../Resources/Fixtures/'.$path, [ + $formats = array_map(fn ($path) => __DIR__.'/../../Resources/Fixtures/'.$path, [ 'config/bc/toolbar.yml', 'config/bc/toolbar_auto.yml', ]); diff --git a/tests/Unit/DependencyInjection/HttplugExtensionTest.php b/tests/Unit/DependencyInjection/HttplugExtensionTest.php index 0754974e..a4a799b9 100644 --- a/tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -186,7 +186,7 @@ public function testClientPlugins(): void 'httplug.client.acme.plugin.cache', 'httplug.client.acme.plugin.error', ]; - $pluginReferences = array_map(fn($id) => new Reference($id), $plugins); + $pluginReferences = array_map(fn ($id) => new Reference($id), $plugins); $this->assertContainerBuilderHasService('httplug.client.acme'); foreach ($plugins as $id) { @@ -504,6 +504,7 @@ public function testClientIsTaggedWithHttplugClientTag(): void /** * @dataProvider provideVcrPluginConfig + * * @group vcr-plugin */ public function testVcrPluginConfiguration(array $config, array $services, array $arguments = []): void From 0725be77660379b018bbbe31deec26084be83a7a Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 25 Aug 2024 22:22:54 +0300 Subject: [PATCH 12/19] :package: Require phpunit 9 because `simple-phpunit` is not compatible with phpunit 10 and `matthiasnoback/symfony-config-test` triggers installation of phpunit 10 From CI ``` PHP Fatal error: Declaration of PHPUnit\Framework\TestSuite::run(): void must be compatible with PHPUnit\Framework\Test::run(?PHPUnit\Framework\TestResult $result = null): PHPUnit\Framework\TestResult in /home/runner/work/HttplugBundle/HttplugBundle/vendor/phpunit/phpunit/src/Framework/TestSuite.php on line 338 Script vendor/bin/simple-phpunit handling the test event returned with error code 255 ``` --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 107ae049..3d52056b 100644 --- a/composer.json +++ b/composer.json @@ -57,6 +57,7 @@ "php-http/cache-plugin": "^1.7", "php-http/mock-client": "^1.2", "php-http/promise": "^1.0", + "phpunit/phpunit": "^9", "symfony/browser-kit": "^6.4 || ^7.1", "symfony/cache": "^6.4 || ^7.1", "symfony/dom-crawler": "^6.4 || ^7.1", From 86bdeae9d7c4b23f7c6943c2a65aac19acae11d8 Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 1 Sep 2024 13:41:15 +0300 Subject: [PATCH 13/19] :lipstick: Multiline constructor parameters --- src/ClientFactory/CurlFactory.php | 6 ++++-- src/ClientFactory/SymfonyFactory.php | 6 ++++-- src/Collector/Formatter.php | 6 ++++-- src/Collector/PluginClientFactory.php | 7 +++++-- src/Collector/ProfileClient.php | 8 ++++++-- src/Collector/ProfileClientFactory.php | 8 ++++++-- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/ClientFactory/CurlFactory.php b/src/ClientFactory/CurlFactory.php index 62134c81..c95fb455 100644 --- a/src/ClientFactory/CurlFactory.php +++ b/src/ClientFactory/CurlFactory.php @@ -13,8 +13,10 @@ */ class CurlFactory implements ClientFactory { - public function __construct(private readonly ResponseFactoryInterface $responseFactory, private readonly StreamFactoryInterface $streamFactory) - { + public function __construct( + private readonly ResponseFactoryInterface $responseFactory, + private readonly StreamFactoryInterface $streamFactory + ) { } public function createClient(array $config = []) diff --git a/src/ClientFactory/SymfonyFactory.php b/src/ClientFactory/SymfonyFactory.php index a531756c..80809bf3 100644 --- a/src/ClientFactory/SymfonyFactory.php +++ b/src/ClientFactory/SymfonyFactory.php @@ -14,8 +14,10 @@ */ class SymfonyFactory implements ClientFactory { - public function __construct(private readonly ResponseFactoryInterface $responseFactory, private readonly StreamFactoryInterface $streamFactory) - { + public function __construct( + private readonly ResponseFactoryInterface $responseFactory, + private readonly StreamFactoryInterface $streamFactory + ) { } public function createClient(array $config = []) diff --git a/src/Collector/Formatter.php b/src/Collector/Formatter.php index 009103bf..b9d0218d 100644 --- a/src/Collector/Formatter.php +++ b/src/Collector/Formatter.php @@ -22,8 +22,10 @@ */ class Formatter implements MessageFormatter { - public function __construct(private readonly MessageFormatter $formatter, private readonly MessageFormatter $curlFormatter) - { + public function __construct( + private readonly MessageFormatter $formatter, + private readonly MessageFormatter $curlFormatter, + ) { } /** diff --git a/src/Collector/PluginClientFactory.php b/src/Collector/PluginClientFactory.php index 8e356e56..fdf48a10 100644 --- a/src/Collector/PluginClientFactory.php +++ b/src/Collector/PluginClientFactory.php @@ -20,8 +20,11 @@ */ final class PluginClientFactory { - public function __construct(private readonly Collector $collector, private readonly Formatter $formatter, private readonly Stopwatch $stopwatch) - { + public function __construct( + private readonly Collector $collector, + private readonly Formatter $formatter, + private readonly Stopwatch $stopwatch + ) { } /** diff --git a/src/Collector/ProfileClient.php b/src/Collector/ProfileClient.php index 81bf0d98..966f759b 100644 --- a/src/Collector/ProfileClient.php +++ b/src/Collector/ProfileClient.php @@ -36,8 +36,12 @@ class ProfileClient implements ClientInterface, HttpAsyncClient * @param ClientInterface|HttpAsyncClient $client The client to profile. Client must implement HttpClient or * HttpAsyncClient interface. */ - public function __construct($client, private readonly Collector $collector, private readonly Formatter $formatter, private readonly Stopwatch $stopwatch) - { + public function __construct( + $client, + private readonly Collector $collector, + private readonly Formatter $formatter, + private readonly Stopwatch $stopwatch + ) { if (!($client instanceof ClientInterface && $client instanceof HttpAsyncClient)) { $client = new FlexibleHttpClient($client); } diff --git a/src/Collector/ProfileClientFactory.php b/src/Collector/ProfileClientFactory.php index ddf0aee7..7c805ad8 100644 --- a/src/Collector/ProfileClientFactory.php +++ b/src/Collector/ProfileClientFactory.php @@ -27,8 +27,12 @@ class ProfileClientFactory implements ClientFactory /** * @param ClientFactory|callable $factory */ - public function __construct($factory, private readonly Collector $collector, private readonly Formatter $formatter, private readonly Stopwatch $stopwatch) - { + public function __construct( + $factory, + private readonly Collector $collector, + private readonly Formatter $formatter, + private readonly Stopwatch $stopwatch + ) { if (!$factory instanceof ClientFactory && !is_callable($factory)) { throw new \RuntimeException(sprintf('First argument to ProfileClientFactory::__construct must be a "%s" or a callable.', ClientFactory::class)); } From a2d6c995e0599d66d96d89be7f818c8441a8bc84 Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 1 Sep 2024 13:43:14 +0300 Subject: [PATCH 14/19] :package: Safe property types - property can be uninitialized. Explicitly mention possible return null --- src/Collector/Profile.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Collector/Profile.php b/src/Collector/Profile.php index ac044530..9288e8bc 100644 --- a/src/Collector/Profile.php +++ b/src/Collector/Profile.php @@ -13,9 +13,9 @@ */ final class Profile { - private string $request; + private ?string $request = null; - private string $response; + private ?string $response = null; private bool $failed = false; @@ -32,7 +32,7 @@ public function getPlugin() } /** - * @return string + * @return string|null */ public function getRequest() { @@ -48,7 +48,7 @@ public function setRequest($request) } /** - * @return string + * @return string|null */ public function getResponse() { From 0c522c1ff6e7827ad8a8f627bf8ea80629b4d49b Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 1 Sep 2024 13:46:35 +0300 Subject: [PATCH 15/19] :package: Explicitly specify possible null in type --- .php-cs-fixer.dist.php | 1 + src/Collector/Twig/HttpMessageMarkupExtension.php | 2 +- src/Discovery/ConfiguredClientsStrategy.php | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 0b3fc4c0..46453281 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -13,6 +13,7 @@ 'array_syntax' => ['syntax' => 'short'], 'declare_strict_types' => true, 'single_line_throw' => false, + 'nullable_type_declaration_for_default_null_value' => true, 'visibility_required' => [ 'elements' => [ 'const', diff --git a/src/Collector/Twig/HttpMessageMarkupExtension.php b/src/Collector/Twig/HttpMessageMarkupExtension.php index 1e1fb637..c95fe212 100644 --- a/src/Collector/Twig/HttpMessageMarkupExtension.php +++ b/src/Collector/Twig/HttpMessageMarkupExtension.php @@ -20,7 +20,7 @@ class HttpMessageMarkupExtension extends AbstractExtension private DataDumperInterface $dumper; - public function __construct(ClonerInterface $cloner = null, DataDumperInterface $dumper = null) + public function __construct(?ClonerInterface $cloner = null, ?DataDumperInterface $dumper = null) { $this->cloner = $cloner ?: new VarCloner(); $this->dumper = $dumper ?: new HtmlDumper(); diff --git a/src/Discovery/ConfiguredClientsStrategy.php b/src/Discovery/ConfiguredClientsStrategy.php index e19faa87..b3222e3a 100644 --- a/src/Discovery/ConfiguredClientsStrategy.php +++ b/src/Discovery/ConfiguredClientsStrategy.php @@ -21,7 +21,7 @@ class ConfiguredClientsStrategy implements DiscoveryStrategy private static ?HttpAsyncClient $asyncClient = null; - public function __construct(ClientInterface $httpClient = null, HttpAsyncClient $asyncClient = null) + public function __construct(?ClientInterface $httpClient = null, ?HttpAsyncClient $asyncClient = null) { self::$client = $httpClient; self::$asyncClient = $asyncClient; From 3d60ca8139f48c32d65e8a0d328fd1d229743e97 Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 1 Sep 2024 13:47:28 +0300 Subject: [PATCH 16/19] :package: Use native union type for parameter --- src/ClientFactory/PluginClientFactory.php | 9 ++++----- src/Collector/ProfileClientFactory.php | 8 +------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/src/ClientFactory/PluginClientFactory.php b/src/ClientFactory/PluginClientFactory.php index 8821a174..be99e9fb 100644 --- a/src/ClientFactory/PluginClientFactory.php +++ b/src/ClientFactory/PluginClientFactory.php @@ -19,14 +19,13 @@ final class PluginClientFactory { /** - * @param Plugin[] $plugins - * @param ClientFactory|callable $factory - * @param array $config config to the client factory - * @param array $pluginClientOptions config forwarded to the PluginClient + * @param Plugin[] $plugins + * @param array $config config to the client factory + * @param array $pluginClientOptions config forwarded to the PluginClient * * @return PluginClient */ - public static function createPluginClient(array $plugins, $factory, array $config, array $pluginClientOptions = []) + public static function createPluginClient(array $plugins, ClientFactory|callable $factory, array $config, array $pluginClientOptions = []) { if ($factory instanceof ClientFactory) { $client = $factory->createClient($config); diff --git a/src/Collector/ProfileClientFactory.php b/src/Collector/ProfileClientFactory.php index 7c805ad8..e84d80b6 100644 --- a/src/Collector/ProfileClientFactory.php +++ b/src/Collector/ProfileClientFactory.php @@ -24,18 +24,12 @@ class ProfileClientFactory implements ClientFactory */ private $factory; - /** - * @param ClientFactory|callable $factory - */ public function __construct( - $factory, + ClientFactory|callable $factory, private readonly Collector $collector, private readonly Formatter $formatter, private readonly Stopwatch $stopwatch ) { - if (!$factory instanceof ClientFactory && !is_callable($factory)) { - throw new \RuntimeException(sprintf('First argument to ProfileClientFactory::__construct must be a "%s" or a callable.', ClientFactory::class)); - } $this->factory = $factory; } From c8ec829ce469028bad4add594eba58dd8790eb94 Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 1 Sep 2024 13:48:25 +0300 Subject: [PATCH 17/19] :package: Use CPP --- src/Collector/Stack.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Collector/Stack.php b/src/Collector/Stack.php index 110fc47e..c48b7808 100644 --- a/src/Collector/Stack.php +++ b/src/Collector/Stack.php @@ -13,8 +13,6 @@ */ final class Stack { - private string $client; - private ?Stack $parent = null; /** @@ -22,8 +20,6 @@ final class Stack */ private array $profiles = []; - private string $request; - private ?string $response = null; private bool $failed = false; @@ -50,10 +46,10 @@ final class Stack private ?string $curlCommand = null; - public function __construct(string $client, string $request) - { - $this->client = $client; - $this->request = $request; + public function __construct( + private readonly string $client, + private readonly string $request, + ) { } /** From 2491a8338ddc6f8c9a320d96825de39082c62d8e Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 1 Sep 2024 15:21:08 +0300 Subject: [PATCH 18/19] :package: Multiline CPP, apply readonly --- src/Collector/ProfilePlugin.php | 7 +++++-- src/Collector/StackPlugin.php | 10 +++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Collector/ProfilePlugin.php b/src/Collector/ProfilePlugin.php index 5032a741..6d30eea9 100644 --- a/src/Collector/ProfilePlugin.php +++ b/src/Collector/ProfilePlugin.php @@ -20,8 +20,11 @@ class ProfilePlugin implements Plugin { use Plugin\VersionBridgePlugin; - public function __construct(private Plugin $plugin, private Collector $collector, private Formatter $formatter) - { + public function __construct( + private readonly Plugin $plugin, + private readonly Collector $collector, + private readonly Formatter $formatter, + ) { } protected function doHandleRequest(RequestInterface $request, callable $next, callable $first) diff --git a/src/Collector/StackPlugin.php b/src/Collector/StackPlugin.php index cd5c1e78..4f0ebdb7 100644 --- a/src/Collector/StackPlugin.php +++ b/src/Collector/StackPlugin.php @@ -20,11 +20,11 @@ class StackPlugin implements Plugin { use Plugin\VersionBridgePlugin; - /** - * @param string $client - */ - public function __construct(private Collector $collector, private Formatter $formatter, private $client) - { + public function __construct( + private readonly Collector $collector, + private readonly Formatter $formatter, + private readonly string $client, + ) { } protected function doHandleRequest(RequestInterface $request, callable $next, callable $first) From 8bd3369ef634f5ca53192aed1742e7d6d11438db Mon Sep 17 00:00:00 2001 From: Andrii Date: Sun, 1 Sep 2024 15:27:08 +0300 Subject: [PATCH 19/19] :package: Enforce installing `matthiasnoback/symfony-config-test` to fix CI for lowest deps --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 3d52056b..3c5d0cb1 100644 --- a/composer.json +++ b/composer.json @@ -51,6 +51,7 @@ }, "require-dev": { "guzzlehttp/psr7": "^1.7 || ^2.0", + "matthiasnoback/symfony-config-test": "^5.2", "matthiasnoback/symfony-dependency-injection-test": "^4.0 || ^5.0", "nyholm/nsa": "^1.1", "nyholm/psr7": "^1.2.1",