Skip to content

Commit 059365b

Browse files
authored
Cleaning things up (#175)
* 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 * Remove ReturnTypeWillChange attribute Restore check * Re-introduce @return in dockblocks * Fix rector rule
1 parent acfc65b commit 059365b

32 files changed

+194
-174
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\DeadCode\Rector\ClassMethod\RemoveNullTagValueNodeRector;
8+
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
9+
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
10+
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector;
11+
use Rector\ValueObject\PhpVersion;
12+
13+
return RectorConfig::configure()
14+
->withPhpVersion(PhpVersion::PHP_82)
15+
->withPaths([
16+
__DIR__ . '/src',
17+
__DIR__ . '/tests',
18+
])
19+
->withSkip([
20+
DisallowedEmptyRuleFixerRector::class,
21+
SimplifyIfElseToTernaryRector::class,
22+
MakeInheritedMethodVisibilitySameAsParentRector::class,
23+
RemoveNullTagValueNodeRector::class,
24+
RemoveUselessReturnTagRector::class,
25+
DocblockReturnArrayFromDirectArrayInstanceRector::class => [
26+
__DIR__ . '/src/Mailer/Transport/QueueTransport.php',
27+
],
28+
])
29+
->withParallel()
30+
->withPreparedSets(
31+
deadCode: true,
32+
codeQuality: true,
33+
codingStyle: true,
34+
typeDeclarationDocblocks: true,
35+
);

src/Command/PurgeFailedCommand.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ public function getOptionParser(): ConsoleOptionParser
7272
/**
7373
* @param \Cake\Console\Arguments $args Arguments
7474
* @param \Cake\Console\ConsoleIo $io ConsoleIo
75-
* @return void
75+
* @return int
7676
*/
77-
public function execute(Arguments $args, ConsoleIo $io): void
77+
public function execute(Arguments $args, ConsoleIo $io): int
7878
{
7979
/** @var \Cake\Queue\Model\Table\FailedJobsTable $failedJobsTable */
8080
$failedJobsTable = $this->getTableLocator()->get('Cake/Queue.FailedJobs');
@@ -108,21 +108,23 @@ public function execute(Arguments $args, ConsoleIo $io): void
108108
if (!$deletingCount) {
109109
$io->out('0 jobs found.');
110110

111-
return;
111+
return self::CODE_SUCCESS;
112112
}
113113

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

117117
if ($confirmed !== 'y') {
118-
return;
118+
return self::CODE_SUCCESS;
119119
}
120120
}
121121

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

124124
$failedJobsTable->deleteManyOrFail($jobsToDelete);
125125

126-
$io->success("{$deletingCount} jobs deleted.");
126+
$io->success($deletingCount . ' jobs deleted.');
127+
128+
return self::CODE_SUCCESS;
127129
}
128130
}

src/Command/RequeueCommand.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ public function getOptionParser(): ConsoleOptionParser
7474
/**
7575
* @param \Cake\Console\Arguments $args Arguments
7676
* @param \Cake\Console\ConsoleIo $io ConsoleIo
77-
* @return void
77+
* @return int
7878
*/
79-
public function execute(Arguments $args, ConsoleIo $io): void
79+
public function execute(Arguments $args, ConsoleIo $io): int
8080
{
8181
/** @var \Cake\Queue\Model\Table\FailedJobsTable $failedJobsTable */
8282
$failedJobsTable = $this->getTableLocator()->get('Cake/Queue.FailedJobs');
@@ -110,26 +110,26 @@ public function execute(Arguments $args, ConsoleIo $io): void
110110
if (!$requeueingCount) {
111111
$io->out('0 jobs found.');
112112

113-
return;
113+
return self::CODE_SUCCESS;
114114
}
115115

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

119119
if ($confirmed !== 'y') {
120-
return;
120+
return self::CODE_SUCCESS;
121121
}
122122
}
123123

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

126126
$succeededCount = 0;
127127
$failedCount = 0;
128128

129129
/** @var array<\Cake\Queue\Model\Entity\FailedJob> $jobsToRequeue */
130130
$jobsToRequeue = $jobsToRequeueQuery->all();
131131
foreach ($jobsToRequeue as $failedJob) {
132-
$io->verbose("Requeueing FailedJob with ID {$failedJob->id}.");
132+
$io->verbose(sprintf('Requeueing FailedJob with ID %d.', $failedJob->id));
133133
try {
134134
QueueManager::push(
135135
[$failedJob->class, $failedJob->method],
@@ -145,19 +145,21 @@ public function execute(Arguments $args, ConsoleIo $io): void
145145

146146
$succeededCount++;
147147
} catch (Exception $e) {
148-
$io->err("Exception occurred while requeueing FailedJob with ID {$failedJob->id}");
148+
$io->err('Exception occurred while requeueing FailedJob with ID ' . $failedJob->id);
149149
$io->err((string)$e);
150150

151151
$failedCount++;
152152
}
153153
}
154154

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

159-
if ($succeededCount) {
160-
$io->success("{$succeededCount} jobs requeued.");
159+
if ($succeededCount !== 0) {
160+
$io->success($succeededCount . ' jobs requeued.');
161161
}
162+
163+
return self::CODE_SUCCESS;
162164
}
163165
}

