Skip to content

Commit

Permalink
Add rector CI and apply suggestions (#128)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
  • Loading branch information
xepozz and vjik authored Nov 22, 2023
1 parent 339d713 commit 2639357
Show file tree
Hide file tree
Showing 33 changed files with 82 additions and 123 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
on:
pull_request:
paths-ignore:
- 'docs/**'
- 'README.md'
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'infection.json.dist'
- 'psalm.xml'

name: rector

jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector.yml@master
secrets:
token: ${{ secrets.YIISOFT_GITHUB_TOKEN }}
with:
os: >-
['ubuntu-latest']
php: >-
['8.2']
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"require-dev": {
"maglnet/composer-require-checker": "^4.2",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.18.10",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"yiisoft/yii-debug": "dev-master",
Expand Down
29 changes: 29 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);

$rectorConfig->skip([
ClosureToArrowFunctionRector::class,
JsonThrowOnErrorRector::class,
]);
};
2 changes: 0 additions & 2 deletions src/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public function status(string $id): JobStatus;

/**
* Pushing a message to the queue. Adapter sets message ID if available.
*
* @param MessageInterface $message
*/
public function push(MessageInterface $message): void;

Expand Down
6 changes: 1 addition & 5 deletions src/Cli/SignalLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ class SignalLoop implements LoopInterface
protected const SIGNALS_SUSPEND = [SIGTSTP];
/** @psalm-suppress UndefinedConstant */
protected const SIGNALS_RESUME = [SIGCONT];

protected int $memorySoftLimit;
protected bool $pause = false;
protected bool $exit = false;

