From daa52937b55c06284ed1d7e43845ba87256d0666 Mon Sep 17 00:00:00 2001 From: Oh My Felix Date: Fri, 13 Feb 2026 14:39:37 +0000 Subject: [PATCH] Fix phpstan failures on PHP 8.1 --- composer.json | 2 +- phpstan.neon | 18 ------------------ src/DI/MessengerExtension.php | 7 ++++--- src/DI/Pass/EventPass.php | 13 +++++++------ src/DI/Pass/HandlerPass.php | 8 ++++++-- src/DI/Utils/Reflector.php | 14 ++++++++++---- 6 files changed, 28 insertions(+), 34 deletions(-) diff --git a/composer.json b/composer.json index d8687ad..bfd619f 100644 --- a/composer.json +++ b/composer.json @@ -38,7 +38,7 @@ "symfony/doctrine-messenger": "^6.0.19 || ^6.2.10", "contributte/console": "^0.10.0", "contributte/event-dispatcher": "^0.9.0", - "contributte/qa": "^0.4.0", + "contributte/qa": "^0.3.2", "contributte/tester": "^0.3.0", "contributte/phpstan": "^0.1.0", "tracy/tracy": "^2.10.2" diff --git a/phpstan.neon b/phpstan.neon index 485bc1c..8a5465b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -21,25 +21,7 @@ parameters: count: 1 path: src/DI/Pass/HandlerPass.php - - - message: """ - #^Fetching class constant class of deprecated class Symfony\\\\Component\\\\Messenger\\\\Handler\\\\MessageHandlerInterface\\: - since Symfony 6\\.2, use the \\{@see AsMessageHandler\\} attribute instead$# - """ - count: 1 - path: src/DI/Pass/HandlerPass.php - - - - message: "#^Class ReflectionIntersectionType not found\\.$#" - count: 1 - path: src/DI/Utils/Reflector.php - - message: "#^Dead catch \\- ReflectionException is never thrown in the try block\\.$#" count: 1 path: src/DI/Utils/Reflector.php - - - - message: "#^PHPDoc tag @var for variable \\$type contains unknown class ReflectionIntersectionType\\.$#" - count: 1 - path: src/DI/Utils/Reflector.php diff --git a/src/DI/MessengerExtension.php b/src/DI/MessengerExtension.php index f144706..dd4a5b5 100644 --- a/src/DI/MessengerExtension.php +++ b/src/DI/MessengerExtension.php @@ -21,6 +21,7 @@ use Nette\Utils\ArrayHash; use stdClass; use Symfony\Component\Messenger\MessageBusInterface; +use function is_string; /** * @property-write stdClass $config @@ -61,9 +62,9 @@ public function __construct() public function getConfigSchema(): Schema { - $expectClass = Expect::string()->required()->assert(fn ($input) => class_exists($input) || interface_exists($input)); + $expectClass = Expect::string()->required()->assert(fn ($input): bool => is_string($input) && (class_exists($input) || interface_exists($input))); $expectService = Expect::anyOf( - Expect::string()->required()->assert(fn ($input) => str_starts_with($input, '@') || class_exists($input) || interface_exists($input)), + Expect::string()->required()->assert(fn ($input): bool => is_string($input) && (str_starts_with($input, '@') || class_exists($input) || interface_exists($input))), Expect::type(Statement::class)->required(), ); $expectLoosyService = Expect::anyOf( @@ -82,7 +83,7 @@ public function getConfigSchema(): Schema 'allowNoHandlers' => Expect::bool(false), 'allowNoSenders' => Expect::bool(true), 'autowired' => Expect::bool(), - 'class' => (clone $expectClass)->required(false)->assert(fn ($input) => is_subclass_of($input, MessageBusInterface::class), 'Specified bus class must implements "MessageBusInterface"'), + 'class' => (clone $expectClass)->required(false)->assert(fn ($input): bool => is_string($input) && is_subclass_of($input, MessageBusInterface::class), 'Specified bus class must implements "MessageBusInterface"'), 'wrapper' => (clone $expectClass)->required(false), ]), Expect::string()->required(), diff --git a/src/DI/Pass/EventPass.php b/src/DI/Pass/EventPass.php index c56926f..8d04533 100644 --- a/src/DI/Pass/EventPass.php +++ b/src/DI/Pass/EventPass.php @@ -12,12 +12,13 @@ use Symfony\Component\Messenger\EventListener\DispatchPcntlSignalListener; use Symfony\Component\Messenger\EventListener\SendFailedMessageForRetryListener; use Symfony\Component\Messenger\EventListener\SendFailedMessageToFailureTransportListener; -use Symfony\Component\Messenger\EventListener\StopWorkerOnSignalsListener; -use Symfony\Component\Messenger\EventListener\StopWorkerOnSigtermSignalListener; class EventPass extends AbstractPass { + private const STOP_WORKER_ON_SIGNALS_LISTENER = 'Symfony\\Component\\Messenger\\EventListener\\StopWorkerOnSignalsListener'; + private const STOP_WORKER_ON_SIGTERM_SIGNAL_LISTENER = 'Symfony\\Component\\Messenger\\EventListener\\StopWorkerOnSigtermSignalListener'; + /** * Register services */ @@ -97,13 +98,13 @@ public function beforePassCompile(): void ]); // Stop on signal - if (class_exists(StopWorkerOnSignalsListener::class)) { + if (class_exists(self::STOP_WORKER_ON_SIGNALS_LISTENER)) { $dispatcher->addSetup('addSubscriber', [ - new Statement(StopWorkerOnSignalsListener::class, [null, $this->prefix('@logger.logger')]), + new Statement(self::STOP_WORKER_ON_SIGNALS_LISTENER, [null, $this->prefix('@logger.logger')]), ]); - } elseif (class_exists(StopWorkerOnSigtermSignalListener::class)) { + } elseif (class_exists(self::STOP_WORKER_ON_SIGTERM_SIGNAL_LISTENER)) { $dispatcher->addSetup('addSubscriber', [ - new Statement(StopWorkerOnSigtermSignalListener::class, [$this->prefix('@logger.logger')]), // @phpstan-ignore-line + new Statement(self::STOP_WORKER_ON_SIGTERM_SIGNAL_LISTENER, [$this->prefix('@logger.logger')]), ]); } } diff --git a/src/DI/Pass/HandlerPass.php b/src/DI/Pass/HandlerPass.php index 379fcbb..9a88a4b 100644 --- a/src/DI/Pass/HandlerPass.php +++ b/src/DI/Pass/HandlerPass.php @@ -9,8 +9,8 @@ use Nette\DI\Definitions\ServiceDefinition; use ReflectionClass; use ReflectionException; -use Symfony\Component\Messenger\Handler\MessageHandlerInterface; use function array_merge; +use function interface_exists; use function is_numeric; use function is_string; @@ -19,6 +19,7 @@ class HandlerPass extends AbstractPass private const DEFAULT_METHOD_NAME = '__invoke'; private const DEFAULT_PRIORITY = 0; + private const MESSAGE_HANDLER_INTERFACE = 'Symfony\\Component\\Messenger\\Handler\\MessageHandlerInterface'; /** * Register services @@ -97,7 +98,10 @@ private function getMessageHandlers(): array // Find all handlers $serviceHandlers = []; $serviceHandlers = array_merge($serviceHandlers, array_keys($builder->findByTag(MessengerExtension::HANDLER_TAG))); - $serviceHandlers = array_merge($serviceHandlers, array_keys($builder->findByType(MessageHandlerInterface::class))); + + if (interface_exists(self::MESSAGE_HANDLER_INTERFACE)) { + $serviceHandlers = array_merge($serviceHandlers, array_keys($builder->findByType(self::MESSAGE_HANDLER_INTERFACE))); + } foreach ($builder->getDefinitions() as $definition) { /** @var class-string $class */ diff --git a/src/DI/Utils/Reflector.php b/src/DI/Utils/Reflector.php index 543814f..015a87d 100644 --- a/src/DI/Utils/Reflector.php +++ b/src/DI/Utils/Reflector.php @@ -6,14 +6,16 @@ use ReflectionAttribute; use ReflectionClass; use ReflectionException; -use ReflectionIntersectionType; use ReflectionNamedType; +use ReflectionType; use ReflectionUnionType; use Symfony\Component\Messenger\Attribute\AsMessageHandler; use Symfony\Component\Messenger\Handler\Acknowledger; use Symfony\Component\Messenger\Handler\BatchHandlerInterface; use function array_map; use function array_merge; +use function class_exists; +use function is_a; final class Reflector { @@ -75,7 +77,7 @@ public static function getMessageHandlerMessage(string $class, array $options): throw new LogicalException(sprintf('Exactly two parameters are required in "%s::%s()."', $class, $options['method'])); } - /** @var ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null $type */ + /** @var ReflectionType|null $type */ $type = $rcMethod->getParameters()[1]->getType(); if (!$type instanceof ReflectionNamedType) { @@ -87,7 +89,7 @@ public static function getMessageHandlerMessage(string $class, array $options): } } - /** @var ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null $type */ + /** @var ReflectionType|null $type */ $type = $rcMethod->getParameters()[0]->getType(); if ($type === null) { @@ -98,10 +100,14 @@ public static function getMessageHandlerMessage(string $class, array $options): throw new LogicalException(sprintf('Union parameter type for "%s::%s() is not supported."', $class, $options['method'])); } - if ($type instanceof ReflectionIntersectionType) { + if (class_exists('ReflectionIntersectionType') && is_a($type, 'ReflectionIntersectionType')) { throw new LogicalException(sprintf('Intersection parameter type for "%s::%s() is not supported."', $class, $options['method'])); } + if (!$type instanceof ReflectionNamedType) { + throw new LogicalException(sprintf('Parameter type for "%s::%s() is not supported."', $class, $options['method'])); + } + return $type->getName(); }