Skip to content

Commit 7cb2c4a

Browse files
committed
Add types and support for PHP 8
1 parent ed7d1a2 commit 7cb2c4a

58 files changed

Lines changed: 3520 additions & 4293 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

composer.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,15 @@
2525
"php-amqplib/php-amqplib": "^3.0.0",
2626
"laminas/laminas-stdlib": "^3.3.0",
2727
"laminas/laminas-servicemanager": "^3.11",
28-
"laminas/laminas-modulemanager": "^2.10.0",
29-
"laminas/laminas-mvc": "^3.3",
30-
"laminas/laminas-cli": "^1.0",
3128
"laminas/laminas-serializer": "^2.10.0"
3229
},
3330
"require-dev": {
3431
"phpunit/phpunit": "^9.3",
3532
"friendsofphp/php-cs-fixer": "^3.11",
3633
"phpspec/prophecy": "^1.12.0",
3734
"phpspec/prophecy-phpunit": "^2.0",
38-
"vimeo/psalm": "^4.27"
35+
"vimeo/psalm": "^4.27",
36+
"laminas/laminas-cli": "^1.0"
3937
},
4038
"autoload": {
4139
"psr-4": {

composer.lock

Lines changed: 2644 additions & 3540 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BaseAmqp.php

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
use PhpAmqpLib\Connection\AbstractConnection;
1010
use PhpAmqpLib\Wire\AMQPTable;
1111
use RabbitMqModule\Options\Exchange as ExchangeOptions;
12+
use RabbitMqModule\Options\Qos;
1213
use RabbitMqModule\Options\Queue as QueueOptions;
1314
use RabbitMqModule\Service\SetupFabricAwareInterface;
1415

1516
abstract class BaseAmqp implements SetupFabricAwareInterface
1617
{
1718
protected AbstractConnection $connection;
1819

20+
protected ?Qos $qos = null;
21+
1922
private ?AMQPChannel $channel = null;
2023

2124
protected ?QueueOptions $queueOptions = null;
@@ -28,31 +31,65 @@ abstract class BaseAmqp implements SetupFabricAwareInterface
2831

2932
protected bool $queueDeclared = false;
3033

31-
public function __construct(AbstractConnection $connection, AMQPChannel $channel = null)
34+
public function __construct(AbstractConnection $connection)
3235
{
3336
$this->connection = $connection;
34-
$this->channel = $channel;
3537
}
3638

39+
/**
40+
* @internal
41+
*
42+
* @psalm-internal RabbitMqModule
43+
*/
3744
public function getConnection(): AbstractConnection
3845
{
3946
return $this->connection;
4047
}
4148

49+
/**
50+
* @internal
51+
*
52+
* @psalm-internal RabbitMqModule
53+
*/
4254
public function getChannel(): AMQPChannel
4355
{
44-
if (! $this->channel) {
45-
$this->channel = $this->getConnection()->channel();
56+
if ($this->channel) {
57+
return $this->channel;
58+
}
59+
60+
$this->channel = $this->getConnection()->channel();
61+
62+
if ($this->qos) {
63+
$this->channel->basic_qos(
64+
$this->qos->getPrefetchSize(),
65+
$this->qos->getPrefetchCount(),
66+
false
67+
);
4668
}
4769

4870
return $this->channel;
4971
}
5072

73+
public function setQos(?Qos $qos): void
74+
{
75+
$this->qos = $qos;
76+
}
77+
78+
/**
79+
* @internal
80+
*
81+
* @psalm-internal RabbitMqModule
82+
*/
5183
public function setChannel(AMQPChannel $channel): void
5284
{
5385
$this->channel = $channel;
5486
}
5587

88+
/**
89+
* @internal
90+
*
91+
* @psalm-internal RabbitMqModule
92+
*/
5693
public function getQueueOptions(): ?QueueOptions
5794
{
5895
return $this->queueOptions;
@@ -63,6 +100,11 @@ public function setQueueOptions(?QueueOptions $queueOptions): void
63100
$this->queueOptions = $queueOptions;
64101
}
65102

103+
/**
104+
* @internal
105+
*
106+
* @psalm-internal RabbitMqModule
107+
*/
66108
public function getExchangeOptions(): ?ExchangeOptions
67109
{
68110
return $this->exchangeOptions;
@@ -73,6 +115,11 @@ public function setExchangeOptions(?ExchangeOptions $exchangeOptions): void
73115
$this->exchangeOptions = $exchangeOptions;
74116
}
75117

118+
/**
119+
* @internal
120+
*
121+
* @psalm-internal RabbitMqModule
122+
*/
76123
public function isAutoSetupFabricEnabled(): bool
77124
{
78125
return $this->autoSetupFabricEnabled;
@@ -195,6 +242,10 @@ public function setupFabric(): void
195242

196243
/**
197244
* Reconnect
245+
*
246+
* @internal
247+
*
248+
* @psalm-internal RabbitMqModule
198249
*/
199250
public function reconnect(): void
200251
{

src/BaseConsumer.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
namespace RabbitMqModule;
66

77
use BadFunctionCallException;
8-
use PhpAmqpLib\Channel\AMQPChannel;
9-
use PhpAmqpLib\Connection\AbstractConnection;
10-
use RabbitMqModule\Options\Queue;
118
use function extension_loaded;
129
use function function_exists;
10+
use PhpAmqpLib\Connection\AbstractConnection;
1311
use PhpAmqpLib\Message\AMQPMessage;
12+
use RabbitMqModule\Options\Queue;
1413

1514
/**
1615
* @psalm-type ConsumerHandler = callable(AMQPMessage): (int|null|void)
@@ -21,6 +20,7 @@ abstract class BaseConsumer extends BaseAmqp
2120

2221
/**
2322
* @psalm-var callable(AMQPMessage): (int|void)
23+
*
2424
* @var callable
2525
*/
2626
protected $callback;
@@ -36,14 +36,19 @@ abstract class BaseConsumer extends BaseAmqp
3636
/**
3737
* @psalm-param callable(AMQPMessage): (int|void) $callback
3838
*/
39-
public function __construct(AbstractConnection $connection, Queue $queueOptions, callable $callback, AMQPChannel $channel = null)
39+
public function __construct(AbstractConnection $connection, Queue $queueOptions, callable $callback)
4040
{
41-
parent::__construct($connection, $channel);
41+
parent::__construct($connection);
4242
$this->setQueueOptions($queueOptions);
4343
$this->queueName = $queueOptions->getName();
4444
$this->callback = $callback;
4545
}
4646

47+
/**
48+
* @internal
49+
*
50+
* @psalm-internal RabbitMqModule
51+
*/
4752
public function isSignalsEnabled(): bool
4853
{
4954
return $this->signalsEnabled;
@@ -69,7 +74,12 @@ public function setConsumerTag(string $consumerTag): void
6974
}
7075

7176
/**
77+
* @internal
78+
*
79+
* @psalm-internal RabbitMqModule
80+
*
7281
* @psalm-return callable(AMQPMessage): (int|void)
82+
*
7383
* @return callable
7484
*/
7585
public function getCallback(): callable
@@ -79,13 +89,19 @@ public function getCallback(): callable
7989

8090
/**
8191
* @param callable(AMQPMessage): (int|void) $callback
92+
*
8293
* @return void
8394
*/
8495
public function setCallback(callable $callback): void
8596
{
8697
$this->callback = $callback;
8798
}
8899

100+
/**
101+
* @internal
102+
*
103+
* @psalm-internal RabbitMqModule
104+
*/
89105
public function getIdleTimeout(): int
90106
{
91107
return $this->idleTimeout;
@@ -128,6 +144,8 @@ protected function setupConsumer(): void
128144
/**
129145
* Sets the qos settings for the current channel
130146
* Consider that prefetchSize and global do not work with rabbitMQ version <= 8.0.
147+
*
148+
* @deprecated
131149
*/
132150
public function setQosOptions(int $prefetchSize = 0, int $prefetchCount = 0, bool $global = false): void
133151
{

src/Command/SetupFabricCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5656
* @throws NotFoundExceptionInterface
5757
*
5858
* @psalm-return list<mixed>
59+
*
60+
* @psalm-suppress MixedAssignment
5961
*/
6062
private function getServiceParts(): array
6163
{
@@ -78,7 +80,6 @@ private function getServiceParts(): array
7880

7981
$keys = array_keys($config['rabbitmq'][$serviceKey]);
8082
foreach ($keys as $key) {
81-
/** @psalm-suppress MixedAssignment */
8283
$parts[] = $this->container->get(sprintf('rabbitmq.%s.%s', $serviceKey, $key));
8384
}
8485
}

src/Command/StartConsumerCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\Console\Input\InputOption;
1818
use Symfony\Component\Console\Output\OutputInterface;
1919

20-
final class StartConsumerCommand extends Command
20+
class StartConsumerCommand extends Command
2121
{
2222
public const NAME = 'rabbitmq:consumers:start';
2323

@@ -83,8 +83,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8383
);
8484
}
8585

86-
pcntl_signal(SIGTERM, [$this, 'stopConsumer']);
87-
pcntl_signal(SIGINT, [$this, 'stopConsumer']);
86+
pcntl_signal(SIGTERM, fn () => $this->stopConsumer());
87+
pcntl_signal(SIGINT, fn () => $this->stopConsumer());
8888
}
8989
// @codeCoverageIgnoreEnd
9090

@@ -93,7 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9393
return Command::SUCCESS;
9494
}
9595

96-
public function stopConsumer(): void
96+
protected function stopConsumer(): void
9797
{
9898
if ($this->consumer instanceof Consumer) {
9999
$this->consumer->forceStopConsumer();

src/ConfigProvider.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace RabbitMqModule;
56

67
use Laminas\ServiceManager\Factory\InvokableFactory;
7-
use RabbitMqModule\Command;
8-
use RabbitMqModule\Service;
98
use RabbitMqModule\Service\AbstractFactory;
109

1110
/**
@@ -18,6 +17,7 @@
1817
* @psalm-import-type ConsumerOptions from Options\Consumer
1918
* @psalm-import-type RpcClientOptions from Options\RpcClient
2019
* @psalm-import-type RpcServerOptions from Options\RpcServer
20+
*
2121
* @psalm-type ConfigArray = array{
2222
* rabbitmq_factories: array{
2323
* connection: class-string<AbstractFactory>,
@@ -40,6 +40,7 @@ final class ConfigProvider
4040
{
4141
/**
4242
* @psalm-return ConfigArray
43+
*
4344
* @return array<string, mixed>
4445
*/
4546
public function __invoke(): array
@@ -48,12 +49,12 @@ public function __invoke(): array
4849
'dependencies' => $this->getDependencies(),
4950
'rabbitmq' => [
5051
'connection' => [
51-
'default' => []
52+
'default' => [],
5253
],
5354
'producer' => [],
5455
'consumer' => [],
5556
'rpc_server' => [],
56-
'rpc_client' => []
57+
'rpc_client' => [],
5758
],
5859
'rabbitmq_factories' => [
5960
'connection' => Service\ConnectionFactory::class,
@@ -69,8 +70,8 @@ public function __invoke(): array
6970
Command\StartRpcServerCommand::NAME => Command\StartRpcServerCommand::class,
7071
Command\PublishMessageCommand::NAME => Command\PublishMessageCommand::class,
7172
Command\SetupFabricCommand::NAME => Command\SetupFabricCommand::class,
72-
]
73-
]
73+
],
74+
],
7475
];
7576
}
7677

src/Consumer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function consume(): void
4949
}
5050
}
5151