/**
* @param int $memorySoftLimit Soft RAM limit in bytes. The loop won't let you continue to execute the program if
* soft limit is reached. Zero means no limit.
*/
public function __construct(int $memorySoftLimit = 0)
public function __construct(protected int $memorySoftLimit = 0)
{
$this->memorySoftLimit = $memorySoftLimit;

foreach (self::SIGNALS_EXIT as $signal) {
pcntl_signal($signal, fn () => $this->exit = true);

Check warning on line 27 in src/Cli/SignalLoop.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ public function __construct(protected int $memorySoftLimit = 0) { foreach (self::SIGNALS_EXIT as $signal) { - pcntl_signal($signal, fn() => $this->exit = true); + pcntl_signal($signal, fn() => $this->exit = false); } foreach (self::SIGNALS_SUSPEND as $signal) { pcntl_signal($signal, fn() => $this->pause = true);

Check warning on line 27 in src/Cli/SignalLoop.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ public function __construct(protected int $memorySoftLimit = 0) { foreach (self::SIGNALS_EXIT as $signal) { - pcntl_signal($signal, fn() => $this->exit = true); + } foreach (self::SIGNALS_SUSPEND as $signal) { pcntl_signal($signal, fn() => $this->pause = true);

Check warning on line 27 in src/Cli/SignalLoop.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ public function __construct(protected int $memorySoftLimit = 0) { foreach (self::SIGNALS_EXIT as $signal) { - pcntl_signal($signal, fn() => $this->exit = true); + pcntl_signal($signal, fn() => $this->exit = false); } foreach (self::SIGNALS_SUSPEND as $signal) { pcntl_signal($signal, fn() => $this->pause = true);

Check warning on line 27 in src/Cli/SignalLoop.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ public function __construct(protected int $memorySoftLimit = 0) { foreach (self::SIGNALS_EXIT as $signal) { - pcntl_signal($signal, fn() => $this->exit = true); + } foreach (self::SIGNALS_SUSPEND as $signal) { pcntl_signal($signal, fn() => $this->pause = true);
}
Expand Down
8 changes: 2 additions & 6 deletions src/Cli/SimpleLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ class SimpleLoop implements LoopInterface
{
use SoftLimitTrait;

protected int $memorySoftLimit;

/**
* @param int $memorySoftLimit Soft RAM limit in bytes. The loop won't let you continue to execute the program if
* soft limit is reached. Zero means no limit.
*/
public function __construct(
int $memorySoftLimit = 0
) {
$this->memorySoftLimit = $memorySoftLimit;
public function __construct(protected int $memorySoftLimit = 0)
{
}

public function canContinue(): bool
Expand Down
5 changes: 1 addition & 4 deletions src/Command/ListenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ final class ListenCommand extends Command
protected static $defaultName = 'queue:listen';
protected static $defaultDescription = 'Listens the queue and executes messages as they come. Needs to be stopped manually.';

private QueueFactoryInterface $queueFactory;

public function __construct(QueueFactoryInterface $queueFactory)
public function __construct(private QueueFactoryInterface $queueFactory)
{
parent::__construct();
$this->queueFactory = $queueFactory;
}

public function configure(): void
Expand Down
5 changes: 1 addition & 4 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ final class RunCommand extends Command
protected static $defaultName = 'queue:run';
protected static $defaultDescription = 'Runs all the existing messages in the queue. Exits once messages are over.';

private QueueFactoryInterface $queueFactory;

public function __construct(QueueFactoryInterface $queueFactory)
public function __construct(private QueueFactoryInterface $queueFactory)
{
parent::__construct();
$this->queueFactory = $queueFactory;
}

public function configure(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ class ChannelIncorrectlyConfigured extends InvalidArgumentException implements F
/**
* ChannelIncorrectlyConfigured constructor.
*
* @param string $channel
* @param mixed|object $definition
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(string $channel, $definition, int $code = 0, ?Throwable $previous = null)

Check warning on line 23 in src/Exception/AdapterConfiguration/ChannelIncorrectlyConfigured.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.0-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ * @param mixed|object $definition * @param Throwable|null $previous */ - public function __construct(string $channel, $definition, int $code = 0, ?Throwable $previous = null) + public function __construct(string $channel, $definition, int $code = -1, ?Throwable $previous = null) { $adapterClass = AdapterInterface::class; $realType = get_debug_type($definition);

Check warning on line 23 in src/Exception/AdapterConfiguration/ChannelIncorrectlyConfigured.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.0-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ * @param mixed|object $definition * @param Throwable|null $previous */ - public function __construct(string $channel, $definition, int $code = 0, ?Throwable $previous = null) + public function __construct(string $channel, $definition, int $code = 1, ?Throwable $previous = null) { $adapterClass = AdapterInterface::class; $realType = get_debug_type($definition);
Expand Down
2 changes: 0 additions & 2 deletions src/Exception/InvalidStatusException.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public function getSolution(): ?string

/**
* Get the wrong status value
*
* @return int
*/
public function getStatus(): int
{
Expand Down
8 changes: 2 additions & 6 deletions src/Exception/JobFailureException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@

class JobFailureException extends RuntimeException
{
private MessageInterface $queueMessage;

public function __construct(MessageInterface $message, Throwable $previous)
public function __construct(private MessageInterface $queueMessage, Throwable $previous)
{
$this->queueMessage = $message;

$error = $previous->getMessage();
$messageId = $message->getId() ?? 'null';
$messageId = $queueMessage->getId() ?? 'null';
$messageText = "Processing of message #$messageId is stopped because of an exception:\n$error.";

parent::__construct($messageText, 0, $previous);
Expand Down
1 change: 0 additions & 1 deletion src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
final class Message implements MessageInterface
{
/**
* @param string $handlerName
* @param mixed $data Message data, encodable by a queue adapter
* @param array $metadata Message metadata, encodable by a queue adapter
* @param string|null $id Message id
Expand Down
10 changes: 1 addition & 9 deletions src/Middleware/CallableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
*/
final class CallableFactory
{
private ContainerInterface $container;

public function __construct(ContainerInterface $container)
public function __construct(private ContainerInterface $container)
{
$this->container = $container;
}

/**
Expand Down Expand Up @@ -64,13 +61,8 @@ public function create(mixed $definition): callable
}

/**
* @param string $className
* @param string $methodName
*
* @throws ContainerExceptionInterface Error while retrieving the entry from container.
* @throws NotFoundExceptionInterface
*
* @return callable|null
*/
private function fromDefinition(string $className, string $methodName): ?callable
{
Expand Down
3 changes: 0 additions & 3 deletions src/Middleware/Consume/ConsumeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ public function __construct(private MessageInterface $message, private QueueInte
{
}

/**
* @return MessageInterface
*/
public function getMessage(): MessageInterface
{
return $this->message;
Expand Down
4 changes: 1 addition & 3 deletions src/Middleware/Consume/MiddlewareFactoryConsume.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,11 @@ private function getFromContainer(string $middlewareDefinition): MiddlewareConsu
private function wrapCallable(callable $callback): MiddlewareConsumeInterface
{
return new class ($callback, $this->container) implements MiddlewareConsumeInterface {
private ContainerInterface $container;
private $callback;

public function __construct(callable $callback, ContainerInterface $container)
public function __construct(callable $callback, private ContainerInterface $container)
{
$this->callback = $callback;
$this->container = $container;
}

public function processConsume(ConsumeRequest $request, MessageHandlerConsumeInterface $handler): ConsumeRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ final class FailureMiddlewareDispatcher
private array $stack = [];

/**
* @param MiddlewareFactoryFailureInterface $middlewareFactory
* @param array[][]|callable[][]|MiddlewareFailureInterface[][]|string[][] $middlewareDefinitions
*/
public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ final class ExponentialDelayMiddleware implements MiddlewareFailureInterface
* @param float $delayInitial The first delay period
* @param float $delayMaximum The maximum delay period
* @param float $exponent Message handling delay will be increased by this multiplication each time it fails
* @param DelayMiddlewareInterface $delayMiddleware
* @param QueueInterface|null $queue
*/
public function __construct(
Expand Down
4 changes: 1 addition & 3 deletions src/Middleware/FailureHandling/MiddlewareFactoryFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ private function getFromContainer(string $middlewareDefinition): MiddlewareFailu
private function wrapCallable(callable $callback): MiddlewareFailureInterface
{
return new class ($callback, $this->container) implements MiddlewareFailureInterface {
private ContainerInterface $container;
private $callback;

public function __construct(callable $callback, ContainerInterface $container)
public function __construct(callable $callback, private ContainerInterface $container)
{
$this->callback = $callback;
$this->container = $container;
}

public function processFailure(FailureHandlingRequest $request, MessageFailureHandlerInterface $handler): FailureHandlingRequest
Expand Down
5 changes: 1 addition & 4 deletions src/Middleware/InvalidMiddlewareDefinitionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use Throwable;

use function get_class;
use function is_array;
use function is_object;
use function is_string;
Expand All @@ -31,14 +30,12 @@ public function __construct($middlewareDefinition, int $code = 0, ?Throwable $pr
}

/**
* @param mixed $middlewareDefinition
*
* @return string|null
*/
private function convertDefinitionToString(mixed $middlewareDefinition): ?string
{
if (is_object($middlewareDefinition)) {
return 'an instance of "' . get_class($middlewareDefinition) . '"';
return 'an instance of "' . $middlewareDefinition::class . '"';
}

if (is_string($middlewareDefinition)) {
Expand Down
4 changes: 1 addition & 3 deletions src/Middleware/Push/MiddlewareFactoryPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,11 @@ private function getFromContainer(string $middlewareDefinition): MiddlewarePushI
private function wrapCallable(callable $callback): MiddlewarePushInterface
{
return new class ($callback, $this->container) implements MiddlewarePushInterface {
private ContainerInterface $container;
private $callback;

public function __construct(callable $callback, ContainerInterface $container)
public function __construct(callable $callback, private ContainerInterface $container)
{
$this->callback = $callback;
$this->container = $container;
}

public function processPush(PushRequest $request, MessageHandlerPushInterface $handler): PushRequest
Expand Down
3 changes: 0 additions & 3 deletions src/Middleware/Push/PushRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ public function __construct(private MessageInterface $message, private ?AdapterI
{
}

/**
* @return MessageInterface
*/
public function getMessage(): MessageInterface
{
return $this->message;
Expand Down
6 changes: 0 additions & 6 deletions src/QueueFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ final class QueueFactory implements QueueFactoryInterface
* "Definition" here is a {@see Factory} definition
* @param QueueInterface $queue A default queue implementation. `$queue->withAdapter()` will be returned
* with the `get` method
* @param ContainerInterface $container
* @param CallableFactory $callableFactory
* @param Injector $injector
* @param bool $enableRuntimeChannelDefinition A flag whether to enable a such behavior when there is no
* explicit channel adapter definition: `return $this->queue->withAdapter($this->adapter->withChannel($channel)`
* When this flag is set to false, only explicit definitions from the $definition parameter are used.
Expand Down Expand Up @@ -71,10 +68,7 @@ public function get(string $channel = self::DEFAULT_CHANNEL_NAME): QueueInterfac
}

/**
* @param string $channel
*
* @throws ChannelIncorrectlyConfigured
*
* @return QueueInterface
*/
private function create(string $channel): QueueInterface
Expand Down
7 changes: 0 additions & 7 deletions src/Worker/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ public function __construct(
}

/**
* @param MessageInterface $message
* @param QueueInterface $queue
*
* @throws Throwable
*
* @return MessageInterface
*/
public function process(MessageInterface $message, QueueInterface $queue): MessageInterface
{
Expand Down Expand Up @@ -94,8 +89,6 @@ private function getHandler(string $name): ?callable
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*
* @return callable|null
*/
private function prepare(callable|object|array|string|null $definition): callable|null
{
Expand Down
4 changes: 0 additions & 4 deletions tests/Unit/JobStatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public function getStatusPairs(): array

/**
* @dataProvider getStatusPairs
*
* @param string $statusName
* @param string $positiveMethod
* @param array $negatives
*/
public function testInstanceValue(string $statusName, string $positiveMethod, array $negatives): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ public function dataBase(): array

/**
* @dataProvider dataBase
*
* @param mixed $definition
* @param string $expected
*/
public function testBase(mixed $definition, string $expected): void
{
Expand All @@ -55,8 +52,6 @@ public function dataUnknownDefinition(): array

/**
* @dataProvider dataUnknownDefinition
*
* @param mixed $definition
*/
public function testUnknownDefinition(mixed $definition): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Middleware/Consume/MiddlewareDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function handleConsume(ConsumeRequest $request): ConsumeRequest
private function createDispatcher(
ContainerInterface $container = null,
): ConsumeMiddlewareDispatcher {
$container = $container ?? $this->createContainer([AdapterInterface::class => new FakeAdapter()]);
$container ??= $this->createContainer([AdapterInterface::class => new FakeAdapter()]);
$callableFactory = new CallableFactory($container);

return new ConsumeMiddlewareDispatcher(
Expand Down
Loading

0 comments on commit 2639357

Please sign in to comment.