src/Command/WorkerCommand.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,12 @@
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
/**
@@ -138,12 +133,12 @@ protected function getQueueExtension(Arguments $args, LoggerInterface $logger):
138133
$limitAttempsExtension,
139134
];
140135

141-
if (!is_null($args->getOption('max-jobs'))) {
136+
if ($args->getOption('max-jobs') !== null) {
142137
$maxJobs = (int)$args->getOption('max-jobs');
143138
$extensions[] = new LimitConsumedMessagesExtension($maxJobs);
144139
}
145140

146-
if (!is_null($args->getOption('max-runtime'))) {
141+
if ($args->getOption('max-runtime') !== null) {
147142
$endTime = new DateTime(sprintf('+%d seconds', (int)$args->getOption('max-runtime')));
148143
$extensions[] = new LimitConsumptionTimeExtension($endTime);
149144
}
@@ -187,12 +182,12 @@ protected function getProcessor(Arguments $args, ConsoleIo $io, LoggerInterface
187182
$processorClass = $config['processor'] ?? Processor::class;
188183

189184
if (!class_exists($processorClass)) {
190-
$io->error(sprintf(sprintf('Processor class %s not found', $processorClass)));
185+
$io->error(sprintf('Processor class %s not found', $processorClass));
191186
$this->abort();
192187
}
193188

194189
if (!is_subclass_of($processorClass, InteropProcessor::class)) {
195-
$io->error(sprintf(sprintf('Processor class %s must implement Interop\Queue\Processor', $processorClass)));
190+
$io->error(sprintf('Processor class %s must implement Interop\Queue\Processor', $processorClass));
196191
$this->abort();
197192
}
198193

@@ -231,10 +226,11 @@ public function execute(Arguments $args, ConsoleIo $io): int
231226
$processor->getEventManager()->on($listener);
232227
}
233228
}
229+
234230
$client = QueueManager::engine($config);
235231
$queue = $args->getOption('queue')
236232
? (string)$args->getOption('queue')
237-
: Configure::read("Queue.{$config}.queue", 'default');
233+
: Configure::read(sprintf('Queue.%s.queue', $config), 'default');
238234
$processorName = $args->getOption('processor') ? (string)$args->getOption('processor') : 'default';
239235

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

src/Consumption/LimitAttemptsExtension.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,12 @@ 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
32-
*/
33-
protected ?int $maxAttempts = null;
34-
35-
/**
36-
* @param int|null $maxAttempts The maximum number of times a job may be attempted.
28+
* @param int|null $maxAttempts The maximum number of times a job may be attempted. $maxAttempts defined on a Job will override this value.
3729
* @return void
3830
*/
39-
public function __construct(?int $maxAttempts = null)
40-
{
41-
$this->maxAttempts = $maxAttempts;
31+
public function __construct(
32+
protected readonly ?int $maxAttempts = null,
33+
) {
4234
}
4335

4436
/**

src/Consumption/LimitConsumedMessagesExtension.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,15 @@
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.
25+
* @return void
3326
*/
34-
public function __construct(int $messageLimit)
35-
{
36-
$this->messageLimit = $messageLimit;
27+
public function __construct(
28+
protected readonly int $messageLimit,
29+
) {
3730
}
3831

3932
/**

src/Consumption/RemoveUniqueJobIdFromCacheExtension.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,13 @@
1111

1212
class RemoveUniqueJobIdFromCacheExtension implements MessageResultExtensionInterface
1313
{
14-
/**
15-
* Cache engine name.
16-
*
17-
* @var string
18-
*/
19-
protected string $cache;
20-
2114
/**
2215
* @param string $cache Cache engine name.
2316
* @return void
2417
*/
25-
public function __construct(string $cache)
26-
{
27-
$this->cache = $cache;
18+
public function __construct(
19+
protected readonly string $cache,
20+
) {
2821
}
2922

3023
/**

src/Job/MailerJob.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public function execute(Message $message): ?string
4141

4242
try {
4343
$mailer = $this->getMailer($mailerName, $mailerConfig);
44-
} catch (MissingMailerException $e) {
44+
} catch (MissingMailerException $missingMailerException) {
4545
return Processor::REJECT;
4646
}
4747

4848
try {
4949
$mailer->send($action, $args, $headers);
50-
} catch (BadMethodCallException $e) {
50+
} catch (BadMethodCallException $badMethodCallException) {
5151
return Processor::REJECT;
5252
}
5353

0 commit comments

Comments
 (0)