Skip to content

Commit 4326ba7

Browse files
committed
Cleanup up
- Use constructor promotion where possible - Use readonly properties where possible - Add Rectoring - Update some - Use spread operator wher possible - Use strict comparisons where possible - Use null coalescing operator where possible
1 parent 8c31f73 commit 4326ba7

39 files changed

+159
-239
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
"phpstan": "tools/phpstan analyse",
6767
"stan-baseline": "tools/phpstan --generate-baseline",
6868
"stan-setup": "phive install",
69+
"rector-setup": "cp composer.json composer.backup && composer require --dev rector/rector:\"^2.2\" && mv composer.backup composer.json",
70+
"rector-check": "vendor/bin/rector process --dry-run",
71+
"rector-fix": "vendor/bin/rector process",
6972
"test": "phpunit",
7073
"test-coverage": "phpunit --coverage-clover=clover.xml"
7174
}

rector.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
5+
use Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector;
6+
use Rector\Config\RectorConfig;
7+
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
8+
use Rector\ValueObject\PhpVersion;
9+
10+
return RectorConfig::configure()
11+
->withPhpVersion(PhpVersion::PHP_82)
12+
->withPaths([
13+
__DIR__ . '/src',
14+
__DIR__ . '/tests',
15+
])
16+
->withSkip([
17+
DisallowedEmptyRuleFixerRector::class,
18+
SimplifyIfElseToTernaryRector::class,
19+
MakeInheritedMethodVisibilitySameAsParentRector::class,
20+
])
21+
->withParallel()
22+
->withPreparedSets(
23+
deadCode: true,
24+
codeQuality: true,
25+
codingStyle: true,
26+
);

