Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/rector-cs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Rector + PHP CS Fixer

on:
pull_request:
paths:
- 'config/**'
- 'src/**'
- 'tests/**'
- '.github/workflows/rector-cs.yml'
- 'composer.json'
- 'rector.php'
- '.php-cs-fixer.dist.php'

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
rector:
permissions:
contents: write # Required to commit automated Rector and CS fixes
uses: yiisoft/actions/.github/workflows/rector-cs.yml@master
with:
php: '8.1'
23 changes: 0 additions & 23 deletions .github/workflows/rector.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ phpunit.phar
coverage.xml
/coverage.html

# Phan
analysis.txt
# PHP CS Fixer
/.php-cs-fixer.cache

tests/.env
21 changes: 21 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Finder;
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
use Yiisoft\CodeStyle\ConfigBuilder;

$finder = (new Finder())->in([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

return ConfigBuilder::build()
->setRiskyAllowed(true)
->setParallelConfig(ParallelConfigFactory::detect())
->setRules([
'@Yiisoft/Core' => true,
'@Yiisoft/Core:risky' => true,
])
->setFinder($finder);
85 changes: 0 additions & 85 deletions .styleci.yml

This file was deleted.

9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@
"yiisoft/queue": "dev-master"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.95",
"infection/infection": "^0.27.8||^0.29.0",
"maglnet/composer-require-checker": "^4.4",
"phpbench/phpbench": "^1.4",
"phpunit/phpunit": "^9.5",
"rector/rector": "^2.0.15",
"rector/rector": "^2.5.5",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"symfony/console": "^6.0",
"symfony/process": "^6.0",
"vimeo/psalm": "^5.26.1 || ^6.10.3",
"yiisoft/code-style": "^1.0",
"yiisoft/test-support": "^3.0"
},
"autoload": {
Expand Down Expand Up @@ -70,5 +72,10 @@
"infection/extension-installer": true,
"composer/package-versions-deprecated": true
}
},
"scripts": {
"cs-fix": "php-cs-fixer fix",
"psalm": "psalm",
"rector": "rector"
}
}
2 changes: 0 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;

return RectorConfig::configure()
->withPaths([
Expand All @@ -22,5 +21,4 @@
ClosureToArrowFunctionRector::class,
ReadOnlyPropertyRector::class,
NullToStrictStringFuncCallArgRector::class,
ReturnNeverTypeRector::class,
]);
13 changes: 7 additions & 6 deletions src/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
use Yiisoft\Queue\Message\Serializer\MessageSerializerInterface;
use Yiisoft\Queue\MessageStatus;

use function is_string;

final class Adapter implements AdapterInterface
{
public function __construct(
private QueueProviderInterface $queueProvider,
private readonly MessageSerializerInterface $serializer,
private readonly LoopInterface $loop,
) {
}
) {}

public function withChannel(BackedEnum|string $channel): self
{
Expand Down Expand Up @@ -74,7 +75,7 @@
$exchangeSettings?->getName() ?? '',
$exchangeSettings ? '' : $queueProvider
->getQueueSettings()
->getName()
->getName(),
);

