Skip to content

Commit b9df900

Browse files
kenjismichalsn
authored andcommitted
refactor: replace loose comparisons
1 parent ae68bd8 commit b9df900

7 files changed

Lines changed: 36 additions & 13 deletions

File tree

src/Commands/QueueClear.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class QueueClear extends BaseCommand
5050
public function run(array $params)
5151
{
5252
// Read params
53-
if (! $queue = array_shift($params)) {
53+
$queue = array_shift($params);
54+
if ($queue === null) {
5455
CLI::error('The queueName is not specified.');
5556

5657
return EXIT_ERROR;

src/Commands/QueueForget.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class QueueForget extends BaseCommand
5050
public function run(array $params)
5151
{
5252
// Read params
53-
if (! $id = array_shift($params)) {
53+
$id = array_shift($params);
54+
if ($id === null) {
5455
CLI::error('The ID of the failed job is not specified.');
5556

5657
return EXIT_ERROR;

src/Commands/QueueRetry.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class QueueRetry extends BaseCommand
5959
public function run(array $params)
6060
{
6161
// Read params
62-
if (! $id = array_shift($params)) {
62+
$id = array_shift($params);
63+
if ($id === null) {
6364
CLI::error('The ID of the failed job is not specified.');
6465

6566
return EXIT_ERROR;

src/Commands/QueueStop.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class QueueStop extends BaseCommand
5858
public function run(array $params)
5959
{
6060
// Read params
61-
if (! $queue = array_shift($params)) {
61+
$queue = array_shift($params);
62+
if ($queue === null) {
6263
CLI::error('The queueName is not specified.');
6364

6465
return EXIT_ERROR;

src/Commands/QueueWork.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public function run(array $params)
8080
$waiting = false;
8181

8282
// Read queue name from params
83-
if (! $queue = array_shift($params)) {
83+
$queue = array_shift($params);
84+
if ($queue === null) {
8485
CLI::error('The queueName is not specified.');
8586

8687
return EXIT_ERROR;

src/Handlers/PredisHandler.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,22 @@ public function pop(string $queue, array $priorities): ?QueueJob
6262
$now = Time::now()->timestamp;
6363

6464
foreach ($priorities as $priority) {
65-
if ($tasks = $this->predis->zrangebyscore("queues:{$queue}:{$priority}", '-inf', $now, ['LIMIT' => [0, 1]])) {
66-
if ($this->predis->zrem("queues:{$queue}:{$priority}", ...$tasks)) {
65+
$tasks = $this->predis->zrangebyscore(
66+
"queues:{$queue}:{$priority}",
67+
'-inf',
68+
$now,
69+
['LIMIT' => [0, 1]]
70+
);
71+
if ($tasks !== []) {
72+
$removed = $this->predis->zrem("queues:{$queue}:{$priority}", ...$tasks);
73+
if ($removed !== 0) {
6774
break;
6875
}
6976
$tasks = [];
7077
}
7178
}
7279

73-
if (empty($tasks[0])) {
80+
if ($tasks === []) {
7481
return null;
7582
}
7683

@@ -93,7 +100,11 @@ public function later(QueueJob $queueJob, int $seconds): bool
93100
$queueJob->status = Status::PENDING->value;
94101
$queueJob->available_at = Time::now()->addSeconds($seconds)->timestamp;
95102

96-
if ($result = $this->predis->zadd("queues:{$queueJob->queue}:{$queueJob->priority}", [json_encode($queueJob) => $queueJob->available_at->timestamp])) {
103+
$result = $this->predis->zadd(
104+
"queues:{$queueJob->queue}:{$queueJob->priority}",
105+
[json_encode($queueJob) => $queueJob->available_at->timestamp]
106+
);
107+
if ($result !== 0) {
97108
$this->predis->hdel("queues:{$queueJob->queue}::reserved", [$queueJob->id]);
98109
}
99110

@@ -131,14 +142,16 @@ public function done(QueueJob $queueJob, bool $keepJob): bool
131142
public function clear(?string $queue = null): bool
132143
{
133144
if ($queue !== null) {
134-
if ($keys = $this->predis->keys("queues:{$queue}:*")) {
145+
$keys = $this->predis->keys("queues:{$queue}:*");
146+
if ($keys !== []) {
135147
return $this->predis->del($keys) > 0;
136148
}
137149

138150
return true;
139151
}
140152

141-
if ($keys = $this->predis->keys('queues:*')) {
153+
$keys = $this->predis->keys('queues:*');
154+
if ($keys !== []) {
142155
return $this->predis->del($keys) > 0;
143156
}
144157

src/Handlers/RedisHandler.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function pop(string $queue, array $priorities): ?QueueJob
8989
}
9090
}
9191

92-
if (empty($tasks[0])) {
92+
if ($tasks === []) {
9393
return null;
9494
}
9595

@@ -114,7 +114,12 @@ public function later(QueueJob $queueJob, int $seconds): bool
114114
$queueJob->status = Status::PENDING->value;
115115
$queueJob->available_at = Time::now()->addSeconds($seconds)->timestamp;
116116

117-
if ($result = (int) $this->redis->zAdd("queues:{$queueJob->queue}:{$queueJob->priority}", $queueJob->available_at->timestamp, json_encode($queueJob))) {
117+
$result = (int) $this->redis->zAdd(
118+
"queues:{$queueJob->queue}:{$queueJob->priority}",
119+
$queueJob->available_at->timestamp,
120+
json_encode($queueJob)
121+
);
122+
if ($result !== 0) {
118123
$this->redis->hDel("queues:{$queueJob->queue}::reserved", (string) $queueJob->id);
119124
}
120125

0 commit comments

Comments
 (0)