From 9ca848b7cab6aa5bd5ad5fd43e0a63419b5c308a Mon Sep 17 00:00:00 2001 From: Thibaud Girard Date: Wed, 28 Feb 2024 14:18:24 +0100 Subject: [PATCH 01/10] feat(amqp): make amqp v2 compatible --- composer.json | 2 +- src/AmqpBundle/Amqp/Consumer.php | 15 +- src/AmqpBundle/Amqp/Producer.php | 11 +- src/AmqpBundle/Event/AckEvent.php | 6 +- src/AmqpBundle/Event/NackEvent.php | 6 +- src/AmqpBundle/Sandbox/NullConnection.php | 18 +- src/AmqpBundle/Sandbox/NullEnvelope.php | 205 +++++------------- src/AmqpBundle/Sandbox/NullQueue.php | 8 +- src/AmqpBundle/Tests/Units/Amqp/Consumer.php | 125 +++++------ src/AmqpBundle/Tests/Units/Amqp/Producer.php | 9 +- .../Units/Factory/Mock/MockAMQPChannel.php | 10 +- .../Units/Factory/Mock/MockAMQPExchange.php | 2 +- .../Units/Factory/Mock/MockAMQPQueue.php | 5 +- 13 files changed, 162 insertions(+), 260 deletions(-) diff --git a/composer.json b/composer.json index 9728b6b..0ed6d16 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ }, "require" : { "php": ">=7.4", - "ext-amqp": "*", + "ext-amqp": ">=2.0", "symfony/dependency-injection": "^4.4 || ^5.0", "symfony/framework-bundle": "^4.4 || ^5.0", "symfony/http-kernel": "^4.4 || ^5.0", diff --git a/src/AmqpBundle/Amqp/Consumer.php b/src/AmqpBundle/Amqp/Consumer.php index 766e7ad..08eaad0 100644 --- a/src/AmqpBundle/Amqp/Consumer.php +++ b/src/AmqpBundle/Amqp/Consumer.php @@ -34,7 +34,6 @@ public function __construct(\AMQPQueue $queue, array $queueOptions) public function getMessage(int $flags = AMQP_AUTOACK): ?\AMQPEnvelope { $envelope = $this->call($this->queue, 'get', [$flags]); - $envelope = $envelope === false ? null : $envelope; if ($this->eventDispatcher) { $preRetrieveEvent = new PreRetrieveEvent($envelope); @@ -49,13 +48,13 @@ public function getMessage(int $flags = AMQP_AUTOACK): ?\AMQPEnvelope /** * Acknowledge the receipt of a message. * - * @param string $deliveryTag delivery tag of last message to ack + * @param int $deliveryTag delivery tag of last message to ack * @param int $flags AMQP_MULTIPLE or AMQP_NOPARAM * * @throws \AMQPChannelException if the channel is not open * @throws \AMQPConnectionException if the connection to the broker was lost */ - public function ackMessage(string $deliveryTag, int $flags = AMQP_NOPARAM): bool + public function ackMessage(int $deliveryTag, int $flags = AMQP_NOPARAM): void { if ($this->eventDispatcher) { $ackEvent = new AckEvent($deliveryTag, $flags); @@ -63,19 +62,19 @@ public function ackMessage(string $deliveryTag, int $flags = AMQP_NOPARAM): bool $this->eventDispatcher->dispatch($ackEvent,AckEvent::NAME); } - return $this->call($this->queue, 'ack', [$deliveryTag, $flags]); + $this->call($this->queue, 'ack', [$deliveryTag, $flags]); } /** * Mark a message as explicitly not acknowledged. * - * @param string $deliveryTag delivery tag of last message to nack + * @param int $deliveryTag delivery tag of last message to nack * @param int $flags AMQP_NOPARAM or AMQP_REQUEUE to requeue the message(s) * * @throws \AMQPConnectionException if the connection to the broker was lost * @throws \AMQPChannelException if the channel is not open */ - public function nackMessage(string $deliveryTag, int $flags = AMQP_NOPARAM): bool + public function nackMessage(int $deliveryTag, int $flags = AMQP_NOPARAM): void { if ($this->eventDispatcher) { $nackEvent = new NackEvent($deliveryTag, $flags); @@ -83,7 +82,7 @@ public function nackMessage(string $deliveryTag, int $flags = AMQP_NOPARAM): boo $this->eventDispatcher->dispatch($nackEvent, NackEvent::NAME); } - return $this->call($this->queue, 'nack', [$deliveryTag, $flags]); + $this->call($this->queue, 'nack', [$deliveryTag, $flags]); } /** @@ -92,7 +91,7 @@ public function nackMessage(string $deliveryTag, int $flags = AMQP_NOPARAM): boo * @throws \AMQPChannelException if the channel is not open * @throws \AMQPConnectionException if the connection to the broker was lost */ - public function purge(): bool + public function purge(): int { if ($this->eventDispatcher) { $purgeEvent = new PurgeEvent($this->queue); diff --git a/src/AmqpBundle/Amqp/Producer.php b/src/AmqpBundle/Amqp/Producer.php index 3af083f..0760691 100644 --- a/src/AmqpBundle/Amqp/Producer.php +++ b/src/AmqpBundle/Amqp/Producer.php @@ -33,7 +33,7 @@ public function __construct(\AMQPExchange $exchange, array $exchangeOptions) * timestamp, expiration, type or reply_to * @param array $routingKeys If set, overrides the Producer 'routing_keys' for this message * - * @return bool tRUE on success or FALSE on failure + * @return bool TRUE on success or throws on failure * * @throws \AMQPExchangeException on failure * @throws \AMQPChannelException if the channel is not open @@ -63,16 +63,17 @@ public function publishMessage(string $message, int $flags = AMQP_NOPARAM, array } if (!$routingKeys) { - return $this->call($this->exchange, 'publish', [$message, null, $flags, $attributes]); + $this->call($this->exchange, 'publish', [$message, null, $flags, $attributes]); + + return true; } // Publish the message for each routing keys - $success = true; foreach ($routingKeys as $routingKey) { - $success &= $this->call($this->exchange, 'publish', [$message, $routingKey, $flags, $attributes]); + $this->call($this->exchange, 'publish', [$message, $routingKey, $flags, $attributes]); } - return (bool) $success; + return true; } public function getExchange(): \AMQPExchange diff --git a/src/AmqpBundle/Event/AckEvent.php b/src/AmqpBundle/Event/AckEvent.php index 518f6b5..849b36c 100644 --- a/src/AmqpBundle/Event/AckEvent.php +++ b/src/AmqpBundle/Event/AckEvent.php @@ -11,17 +11,17 @@ class AckEvent extends Event { const NAME = 'amqp.ack'; - private string $deliveryTag; + private int $deliveryTag; private int $flags; - public function __construct(string $deliveryTag, int $flags) + public function __construct(int $deliveryTag, int $flags) { $this->deliveryTag = $deliveryTag; $this->flags = $flags; } - public function getDeliveryTag(): string + public function getDeliveryTag(): int { return $this->deliveryTag; } diff --git a/src/AmqpBundle/Event/NackEvent.php b/src/AmqpBundle/Event/NackEvent.php index 46a02d7..eb977d8 100644 --- a/src/AmqpBundle/Event/NackEvent.php +++ b/src/AmqpBundle/Event/NackEvent.php @@ -11,16 +11,16 @@ class NackEvent extends Event { const NAME = 'amqp.nack'; - private string $deliveryTag; + private int $deliveryTag; private int $flags; - public function __construct(string $deliveryTag, int $flags) + public function __construct(int $deliveryTag, int $flags) { $this->deliveryTag = $deliveryTag; $this->flags = $flags; } - public function getDeliveryTag(): string + public function getDeliveryTag(): int { return $this->deliveryTag; } diff --git a/src/AmqpBundle/Sandbox/NullConnection.php b/src/AmqpBundle/Sandbox/NullConnection.php index 77c69e8..1998f6d 100644 --- a/src/AmqpBundle/Sandbox/NullConnection.php +++ b/src/AmqpBundle/Sandbox/NullConnection.php @@ -10,48 +10,42 @@ class NullConnection extends \AMQPConnection /** * {@inheritdoc} */ - public function connect() + public function connect(): void { - return true; } /** * {@inheritdoc} */ - public function pconnect() + public function pconnect(): void { - return true; } /** * {@inheritdoc} */ - public function pdisconnect() + public function pdisconnect(): void { - return true; } /** * {@inheritdoc} */ - public function disconnect() + public function disconnect(): void { - return true; } /** * {@inheritdoc} */ - public function reconnect() + public function reconnect(): void { - return true; } /** * {@inheritdoc} */ - public function preconnect() + public function preconnect(): void { - return true; } } diff --git a/src/AmqpBundle/Sandbox/NullEnvelope.php b/src/AmqpBundle/Sandbox/NullEnvelope.php index 588c63c..381fb6b 100644 --- a/src/AmqpBundle/Sandbox/NullEnvelope.php +++ b/src/AmqpBundle/Sandbox/NullEnvelope.php @@ -7,87 +7,33 @@ */ class NullEnvelope extends \AMQPEnvelope { - /** - * @var string - */ - private $body; - /** - * @var string - */ - private $routingKey; - /** - * @var string - */ - private $deliveryTag; - /** - * @var string - */ - private $deliveryMode; - /** - * @var bool - */ - private $redelivery; - /** - * @var string - */ - private $contentType; - /** - * @var string - */ - private $contentEncoding; - /** - * @var string - */ - private $type; - /** - * @var string - */ - private $timestamp; - /** - * @var int - */ - private $priority; - /** - * @var string - */ - private $expiration; - /** - * @var string - */ - private $userId; - /** - * @var string - */ - private $appId; - /** - * @var string - */ - private $messageId; - /** - * @var string - */ - private $replyTo; - /** - * @var string - */ - private $correlationId; - /** - * @var array - */ - private $headers; + private string $body; + private string $routingKey; + private int $deliveryTag; + private int $deliveryMode; + private bool $redelivery; + private string $contentType; + private string $contentEncoding; + private string $type; + private string $timestamp; + private int $priority; + private string $expiration; + private string $userId; + private string $appId; + private string $messageId; + private string $replyTo; + private string $correlationId; + private array $headers; /** * {@inheritdoc} */ - public function getBody() + public function getBody(): string { return $this->body; } - /** - * @param string $body - */ - public function setBody($body) + public function setBody(string $body): void { $this->body = $body; } @@ -95,15 +41,12 @@ public function setBody($body) /** * {@inheritdoc} */ - public function getRoutingKey() + public function getRoutingKey(): string { return $this->routingKey; } - /** - * @param string $routingKey - */ - public function setRoutingKey($routingKey) + public function setRoutingKey(string $routingKey): void { $this->routingKey = $routingKey; } @@ -111,15 +54,12 @@ public function setRoutingKey($routingKey) /** * {@inheritdoc} */ - public function getDeliveryTag() + public function getDeliveryTag(): int { return $this->deliveryTag; } - /** - * @param string $deliveryTag - */ - public function setDeliveryTag($deliveryTag) + public function setDeliveryTag(int $deliveryTag): void { $this->deliveryTag = $deliveryTag; } @@ -127,15 +67,12 @@ public function setDeliveryTag($deliveryTag) /** * {@inheritdoc} */ - public function getDeliveryMode() + public function getDeliveryMode(): int { return $this->deliveryMode; } - /** - * @param string $deliveryMode - */ - public function setDeliveryMode($deliveryMode) + public function setDeliveryMode(int $deliveryMode): void { $this->deliveryMode = $deliveryMode; } @@ -143,15 +80,12 @@ public function setDeliveryMode($deliveryMode) /** * {@inheritdoc} */ - public function isRedelivery() + public function isRedelivery(): bool { return $this->redelivery; } - /** - * @param bool $redelivery - */ - public function setRedelivery($redelivery) + public function setRedelivery(bool $redelivery): void { $this->redelivery = $redelivery; } @@ -159,15 +93,12 @@ public function setRedelivery($redelivery) /** * {@inheritdoc} */ - public function getContentType() + public function getContentType(): string { return $this->contentType; } - /** - * @param string $contentType - */ - public function setContentType($contentType) + public function setContentType(string $contentType): void { $this->contentType = $contentType; } @@ -175,15 +106,12 @@ public function setContentType($contentType) /** * {@inheritdoc} */ - public function getContentEncoding() + public function getContentEncoding(): string { return $this->contentEncoding; } - /** - * @param string $contentEncoding - */ - public function setContentEncoding($contentEncoding) + public function setContentEncoding(string $contentEncoding): void { $this->contentEncoding = $contentEncoding; } @@ -191,15 +119,12 @@ public function setContentEncoding($contentEncoding) /** * {@inheritdoc} */ - public function getType() + public function getType(): string { return $this->type; } - /** - * @param string $type - */ - public function setType($type) + public function setType(string $type): void { $this->type = $type; } @@ -207,15 +132,12 @@ public function setType($type) /** * {@inheritdoc} */ - public function getTimestamp() + public function getTimestamp(): ?int { return $this->timestamp; } - /** - * @param string $timestamp - */ - public function setTimestamp($timestamp) + public function setTimestamp(string $timestamp): void { $this->timestamp = $timestamp; } @@ -223,15 +145,12 @@ public function setTimestamp($timestamp) /** * {@inheritdoc} */ - public function getPriority() + public function getPriority(): int { return $this->priority; } - /** - * @param int $priority - */ - public function setPriority($priority) + public function setPriority(int $priority): void { $this->priority = $priority; } @@ -239,15 +158,12 @@ public function setPriority($priority) /** * {@inheritdoc} */ - public function getExpiration() + public function getExpiration(): string { return $this->expiration; } - /** - * @param string $expiration - */ - public function setExpiration($expiration) + public function setExpiration(string $expiration): void { $this->expiration = $expiration; } @@ -255,15 +171,12 @@ public function setExpiration($expiration) /** * {@inheritdoc} */ - public function getUserId() + public function getUserId(): string { return $this->userId; } - /** - * @param string $userId - */ - public function setUserId($userId) + public function setUserId(string $userId): void { $this->userId = $userId; } @@ -271,15 +184,12 @@ public function setUserId($userId) /** * {@inheritdoc} */ - public function getAppId() + public function getAppId(): string { return $this->appId; } - /** - * @param string $appId - */ - public function setAppId($appId) + public function setAppId(string $appId): void { $this->appId = $appId; } @@ -287,15 +197,12 @@ public function setAppId($appId) /** * {@inheritdoc} */ - public function getMessageId() + public function getMessageId(): string { return $this->messageId; } - /** - * @param string $messageId - */ - public function setMessageId($messageId) + public function setMessageId(string $messageId): void { $this->messageId = $messageId; } @@ -303,15 +210,12 @@ public function setMessageId($messageId) /** * {@inheritdoc} */ - public function getReplyTo() + public function getReplyTo(): string { return $this->replyTo; } - /** - * @param string $replyTo - */ - public function setReplyTo($replyTo) + public function setReplyTo(string $replyTo): void { $this->replyTo = $replyTo; } @@ -319,15 +223,12 @@ public function setReplyTo($replyTo) /** * {@inheritdoc} */ - public function getCorrelationId() + public function getCorrelationId(): string { return $this->correlationId; } - /** - * @param string $correlationId - */ - public function setCorrelationId($correlationId) + public function setCorrelationId(string $correlationId): void { $this->correlationId = $correlationId; } @@ -335,7 +236,7 @@ public function setCorrelationId($correlationId) /** * {@inheritdoc} */ - public function getHeaders() + public function getHeaders(): array { return $this->headers; } @@ -343,7 +244,7 @@ public function getHeaders() /** * @param array $headers */ - public function setHeaders($headers) + public function setHeaders(array $headers): void { $this->headers = $headers; } @@ -351,8 +252,8 @@ public function setHeaders($headers) /** * {@inheritdoc} */ - public function getHeader($headerKey) + public function getHeader(string $headerName): ?string { - return isset($this->headers[$headerKey]) ? $this->headers[$headerKey] : false; + return $this->headers[$headerName] ?? null; } } diff --git a/src/AmqpBundle/Sandbox/NullQueue.php b/src/AmqpBundle/Sandbox/NullQueue.php index d5a5d15..97b4f1f 100644 --- a/src/AmqpBundle/Sandbox/NullQueue.php +++ b/src/AmqpBundle/Sandbox/NullQueue.php @@ -35,19 +35,19 @@ public function enqueue(\AMQPEnvelope $envelope = null) /** * {@inheritdoc} */ - public function get($flags = AMQP_NOPARAM) + public function get($flags = AMQP_NOPARAM): ?\AMQPEnvelope { if (!$this->envelopes->isEmpty()) { return $this->envelopes->dequeue(); - } else { - return false; } + + return null; } /** * {@inheritdoc} */ - public function declareQueue() + public function declareQueue(): int { return $this->envelopes->count(); } diff --git a/src/AmqpBundle/Tests/Units/Amqp/Consumer.php b/src/AmqpBundle/Tests/Units/Amqp/Consumer.php index 9360834..8965e50 100644 --- a/src/AmqpBundle/Tests/Units/Amqp/Consumer.php +++ b/src/AmqpBundle/Tests/Units/Amqp/Consumer.php @@ -26,8 +26,9 @@ public function testConstruct() public function testGetMessageAutoAck() { + $msgList = ['wait' => [1 => 'message1', '2' => 'message2']]; $this - ->if($msgList = ['wait' => ['m1' => 'message1', 'm2' => 'message2']]) + ->if($msgList) ->and($queue = $this->getQueue($msgList)) ->and($consumer = new Base($queue, [])) // First message : auto ack @@ -35,12 +36,12 @@ public function testGetMessageAutoAck() ->isInstanceOf('\AMQPEnvelope') ->string($message->getBody()) ->isEqualTo('message1') - ->string($message->getDeliveryTag()) - ->isEqualTo('m1') + ->integer($message->getDeliveryTag()) + ->isEqualTo(1) ->array($msgList['wait']) - ->isEqualTo(['m2' => 'message2']) + ->isEqualTo([2 => 'message2']) ->array($msgList['ack']) - ->isEqualTo(['m1' => 'message1']) + ->isEqualTo([1 => 'message1']) ->array($msgList['unack']) ->isEmpty() @@ -49,12 +50,12 @@ public function testGetMessageAutoAck() ->isInstanceOf('\AMQPEnvelope') ->string($message->getBody()) ->isEqualTo('message2') - ->string($message->getDeliveryTag()) - ->isEqualTo('m2') + ->integer($message->getDeliveryTag()) + ->isEqualTo(2) ->array($msgList['wait']) ->isEmpty() ->array($msgList['ack']) - ->isEqualTo(['m1' => 'message1', 'm2' => 'message2']) + ->isEqualTo([1 => 'message1', 2 => 'message2']) ->array($msgList['unack']) ->isEmpty() @@ -71,7 +72,7 @@ public function testGetMessageAutoAck() public function testGetMessageManualAck() { $this - ->if($msgList = ['wait' => ['m1' => 'message1']]) + ->if($msgList = ['wait' => [1 => 'message1']]) ->and($queue = $this->getQueue($msgList)) ->and($consumer = new Base($queue, [])) // First message : manual ack @@ -79,58 +80,56 @@ public function testGetMessageManualAck() ->isInstanceOf('\AMQPEnvelope') ->string($message->getBody()) ->isEqualTo('message1') - ->string($message->getDeliveryTag()) - ->isEqualTo('m1') + ->integer($message->getDeliveryTag()) + ->isEqualTo(1) // Check data ->array($msgList['wait']) ->isEmpty() ->array($msgList['unack']) - ->isEqualTo(['m1' => 'message1']) + ->isEqualTo([1 => 'message1']) ->array($msgList['ack']) ->isEmpty() // Ack (unknown delivery id) - ->boolean($message = $consumer->ackMessage(12345)) - ->isFalse() - ->mock($queue) - ->call('ack') - ->withArguments(12345) - ->once() + ->and($message = $consumer->ackMessage(12345)) + ->variable($message) + ->isNull() + ->mock($queue) + ->call('ack') + ->withArguments(12345) + ->once() // Check data ->array($msgList['wait']) ->isEmpty() ->array($msgList['unack']) - ->isEqualTo(['m1' => 'message1']) + ->isEqualTo([1 => 'message1']) ->array($msgList['ack']) ->isEmpty() // Ack (known delivery id) - ->boolean($message = $consumer->ackMessage('m1')) - ->isTrue() - ->mock($queue) - ->call('ack') - ->withArguments('m1') - ->once() + ->and($message = $consumer->ackMessage(1)) + ->variable($message) + ->isNull() + ->mock($queue) + ->call('ack') + ->withArguments(1) + ->once() // Check data ->array($msgList['wait']) ->isEmpty() ->array($msgList['ack']) - ->isEqualTo(['m1' => 'message1']) + ->isEqualTo([1 => 'message1']) ->array($msgList['unack']) - ->isEmpty() - - // Ack (old delivery id) - ->boolean($message = $consumer->ackMessage('m1')) - ->isFalse(); + ->isEmpty(); } public function testGetMessageManualNack() { $this - ->if($msgList = ['wait' => ['m1' => 'message1']]) + ->if($msgList = ['wait' => [1 => 'message1']]) ->and($queue = $this->getQueue($msgList)) ->and($consumer = new Base($queue, [])) // First message : manual ack @@ -138,44 +137,46 @@ public function testGetMessageManualNack() ->isInstanceOf('\AMQPEnvelope') ->string($message->getBody()) ->isEqualTo('message1') - ->string($message->getDeliveryTag()) - ->isEqualTo('m1') + ->integer($message->getDeliveryTag()) + ->isEqualTo(1) // Check data ->array($msgList['wait']) ->isEmpty() ->array($msgList['unack']) - ->isEqualTo(['m1' => 'message1']) + ->isEqualTo([1 => 'message1']) ->array($msgList['ack']) ->isEmpty() // Nack (unknown delivery id) - ->boolean($message = $consumer->nackMessage(12345)) - ->isFalse() - ->mock($queue) - ->call('nack') - ->withArguments(12345) - ->once() + ->and($message = $consumer->nackMessage(12345)) + ->variable($message) + ->isNull() + ->mock($queue) + ->call('nack') + ->withArguments(12345) + ->once() // Check data ->array($msgList['wait']) ->isEmpty() ->array($msgList['unack']) - ->isEqualTo(['m1' => 'message1']) + ->isEqualTo([1 => 'message1']) ->array($msgList['ack']) ->isEmpty() // Nack and requeue (known delivery id) - ->boolean($message = $consumer->nackMessage('m1', AMQP_REQUEUE)) - ->isTrue() - ->mock($queue) - ->call('nack') - ->withArguments('m1') - ->once() + ->and($message = $consumer->nackMessage(1, AMQP_REQUEUE)) + ->variable($message) + ->isNull() + ->mock($queue) + ->call('nack') + ->withArguments(1) + ->once() // Check data ->array($msgList['wait']) - ->isEqualTo(['m1' => 'message1']) + ->isEqualTo([1 => 'message1']) ->array($msgList['ack']) ->isEmpty() ->array($msgList['unack']) @@ -186,20 +187,21 @@ public function testGetMessageManualNack() ->isInstanceOf('\AMQPEnvelope') ->string($message->getBody()) ->isEqualTo('message1') - ->string($message->getDeliveryTag()) - ->isEqualTo('m1') + ->integer($message->getDeliveryTag()) + ->isEqualTo(1) // Check data ->array($msgList['wait']) ->isEmpty() ->array($msgList['unack']) - ->isEqualTo(['m1' => 'message1']) + ->isEqualTo([1 => 'message1']) ->array($msgList['ack']) ->isEmpty() // Nack and forget (known delivery id) - ->boolean($message = $consumer->nackMessage('m1')) - ->isTrue() + ->and($message = $consumer->nackMessage(1)) + ->variable($message) + ->isNull() // Check data ->array($msgList['wait']) @@ -210,19 +212,19 @@ public function testGetMessageManualNack() ->isEmpty() // Nack (old delivery id) - ->boolean($message = $consumer->nackMessage('m1')) - ->isFalse(); + ->variable($message = $consumer->nackMessage(1)) + ->isEqualTo(null); } public function testPurge() { $this - ->if($msgList = ['wait' => ['m1' => 'message1']]) + ->if($msgList = ['wait' => [1 => 'message1']]) ->and($queue = $this->getQueue($msgList)) ->and($consumer = new Base($queue, [])) // Purge the queue - ->boolean($consumer->purge()) - ->isTrue() + ->integer($consumer->purge()) + ->isEqualTo(1) ->mock($queue) ->call('purge') ->once() @@ -244,7 +246,7 @@ public function testGetMessageCurrentCount() { $this ->if($msgList = [ - 'wait' => ['m1' => 'message1', 'm2' => 'message2'], + 'wait' => [1 => 'message1', 2 => 'message2'], 'flags' => AMQP_DURABLE | AMQP_EXCLUSIVE | AMQP_AUTODELETE, ]) ->and($queue = $this->getQueue($msgList)) @@ -313,7 +315,8 @@ protected function getQueue(&$msgList = []) // Simulate a simple queue reset($msgList['wait']); $key = key($msgList['wait']); - $msg = array_shift($msgList['wait']); + $msg = reset($msgList['wait']); + unset($msgList['wait'][$key]); if (!$key) { return null; diff --git a/src/AmqpBundle/Tests/Units/Amqp/Producer.php b/src/AmqpBundle/Tests/Units/Amqp/Producer.php index a5854d6..df6e346 100644 --- a/src/AmqpBundle/Tests/Units/Amqp/Producer.php +++ b/src/AmqpBundle/Tests/Units/Amqp/Producer.php @@ -72,8 +72,11 @@ public function testSendMessagesError() ->and($producer = new Base($exchange, $exchangeOptions)) ->boolean($producer->publishMessage('message1')) ->isTrue() - ->boolean($producer->publishMessage('error')) - ->isFalse() + ->exception( + function() use($producer) { + $producer->publishMessage('error'); + } + )->isInstanceOf(\AMQPExchangeException::class) ->array($msgList) ->isEqualTo([ ['message1', 'routing_test', AMQP_NOPARAM, $exchangeOptions['publish_attributes']], @@ -182,7 +185,7 @@ protected function getExchange(&$msgList = []) $exchange->getMockController()->publish = function ($message, $routing_key, $flags = AMQP_NOPARAM, array $attributes = []) use (&$msgList) { if (($message == 'error') && ($routing_key == 'error')) { - return false; + throw new \AMQPExchangeException(); } $msgList[] = [$message, $routing_key, $flags, $attributes]; diff --git a/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPChannel.php b/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPChannel.php index 935d1ec..759cd4a 100644 --- a/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPChannel.php +++ b/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPChannel.php @@ -11,15 +11,15 @@ public function __construct(\AMQPConnection $amqp_connection) { } - public function qos($prefetchSize, $prefetchCount, $global = NULL) + public function qos(int $size, int $count, bool $global = NULL): void { } - public function setPrefetchSize($count){ - + public function setPrefetchSize(int $size): void + { } - public function setPrefetchCount($count){ - + public function setPrefetchCount(int $count): void + { } } diff --git a/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPExchange.php b/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPExchange.php index 04040b7..dc5917f 100644 --- a/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPExchange.php +++ b/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPExchange.php @@ -11,7 +11,7 @@ public function __construct(\AMQPChannel $amqp_channel) { } - public function declareExchange() + public function declareExchange(): void { } } diff --git a/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPQueue.php b/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPQueue.php index 9beb77f..a1d481a 100644 --- a/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPQueue.php +++ b/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPQueue.php @@ -11,11 +11,12 @@ public function __construct(\AMQPChannel $amqp_channel) { } - public function bind($exchange_name, $routing_key = null, $arguments = array()) + public function bind(string $exchange_name, string $routing_key = null, array $arguments = array()): void { } - public function declareQueue() + public function declareQueue(): int { + return 1; } } From 918df87ce32a6078b3b24d893298ca0ed68858bf Mon Sep 17 00:00:00 2001 From: Thibaud Girard Date: Fri, 1 Mar 2024 16:14:25 +0100 Subject: [PATCH 02/10] feat(php): make code php 8 complient --- src/AmqpBundle/Amqp/AbstractAmqp.php | 8 +++--- src/AmqpBundle/Amqp/Consumer.php | 8 +----- src/AmqpBundle/Amqp/DataCollector.php | 10 +++---- src/AmqpBundle/Amqp/Locator.php | 4 +-- .../CompilerPass/M6webAmqpLocatorPass.php | 2 +- .../DependencyInjection/Configuration.php | 14 +++++----- .../M6WebAmqpExtension.php | 4 +-- src/AmqpBundle/Event/AckEvent.php | 10 ++----- src/AmqpBundle/Event/DispatcherInterface.php | 2 +- src/AmqpBundle/Event/NackEvent.php | 9 ++----- src/AmqpBundle/Event/PrePublishEvent.php | 25 ++++++------------ src/AmqpBundle/Event/PreRetrieveEvent.php | 9 +++---- src/AmqpBundle/Event/PurgeEvent.php | 7 ++--- src/AmqpBundle/M6WebAmqpBundle.php | 2 +- .../Resources/config/sandbox_services.yml | 2 +- src/AmqpBundle/Sandbox/NullEnvelope.php | 3 --- src/AmqpBundle/Sandbox/NullExchange.php | 4 +-- src/AmqpBundle/Sandbox/NullQueue.php | 2 +- src/AmqpBundle/Tests/Units/Amqp/Consumer.php | 26 +++++++------------ src/AmqpBundle/Tests/Units/Amqp/Producer.php | 16 ++++++------ .../M6WebAmqpExtension.php | 23 +++++++++------- .../Tests/Units/Factory/ConsumerFactory.php | 6 ++--- .../Units/Factory/Mock/MockAMQPQueue.php | 2 +- .../Tests/Units/Factory/ProducerFactory.php | 6 ++--- 24 files changed, 82 insertions(+), 122 deletions(-) diff --git a/src/AmqpBundle/Amqp/AbstractAmqp.php b/src/AmqpBundle/Amqp/AbstractAmqp.php index 09fa6be..da14847 100644 --- a/src/AmqpBundle/Amqp/AbstractAmqp.php +++ b/src/AmqpBundle/Amqp/AbstractAmqp.php @@ -25,7 +25,7 @@ abstract class AbstractAmqp * @param mixed $return Return value of the command * @param float $time Exec time */ - protected function notifyEvent(string $command, array $arguments, $return, float $time = 0) + protected function notifyEvent(string $command, array $arguments, mixed $return, float $time = 0) { if ($this->eventDispatcher) { $event = new $this->eventClass(); @@ -51,7 +51,7 @@ protected function call(object $object, string $name, array $arguments = []) { $start = microtime(true); - $ret = call_user_func_array(array($object, $name), $arguments); + $ret = call_user_func_array([$object, $name], $arguments); $this->notifyEvent($name, $arguments, $ret, microtime(true) - $start); @@ -66,10 +66,10 @@ protected function call(object $object, string $name, array $arguments = []) * * @throws \Exception */ - public function setEventDispatcher(EventDispatcherInterface $eventDispatcher, string $eventClass) + public function setEventDispatcher(EventDispatcherInterface $eventDispatcher, string $eventClass): void { $class = new \ReflectionClass($eventClass); - if (!$class->implementsInterface('\M6Web\Bundle\AmqpBundle\Event\DispatcherInterface')) { + if (!$class->implementsInterface(\M6Web\Bundle\AmqpBundle\Event\DispatcherInterface::class)) { throw new Exception('The Event class : '.$eventClass.' must implement DispatcherInterface'); } diff --git a/src/AmqpBundle/Amqp/Consumer.php b/src/AmqpBundle/Amqp/Consumer.php index 08eaad0..a84a8a3 100644 --- a/src/AmqpBundle/Amqp/Consumer.php +++ b/src/AmqpBundle/Amqp/Consumer.php @@ -13,14 +13,8 @@ class Consumer extends AbstractAmqp { - protected \AMQPQueue $queue; - - protected array $queueOptions = []; - - public function __construct(\AMQPQueue $queue, array $queueOptions) + public function __construct(protected \AMQPQueue $queue, protected array $queueOptions) { - $this->queue = $queue; - $this->queueOptions = $queueOptions; } /** diff --git a/src/AmqpBundle/Amqp/DataCollector.php b/src/AmqpBundle/Amqp/DataCollector.php index 410bd1b..449a770 100644 --- a/src/AmqpBundle/Amqp/DataCollector.php +++ b/src/AmqpBundle/Amqp/DataCollector.php @@ -34,13 +34,9 @@ public function collect(Request $request, Response $response, ?\Throwable $excep * * @param DispatcherInterface $event The event object */ - public function onCommand(DispatcherInterface $event) + public function onCommand(DispatcherInterface $event): void { - $this->data['commands'][] = array( - 'command' => $event->getCommand(), - 'arguments' => $event->getArguments(), - 'executiontime' => $event->getExecutionTime(), - ); + $this->data['commands'][] = ['command' => $event->getCommand(), 'arguments' => $event->getArguments(), 'executiontime' => $event->getExecutionTime()]; } /** @@ -82,7 +78,7 @@ public function getAvgExecutionTime(): float /** * {@inheritdoc} */ - public function reset() + public function reset(): void { // Reset the current data, while keeping the 'name' intact. $this->data = [ diff --git a/src/AmqpBundle/Amqp/Locator.php b/src/AmqpBundle/Amqp/Locator.php index 6ca7237..ab85166 100644 --- a/src/AmqpBundle/Amqp/Locator.php +++ b/src/AmqpBundle/Amqp/Locator.php @@ -16,7 +16,7 @@ public function getConsumer(string $id): Consumer } /** @param Consumer[] $consumers */ - public function setConsumers(array $consumers) + public function setConsumers(array $consumers): void { $this->consumers = $consumers; } @@ -27,7 +27,7 @@ public function getProducer(string $id): Producer } /** @param Producer[] $producers */ - public function setProducers(array $producers) + public function setProducers(array $producers): void { $this->producers = $producers; } diff --git a/src/AmqpBundle/DependencyInjection/CompilerPass/M6webAmqpLocatorPass.php b/src/AmqpBundle/DependencyInjection/CompilerPass/M6webAmqpLocatorPass.php index d0d4466..2bb6154 100644 --- a/src/AmqpBundle/DependencyInjection/CompilerPass/M6webAmqpLocatorPass.php +++ b/src/AmqpBundle/DependencyInjection/CompilerPass/M6webAmqpLocatorPass.php @@ -8,7 +8,7 @@ class M6webAmqpLocatorPass implements CompilerPassInterface { - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { if (!$container->has('m6_web_amqp.locator')) { return; diff --git a/src/AmqpBundle/DependencyInjection/Configuration.php b/src/AmqpBundle/DependencyInjection/Configuration.php index def68ad..a80f708 100644 --- a/src/AmqpBundle/DependencyInjection/Configuration.php +++ b/src/AmqpBundle/DependencyInjection/Configuration.php @@ -99,14 +99,14 @@ protected function addProducers(ArrayNodeDefinition $node) // args ->arrayNode('arguments') ->prototype('scalar')->end() - ->defaultValue(array()) + ->defaultValue([]) ->normalizeKeys(false) ->end() // binding ->arrayNode('routing_keys') ->prototype('scalar')->end() - ->defaultValue(array()) + ->defaultValue([]) ->end() ->end() ->end() @@ -142,14 +142,14 @@ protected function addConsumers(ArrayNodeDefinition $node) // args ->arrayNode('arguments') ->prototype('scalar')->end() - ->defaultValue(array()) + ->defaultValue([]) ->normalizeKeys(false) ->end() // binding ->arrayNode('routing_keys') ->prototype('scalar')->end() - ->defaultValue(array()) + ->defaultValue([]) ->end() ->end() ->end() @@ -193,20 +193,20 @@ private function exchangeOptions() // args ->arrayNode('arguments') ->prototype('scalar')->end() - ->defaultValue(array()) + ->defaultValue([]) ->normalizeKeys(false) ->end() // binding ->arrayNode('routing_keys') ->prototype('scalar')->end() - ->defaultValue(array()) + ->defaultValue([]) ->end() // default message attributes ->arrayNode('publish_attributes') ->prototype('scalar')->end() - ->defaultValue(array()) + ->defaultValue([]) ->end() ->end(); //last end is missed here intentionally because arrayNode doesn't have an actual parent diff --git a/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php b/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php index 5cbf08c..245ee29 100644 --- a/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php +++ b/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php @@ -19,7 +19,7 @@ class M6WebAmqpExtension extends Extension /** * {@inheritdoc} */ - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); @@ -159,7 +159,7 @@ protected function loadConsumers(ContainerBuilder $container, array $config) } } - private function setEventDispatcher(ContainerBuilder $container, bool $enableEventDispatcher, Definition $definition) + private function setEventDispatcher(ContainerBuilder $container, bool $enableEventDispatcher, Definition $definition): void { // Add the Event dispatcher & Command Event if ($enableEventDispatcher === true) { diff --git a/src/AmqpBundle/Event/AckEvent.php b/src/AmqpBundle/Event/AckEvent.php index 849b36c..81b2ea5 100644 --- a/src/AmqpBundle/Event/AckEvent.php +++ b/src/AmqpBundle/Event/AckEvent.php @@ -9,16 +9,10 @@ */ class AckEvent extends Event { - const NAME = 'amqp.ack'; + public const NAME = 'amqp.ack'; - private int $deliveryTag; - - private int $flags; - - public function __construct(int $deliveryTag, int $flags) + public function __construct(private readonly int $deliveryTag, private readonly int $flags) { - $this->deliveryTag = $deliveryTag; - $this->flags = $flags; } public function getDeliveryTag(): int diff --git a/src/AmqpBundle/Event/DispatcherInterface.php b/src/AmqpBundle/Event/DispatcherInterface.php index fb4401f..2f672c8 100644 --- a/src/AmqpBundle/Event/DispatcherInterface.php +++ b/src/AmqpBundle/Event/DispatcherInterface.php @@ -33,5 +33,5 @@ public function setArguments(array $v); * * @param mixed $v value */ - public function setReturn($v); + public function setReturn(mixed $v); } diff --git a/src/AmqpBundle/Event/NackEvent.php b/src/AmqpBundle/Event/NackEvent.php index eb977d8..577f21c 100644 --- a/src/AmqpBundle/Event/NackEvent.php +++ b/src/AmqpBundle/Event/NackEvent.php @@ -9,15 +9,10 @@ */ class NackEvent extends Event { - const NAME = 'amqp.nack'; + public const NAME = 'amqp.nack'; - private int $deliveryTag; - private int $flags; - - public function __construct(int $deliveryTag, int $flags) + public function __construct(private readonly int $deliveryTag, private readonly int $flags) { - $this->deliveryTag = $deliveryTag; - $this->flags = $flags; } public function getDeliveryTag(): int diff --git a/src/AmqpBundle/Event/PrePublishEvent.php b/src/AmqpBundle/Event/PrePublishEvent.php index 7902d02..43c5d55 100644 --- a/src/AmqpBundle/Event/PrePublishEvent.php +++ b/src/AmqpBundle/Event/PrePublishEvent.php @@ -9,20 +9,11 @@ */ class PrePublishEvent extends Event { - const NAME = 'amqp.pre_publish'; - - private string $message; - private array $routingKeys; - private int $flags; - private array $attributes; + public const NAME = 'amqp.pre_publish'; private bool $canPublish; - public function __construct(string $message, array $routingKeys, int $flags, array $attributes) + public function __construct(private string $message, private array $routingKeys, private int $flags, private array $attributes) { - $this->message = $message; - $this->routingKeys = $routingKeys; - $this->flags = $flags; - $this->attributes = $attributes; $this->canPublish = true; } @@ -31,12 +22,12 @@ public function canPublish(): bool return $this->canPublish; } - public function allowPublish() + public function allowPublish(): void { $this->canPublish = true; } - public function denyPublish() + public function denyPublish(): void { $this->canPublish = false; } @@ -46,7 +37,7 @@ public function getMessage(): string return $this->message; } - public function setMessage(string $message) + public function setMessage(string $message): void { $this->message = $message; } @@ -56,7 +47,7 @@ public function getRoutingKeys(): array return $this->routingKeys; } - public function setRoutingKeys(array $routingKeys) + public function setRoutingKeys(array $routingKeys): void { $this->routingKeys = $routingKeys; } @@ -66,7 +57,7 @@ public function getFlags(): int return $this->flags; } - public function setFlags(int $flags) + public function setFlags(int $flags): void { $this->flags = $flags; } @@ -76,7 +67,7 @@ public function getAttributes(): array return $this->attributes; } - public function setAttributes(array $attributes) + public function setAttributes(array $attributes): void { $this->attributes = $attributes; } diff --git a/src/AmqpBundle/Event/PreRetrieveEvent.php b/src/AmqpBundle/Event/PreRetrieveEvent.php index f520801..f7719f2 100644 --- a/src/AmqpBundle/Event/PreRetrieveEvent.php +++ b/src/AmqpBundle/Event/PreRetrieveEvent.php @@ -10,13 +10,10 @@ */ class PreRetrieveEvent extends Event { - const NAME = 'amqp.pre_retrieve'; + public const NAME = 'amqp.pre_retrieve'; - private ?AMQPEnvelope $envelope; - - public function __construct(?AMQPEnvelope $envelope) + public function __construct(private ?AMQPEnvelope $envelope) { - $this->envelope = $envelope; } public function getEnvelope(): ?AMQPEnvelope @@ -24,7 +21,7 @@ public function getEnvelope(): ?AMQPEnvelope return $this->envelope; } - public function setEnvelope(?AMQPEnvelope $envelope) + public function setEnvelope(?AMQPEnvelope $envelope): void { $this->envelope = $envelope; } diff --git a/src/AmqpBundle/Event/PurgeEvent.php b/src/AmqpBundle/Event/PurgeEvent.php index 7e14439..5241fab 100644 --- a/src/AmqpBundle/Event/PurgeEvent.php +++ b/src/AmqpBundle/Event/PurgeEvent.php @@ -9,13 +9,10 @@ */ class PurgeEvent extends Event { - const NAME = 'amqp.purge'; + public const NAME = 'amqp.purge'; - private \AMQPQueue $queue; - - public function __construct(\AMQPQueue $queue) + public function __construct(private readonly \AMQPQueue $queue) { - $this->queue = $queue; } public function getQueue(): \AMQPQueue diff --git a/src/AmqpBundle/M6WebAmqpBundle.php b/src/AmqpBundle/M6WebAmqpBundle.php index 136731c..f26756e 100644 --- a/src/AmqpBundle/M6WebAmqpBundle.php +++ b/src/AmqpBundle/M6WebAmqpBundle.php @@ -11,7 +11,7 @@ */ class M6WebAmqpBundle extends Bundle { - public function build(ContainerBuilder $container) + public function build(ContainerBuilder $container): void { parent::build($container); $container->addCompilerPass(new M6webAmqpLocatorPass()); diff --git a/src/AmqpBundle/Resources/config/sandbox_services.yml b/src/AmqpBundle/Resources/config/sandbox_services.yml index 7fd4a4e..ee8b908 100644 --- a/src/AmqpBundle/Resources/config/sandbox_services.yml +++ b/src/AmqpBundle/Resources/config/sandbox_services.yml @@ -3,5 +3,5 @@ parameters: m6_web_amqp.queue.class: 'M6Web\Bundle\AmqpBundle\Sandbox\NullQueue' m6_web_amqp.connection.class: 'M6Web\Bundle\AmqpBundle\Sandbox\NullConnection' m6_web_amqp.channel.class: 'M6Web\Bundle\AmqpBundle\Sandbox\NullChannel' - m6_web_amqp.envelope.class: '\M6Web\Bundle\AmqpBundle\Sandbox\NullEnvelope' + m6_web_amqp.envelope.class: 'M6Web\Bundle\AmqpBundle\Sandbox\NullEnvelope' diff --git a/src/AmqpBundle/Sandbox/NullEnvelope.php b/src/AmqpBundle/Sandbox/NullEnvelope.php index 381fb6b..b062674 100644 --- a/src/AmqpBundle/Sandbox/NullEnvelope.php +++ b/src/AmqpBundle/Sandbox/NullEnvelope.php @@ -241,9 +241,6 @@ public function getHeaders(): array return $this->headers; } - /** - * @param array $headers - */ public function setHeaders(array $headers): void { $this->headers = $headers; diff --git a/src/AmqpBundle/Sandbox/NullExchange.php b/src/AmqpBundle/Sandbox/NullExchange.php index de49aa5..50e7179 100644 --- a/src/AmqpBundle/Sandbox/NullExchange.php +++ b/src/AmqpBundle/Sandbox/NullExchange.php @@ -24,8 +24,8 @@ public function publish( $message, $routingKey = null, $flags = AMQP_NOPARAM, - array $attributes = array() - ) { + array $attributes = [] + ): void { //noop } diff --git a/src/AmqpBundle/Sandbox/NullQueue.php b/src/AmqpBundle/Sandbox/NullQueue.php index 97b4f1f..8c4ea24 100644 --- a/src/AmqpBundle/Sandbox/NullQueue.php +++ b/src/AmqpBundle/Sandbox/NullQueue.php @@ -27,7 +27,7 @@ public function __construct(\AMQPChannel $channel) * * @param \AMQPEnvelope|null $envelope */ - public function enqueue(\AMQPEnvelope $envelope = null) + public function enqueue(\AMQPEnvelope $envelope = null): void { $this->envelopes->enqueue($envelope); } diff --git a/src/AmqpBundle/Tests/Units/Amqp/Consumer.php b/src/AmqpBundle/Tests/Units/Amqp/Consumer.php index 8965e50..eab8d5e 100644 --- a/src/AmqpBundle/Tests/Units/Amqp/Consumer.php +++ b/src/AmqpBundle/Tests/Units/Amqp/Consumer.php @@ -14,7 +14,7 @@ */ class Consumer extends atoum { - public function testConstruct() + public function testConstruct(): void { $this ->if($queue = $this->getQueue()) @@ -24,7 +24,7 @@ public function testConstruct() ->isIdenticalTo($queue); } - public function testGetMessageAutoAck() + public function testGetMessageAutoAck(): void { $msgList = ['wait' => [1 => 'message1', '2' => 'message2']]; $this @@ -69,7 +69,7 @@ public function testGetMessageAutoAck() ->exactly(3); } - public function testGetMessageManualAck() + public function testGetMessageManualAck(): void { $this ->if($msgList = ['wait' => [1 => 'message1']]) @@ -126,7 +126,7 @@ public function testGetMessageManualAck() ->isEmpty(); } - public function testGetMessageManualNack() + public function testGetMessageManualNack(): void { $this ->if($msgList = ['wait' => [1 => 'message1']]) @@ -216,7 +216,7 @@ public function testGetMessageManualNack() ->isEqualTo(null); } - public function testPurge() + public function testPurge(): void { $this ->if($msgList = ['wait' => [1 => 'message1']]) @@ -242,7 +242,7 @@ public function testPurge() ->isNull(); } - public function testGetMessageCurrentCount() + public function testGetMessageCurrentCount(): void { $this ->if($msgList = [ @@ -270,7 +270,7 @@ public function testGetMessageCurrentCount() ->isEqualTo($msgList['flags']); } - public function testConsumerWithNullQueue() + public function testConsumerWithNullQueue(): void { $this ->if($connection = new NullConnection()) @@ -312,9 +312,7 @@ protected function getQueue(&$msgList = []) $queue = new \mock\AMQPQueue(); $queue->getMockController()->get = function ($flags = AMQP_AUTOACK) use (&$msgList) { - // Simulate a simple queue - reset($msgList['wait']); - $key = key($msgList['wait']); + $key = array_key_first($msgList['wait']); $msg = reset($msgList['wait']); unset($msgList['wait'][$key]); @@ -331,13 +329,9 @@ protected function getQueue(&$msgList = []) // Message $message = new \mock\AMQPEnvelope(); - $message->getMockController()->getBody = function () use ($key, $msg) { - return $msg; - }; + $message->getMockController()->getBody = fn() => $msg; - $message->getMockController()->getDeliveryTag = function () use ($key, $msg) { - return $key; - }; + $message->getMockController()->getDeliveryTag = fn() => $key; return $message; }; diff --git a/src/AmqpBundle/Tests/Units/Amqp/Producer.php b/src/AmqpBundle/Tests/Units/Amqp/Producer.php index df6e346..f392c49 100644 --- a/src/AmqpBundle/Tests/Units/Amqp/Producer.php +++ b/src/AmqpBundle/Tests/Units/Amqp/Producer.php @@ -10,7 +10,7 @@ */ class Producer extends atoum { - public function testConstruct() + public function testConstruct(): void { $this ->if($exchange = $this->getExchange()) @@ -25,7 +25,7 @@ public function testConstruct() ->contains('test'); } - public function testSetOptions() + public function testSetOptions(): void { $this ->if($exchange = $this->getExchange()) @@ -35,7 +35,7 @@ public function testSetOptions() ->isEqualTo($exchangeOptions); } - public function testSendMessagesOk() + public function testSendMessagesOk(): void { $msgList = []; @@ -58,7 +58,7 @@ public function testSendMessagesOk() ]); } - public function testSendMessagesError() + public function testSendMessagesError(): void { $msgList = []; @@ -86,7 +86,7 @@ function() use($producer) { ->notContains(['error', 'error', AMQP_NOPARAM, $exchangeOptions['publish_attributes']]); } - public function testSendMessagesWithAttributes() + public function testSendMessagesWithAttributes(): void { $msgList = []; @@ -115,7 +115,7 @@ public function testSendMessagesWithAttributes() ]); } - public function testSendMessageOk() + public function testSendMessageOk(): void { $this ->if($exchange = $this->getExchange()) @@ -127,7 +127,7 @@ public function testSendMessageOk() ->isTrue(); } - public function testSendMessageWithOverridedRoutingKey() + public function testSendMessageWithOverridedRoutingKey(): void { $msgList = []; @@ -153,7 +153,7 @@ public function testSendMessageWithOverridedRoutingKey() ]); } - public function testSendMessagesWithoutRoutingKey() + public function testSendMessagesWithoutRoutingKey(): void { $msgList = []; diff --git a/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php b/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php index 7f33802..39f8f6d 100644 --- a/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php +++ b/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php @@ -3,6 +3,11 @@ namespace M6Web\Bundle\AmqpBundle\Tests\Units\DependencyInjection; use M6Web\Bundle\AmqpBundle\DependencyInjection\M6WebAmqpExtension as Base; +use M6Web\Bundle\AmqpBundle\Sandbox\NullChannel; +use M6Web\Bundle\AmqpBundle\Sandbox\NullConnection; +use M6Web\Bundle\AmqpBundle\Sandbox\NullEnvelope; +use M6Web\Bundle\AmqpBundle\Sandbox\NullExchange; +use M6Web\Bundle\AmqpBundle\Sandbox\NullQueue; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Yaml\Parser; @@ -17,7 +22,7 @@ protected function getContainerForConfiguration(string $fixtureName): ContainerB { $extension = new Base(); - $parameterBag = new ParameterBag(array('kernel.debug' => true)); + $parameterBag = new ParameterBag(['kernel.debug' => true]); $container = new ContainerBuilder($parameterBag); $parser = new Parser(); @@ -28,7 +33,7 @@ protected function getContainerForConfiguration(string $fixtureName): ContainerB return $container; } - public function testQueueArgumentsConfig() + public function testQueueArgumentsConfig(): void { $container = $this->getContainerForConfiguration('queue-arguments-config'); @@ -91,24 +96,24 @@ public function testQueueArgumentsConfig() ->isEqualTo(1); } - public function testSandboxClasses() + public function testSandboxClasses(): void { $container = $this->getContainerForConfiguration('queue-arguments-config'); $this ->string($container->getParameter('m6_web_amqp.exchange.class')) - ->isEqualTo('M6Web\Bundle\AmqpBundle\Sandbox\NullExchange') + ->isEqualTo(NullExchange::class) ->string($container->getParameter('m6_web_amqp.queue.class')) - ->isEqualTo('M6Web\Bundle\AmqpBundle\Sandbox\NullQueue') + ->isEqualTo(NullQueue::class) ->string($container->getParameter('m6_web_amqp.connection.class')) - ->isEqualTo('M6Web\Bundle\AmqpBundle\Sandbox\NullConnection') + ->isEqualTo(NullConnection::class) ->string($container->getParameter('m6_web_amqp.channel.class')) - ->isEqualTo('M6Web\Bundle\AmqpBundle\Sandbox\NullChannel') + ->isEqualTo(NullChannel::class) ->string($container->getParameter('m6_web_amqp.envelope.class')) - ->isEqualTo('\M6Web\Bundle\AmqpBundle\Sandbox\NullEnvelope'); + ->isEqualTo(NullEnvelope::class); } - public function testDefaultConfiguration() + public function testDefaultConfiguration(): void { $container = $this->getContainerForConfiguration('queue-defaults'); diff --git a/src/AmqpBundle/Tests/Units/Factory/ConsumerFactory.php b/src/AmqpBundle/Tests/Units/Factory/ConsumerFactory.php index e979c02..85ecb17 100644 --- a/src/AmqpBundle/Tests/Units/Factory/ConsumerFactory.php +++ b/src/AmqpBundle/Tests/Units/Factory/ConsumerFactory.php @@ -10,14 +10,14 @@ */ class ConsumerFactory extends atoum { - public function testConstruct() + public function testConstruct(): void { $this ->if($channelClass = '\AMQPChannel') ->and($queueClass = '\AMQPQueue') ->and($exchangeClass = '\AMQPExchange') ->object($factory = new Base($channelClass, $queueClass, $exchangeClass)) - ->isInstanceOf('M6Web\Bundle\AmqpBundle\Factory\ConsumerFactory'); + ->isInstanceOf(\M6Web\Bundle\AmqpBundle\Factory\ConsumerFactory::class); $this ->if($channelClass = '\DateTime') @@ -56,7 +56,7 @@ function () use ($channelClass, $queueClass, $exchangeClass) { ->hasMessage("exchangeClass '\DateTime' doesn't exist or not a AMQPExchange"); } - public function testFactory() + public function testFactory(): void { $this->mockGenerator->orphanize('__construct'); $this->mockGenerator->shuntParentClassCalls(); diff --git a/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPQueue.php b/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPQueue.php index a1d481a..30eb5d4 100644 --- a/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPQueue.php +++ b/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPQueue.php @@ -11,7 +11,7 @@ public function __construct(\AMQPChannel $amqp_channel) { } - public function bind(string $exchange_name, string $routing_key = null, array $arguments = array()): void + public function bind(string $exchange_name, string $routing_key = null, array $arguments = []): void { } diff --git a/src/AmqpBundle/Tests/Units/Factory/ProducerFactory.php b/src/AmqpBundle/Tests/Units/Factory/ProducerFactory.php index 78d70e7..fa45dec 100644 --- a/src/AmqpBundle/Tests/Units/Factory/ProducerFactory.php +++ b/src/AmqpBundle/Tests/Units/Factory/ProducerFactory.php @@ -10,14 +10,14 @@ */ class ProducerFactory extends atoum { - public function testConstruct() + public function testConstruct(): void { $this ->if($channelClass = '\AMQPChannel') ->and($exchangeClass = '\AMQPExchange') ->and($queueClass = '\AMQPQueue') ->object($factory = new Base($channelClass, $exchangeClass, $queueClass)) - ->isInstanceOf('M6Web\Bundle\AmqpBundle\Factory\ProducerFactory'); + ->isInstanceOf(\M6Web\Bundle\AmqpBundle\Factory\ProducerFactory::class); $this ->if($channelClass = '\DateTime') @@ -56,7 +56,7 @@ function () use ($channelClass, $exchangeClass, $queueClass) { ->hasMessage("queueClass '\DateTime' doesn't exist or not a AMQPQueue"); } - public function testFactory() + public function testFactory(): void { $this->mockGenerator->orphanize('__construct'); $this->mockGenerator->shuntParentClassCalls(); From 01b82ec9d684a1715f91e37ffea9dac972039efe Mon Sep 17 00:00:00 2001 From: Thibaud Girard Date: Fri, 1 Mar 2024 16:15:19 +0100 Subject: [PATCH 03/10] chore(bin): remove deprecated binaries --- bin/coke | 1 - bin/phpcs | 1 - 2 files changed, 2 deletions(-) delete mode 120000 bin/coke delete mode 120000 bin/phpcs diff --git a/bin/coke b/bin/coke deleted file mode 120000 index e674adc..0000000 --- a/bin/coke +++ /dev/null @@ -1 +0,0 @@ -../vendor/m6web/coke/coke \ No newline at end of file diff --git a/bin/phpcs b/bin/phpcs deleted file mode 120000 index 6fac062..0000000 --- a/bin/phpcs +++ /dev/null @@ -1 +0,0 @@ -../vendor/squizlabs/php_codesniffer/scripts/phpcs \ No newline at end of file From ccb1ce50384fb5c85c2695685d09d43349c380b3 Mon Sep 17 00:00:00 2001 From: Thibaud Girard Date: Fri, 1 Mar 2024 16:15:53 +0100 Subject: [PATCH 04/10] chore(composer): upgrade dependencies for php 8 --- composer.json | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 0ed6d16..8dd3d41 100644 --- a/composer.json +++ b/composer.json @@ -7,16 +7,14 @@ "require" : { "php": ">=7.4", "ext-amqp": ">=2.0", - "symfony/dependency-injection": "^4.4 || ^5.0", - "symfony/framework-bundle": "^4.4 || ^5.0", - "symfony/http-kernel": "^4.4 || ^5.0", - "symfony/yaml": "^4.4 || ^5.0", + "symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0", + "symfony/framework-bundle": "^5.4 || ^6.4 || ^7.0", + "symfony/http-kernel": "^5.4 || ^6.4 || ^7.0", + "symfony/yaml": "^5.4 || ^6.4 || ^7.0", "twig/twig": "^2.13 || ^3.0" }, "require-dev" : { - "atoum/atoum": "~4.0", - "m6web/coke": "^2.2", - "m6web/symfony2-coding-standard": "^3.3" + "atoum/atoum": "~4.0" }, "suggest": { "ocramius/proxy-manager": "Required for lazy connections" From 91105e363aad31534aa946e6388ad19d6030a109 Mon Sep 17 00:00:00 2001 From: Thibaud Girard Date: Fri, 1 Mar 2024 16:17:24 +0100 Subject: [PATCH 05/10] chore(composer): add cs-fixer with Makefile --- .gitignore | 1 + .php-cs-fixer.dist.php | 49 +++++++++++++++++ Makefile | 54 +++++++++++++++++++ bin/atoum | 120 ++++++++++++++++++++++++++++++++++++++++- composer.json | 6 ++- 5 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 .php-cs-fixer.dist.php create mode 100644 Makefile mode change 120000 => 100755 bin/atoum diff --git a/.gitignore b/.gitignore index 4fbb073..ddc5aca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ /composer.lock +bin/ diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..da2c825 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,49 @@ +in([ + __DIR__.'/src', + __DIR__.'/tests' + ]); + +$config = new class() extends PhpCsFixer\Config { + public function __construct() + { + parent::__construct('Bedrock Streaming'); + + $this->setRiskyAllowed(true); + } + + public function getRules(): array + { + return array_merge((new M6Web\CS\Config\BedrockStreaming())->getRules(), [ + 'no_unreachable_default_argument_value' => true, + 'trailing_comma_in_multiline' => [ + 'after_heredoc' => true, + 'elements' => ['arrays', 'arguments', 'parameters'], + ], + 'native_function_invocation' => [ + 'include' => ['@compiler_optimized'] + ], + 'simplified_null_return' => false, + 'void_return' => true, + 'phpdoc_order' => true, + 'phpdoc_types_order' => false, + 'no_superfluous_phpdoc_tags' => true, + 'php_unit_test_case_static_method_calls' => [ + 'call_type' => 'static', + ], + 'yoda_style' => [ + 'equal' => false, + 'identical' => false, + 'less_and_greater' => false + ], + ]); + } +}; + +$config + ->setFinder($finder) + ->setCacheFile('var/cache/tools/.php-cs-fixer.cache'); + +return $config; diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d0c4cc1 --- /dev/null +++ b/Makefile @@ -0,0 +1,54 @@ +SOURCE_DIR = $(shell pwd) +BIN_DIR ?= ${SOURCE_DIR}/bin + +define printSection + @printf "\033[36m\n==================================================\033[0m\n" + @printf "\033[36m $1 \033[0m" + @printf "\033[36m\n==================================================\033[0m\n" +endef + +.PHONY: all +all: install ci + +.PHONY: ci +ci: quality test + +.PHONY: install +install: clean-vendor composer-install + +.PHONY: quality +quality: cs-ci phpstan + +.PHONY: test +test: atoum + +.PHONY: clean-vendor +clean-vendor: + $(call printSection,CLEAN VENDOR) + rm -rf ${SOURCE_DIR}/vendor + +.PHONY: phpstan +phpstan: composer-install + $(call printSection,PHPSTAN) + ${BIN_DIR}/phpstan analyse -c phpstan.neon --memory-limit=1G + +composer-install: + $(call printSection,COMPOSER INSTALL) + composer --no-interaction install --ansi --no-progress --prefer-dist + +atoum: + $(call printSection,TESTING) + ${BIN_DIR}/atoum --no-code-coverage --verbose + +.PHONY: cs +cs: composer-install + ${BIN_DIR}/php-cs-fixer fix --dry-run --stop-on-violation --diff + +.PHONY: cs-fix +cs-fix: composer-install + ${BIN_DIR}/php-cs-fixer fix + +.PHONY: cs-ci +cs-ci: composer-install + $(call printSection,PHPCS) + ${BIN_DIR}/php-cs-fixer fix --dry-run --using-cache=no --verbose diff --git a/bin/atoum b/bin/atoum deleted file mode 120000 index ac5b7ef..0000000 --- a/bin/atoum +++ /dev/null @@ -1 +0,0 @@ -../vendor/atoum/atoum/bin/atoum \ No newline at end of file diff --git a/bin/atoum b/bin/atoum new file mode 100755 index 0000000..c2fa05e --- /dev/null +++ b/bin/atoum @@ -0,0 +1,119 @@ +#!/usr/bin/env php +realpath = realpath($opened_path) ?: $opened_path; + $opened_path = $this->realpath; + $this->handle = fopen($this->realpath, $mode); + $this->position = 0; + + return (bool) $this->handle; + } + + public function stream_read($count) + { + $data = fread($this->handle, $count); + + if ($this->position === 0) { + $data = preg_replace('{^#!.*\r?\n}', '', $data); + } + + $this->position += strlen($data); + + return $data; + } + + public function stream_cast($castAs) + { + return $this->handle; + } + + public function stream_close() + { + fclose($this->handle); + } + + public function stream_lock($operation) + { + return $operation ? flock($this->handle, $operation) : true; + } + + public function stream_seek($offset, $whence) + { + if (0 === fseek($this->handle, $offset, $whence)) { + $this->position = ftell($this->handle); + return true; + } + + return false; + } + + public function stream_tell() + { + return $this->position; + } + + public function stream_eof() + { + return feof($this->handle); + } + + public function stream_stat() + { + return array(); + } + + public function stream_set_option($option, $arg1, $arg2) + { + return true; + } + + public function url_stat($path, $flags) + { + $path = substr($path, 17); + if (file_exists($path)) { + return stat($path); + } + + return false; + } + } + } + + if ( + (function_exists('stream_get_wrappers') && in_array('phpvfscomposer', stream_get_wrappers(), true)) + || (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) + ) { + return include("phpvfscomposer://" . __DIR__ . '/..'.'/vendor/atoum/atoum/bin/atoum'); + } +} + +return include __DIR__ . '/..'.'/vendor/atoum/atoum/bin/atoum'; diff --git a/composer.json b/composer.json index 8dd3d41..ba2c4b2 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,9 @@ "autoload" : { "psr-4": { "M6Web\\Bundle\\": "src/" } }, + "config": { + "bin-dir": "bin" + }, "require" : { "php": ">=7.4", "ext-amqp": ">=2.0", @@ -14,7 +17,8 @@ "twig/twig": "^2.13 || ^3.0" }, "require-dev" : { - "atoum/atoum": "~4.0" + "atoum/atoum": "~4.0", + "m6web/php-cs-fixer-config": "^3.2" }, "suggest": { "ocramius/proxy-manager": "Required for lazy connections" From 6e3e6b700adafcfab23563b477d4bac561e79bdf Mon Sep 17 00:00:00 2001 From: Thibaud Girard Date: Fri, 1 Mar 2024 16:30:22 +0100 Subject: [PATCH 06/10] chore(cs-fix): run cs-fixer --- .gitignore | 1 + .php-cs-fixer.dist.php | 1 - rector.php | 16 +++++++++++ src/AmqpBundle/Amqp/AbstractAmqp.php | 13 ++++----- src/AmqpBundle/Amqp/Consumer.php | 9 ++++--- src/AmqpBundle/Amqp/DataCollector.php | 14 +++++----- src/AmqpBundle/Amqp/Exception.php | 2 ++ src/AmqpBundle/Amqp/Locator.php | 12 ++++++--- src/AmqpBundle/Amqp/Producer.php | 12 +++++---- .../CompilerPass/M6webAmqpLocatorPass.php | 2 ++ .../DependencyInjection/Configuration.php | 12 +++++---- .../M6WebAmqpExtension.php | 26 +++++++++--------- src/AmqpBundle/Event/AckEvent.php | 2 ++ src/AmqpBundle/Event/Command.php | 11 +++----- src/AmqpBundle/Event/DispatcherInterface.php | 2 ++ src/AmqpBundle/Event/NackEvent.php | 2 ++ src/AmqpBundle/Event/PrePublishEvent.php | 2 ++ src/AmqpBundle/Event/PreRetrieveEvent.php | 9 ++++--- src/AmqpBundle/Event/PurgeEvent.php | 2 ++ src/AmqpBundle/Factory/AMQPFactory.php | 4 ++- src/AmqpBundle/Factory/ConsumerFactory.php | 18 ++++++------- src/AmqpBundle/Factory/ProducerFactory.php | 16 +++++------ src/AmqpBundle/M6WebAmqpBundle.php | 2 ++ src/AmqpBundle/Sandbox/NullChannel.php | 8 +++--- src/AmqpBundle/Sandbox/NullConnection.php | 2 ++ src/AmqpBundle/Sandbox/NullEnvelope.php | 2 ++ src/AmqpBundle/Sandbox/NullExchange.php | 12 ++++----- src/AmqpBundle/Sandbox/NullQueue.php | 6 ++--- src/AmqpBundle/Tests/Units/Amqp/Consumer.php | 13 ++++----- src/AmqpBundle/Tests/Units/Amqp/Producer.php | 11 ++++---- .../M6WebAmqpExtension.php | 11 ++++---- .../Tests/Units/Factory/ConsumerFactory.php | 27 ++++++++++--------- .../Units/Factory/Mock/MockAMQPChannel.php | 4 ++- .../Units/Factory/Mock/MockAMQPExchange.php | 2 ++ .../Units/Factory/Mock/MockAMQPQueue.php | 4 ++- .../Tests/Units/Factory/ProducerFactory.php | 27 ++++++++++--------- 36 files changed, 190 insertions(+), 129 deletions(-) create mode 100644 rector.php diff --git a/.gitignore b/.gitignore index ddc5aca..0ed69a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor/ /composer.lock bin/ +var diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index da2c825..2a5c19a 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -3,7 +3,6 @@ $finder = PhpCsFixer\Finder::create() ->in([ __DIR__.'/src', - __DIR__.'/tests' ]); $config = new class() extends PhpCsFixer\Config { diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..d8ad267 --- /dev/null +++ b/rector.php @@ -0,0 +1,16 @@ +withPaths([ + __DIR__ . '/src', + ]) + // uncomment to reach your current PHP version + // ->withPhpSets() + ->withRules([ + AddVoidReturnTypeWhereNoReturnRector::class, + ]); diff --git a/src/AmqpBundle/Amqp/AbstractAmqp.php b/src/AmqpBundle/Amqp/AbstractAmqp.php index da14847..00fded1 100644 --- a/src/AmqpBundle/Amqp/AbstractAmqp.php +++ b/src/AmqpBundle/Amqp/AbstractAmqp.php @@ -1,5 +1,7 @@ eventDispatcher) { $event = new $this->eventClass(); @@ -44,14 +47,12 @@ protected function notifyEvent(string $command, array $arguments, mixed $return, * @param object $object Method object * @param string $name Method name * @param array $arguments Method arguments - * - * @return mixed */ protected function call(object $object, string $name, array $arguments = []) { $start = microtime(true); - $ret = call_user_func_array([$object, $name], $arguments); + $ret = \call_user_func_array([$object, $name], $arguments); $this->notifyEvent($name, $arguments, $ret, microtime(true) - $start); diff --git a/src/AmqpBundle/Amqp/Consumer.php b/src/AmqpBundle/Amqp/Consumer.php index a84a8a3..ae6b860 100644 --- a/src/AmqpBundle/Amqp/Consumer.php +++ b/src/AmqpBundle/Amqp/Consumer.php @@ -1,5 +1,7 @@ eventDispatcher) { $ackEvent = new AckEvent($deliveryTag, $flags); - $this->eventDispatcher->dispatch($ackEvent,AckEvent::NAME); + $this->eventDispatcher->dispatch($ackEvent, AckEvent::NAME); } $this->call($this->queue, 'ack', [$deliveryTag, $flags]); @@ -63,7 +64,7 @@ public function ackMessage(int $deliveryTag, int $flags = AMQP_NOPARAM): void * Mark a message as explicitly not acknowledged. * * @param int $deliveryTag delivery tag of last message to nack - * @param int $flags AMQP_NOPARAM or AMQP_REQUEUE to requeue the message(s) + * @param int $flags AMQP_NOPARAM or AMQP_REQUEUE to requeue the message(s) * * @throws \AMQPConnectionException if the connection to the broker was lost * @throws \AMQPChannelException if the channel is not open diff --git a/src/AmqpBundle/Amqp/DataCollector.php b/src/AmqpBundle/Amqp/DataCollector.php index 449a770..ec48e29 100644 --- a/src/AmqpBundle/Amqp/DataCollector.php +++ b/src/AmqpBundle/Amqp/DataCollector.php @@ -1,11 +1,13 @@ consumers[$id]; } - /** @param Consumer[] $consumers */ + /** + * @param Consumer[] $consumers + */ public function setConsumers(array $consumers): void { $this->consumers = $consumers; @@ -26,7 +30,9 @@ public function getProducer(string $id): Producer return $this->producers[$id]; } - /** @param Producer[] $producers */ + /** + * @param Producer[] $producers + */ public function setProducers(array $producers): void { $this->producers = $producers; diff --git a/src/AmqpBundle/Amqp/Producer.php b/src/AmqpBundle/Amqp/Producer.php index 0760691..fa056c5 100644 --- a/src/AmqpBundle/Amqp/Producer.php +++ b/src/AmqpBundle/Amqp/Producer.php @@ -1,5 +1,7 @@ eventDispatcher) { $prePublishEvent = new PrePublishEvent($message, $routingKeys, $flags, $attributes); - $this->eventDispatcher->dispatch($prePublishEvent,PrePublishEvent::NAME); + $this->eventDispatcher->dispatch($prePublishEvent, PrePublishEvent::NAME); if (!$prePublishEvent->canPublish()) { return true; @@ -97,11 +99,11 @@ public function setExchangeOptions(array $exchangeOptions): self { $this->exchangeOptions = $exchangeOptions; - if (!array_key_exists('publish_attributes', $this->exchangeOptions)) { + if (!\array_key_exists('publish_attributes', $this->exchangeOptions)) { $this->exchangeOptions['publish_attributes'] = []; } - if (!array_key_exists('routing_keys', $this->exchangeOptions)) { + if (!\array_key_exists('routing_keys', $this->exchangeOptions)) { $this->exchangeOptions['routing_keys'] = []; } diff --git a/src/AmqpBundle/DependencyInjection/CompilerPass/M6webAmqpLocatorPass.php b/src/AmqpBundle/DependencyInjection/CompilerPass/M6webAmqpLocatorPass.php index 2bb6154..9237f6b 100644 --- a/src/AmqpBundle/DependencyInjection/CompilerPass/M6webAmqpLocatorPass.php +++ b/src/AmqpBundle/DependencyInjection/CompilerPass/M6webAmqpLocatorPass.php @@ -1,5 +1,7 @@ children() @@ -73,7 +75,7 @@ protected function addConnections(ArrayNodeDefinition $node) ->end(); } - protected function addProducers(ArrayNodeDefinition $node) + protected function addProducers(ArrayNodeDefinition $node): void { $node ->children() @@ -116,7 +118,7 @@ protected function addProducers(ArrayNodeDefinition $node) ->end(); } - protected function addConsumers(ArrayNodeDefinition $node) + protected function addConsumers(ArrayNodeDefinition $node): void { $node ->children() @@ -209,6 +211,6 @@ private function exchangeOptions() ->defaultValue([]) ->end() ->end(); - //last end is missed here intentionally because arrayNode doesn't have an actual parent + // last end is missed here intentionally because arrayNode doesn't have an actual parent } } diff --git a/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php b/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php index 245ee29..e9933fc 100644 --- a/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php +++ b/src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php @@ -1,13 +1,15 @@ loadConsumers($container, $config); } - protected function loadConnections(ContainerBuilder $container, array $config) + protected function loadConnections(ContainerBuilder $container, array $config): void { foreach ($config['connections'] as $key => $connection) { $connectionDefinition = new Definition($connection['class']); @@ -63,12 +65,12 @@ protected function loadConnections(ContainerBuilder $container, array $config) $container->setDefinition( sprintf('m6_web_amqp.connection.%s', $key), - $connectionDefinition + $connectionDefinition, ); } } - protected function loadProducers(ContainerBuilder $container, array $config) + protected function loadProducers(ContainerBuilder $container, array $config): void { foreach ($config['producers'] as $key => $producer) { $lazy = $config['connections'][$producer['connection']]['lazy']; @@ -82,7 +84,7 @@ protected function loadProducers(ContainerBuilder $container, array $config) $producer['exchange_options'], $producer['queue_options'], $lazy, - ] + ], ); $this->setEventDispatcher($container, $config['event_dispatcher'], $producerDefinition); @@ -108,12 +110,12 @@ protected function loadProducers(ContainerBuilder $container, array $config) $producerDefinition->addTag('m6_web_amqp.producers'); $container->setDefinition( sprintf('m6_web_amqp.producer.%s', $key), - $producerDefinition + $producerDefinition, ); } } - protected function loadConsumers(ContainerBuilder $container, array $config) + protected function loadConsumers(ContainerBuilder $container, array $config): void { foreach ($config['consumers'] as $key => $consumer) { $lazy = $config['connections'][$consumer['connection']]['lazy']; @@ -128,7 +130,7 @@ protected function loadConsumers(ContainerBuilder $container, array $config) $consumer['queue_options'], $lazy, $consumer['qos_options'], - ] + ], ); $this->setEventDispatcher($container, $config['event_dispatcher'], $consumerDefinition); @@ -154,7 +156,7 @@ protected function loadConsumers(ContainerBuilder $container, array $config) $consumerDefinition->addTag('m6_web_amqp.consumers'); $container->setDefinition( sprintf('m6_web_amqp.consumer.%s', $key), - $consumerDefinition + $consumerDefinition, ); } } @@ -168,7 +170,7 @@ private function setEventDispatcher(ContainerBuilder $container, bool $enableEve [ new Reference('event_dispatcher'), $container->getParameter('m6_web_amqp.event.command.class'), - ] + ], ); } } diff --git a/src/AmqpBundle/Event/AckEvent.php b/src/AmqpBundle/Event/AckEvent.php index 81b2ea5..77744ba 100644 --- a/src/AmqpBundle/Event/AckEvent.php +++ b/src/AmqpBundle/Event/AckEvent.php @@ -1,5 +1,7 @@ envelope; } - public function setEnvelope(?AMQPEnvelope $envelope): void + public function setEnvelope(?\AMQPEnvelope $envelope): void { $this->envelope = $envelope; } diff --git a/src/AmqpBundle/Event/PurgeEvent.php b/src/AmqpBundle/Event/PurgeEvent.php index 5241fab..fe448b7 100644 --- a/src/AmqpBundle/Event/PurgeEvent.php +++ b/src/AmqpBundle/Event/PurgeEvent.php @@ -1,5 +1,7 @@ setFlags( ($exchangeOptions['passive'] ? AMQP_PASSIVE : AMQP_NOPARAM) | ($exchangeOptions['durable'] ? AMQP_DURABLE : AMQP_NOPARAM) | - ($exchangeOptions['auto_delete'] ? AMQP_AUTODELETE : AMQP_NOPARAM) + ($exchangeOptions['auto_delete'] ? AMQP_AUTODELETE : AMQP_NOPARAM), ); $exchange->declareExchange(); } diff --git a/src/AmqpBundle/Factory/ConsumerFactory.php b/src/AmqpBundle/Factory/ConsumerFactory.php index 6af104a..b08261f 100644 --- a/src/AmqpBundle/Factory/ConsumerFactory.php +++ b/src/AmqpBundle/Factory/ConsumerFactory.php @@ -1,5 +1,7 @@ setPrefetchCount($qosOptions['prefetch_count']); } - //ensure that exchange exists + // ensure that exchange exists $this->createExchange($this->exchangeClass, $channel, $exchangeOptions); /** @var \AMQPQueue $queue */ @@ -91,13 +91,13 @@ public function get(string $class, \AMQPConnection $connexion, array $exchangeOp ($queueOptions['passive'] ? AMQP_PASSIVE : AMQP_NOPARAM) | ($queueOptions['durable'] ? AMQP_DURABLE : AMQP_NOPARAM) | ($queueOptions['exclusive'] ? AMQP_EXCLUSIVE : AMQP_NOPARAM) | - ($queueOptions['auto_delete'] ? AMQP_AUTODELETE : AMQP_NOPARAM) + ($queueOptions['auto_delete'] ? AMQP_AUTODELETE : AMQP_NOPARAM), ); // Declare the queue $queue->declareQueue(); - if (count($queueOptions['routing_keys'])) { + if (\count($queueOptions['routing_keys'])) { foreach ($queueOptions['routing_keys'] as $routingKey) { $queue->bind($exchangeOptions['name'], $routingKey); } diff --git a/src/AmqpBundle/Factory/ProducerFactory.php b/src/AmqpBundle/Factory/ProducerFactory.php index 0502cc2..0c61ec6 100644 --- a/src/AmqpBundle/Factory/ProducerFactory.php +++ b/src/AmqpBundle/Factory/ProducerFactory.php @@ -1,5 +1,7 @@ setFlags( ($queueOptions['passive'] ? AMQP_PASSIVE : AMQP_NOPARAM) | ($queueOptions['durable'] ? AMQP_DURABLE : AMQP_NOPARAM) | - ($queueOptions['auto_delete'] ? AMQP_AUTODELETE : AMQP_NOPARAM) + ($queueOptions['auto_delete'] ? AMQP_AUTODELETE : AMQP_NOPARAM), ); $queue->declareQueue(); $queue->bind($exchangeOptions['name']); diff --git a/src/AmqpBundle/M6WebAmqpBundle.php b/src/AmqpBundle/M6WebAmqpBundle.php index f26756e..aba2857 100644 --- a/src/AmqpBundle/M6WebAmqpBundle.php +++ b/src/AmqpBundle/M6WebAmqpBundle.php @@ -1,5 +1,7 @@ envelopes->enqueue($envelope); } diff --git a/src/AmqpBundle/Tests/Units/Amqp/Consumer.php b/src/AmqpBundle/Tests/Units/Amqp/Consumer.php index eab8d5e..ab2b90e 100644 --- a/src/AmqpBundle/Tests/Units/Amqp/Consumer.php +++ b/src/AmqpBundle/Tests/Units/Amqp/Consumer.php @@ -1,8 +1,9 @@ and($consumer = new Base($queue, [])) // Declare queue in passive mode ->integer($consumer->getCurrentMessageCount()) - ->isEqualTo(count($msgList['wait'])) + ->isEqualTo(\count($msgList['wait'])) ->mock($queue) ->call('getFlags') ->once() @@ -329,9 +330,9 @@ protected function getQueue(&$msgList = []) // Message $message = new \mock\AMQPEnvelope(); - $message->getMockController()->getBody = fn() => $msg; + $message->getMockController()->getBody = fn () => $msg; - $message->getMockController()->getDeliveryTag = fn() => $key; + $message->getMockController()->getDeliveryTag = fn () => $key; return $message; }; @@ -377,7 +378,7 @@ protected function getQueue(&$msgList = []) }; $queue->getMockController()->declareQueue = function () use (&$msgList) { - return count($msgList['wait']); + return \count($msgList['wait']); }; return $queue; diff --git a/src/AmqpBundle/Tests/Units/Amqp/Producer.php b/src/AmqpBundle/Tests/Units/Amqp/Producer.php index f392c49..98d3367 100644 --- a/src/AmqpBundle/Tests/Units/Amqp/Producer.php +++ b/src/AmqpBundle/Tests/Units/Amqp/Producer.php @@ -1,14 +1,15 @@ boolean($producer->publishMessage('message1')) ->isTrue() ->exception( - function() use($producer) { - $producer->publishMessage('error'); - } + function () use ($producer): void { + $producer->publishMessage('error'); + }, )->isInstanceOf(\AMQPExchangeException::class) ->array($msgList) ->isEqualTo([ diff --git a/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php b/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php index 39f8f6d..c506a08 100644 --- a/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php +++ b/src/AmqpBundle/Tests/Units/DependencyInjection/M6WebAmqpExtension.php @@ -1,5 +1,7 @@ contains('super_routing_key') ; - //test connection options + // test connection options $this ->boolean($container->hasDefinition('m6_web_amqp.connection.with_heartbeat')) ->isTrue() @@ -117,7 +118,7 @@ public function testDefaultConfiguration(): void { $container = $this->getContainerForConfiguration('queue-defaults'); - //sandbox is off by default, check indirectly via classes definition + // sandbox is off by default, check indirectly via classes definition $this ->string($container->getParameter('m6_web_amqp.exchange.class')) ->isEqualTo('AMQPExchange') diff --git a/src/AmqpBundle/Tests/Units/Factory/ConsumerFactory.php b/src/AmqpBundle/Tests/Units/Factory/ConsumerFactory.php index 85ecb17..30c0928 100644 --- a/src/AmqpBundle/Tests/Units/Factory/ConsumerFactory.php +++ b/src/AmqpBundle/Tests/Units/Factory/ConsumerFactory.php @@ -1,14 +1,15 @@ and($queueClass = '\AMQPQueue') ->and($exchangeClass = '\AMQPExchange') ->object($factory = new Base($channelClass, $queueClass, $exchangeClass)) - ->isInstanceOf(\M6Web\Bundle\AmqpBundle\Factory\ConsumerFactory::class); + ->isInstanceOf(Base::class); $this ->if($channelClass = '\DateTime') ->and($queueClass = '\AMQPQueue') ->and($exchangeClass = '\AMQPExchange') ->exception( - function () use ($channelClass, $queueClass, $exchangeClass) { - $factory = new Base($channelClass, $queueClass, $exchangeClass); - } - ) + function () use ($channelClass, $queueClass, $exchangeClass): void { + $factory = new Base($channelClass, $queueClass, $exchangeClass); + }, + ) ->isInstanceOf('InvalidArgumentException') ->hasMessage("channelClass '\DateTime' doesn't exist or not a AMQPChannel"); @@ -36,10 +37,10 @@ function () use ($channelClass, $queueClass, $exchangeClass) { ->and($queueClass = '\DateTime') ->and($exchangeClass = '\AMQPExchange') ->exception( - function () use ($channelClass, $queueClass, $exchangeClass) { - $factory = new Base($channelClass, $queueClass, $exchangeClass); - } - ) + function () use ($channelClass, $queueClass, $exchangeClass): void { + $factory = new Base($channelClass, $queueClass, $exchangeClass); + }, + ) ->isInstanceOf('InvalidArgumentException') ->hasMessage("exchangeClass '\DateTime' doesn't exist or not a AMQPQueue"); @@ -48,9 +49,9 @@ function () use ($channelClass, $queueClass, $exchangeClass) { ->and($queueClass = '\AMQPQueue') ->and($exchangeClass = '\DateTime') ->exception( - function () use ($channelClass, $queueClass, $exchangeClass) { + function () use ($channelClass, $queueClass, $exchangeClass): void { $factory = new Base($channelClass, $queueClass, $exchangeClass); - } + }, ) ->isInstanceOf('InvalidArgumentException') ->hasMessage("exchangeClass '\DateTime' doesn't exist or not a AMQPExchange"); diff --git a/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPChannel.php b/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPChannel.php index 759cd4a..8862b96 100644 --- a/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPChannel.php +++ b/src/AmqpBundle/Tests/Units/Factory/Mock/MockAMQPChannel.php @@ -1,5 +1,7 @@ and($exchangeClass = '\AMQPExchange') ->and($queueClass = '\AMQPQueue') ->object($factory = new Base($channelClass, $exchangeClass, $queueClass)) - ->isInstanceOf(\M6Web\Bundle\AmqpBundle\Factory\ProducerFactory::class); + ->isInstanceOf(Base::class); $this ->if($channelClass = '\DateTime') ->and($exchangeClass = '\AMQPExchange') ->and($queueClass = '\AMQPQueue') ->exception( - function () use ($channelClass, $exchangeClass, $queueClass) { - $factory = new Base($channelClass, $exchangeClass, $queueClass); - } - ) + function () use ($channelClass, $exchangeClass, $queueClass): void { + $factory = new Base($channelClass, $exchangeClass, $queueClass); + }, + ) ->isInstanceOf('InvalidArgumentException') ->hasMessage("channelClass '\DateTime' doesn't exist or not a AMQPChannel"); @@ -36,10 +37,10 @@ function () use ($channelClass, $exchangeClass, $queueClass) { ->and($exchangeClass = '\DateTime') ->and($queueClass = '\AMQPQueue') ->exception( - function () use ($channelClass, $exchangeClass, $queueClass) { - $factory = new Base($channelClass, $exchangeClass, $queueClass); - } - ) + function () use ($channelClass, $exchangeClass, $queueClass): void { + $factory = new Base($channelClass, $exchangeClass, $queueClass); + }, + ) ->isInstanceOf('InvalidArgumentException') ->hasMessage("exchangeClass '\DateTime' doesn't exist or not a AMQPExchange"); @@ -48,9 +49,9 @@ function () use ($channelClass, $exchangeClass, $queueClass) { ->and($exchangeClass = '\AMQPExchange') ->and($queueClass = '\DateTime') ->exception( - function () use ($channelClass, $exchangeClass, $queueClass) { + function () use ($channelClass, $exchangeClass, $queueClass): void { $factory = new Base($channelClass, $exchangeClass, $queueClass); - } + }, ) ->isInstanceOf('InvalidArgumentException') ->hasMessage("queueClass '\DateTime' doesn't exist or not a AMQPQueue"); From 2c6822d3c71f75e4671675ebc95bf960f0b0970f Mon Sep 17 00:00:00 2001 From: Thibaud Girard Date: Fri, 1 Mar 2024 17:16:31 +0100 Subject: [PATCH 07/10] chore(composer): install phpstan --- Makefile | 9 +++++++++ composer.json | 3 ++- phpstan.neon.dist | 10 ++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 phpstan.neon.dist diff --git a/Makefile b/Makefile index d0c4cc1..6d75947 100644 --- a/Makefile +++ b/Makefile @@ -52,3 +52,12 @@ cs-fix: composer-install cs-ci: composer-install $(call printSection,PHPCS) ${BIN_DIR}/php-cs-fixer fix --dry-run --using-cache=no --verbose + +.PHONY: phpstan-cache-clear +phpstan-cache-clear: + ${BIN_DIR}/phpstan.phar clear-result-cache + +.PHONY: phpstan +phpstan: phpstan-cache-clear + $(call printSection,PHPSTAN) + ${BIN_DIR}/phpstan.phar analyse --memory-limit=1G diff --git a/composer.json b/composer.json index ba2c4b2..9356fd5 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ }, "require-dev" : { "atoum/atoum": "~4.0", - "m6web/php-cs-fixer-config": "^3.2" + "m6web/php-cs-fixer-config": "^3.2", + "phpstan/phpstan": "^1.10" }, "suggest": { "ocramius/proxy-manager": "Required for lazy connections" diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..15becb7 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,10 @@ +parameters: + paths: + - 'src' + excludePaths: + - 'src/AmqpBundle/Tests' + + level: 8 + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false + treatPhpDocTypesAsCertain: false From ee4edafd7e0d4649a6706402d5be678aacba79cf Mon Sep 17 00:00:00 2001 From: Thibaud Girard Date: Mon, 4 Mar 2024 17:58:04 +0100 Subject: [PATCH 08/10] refactor(makefile): add makefile with phpstan and fix issues --- Makefile | 17 ++++++----------- composer.json | 2 +- phpstan.neon.dist | 5 +++++ src/AmqpBundle/Amqp/AbstractAmqp.php | 8 +++++--- .../DependencyInjection/Configuration.php | 7 ++++--- src/AmqpBundle/Event/Command.php | 6 +++--- src/AmqpBundle/Event/DispatcherInterface.php | 17 +++++++++++++---- src/AmqpBundle/Factory/ConsumerFactory.php | 8 +++++++- src/AmqpBundle/Factory/ProducerFactory.php | 2 ++ src/AmqpBundle/Sandbox/NullChannel.php | 4 ---- src/AmqpBundle/Sandbox/NullExchange.php | 11 +---------- src/AmqpBundle/Sandbox/NullQueue.php | 2 +- src/AmqpBundle/Tests/Units/Amqp/Consumer.php | 10 +++++++--- 13 files changed, 55 insertions(+), 44 deletions(-) diff --git a/Makefile b/Makefile index 6d75947..f57ba84 100644 --- a/Makefile +++ b/Makefile @@ -28,9 +28,13 @@ clean-vendor: rm -rf ${SOURCE_DIR}/vendor .PHONY: phpstan -phpstan: composer-install +phpstan: phpstan-cache-clear $(call printSection,PHPSTAN) - ${BIN_DIR}/phpstan analyse -c phpstan.neon --memory-limit=1G + ${BIN_DIR}/phpstan.phar analyse --memory-limit=1G + +.PHONY: phpstan-cache-clear +phpstan-cache-clear: + ${BIN_DIR}/phpstan.phar clear-result-cache composer-install: $(call printSection,COMPOSER INSTALL) @@ -52,12 +56,3 @@ cs-fix: composer-install cs-ci: composer-install $(call printSection,PHPCS) ${BIN_DIR}/php-cs-fixer fix --dry-run --using-cache=no --verbose - -.PHONY: phpstan-cache-clear -phpstan-cache-clear: - ${BIN_DIR}/phpstan.phar clear-result-cache - -.PHONY: phpstan -phpstan: phpstan-cache-clear - $(call printSection,PHPSTAN) - ${BIN_DIR}/phpstan.phar analyse --memory-limit=1G diff --git a/composer.json b/composer.json index 9356fd5..918e2ce 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0", "symfony/framework-bundle": "^5.4 || ^6.4 || ^7.0", "symfony/http-kernel": "^5.4 || ^6.4 || ^7.0", - "symfony/yaml": "^5.4 || ^6.4 || ^7.0", + "symfony/yaml": "^5.4 || 6.4 || ^7.0", "twig/twig": "^2.13 || ^3.0" }, "require-dev" : { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 15becb7..85ce434 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -3,8 +3,13 @@ parameters: - 'src' excludePaths: - 'src/AmqpBundle/Tests' + - 'src/AmqpBundle/Sandbox' + - 'src/AmqpBundle/DependencyInjection' level: 8 checkMissingIterableValueType: false checkGenericClassInNonGenericObjectType: false treatPhpDocTypesAsCertain: false + + ignoreErrors: + - '#Unable to resolve the template type T in call to method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\)#' diff --git a/src/AmqpBundle/Amqp/AbstractAmqp.php b/src/AmqpBundle/Amqp/AbstractAmqp.php index 00fded1..19c3d61 100644 --- a/src/AmqpBundle/Amqp/AbstractAmqp.php +++ b/src/AmqpBundle/Amqp/AbstractAmqp.php @@ -48,11 +48,13 @@ protected function notifyEvent(string $command, array $arguments, mixed $return, * @param string $name Method name * @param array $arguments Method arguments */ - protected function call(object $object, string $name, array $arguments = []) + protected function call(object $object, string $name, array $arguments = []): mixed { $start = microtime(true); - $ret = \call_user_func_array([$object, $name], $arguments); + /** @var callable $callable */ + $callable = [$object, $name]; + $ret = \call_user_func_array($callable, $arguments); $this->notifyEvent($name, $arguments, $ret, microtime(true) - $start); @@ -63,7 +65,7 @@ protected function call(object $object, string $name, array $arguments = []) * Set an event dispatcher to notify amqp command. * * @param EventDispatcherInterface $eventDispatcher The eventDispatcher object, which implement the notify method - * @param string $eventClass The event class used to create an event and send it to the event dispatcher + * @param class-string $eventClass The event class used to create an event and send it to the event dispatcher * * @throws \Exception */ diff --git a/src/AmqpBundle/DependencyInjection/Configuration.php b/src/AmqpBundle/DependencyInjection/Configuration.php index 43f9e30..733577e 100644 --- a/src/AmqpBundle/DependencyInjection/Configuration.php +++ b/src/AmqpBundle/DependencyInjection/Configuration.php @@ -23,6 +23,7 @@ class Configuration implements ConfigurationInterface public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('m6_web_amqp'); + /** @var ArrayNodeDefinition $rootNode */ $rootNode = $treeBuilder->getRootNode(); $rootNode @@ -55,7 +56,7 @@ protected function addConnections(ArrayNodeDefinition $node): void ->arrayNode('connections') ->useAttributeAsKey('key') ->canBeUnset() - ->prototype('array') + ->arrayPrototype() ->children() ->scalarNode('class')->defaultValue('%m6_web_amqp.connection.class%')->end() ->scalarNode('host')->defaultValue('localhost')->end() @@ -82,7 +83,7 @@ protected function addProducers(ArrayNodeDefinition $node): void ->arrayNode('producers') ->canBeUnset() ->useAttributeAsKey('key') - ->prototype('array') + ->arrayPrototype() ->children() ->scalarNode('class')->defaultValue('%m6_web_amqp.producer.class%')->end() ->scalarNode('connection')->defaultValue('default')->end() @@ -125,7 +126,7 @@ protected function addConsumers(ArrayNodeDefinition $node): void ->arrayNode('consumers') ->canBeUnset() ->useAttributeAsKey('key') - ->prototype('array') + ->arrayPrototype() ->children() ->scalarNode('class')->defaultValue('%m6_web_amqp.consumer.class%')->end() ->scalarNode('connection')->defaultValue('default')->end() diff --git a/src/AmqpBundle/Event/Command.php b/src/AmqpBundle/Event/Command.php index fa78cf3..cc52720 100644 --- a/src/AmqpBundle/Event/Command.php +++ b/src/AmqpBundle/Event/Command.php @@ -15,7 +15,7 @@ class Command extends Event implements DispatcherInterface protected string $command; protected array $arguments; - protected $return; + protected mixed $return; /** * {@inheritDoc} @@ -58,7 +58,7 @@ public function getArguments(): array * * @param mixed $v value */ - public function setReturn($v): self + public function setReturn(mixed $v): self { $this->return = $v; @@ -68,7 +68,7 @@ public function setReturn($v): self /** * get the return value. */ - public function getReturn() + public function getReturn(): mixed { return $this->return; } diff --git a/src/AmqpBundle/Event/DispatcherInterface.php b/src/AmqpBundle/Event/DispatcherInterface.php index dafa010..80a0484 100644 --- a/src/AmqpBundle/Event/DispatcherInterface.php +++ b/src/AmqpBundle/Event/DispatcherInterface.php @@ -14,26 +14,35 @@ interface DispatcherInterface * * @param string $command The sqs command */ - public function setCommand(string $command); + public function setCommand(string $command): self; /** * set execution time. * * @param float $v temps */ - public function setExecutionTime(float $v); + public function setExecutionTime(float $v): self; /** * set the arguments. * * @param array $v argus */ - public function setArguments(array $v); + public function setArguments(array $v): self; /** * set the return value. * * @param mixed $v value */ - public function setReturn(mixed $v); + public function setReturn(mixed $v): self; + + public function getCommand(): string; + + public function getArguments(): array; + + public function getReturn(): mixed; + + public function getExecutionTime(): float; + } diff --git a/src/AmqpBundle/Factory/ConsumerFactory.php b/src/AmqpBundle/Factory/ConsumerFactory.php index b08261f..6aee576 100644 --- a/src/AmqpBundle/Factory/ConsumerFactory.php +++ b/src/AmqpBundle/Factory/ConsumerFactory.php @@ -105,6 +105,12 @@ public function get(string $class, \AMQPConnection $connexion, array $exchangeOp $queue->bind($exchangeOptions['name']); } - return new $class($queue, $queueOptions); + $consumer = new $class($queue, $queueOptions); + + if (!$consumer instanceof Consumer) { + throw new \InvalidArgumentException("$class must be an instance of Consumer"); + } + + return $consumer; } } diff --git a/src/AmqpBundle/Factory/ProducerFactory.php b/src/AmqpBundle/Factory/ProducerFactory.php index 0c61ec6..3b0033a 100644 --- a/src/AmqpBundle/Factory/ProducerFactory.php +++ b/src/AmqpBundle/Factory/ProducerFactory.php @@ -70,6 +70,7 @@ public function get(string $class, \AMQPConnection $connexion, array $exchangeOp } // Open a new channel + /** @var \AMQPChannel $channel */ $channel = new $this->channelClass($connexion); $exchange = $this->createExchange($this->exchangeClass, $channel, $exchangeOptions); @@ -94,6 +95,7 @@ public function get(string $class, \AMQPConnection $connexion, array $exchangeOp } // Create the producer + /** @var Producer */ return new $class($exchange, $exchangeOptions); } } diff --git a/src/AmqpBundle/Sandbox/NullChannel.php b/src/AmqpBundle/Sandbox/NullChannel.php index 0ea3c75..ce305d7 100644 --- a/src/AmqpBundle/Sandbox/NullChannel.php +++ b/src/AmqpBundle/Sandbox/NullChannel.php @@ -9,8 +9,4 @@ */ class NullChannel extends \AMQPChannel { - public function __construct(\AMQPConnection $amqp_connection) - { - // noop - } } diff --git a/src/AmqpBundle/Sandbox/NullExchange.php b/src/AmqpBundle/Sandbox/NullExchange.php index 07cd532..2525741 100644 --- a/src/AmqpBundle/Sandbox/NullExchange.php +++ b/src/AmqpBundle/Sandbox/NullExchange.php @@ -9,14 +9,6 @@ */ class NullExchange extends \AMQPExchange { - /** - * {@inheritdoc} - */ - public function __construct(\AMQPChannel $amqp_channel) - { - // noop - } - /** * {@inheritdoc} */ @@ -32,8 +24,7 @@ public function publish( /** * {@inheritdoc} */ - public function declareExchange() + public function declareExchange(): void { - return true; } } diff --git a/src/AmqpBundle/Sandbox/NullQueue.php b/src/AmqpBundle/Sandbox/NullQueue.php index dbe2f09..983dcd5 100644 --- a/src/AmqpBundle/Sandbox/NullQueue.php +++ b/src/AmqpBundle/Sandbox/NullQueue.php @@ -19,7 +19,7 @@ class NullQueue extends \AMQPQueue /** * {@inheritdoc} */ - public function __construct(\AMQPChannel $channel) + public function __construct() { $this->envelopes = new \SplQueue(); } diff --git a/src/AmqpBundle/Tests/Units/Amqp/Consumer.php b/src/AmqpBundle/Tests/Units/Amqp/Consumer.php index ab2b90e..5a1449c 100644 --- a/src/AmqpBundle/Tests/Units/Amqp/Consumer.php +++ b/src/AmqpBundle/Tests/Units/Amqp/Consumer.php @@ -274,9 +274,13 @@ public function testGetMessageCurrentCount(): void public function testConsumerWithNullQueue(): void { $this - ->if($connection = new NullConnection()) - ->and($channel = new NullChannel($connection)) - ->and($queue = new NullQueue($channel)) + ->if($connection = new NullConnection()) + ->exception( + static fn() => new NullChannel($connection) + ); + + $this + ->if($queue = new NullQueue()) ->and($consumer = new Base($queue, [])) ->then ->variable($consumer->getMessage())->isNull() From cfd697945c7be62c60a5237d91a9dafc2f485cb0 Mon Sep 17 00:00:00 2001 From: Thibaud Girard Date: Tue, 5 Mar 2024 11:45:43 +0100 Subject: [PATCH 09/10] chore(ci): remove symfony 4 support in ci --- .github/workflows/ci.yml | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 29307d8..165f8d0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,13 +2,12 @@ name: Continuous Integration on: [push, pull_request] jobs: - tests: - name: Tests + tests-old-versions: + name: Tests old versions runs-on: ubuntu-20.04 strategy: matrix: - php-version: ['7.4', '8.0', '8.1' ] - symfony-version: ['^4.4', '^5.0'] + php-version: ['7.4', '8.0', '8.1', '8.2' , '8.3' ] fail-fast: false steps: - uses: actions/checkout@master @@ -16,7 +15,31 @@ jobs: with: php-version: ${{ matrix.php-version }} coverage: xdebug2 - extensions: amqp + extensions: amqp-2 + - name: Install symfony v5.4 + env: + SYMFONY_VERSION: '^5.4' + run: composer require symfony/symfony:$SYMFONY_VERSION --no-update + - name: Install dependencies + run: composer update --prefer-dist --no-interaction + - name: Unit tests + run: bin/atoum + + tests-current-versions: + name: Tests current versions + runs-on: ubuntu-20.04 + strategy: + matrix: + php-version: ['8.2' , '8.3' ] + symfony-version: ['^6.4', '^7.0'] + fail-fast: false + steps: + - uses: actions/checkout@master + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + coverage: xdebug + extensions: amqp-2 - name: Install symfony version from matrix env: SYMFONY_VERSION: ${{ matrix.symfony-version }} From 9b53ac6ab19624a675f3f3b5d7ddad0efe459237 Mon Sep 17 00:00:00 2001 From: Thibaud Girard Date: Wed, 6 Mar 2024 09:28:13 +0100 Subject: [PATCH 10/10] chore(php): remove php 7.4 --- .github/workflows/ci.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 165f8d0..0fda398 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-20.04 strategy: matrix: - php-version: ['7.4', '8.0', '8.1', '8.2' , '8.3' ] + php-version: ['8.0', '8.1', '8.2' , '8.3' ] fail-fast: false steps: - uses: actions/checkout@master diff --git a/composer.json b/composer.json index 918e2ce..3a58afa 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "bin-dir": "bin" }, "require" : { - "php": ">=7.4", + "php": "^8.0", "ext-amqp": ">=2.0", "symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0", "symfony/framework-bundle": "^5.4 || ^6.4 || ^7.0",