return $message;
Expand Down Expand Up @@ -108,16 +109,16 @@
function (AMQPMessage $amqpMessage) use ($handlerCallback, $channel): void {
try {
$handlerCallback($this->serializer->unserialize($amqpMessage->getBody()));
$channel->basic_ack($amqpMessage->getDeliveryTag());

Check warning on line 112 in src/Adapter.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ $channel->basic_consume($this->queueProvider->getQueueSettings()->getName(), $this->queueProvider->getQueueSettings()->getName(), false, false, false, true, function (AMQPMessage $amqpMessage) use ($handlerCallback, $channel): void { try { $handlerCallback($this->serializer->unserialize($amqpMessage->getBody())); - $channel->basic_ack($amqpMessage->getDeliveryTag()); + } catch (Throwable $exception) { $consumerTag = $amqpMessage->getConsumerTag(); if ($consumerTag !== null) {
} catch (Throwable $exception) {
$consumerTag = $amqpMessage->getConsumerTag();
if ($consumerTag !== null) {
$channel->basic_cancel($consumerTag);

Check warning on line 116 in src/Adapter.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ } catch (Throwable $exception) { $consumerTag = $amqpMessage->getConsumerTag(); if ($consumerTag !== null) { - $channel->basic_cancel($consumerTag); + } throw $exception; }
}

throw $exception;
}
}
},
);

while ($this->loop->canContinue()) {
Expand Down Expand Up @@ -155,7 +156,7 @@
throw new InvalidArgumentException('Message cannot be delayed to a queue without an exchange. Exchange is mandatory.');
}

$delayMilliseconds = (int) ceil($delaySeconds * 1000);

Check warning on line 159 in src/Adapter.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "RoundingFamily": @@ @@ if ($exchangeSettings === null) { throw new InvalidArgumentException('Message cannot be delayed to a queue without an exchange. Exchange is mandatory.'); } - $delayMilliseconds = (int) ceil($delaySeconds * 1000); + $delayMilliseconds = (int) round($delaySeconds * 1000); return $this->queueProvider->withMessageProperties($this->getDelayMessageProperties($delayMilliseconds))->withExchangeSettings($this->getDelayExchangeSettings($exchangeSettings))->withQueueSettings($this->getDelayQueueSettings($this->queueProvider->getQueueSettings(), $exchangeSettings, $delayMilliseconds)); } /**

Check warning on line 159 in src/Adapter.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "RoundingFamily": @@ @@ if ($exchangeSettings === null) { throw new InvalidArgumentException('Message cannot be delayed to a queue without an exchange. Exchange is mandatory.'); } - $delayMilliseconds = (int) ceil($delaySeconds * 1000); + $delayMilliseconds = (int) floor($delaySeconds * 1000); return $this->queueProvider->withMessageProperties($this->getDelayMessageProperties($delayMilliseconds))->withExchangeSettings($this->getDelayExchangeSettings($exchangeSettings))->withQueueSettings($this->getDelayQueueSettings($this->queueProvider->getQueueSettings(), $exchangeSettings, $delayMilliseconds)); } /**

return $this->queueProvider
->withMessageProperties($this->getDelayMessageProperties($delayMilliseconds))
Expand All @@ -165,7 +166,7 @@
$this->queueProvider->getQueueSettings(),
$exchangeSettings,
$delayMilliseconds,
)
),
);
}

Expand All @@ -174,7 +175,7 @@
*/
private function getDelayMessageProperties(int $delayMilliseconds): array
{
return array_merge(

Check warning on line 178 in src/Adapter.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": @@ @@ */ private function getDelayMessageProperties(int $delayMilliseconds): array { - return array_merge($this->queueProvider->getMessageProperties(), ['expiration' => (string) $delayMilliseconds, 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]); + return ['expiration' => (string) $delayMilliseconds, 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]; } private function getDelayQueueSettings(QueueSettingsInterface $queueSettings, ExchangeSettingsInterface $exchangeSettings, int $delayMilliseconds): QueueSettingsInterface {
$this->queueProvider->getMessageProperties(),
[
'expiration' => (string) $delayMilliseconds,
Expand All @@ -188,7 +189,7 @@
ExchangeSettingsInterface $exchangeSettings,
int $delayMilliseconds,
): QueueSettingsInterface {
$deliveryTime = time() + (int) ceil($delayMilliseconds / 1000);

Check warning on line 192 in src/Adapter.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "RoundingFamily": @@ @@ } private function getDelayQueueSettings(QueueSettingsInterface $queueSettings, ExchangeSettingsInterface $exchangeSettings, int $delayMilliseconds): QueueSettingsInterface { - $deliveryTime = time() + (int) ceil($delayMilliseconds / 1000); + $deliveryTime = time() + (int) round($delayMilliseconds / 1000); return $queueSettings->withName("{$queueSettings->getName()}.dlx.{$deliveryTime}")->withAutoDeletable(true)->withArguments(['x-dead-letter-exchange' => ['S', $exchangeSettings->getName()], 'x-expires' => ['I', $delayMilliseconds + 30000], 'x-message-ttl' => ['I', $delayMilliseconds]]); } private function getDelayExchangeSettings(ExchangeSettingsInterface $exchangeSettings): ExchangeSettingsInterface

Check warning on line 192 in src/Adapter.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "RoundingFamily": @@ @@ } private function getDelayQueueSettings(QueueSettingsInterface $queueSettings, ExchangeSettingsInterface $exchangeSettings, int $delayMilliseconds): QueueSettingsInterface { - $deliveryTime = time() + (int) ceil($delayMilliseconds / 1000); + $deliveryTime = time() + (int) floor($delayMilliseconds / 1000); return $queueSettings->withName("{$queueSettings->getName()}.dlx.{$deliveryTime}")->withAutoDeletable(true)->withArguments(['x-dead-letter-exchange' => ['S', $exchangeSettings->getName()], 'x-expires' => ['I', $delayMilliseconds + 30000], 'x-message-ttl' => ['I', $delayMilliseconds]]); } private function getDelayExchangeSettings(ExchangeSettingsInterface $exchangeSettings): ExchangeSettingsInterface

Check warning on line 192 in src/Adapter.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "Division": @@ @@ } private function getDelayQueueSettings(QueueSettingsInterface $queueSettings, ExchangeSettingsInterface $exchangeSettings, int $delayMilliseconds): QueueSettingsInterface { - $deliveryTime = time() + (int) ceil($delayMilliseconds / 1000); + $deliveryTime = time() + (int) ceil($delayMilliseconds * 1000); return $queueSettings->withName("{$queueSettings->getName()}.dlx.{$deliveryTime}")->withAutoDeletable(true)->withArguments(['x-dead-letter-exchange' => ['S', $exchangeSettings->getName()], 'x-expires' => ['I', $delayMilliseconds + 30000], 'x-message-ttl' => ['I', $delayMilliseconds]]); } private function getDelayExchangeSettings(ExchangeSettingsInterface $exchangeSettings): ExchangeSettingsInterface