src/Command/JobCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public function templateData(Arguments $arguments): array
6969
* Gets the option parser instance and configures it.
7070
*
7171
* @param \Cake\Console\ConsoleOptionParser $parser The parser to update.
72-
* @return \Cake\Console\ConsoleOptionParser
7372
*/
7473
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
7574
{

src/Command/PurgeFailedCommand.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ class PurgeFailedCommand extends Command
2828

2929
/**
3030
* Get the command name.
31-
*
32-
* @return string
3331
*/
3432
public static function defaultName(): string
3533
{
@@ -38,8 +36,6 @@ public static function defaultName(): string
3836

3937
/**
4038
* Gets the option parser instance and configures it.
41-
*
42-
* @return \Cake\Console\ConsoleOptionParser
4339
*/
4440
public function getOptionParser(): ConsoleOptionParser
4541
{
@@ -72,9 +68,8 @@ public function getOptionParser(): ConsoleOptionParser
7268
/**
7369
* @param \Cake\Console\Arguments $args Arguments
7470
* @param \Cake\Console\ConsoleIo $io ConsoleIo
75-
* @return void
7671
*/
77-
public function execute(Arguments $args, ConsoleIo $io): void
72+
public function execute(Arguments $args, ConsoleIo $io): int
7873
{
7974
/** @var \Cake\Queue\Model\Table\FailedJobsTable $failedJobsTable */
8075
$failedJobsTable = $this->getTableLocator()->get('Cake/Queue.FailedJobs');
@@ -108,21 +103,23 @@ public function execute(Arguments $args, ConsoleIo $io): void
108103
if (!$deletingCount) {
109104
$io->out('0 jobs found.');
110105

111-
return;
106+
return self::CODE_SUCCESS;
112107
}
113108

114109
if (!$args->getOption('force')) {
115-
$confirmed = $io->askChoice("Delete {$deletingCount} jobs?", ['y', 'n'], 'n');
110+
$confirmed = $io->askChoice(sprintf('Delete %s jobs?', $deletingCount), ['y', 'n'], 'n');
116111

117112
if ($confirmed !== 'y') {
118-
return;
113+
return self::CODE_SUCCESS;
119114
}
120115
}
121116

122-
$io->out("Deleting {$deletingCount} jobs.");
117+
$io->out(sprintf('Deleting %s jobs.', $deletingCount));
123118

124119
$failedJobsTable->deleteManyOrFail($jobsToDelete);
125120

126-
$io->success("{$deletingCount} jobs deleted.");
121+
$io->success($deletingCount . ' jobs deleted.');
122+
123+
return self::CODE_SUCCESS;
127124
}
128125
}

src/Command/RequeueCommand.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ class RequeueCommand extends Command
3030

3131
/**
3232
* Get the command name.
33-
*
34-
* @return string
3533
*/
3634
public static function defaultName(): string
3735
{
@@ -40,8 +38,6 @@ public static function defaultName(): string
4038

4139
/**
4240
* Gets the option parser instance and configures it.
43-
*
44-
* @return \Cake\Console\ConsoleOptionParser
4541
*/
4642
public function getOptionParser(): ConsoleOptionParser
4743
{
@@ -74,9 +70,8 @@ public function getOptionParser(): ConsoleOptionParser
7470
/**
7571
* @param \Cake\Console\Arguments $args Arguments
7672
* @param \Cake\Console\ConsoleIo $io ConsoleIo
77-
* @return void
7873
*/
79-
public function execute(Arguments $args, ConsoleIo $io): void
74+
public function execute(Arguments $args, ConsoleIo $io): int
8075
{
8176
/** @var \Cake\Queue\Model\Table\FailedJobsTable $failedJobsTable */
8277
$failedJobsTable = $this->getTableLocator()->get('Cake/Queue.FailedJobs');
@@ -110,26 +105,26 @@ public function execute(Arguments $args, ConsoleIo $io): void
110105
if (!$requeueingCount) {
111106
$io->out('0 jobs found.');
112107

113-
return;
108+
return self::CODE_SUCCESS;
114109
}
115110

116111
if (!$args->getOption('force')) {
117-
$confirmed = $io->askChoice("Requeue {$requeueingCount} jobs?", ['y', 'n'], 'n');
112+
$confirmed = $io->askChoice(sprintf('Requeue %s jobs?', $requeueingCount), ['y', 'n'], 'n');
118113

119114
if ($confirmed !== 'y') {
120-
return;
115+
return self::CODE_SUCCESS;
121116
}
122117
}
123118

124-
$io->out("Requeueing {$requeueingCount} jobs.");
119+
$io->out(sprintf('Requeueing %s jobs.', $requeueingCount));
125120

126121
$succeededCount = 0;
127122
$failedCount = 0;
128123

129124
/** @var array<\Cake\Queue\Model\Entity\FailedJob> $jobsToRequeue */
130125
$jobsToRequeue = $jobsToRequeueQuery->all();
131126
foreach ($jobsToRequeue as $failedJob) {
132-
$io->verbose("Requeueing FailedJob with ID {$failedJob->id}.");
127+
$io->verbose(sprintf('Requeueing FailedJob with ID %d.', $failedJob->id));
133128
try {
134129
QueueManager::push(
135130
[$failedJob->class, $failedJob->method],
@@ -145,19 +140,21 @@ public function execute(Arguments $args, ConsoleIo $io): void
145140

146141
$succeededCount++;
147142
} catch (Exception $e) {
148-
$io->err("Exception occurred while requeueing FailedJob with ID {$failedJob->id}");
143+
$io->err('Exception occurred while requeueing FailedJob with ID ' . $failedJob->id);
149144
$io->err((string)$e);
150145

151146
$failedCount++;
152147
}
153148
}
154149

155-
if ($failedCount) {
156-
$io->err("Failed to requeue {$failedCount} jobs.");
150+
if ($failedCount !== 0) {
151+
$io->err(sprintf('Failed to requeue %d jobs.', $failedCount));
157152
}
158153

159-
if ($succeededCount) {
160-
$io->success("{$succeededCount} jobs requeued.");
154+
if ($succeededCount !== 0) {
155+
$io->success($succeededCount . ' jobs requeued.');
161156
}
157+
158+
return self::CODE_SUCCESS;
162159
}
163160
}

src/Command/WorkerCommand.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,16 @@
4343
*/
4444
class WorkerCommand extends Command
4545
{
46-
/**
47-
* @var \Cake\Core\ContainerInterface|null
48-
*/
49-
protected ?ContainerInterface $container = null;
50-
5146
/**
5247
* @param \Cake\Core\ContainerInterface|null $container DI container instance
5348
*/
54-
public function __construct(?ContainerInterface $container = null)
55-
{
56-
$this->container = $container;
49+
public function __construct(
50+
protected readonly ?ContainerInterface $container = null,
51+
) {
5752
}
5853

5954
/**
6055
* Get the command name.
61-
*
62-
* @return string
6356
*/
6457
public static function defaultName(): string
6558
{
@@ -68,8 +61,6 @@ public static function defaultName(): string
6861

6962
/**
7063
* Gets the option parser instance and configures it.
71-
*
72-
* @return \Cake\Console\ConsoleOptionParser
7364
*/
7465
public function getOptionParser(): ConsoleOptionParser
7566
{
@@ -122,7 +113,6 @@ public function getOptionParser(): ConsoleOptionParser
122113
*
123114
* @param \Cake\Console\Arguments $args Arguments
124115
* @param \Psr\Log\LoggerInterface $logger Logger instance.
125-
* @return \Enqueue\Consumption\ExtensionInterface
126116
*/
127117
protected function getQueueExtension(Arguments $args, LoggerInterface $logger): ExtensionInterface
128118
{
@@ -138,12 +128,12 @@ protected function getQueueExtension(Arguments $args, LoggerInterface $logger):
138128
$limitAttempsExtension,
139129
];
140130

141-
if (!is_null($args->getOption('max-jobs'))) {
131+
if ($args->getOption('max-jobs') !== null) {
142132
$maxJobs = (int)$args->getOption('max-jobs');
143133
$extensions[] = new LimitConsumedMessagesExtension($maxJobs);
144134
}
145135

146-
if (!is_null($args->getOption('max-runtime'))) {
136+
if ($args->getOption('max-runtime') !== null) {
147137
$endTime = new DateTime(sprintf('+%d seconds', (int)$args->getOption('max-runtime')));
148138
$extensions[] = new LimitConsumptionTimeExtension($endTime);
149139
}
@@ -159,7 +149,6 @@ protected function getQueueExtension(Arguments $args, LoggerInterface $logger):
159149
* Creates and returns a LoggerInterface object
160150
*
161151
* @param \Cake\Console\Arguments $args Arguments
162-
* @return \Psr\Log\LoggerInterface
163152
*/
164153
protected function getLogger(Arguments $args): LoggerInterface
165154
{
@@ -177,7 +166,6 @@ protected function getLogger(Arguments $args): LoggerInterface
177166
* @param \Cake\Console\Arguments $args Arguments
178167
* @param \Cake\Console\ConsoleIo $io ConsoleIo
179168
* @param \Psr\Log\LoggerInterface $logger Logger instance
180-
* @return \Interop\Queue\Processor
181169
*/
182170
protected function getProcessor(Arguments $args, ConsoleIo $io, LoggerInterface $logger): InteropProcessor
183171
{
@@ -187,12 +175,12 @@ protected function getProcessor(Arguments $args, ConsoleIo $io, LoggerInterface
187175
$processorClass = $config['processor'] ?? Processor::class;
188176

189177
if (!class_exists($processorClass)) {
190-
$io->error(sprintf(sprintf('Processor class %s not found', $processorClass)));
178+
$io->error(sprintf('Processor class %s not found', $processorClass));
191179
$this->abort();
192180
}
193181

194182
if (!is_subclass_of($processorClass, InteropProcessor::class)) {
195-
$io->error(sprintf(sprintf('Processor class %s must implement Interop\Queue\Processor', $processorClass)));
183+
$io->error(sprintf('Processor class %s must implement Interop\Queue\Processor', $processorClass));
196184
$this->abort();
197185
}
198186

@@ -202,7 +190,6 @@ protected function getProcessor(Arguments $args, ConsoleIo $io, LoggerInterface
202190
/**
203191
* @param \Cake\Console\Arguments $args Arguments
204192
* @param \Cake\Console\ConsoleIo $io ConsoleIo
205-
* @return int
206193
*/
207194
public function execute(Arguments $args, ConsoleIo $io): int
208195
{
@@ -231,10 +218,11 @@ public function execute(Arguments $args, ConsoleIo $io): int
231218
$processor->getEventManager()->on($listener);
232219
}
233220
}
221+
234222
$client = QueueManager::engine($config);
235223
$queue = $args->getOption('queue')
236224
? (string)$args->getOption('queue')
237-
: Configure::read("Queue.{$config}.queue", 'default');
225+
: Configure::read(sprintf('Queue.%s.queue', $config), 'default');
238226
$processorName = $args->getOption('processor') ? (string)$args->getOption('processor') : 'default';
239227

240228
$client->bindTopic($queue, $processor, $processorName);

src/Consumption/LimitAttemptsExtension.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,15 @@ class LimitAttemptsExtension implements MessageResultExtensionInterface
2525
public const ATTEMPTS_PROPERTY = 'attempts';
2626

2727
/**
28-
* The maximum number of times a job may be attempted. $maxAttempts defined on a
29-
* Job will override this value.
30-
*
31-
* @var int|null
28+
* @param int|null $maxAttempts The maximum number of times a job may be attempted. $maxAttempts defined on a Job will override this value.
3229
*/
33-
protected ?int $maxAttempts = null;
34-
35-
/**
36-
* @param int|null $maxAttempts The maximum number of times a job may be attempted.
37-
* @return void
38-
*/
39-
public function __construct(?int $maxAttempts = null)
40-
{
41-
$this->maxAttempts = $maxAttempts;
30+
public function __construct(
31+
protected readonly ?int $maxAttempts = null,
32+
) {
4233
}
4334

4435
/**
4536
* @param \Enqueue\Consumption\Context\MessageResult $context The result of the message after it was processed.
46-
* @return void
4737
*/
4838
public function onResult(MessageResult $context): void
4939
{

src/Consumption/LimitConsumedMessagesExtension.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,21 @@
1818
*/
1919
class LimitConsumedMessagesExtension implements PreConsumeExtensionInterface, PostConsumeExtensionInterface
2020
{
21-
/**
22-
* @var int
23-
*/
24-
protected int $messageLimit;
25-
26-
/**
27-
* @var int
28-
*/
2921
protected int $messageConsumed = 0;
3022

3123
/**
3224
* @param int $messageLimit The number of messages to process before exiting.
3325
*/
34-
public function __construct(int $messageLimit)
35-
{
36-
$this->messageLimit = $messageLimit;
26+
public function __construct(
27+
protected readonly int $messageLimit,
28+
) {
3729
}
3830

3931
/**
4032
* Executed at every new cycle before calling SubscriptionConsumer::consume method.
4133
* The consumption could be interrupted at this step.
4234
*
4335
* @param \Enqueue\Consumption\Context\PreConsume $context The PreConsume context.
44-
* @return void
4536
*/
4637
public function onPreConsume(PreConsume $context): void
4738
{
@@ -56,7 +47,6 @@ public function onPreConsume(PreConsume $context): void
5647
* The consumption could be interrupted at this point.
5748
*
5849
* @param \Enqueue\Consumption\Context\PostConsume $context The PostConsume context.
59-
* @return void
6050
*/
6151
public function onPostConsume(PostConsume $context): void
6252
{
@@ -71,7 +61,6 @@ public function onPostConsume(PostConsume $context): void
7161
* Check if the consumer should be stopped.
7262
*
7363
* @param \Psr\Log\LoggerInterface $logger The logger where messages will be logged.
74-
* @return bool
7564
*/
7665
protected function shouldBeStopped(LoggerInterface $logger): bool
7766
{

0 commit comments

Comments
 (0)