52+
/**
53+
* @internal
54+
*
55+
* @psalm-internal RabbitMqModule
56+
*/
5257
public function processMessage(AMQPMessage $message): void
5358
{
5459
$processFlag = call_user_func($this->getCallback(), $message);
@@ -60,11 +65,12 @@ public function processMessage(AMQPMessage $message): void
6065
*/
6166
protected function handleProcessMessage(AMQPMessage $msg, $processFlag): void
6267
{
63-
/** @psalm-suppress DocblockTypeContradiction */
68+
/* @psalm-suppress DocblockTypeContradiction */
6469
if (null !== $processFlag && ! is_int($processFlag)) {
6570
trigger_error(
6671
'Consumer handler should return an integer or void/null. Returning a different type is deprecated',
67-
E_USER_DEPRECATED);
72+
E_USER_DEPRECATED
73+
);
6874
}
6975

7076
$channel = $msg->getChannel() ?: $this->getChannel();

src/ConsumerInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@ interface ConsumerInterface
1414
/**
1515
* Flag for message ack.
1616
*
17-
* @deprecated Use {@see Consumer::MSG_ACK} instead.
17+
* @deprecated use {@see Consumer::MSG_ACK} instead
1818
*/
1919
public const MSG_ACK = Consumer::MSG_ACK;
2020

2121
/**
2222
* Flag single for message nack and requeue.
2323
*
24-
* @deprecated Use {@see Consumer::MSG_SINGLE_NACK_REQUEUE} instead.
24+
* @deprecated use {@see Consumer::MSG_SINGLE_NACK_REQUEUE} instead
2525
*/
2626
public const MSG_SINGLE_NACK_REQUEUE = Consumer::MSG_SINGLE_NACK_REQUEUE;
2727

2828
/**
2929
* Flag for reject and requeue.
3030
*
31-
* @deprecated Use {@see Consumer::MSG_REJECT_REQUEUE} instead.
31+
* @deprecated use {@see Consumer::MSG_REJECT_REQUEUE} instead
3232
*/
3333
public const MSG_REJECT_REQUEUE = Consumer::MSG_REJECT_REQUEUE;
3434

3535
/**
3636
* Flag for reject and drop.
3737
*
38-
* @deprecated Use {@see Consumer::MSG_REJECT} instead.
38+
* @deprecated use {@see Consumer::MSG_REJECT} instead
3939
*/
4040
public const MSG_REJECT = Consumer::MSG_REJECT;
4141

4242
/**
43-
* @deprecated Use an invokable class instead.
43+
* @deprecated use an invokable class instead
4444
*/
4545
public function execute(AMQPMessage $message): ?int;
4646
}

0 commit comments

Comments
 (0)