Check warning on line 192 in src/Adapter.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": @@ @@ } private function getDelayQueueSettings(QueueSettingsInterface $queueSettings, ExchangeSettingsInterface $exchangeSettings, int $delayMilliseconds): QueueSettingsInterface { - $deliveryTime = time() + (int) ceil($delayMilliseconds / 1000); + $deliveryTime = time() + (int) ceil($delayMilliseconds / 1001); return $queueSettings->withName("{$queueSettings->getName()}.dlx.{$deliveryTime}")->withAutoDeletable(true)->withArguments(['x-dead-letter-exchange' => ['S', $exchangeSettings->getName()], 'x-expires' => ['I', $delayMilliseconds + 30000], 'x-message-ttl' => ['I', $delayMilliseconds]]); } private function getDelayExchangeSettings(ExchangeSettingsInterface $exchangeSettings): ExchangeSettingsInterface

Check warning on line 192 in src/Adapter.php

View workflow job for this annotation

GitHub Actions / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": @@ @@ } private function getDelayQueueSettings(QueueSettingsInterface $queueSettings, ExchangeSettingsInterface $exchangeSettings, int $delayMilliseconds): QueueSettingsInterface { - $deliveryTime = time() + (int) ceil($delayMilliseconds / 1000); + $deliveryTime = time() + (int) ceil($delayMilliseconds / 999); return $queueSettings->withName("{$queueSettings->getName()}.dlx.{$deliveryTime}")->withAutoDeletable(true)->withArguments(['x-dead-letter-exchange' => ['S', $exchangeSettings->getName()], 'x-expires' => ['I', $delayMilliseconds + 30000], 'x-message-ttl' => ['I', $delayMilliseconds]]); } private function getDelayExchangeSettings(ExchangeSettingsInterface $exchangeSettings): ExchangeSettingsInterface

