From 7032509d480a73a00c9838aeffddecef2a4d979b Mon Sep 17 00:00:00 2001 From: Alessandro Chitolina Date: Fri, 17 Jan 2025 01:42:27 +0100 Subject: [PATCH] chore: fix code style --- .../Serializer/MessengerSerializer.php | 14 +---------- .../Compiler/CheckDependencyPass.php | 2 +- .../Compiler/RegisterDoctrineEventsPass.php | 2 +- .../DoctrineClearIdentityMapMiddleware.php | 17 +------------ lib/Transport/Dbal/DbalReceiver.php | 25 +++++++++---------- lib/Transport/Dbal/DbalSender.php | 6 +---- lib/Transport/Dbal/DbalTransport.php | 14 +++-------- lib/Transport/Dbal/DbalTransportFactory.php | 16 +----------- lib/Transport/Mongo/MongoReceiver.php | 10 +++----- lib/Transport/Mongo/MongoSender.php | 4 +-- lib/Transport/Mongo/MongoTransport.php | 8 +++--- 11 files changed, 29 insertions(+), 89 deletions(-) diff --git a/lib/Adapter/Serializer/MessengerSerializer.php b/lib/Adapter/Serializer/MessengerSerializer.php index eea9bba..4f50d23 100644 --- a/lib/Adapter/Serializer/MessengerSerializer.php +++ b/lib/Adapter/Serializer/MessengerSerializer.php @@ -22,24 +22,12 @@ class MessengerSerializer implements MessengerSerializerInterface { - private SerializerInterface $serializer; - private string $format; - - /** - * @var array $context - * @phpstan-var array{groups?: string[], object_to_populate?: object} $context - */ - private array $context; - /** * @param array $context * @phpstan-param array{groups?: string[], object_to_populate?: object} $context */ - public function __construct(SerializerInterface $serializer, string $format = 'json', array $context = []) + public function __construct(private SerializerInterface $serializer, private string $format = 'json', private array $context = []) { - $this->serializer = $serializer; - $this->format = $format; - $this->context = $context; } /** diff --git a/lib/DependencyInjection/Compiler/CheckDependencyPass.php b/lib/DependencyInjection/Compiler/CheckDependencyPass.php index ddc2451..4a82fe5 100644 --- a/lib/DependencyInjection/Compiler/CheckDependencyPass.php +++ b/lib/DependencyInjection/Compiler/CheckDependencyPass.php @@ -27,7 +27,7 @@ public function process(ContainerBuilder $container): void try { $urlParams = parse_url($url); - } catch (UrlException $e) { + } catch (UrlException) { continue; } diff --git a/lib/DependencyInjection/Compiler/RegisterDoctrineEventsPass.php b/lib/DependencyInjection/Compiler/RegisterDoctrineEventsPass.php index 3584afd..e3f80c6 100644 --- a/lib/DependencyInjection/Compiler/RegisterDoctrineEventsPass.php +++ b/lib/DependencyInjection/Compiler/RegisterDoctrineEventsPass.php @@ -20,7 +20,7 @@ public function process(ContainerBuilder $container): void try { $urlParams = parse_url($url); - } catch (UrlException $e) { + } catch (UrlException) { continue; } diff --git a/lib/Middleware/DoctrineClearIdentityMapMiddleware.php b/lib/Middleware/DoctrineClearIdentityMapMiddleware.php index 80bf218..1ef8f8a 100644 --- a/lib/Middleware/DoctrineClearIdentityMapMiddleware.php +++ b/lib/Middleware/DoctrineClearIdentityMapMiddleware.php @@ -10,29 +10,14 @@ use Symfony\Component\Messenger\Middleware\MiddlewareInterface; use Symfony\Component\Messenger\Middleware\StackInterface; use Symfony\Component\Messenger\Stamp\ReceivedStamp; -use TypeError; - -use function get_class; -use function gettype; -use function is_object; -use function Safe\sprintf; /** * Middleware that clears the doctrine ORM identity map after processing a message. */ class DoctrineClearIdentityMapMiddleware implements MiddlewareInterface { - /** @var ManagerRegistryV2|ManagerRegistryV3|null */ - private $doctrine; - - /** @param ManagerRegistryV2|ManagerRegistryV3|null $doctrine */ - public function __construct($doctrine) + public function __construct(private ManagerRegistryV2|ManagerRegistryV3|null $doctrine = null) { - if ($doctrine !== null && ! $doctrine instanceof ManagerRegistryV2 && ! $doctrine instanceof ManagerRegistryV3) { - throw new TypeError(sprintf('Argument 1 passed to %s must be an instance of Doctrine\Persistence\ManagerRegistry, Doctrine\Common\Persistence\ManagerRegistry or null, %s given', __METHOD__, is_object($doctrine) ? 'instance of ' . get_class($doctrine) : gettype($doctrine))); - } - - $this->doctrine = $doctrine; } public function handle(Envelope $envelope, StackInterface $stack): Envelope diff --git a/lib/Transport/Dbal/DbalReceiver.php b/lib/Transport/Dbal/DbalReceiver.php index 34e7ee4..8479fda 100644 --- a/lib/Transport/Dbal/DbalReceiver.php +++ b/lib/Transport/Dbal/DbalReceiver.php @@ -26,6 +26,7 @@ use function bin2hex; use function class_exists; use function is_resource; +use function is_string; use function json_decode; use function microtime; use function Safe\hex2bin; @@ -40,18 +41,17 @@ class DbalReceiver implements ReceiverInterface, MessageCountAwareInterface, ListableReceiverInterface { private SerializerInterface $serializer; - private string $tableName; - private Connection $connection; private float $redeliverMessagesLastExecutedAt; private float $removeExpiredMessagesLastExecutedAt; private QueryBuilder $select; private QueryBuilder $update; private int $retryingSafetyCounter = 0; - public function __construct(Connection $connection, string $tableName, ?SerializerInterface $serializer = null) - { - $this->connection = $connection; - $this->tableName = $tableName; + public function __construct( + private readonly Connection $connection, + private readonly string $tableName, + SerializerInterface|null $serializer = null, + ) { $this->serializer = $serializer ?? Serializer::create(); $this->select = $this->connection->createQueryBuilder() @@ -115,7 +115,7 @@ public function reject(Envelope $envelope): void /** * {@inheritDoc} */ - public function all(?int $limit = null): iterable + public function all(int|null $limit = null): iterable { $result = $this->connection->createQueryBuilder() ->select('body', 'headers', 'id') @@ -139,7 +139,7 @@ public function all(?int $limit = null): iterable * * @param mixed $id */ - public function find($id): ?Envelope + public function find($id): Envelope|null { if (preg_match('/^[0-9a-f]+$/i', $id)) { $id = hex2bin($id); @@ -180,17 +180,16 @@ private function hydrate(array $row): Envelope 'headers' => json_decode($row['headers'], true, 512, JSON_THROW_ON_ERROR), ]); - if (is_resource($row['id'])) { - $row['id'] = stream_get_contents($row['id']); - } + $id = is_resource($row['id']) ? stream_get_contents($row['id']) : $row['id']; + assert(is_string($id)); - return $envelope->with(new TransportMessageIdStamp(bin2hex($row['id']))); + return $envelope->with(new TransportMessageIdStamp(bin2hex($id))); } /** * Fetches a message if it is any. */ - private function fetchMessage(): ?Envelope + private function fetchMessage(): Envelope|null { $deliveryId = class_exists(SymfonyUuid::class) ? SymfonyUuid::v4()->toRfc4122() : Uuid::uuid4()->toString(); $result = $this->select diff --git a/lib/Transport/Dbal/DbalSender.php b/lib/Transport/Dbal/DbalSender.php index 93ccad1..1ac8ddd 100644 --- a/lib/Transport/Dbal/DbalSender.php +++ b/lib/Transport/Dbal/DbalSender.php @@ -33,13 +33,9 @@ class DbalSender implements SenderInterface { private SerializerInterface $serializer; - private string $tableName; - private Connection $connection; - public function __construct(Connection $connection, string $tableName, ?SerializerInterface $serializer = null) + public function __construct(private Connection $connection, private string $tableName, SerializerInterface|null $serializer = null) { - $this->connection = $connection; - $this->tableName = $tableName; $this->serializer = $serializer ?? Serializer::create(); } diff --git a/lib/Transport/Dbal/DbalTransport.php b/lib/Transport/Dbal/DbalTransport.php index 7211477..a168c63 100644 --- a/lib/Transport/Dbal/DbalTransport.php +++ b/lib/Transport/Dbal/DbalTransport.php @@ -21,20 +21,12 @@ */ class DbalTransport implements TransportInterface, ListableReceiverInterface, MessageCountAwareInterface, SetupableTransportInterface { - private Connection $connection; - private ?SerializerInterface $serializer; private DbalReceiver $receiver; private DbalSender $sender; - /** @var array $options */ - private array $options; - /** @param array $options */ - public function __construct(Connection $connection, ?SerializerInterface $serializer = null, array $options = []) + public function __construct(private Connection $connection, private SerializerInterface|null $serializer = null, private array $options = []) { - $this->connection = $connection; - $this->serializer = $serializer; - $this->options = $options; } /** @@ -94,7 +86,7 @@ public function reject(Envelope $envelope): void * * @return iterable */ - public function all(?int $limit = null): iterable + public function all(int|null $limit = null): iterable { yield from ($this->receiver ?? $this->getReceiver())->all($limit); } @@ -104,7 +96,7 @@ public function all(?int $limit = null): iterable * * @param mixed $id */ - public function find($id): ?Envelope + public function find($id): Envelope|null { return ($this->receiver ?? $this->getReceiver())->find($id); } diff --git a/lib/Transport/Dbal/DbalTransportFactory.php b/lib/Transport/Dbal/DbalTransportFactory.php index 6dc9b55..44c7a7a 100644 --- a/lib/Transport/Dbal/DbalTransportFactory.php +++ b/lib/Transport/Dbal/DbalTransportFactory.php @@ -13,23 +13,18 @@ use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface; use Symfony\Component\Messenger\Transport\TransportFactoryInterface; use Symfony\Component\Messenger\Transport\TransportInterface; -use TypeError; use function array_merge; use function assert; use function explode; -use function get_class; -use function gettype; use function in_array; use function is_array; use function is_file; -use function is_object; use function is_string; use function parse_str; use function pathinfo; use function Safe\parse_url; use function Safe\preg_replace; -use function sprintf; use function strpos; use function strrev; use function substr; @@ -55,17 +50,8 @@ class DbalTransportFactory implements TransportFactoryInterface 'sqlite3', ]; - /** @var ManagerRegistryV2|ManagerRegistryV3|null */ - private $managerRegistry; - - /** @param ManagerRegistryV2|ManagerRegistryV3|null $managerRegistry */ - public function __construct($managerRegistry = null) + public function __construct(private ManagerRegistryV2|ManagerRegistryV3|null $managerRegistry = null) { - if ($managerRegistry !== null && ! $managerRegistry instanceof ManagerRegistryV2 && ! $managerRegistry instanceof ManagerRegistryV3) { - throw new TypeError(sprintf('Argument 1 passed to %s must be an instance of Doctrine\Persistence\ManagerRegistry, Doctrine\Common\Persistence\ManagerRegistry or null, %s given', __METHOD__, is_object($managerRegistry) ? 'instance of ' . get_class($managerRegistry) : gettype($managerRegistry))); - } - - $this->managerRegistry = $managerRegistry; } /** @param array $options */ diff --git a/lib/Transport/Mongo/MongoReceiver.php b/lib/Transport/Mongo/MongoReceiver.php index 61f2269..94eee96 100644 --- a/lib/Transport/Mongo/MongoReceiver.php +++ b/lib/Transport/Mongo/MongoReceiver.php @@ -30,13 +30,11 @@ class MongoReceiver implements ReceiverInterface, ListableReceiverInterface, MessageCountAwareInterface { private SerializerInterface $serializer; - private Collection $collection; private float $removeExpiredMessagesLastExecutedAt; private int $retryingSafetyCounter = 0; - public function __construct(Collection $collection, ?SerializerInterface $serializer = null) + public function __construct(private Collection $collection, SerializerInterface|null $serializer = null) { - $this->collection = $collection; $this->serializer = $serializer ?? Serializer::create(); } @@ -81,7 +79,7 @@ public function reject(Envelope $envelope): void /** * {@inheritDoc} */ - public function all(?int $limit = null): iterable + public function all(int|null $limit = null): iterable { $options = []; if ($options !== null) { @@ -99,7 +97,7 @@ public function all(?int $limit = null): iterable * * @param mixed $id */ - public function find($id): ?Envelope + public function find($id): Envelope|null { $id = $id instanceof ObjectId ? $id : new ObjectId($id); @@ -131,7 +129,7 @@ private function hydrate(array $row): Envelope /** * Fetches a message if it is any. */ - private function fetchMessage(): ?Envelope + private function fetchMessage(): Envelope|null { $deliveryId = class_exists(SymfonyUuid::class) ? SymfonyUuid::v4()->toRfc4122() : Uuid::uuid4()->toString(); $now = time(); diff --git a/lib/Transport/Mongo/MongoSender.php b/lib/Transport/Mongo/MongoSender.php index f90b924..2f15aac 100644 --- a/lib/Transport/Mongo/MongoSender.php +++ b/lib/Transport/Mongo/MongoSender.php @@ -28,12 +28,10 @@ */ class MongoSender implements SenderInterface { - private Collection $collection; private SerializerInterface $serializer; - public function __construct(Collection $collection, ?SerializerInterface $serializer = null) + public function __construct(private Collection $collection, SerializerInterface|null $serializer = null) { - $this->collection = $collection; $this->serializer = $serializer ?? Serializer::create(); } diff --git a/lib/Transport/Mongo/MongoTransport.php b/lib/Transport/Mongo/MongoTransport.php index ae8826a..cfcccc3 100644 --- a/lib/Transport/Mongo/MongoTransport.php +++ b/lib/Transport/Mongo/MongoTransport.php @@ -18,15 +18,13 @@ class MongoTransport implements TransportInterface, ListableReceiverInterface, MessageCountAwareInterface { private Collection $collection; - private ?SerializerInterface $serializer; private MongoReceiver $receiver; private MongoSender $sender; /** @param array $options */ - public function __construct(Client $client, ?SerializerInterface $serializer = null, array $options = []) + public function __construct(Client $client, private SerializerInterface|null $serializer = null, array $options = []) { $this->collection = $client->{$options['database_name']}->{$options['collection_name']}; - $this->serializer = $serializer; } /** @@ -50,7 +48,7 @@ public function reject(Envelope $envelope): void /** * {@inheritDoc} */ - public function all(?int $limit = null): iterable + public function all(int|null $limit = null): iterable { yield from ($this->receiver ?? $this->getReceiver())->all($limit); } @@ -60,7 +58,7 @@ public function all(?int $limit = null): iterable * * @param mixed $id */ - public function find($id): ?Envelope + public function find($id): Envelope|null { return ($this->receiver ?? $this->getReceiver())->find($id); }