return $queueSettings
->withName("{$queueSettings->getName()}.dlx.$deliveryTime")
Expand All @@ -198,7 +199,7 @@
'x-dead-letter-exchange' => ['S', $exchangeSettings->getName()],
'x-expires' => ['I', $delayMilliseconds + 30000],
'x-message-ttl' => ['I', $delayMilliseconds],
]
],
);
}

Expand Down
4 changes: 1 addition & 3 deletions src/Exception/NotImplementedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use RuntimeException;

final class NotImplementedException extends RuntimeException
{
}
final class NotImplementedException extends RuntimeException {}
7 changes: 3 additions & 4 deletions src/ExistingMessagesConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ final class ExistingMessagesConsumer

public function __construct(
private readonly QueueProviderInterface $queueProvider,
private readonly MessageSerializerInterface $serializer
) {
}
private readonly MessageSerializerInterface $serializer,
) {}

/**
* @param callable(MessageInterface): bool $callback
Expand Down Expand Up @@ -51,7 +50,7 @@ function (AMQPMessage $amqpMessage) use ($callback, $channel): void {

throw $exception;
}
}
},
);

do {
Expand Down
4 changes: 1 addition & 3 deletions src/Middleware/DelayMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@

final class DelayMiddleware implements PushMiddlewareInterface
{
public function __construct(private float $delayInSeconds)
{
}
public function __construct(private float $delayInSeconds) {}

/**
* @param float $seconds
Expand Down
13 changes: 6 additions & 7 deletions src/QueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ public function __construct(
private QueueSettingsInterface $queueSettings,
private ?ExchangeSettingsInterface $exchangeSettings = null,
private array $messageProperties = [],
) {
}

public function __clone()
{
$this->channelId = null;
}
) {}

public function __destruct()
{
Expand All @@ -39,6 +33,11 @@ public function __destruct()
}
}

public function __clone()
{
$this->channelId = null;
}

/**
* Returns an AMQPChannel instance.
* IMPORTANT: Do NOT memorise the channel instance, as this will cause memory leaks on channel close!
Expand Down
5 changes: 2 additions & 3 deletions src/Settings/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ public function __construct(
private bool $internal = false,
private bool $nowait = false,
private AMQPTable|array $arguments = [],
private ?int $ticket = null
) {
}
private ?int $ticket = null,
) {}

public function getArguments(): AMQPTable|array
{
Expand Down
6 changes: 4 additions & 2 deletions src/Settings/ExchangeSettingsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Yiisoft\Queue\Amqp\Settings;

use PhpAmqpLib\Wire\AMQPTable;
use PhpAmqpLib\Channel\AMQPChannel;
use Yiisoft\Queue\Amqp\QueueProvider;

interface ExchangeSettingsInterface
{
Expand All @@ -27,9 +29,9 @@ public function hasNowait(): bool;
public function isPassive(): bool;

/**
* Positional arguments to be used with {@see \PhpAmqpLib\Channel\AMQPChannel::exchange_declare()}
* Positional arguments to be used with {@see AMQPChannel::exchange_declare()}
*
* @see \Yiisoft\Queue\Amqp\QueueProvider::getChannel()
* @see QueueProvider::getChannel()
*
* @return (AMQPTable|array|bool|int|string|null)[]
*
Expand Down
3 changes: 2 additions & 1 deletion src/Settings/QosSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
namespace Yiisoft\Queue\Amqp\Settings;

use InvalidArgumentException;
use PhpAmqpLib\Channel\AMQPChannel;

/**
* Quality of Service settings for AMQP consumers.
*
* @see \PhpAmqpLib\Channel\AMQPChannel::basic_qos()
* @see AMQPChannel::basic_qos()
*/
final class QosSettings
{
Expand Down
3 changes: 1 addition & 2 deletions src/Settings/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public function __construct(
private AMQPTable|array $arguments = [],
private ?int $ticket = null,
private ?QosSettings $qosSettings = null,
) {
}
) {}

public function getArguments(): AMQPTable|array
{
Expand Down
Loading