Skip to content

Commit 10e92b9

Browse files
Merge pull request #31 from daniellienert/task/set-types
!!! TASK: Add (return) type hints to all public methods
2 parents acb68e9 + 322ec0c commit 10e92b9

10 files changed

Lines changed: 74 additions & 76 deletions

File tree

Classes/Job/JobInterface.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
* source code.
1212
*/
1313

14-
use Neos\Flow\Annotations as Flow;
1514
use Flowpack\JobQueue\Common\Queue\Message;
1615
use Flowpack\JobQueue\Common\Queue\QueueInterface;
1716

@@ -27,14 +26,14 @@ interface JobInterface
2726
*
2827
* @param QueueInterface $queue
2928
* @param Message $message The original message
30-
* @return boolean TRUE if the job was executed successfully and the message should be finished
29+
* @return bool TRUE if the job was executed successfully and the message should be finished
3130
*/
32-
public function execute(QueueInterface $queue, Message $message);
31+
public function execute(QueueInterface $queue, Message $message): bool;
3332

3433
/**
3534
* Get a readable label for the job
3635
*
3736
* @return string A label for the job
3837
*/
39-
public function getLabel();
38+
public function getLabel(): string;
4039
}

Classes/Job/JobManager.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class JobManager
6666
* @return void
6767
* @api
6868
*/
69-
public function queue($queueName, JobInterface $job, array $options = [])
69+
public function queue(string $queueName, JobInterface $job, array $options = []): void
7070
{
7171
$queue = $this->queueManager->getQueue($queueName);
7272

@@ -85,7 +85,7 @@ public function queue($queueName, JobInterface $job, array $options = [])
8585
* @throws \Exception
8686
* @api
8787
*/
88-
public function waitAndExecute($queueName, $timeout = null)
88+
public function waitAndExecute(string $queueName, $timeout = null): ?Message
8989
{
9090
$queue = $this->queueManager->getQueue($queueName);
9191
$message = $queue->waitAndReserve($timeout);
@@ -133,7 +133,7 @@ public function waitAndExecute($queueName, $timeout = null)
133133
* @throws JobQueueException
134134
* @internal This method has to be public so that it can be run from the command handler (when "executeIsolated" is set). It is not meant to be called from "user land"
135135
*/
136-
public function executeJobForMessage(QueueInterface $queue, Message $message)
136+
public function executeJobForMessage(QueueInterface $queue, Message $message): void
137137
{
138138
// TODO stabilize unserialize() call (maybe using PHPs unserialize_callback_func directive)
139139
$job = unserialize($message->getPayload());
@@ -153,7 +153,7 @@ public function executeJobForMessage(QueueInterface $queue, Message $message)
153153
* @return JobInterface[]
154154
* @api
155155
*/
156-
public function peek($queueName, $limit = 1)
156+
public function peek(string $queueName, int $limit = 1): array
157157
{
158158
$queue = $this->queueManager->getQueue($queueName);
159159
$messages = $queue->peek($limit);
@@ -174,7 +174,7 @@ public function peek($queueName, $limit = 1)
174174
* @Flow\Signal
175175
* @api
176176
*/
177-
protected function emitMessageSubmitted(QueueInterface $queue, $messageId, $payload, array $options = [])
177+
protected function emitMessageSubmitted(QueueInterface $queue, $messageId, $payload, array $options = []): void
178178
{
179179
}
180180

@@ -186,7 +186,7 @@ protected function emitMessageSubmitted(QueueInterface $queue, $messageId, $payl
186186
* @Flow\Signal
187187
* @api
188188
*/
189-
protected function emitMessageTimeout(QueueInterface $queue)
189+
protected function emitMessageTimeout(QueueInterface $queue): void
190190
{
191191
}
192192

@@ -199,7 +199,7 @@ protected function emitMessageTimeout(QueueInterface $queue)
199199
* @Flow\Signal
200200
* @api
201201
*/
202-
protected function emitMessageReserved(QueueInterface $queue, Message $message)
202+
protected function emitMessageReserved(QueueInterface $queue, Message $message): void
203203
{
204204
}
205205

@@ -212,7 +212,7 @@ protected function emitMessageReserved(QueueInterface $queue, Message $message)
212212
* @Flow\Signal
213213
* @api
214214
*/
215-
protected function emitMessageFinished(QueueInterface $queue, Message $message)
215+
protected function emitMessageFinished(QueueInterface $queue, Message $message): void
216216
{
217217
}
218218

@@ -227,7 +227,7 @@ protected function emitMessageFinished(QueueInterface $queue, Message $message)
227227
* @Flow\Signal
228228
* @api
229229
*/
230-
protected function emitMessageReleased(QueueInterface $queue, Message $message, array $releaseOptions, \Exception $jobExecutionException = null)
230+
protected function emitMessageReleased(QueueInterface $queue, Message $message, array $releaseOptions, \Exception $jobExecutionException = null): void
231231
{
232232
}
233233

@@ -241,7 +241,7 @@ protected function emitMessageReleased(QueueInterface $queue, Message $message,
241241
* @Flow\Signal
242242
* @api
243243
*/
244-
protected function emitMessageFailed(QueueInterface $queue, Message $message, \Exception $jobExecutionException = null)
244+
protected function emitMessageFailed(QueueInterface $queue, Message $message, \Exception $jobExecutionException = null): void
245245
{
246246
}
247247

Classes/Job/StaticMethodCallJob.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
1717
use Flowpack\JobQueue\Common\Queue\Message;
1818
use Flowpack\JobQueue\Common\Queue\QueueInterface;
19-
use Neos\Utility\TypeHandling;
2019

2120
/**
2221
* Static method call job
@@ -73,7 +72,7 @@ public function __construct($className, $methodName, array $arguments)
7372
* @return boolean TRUE If the execution was successful
7473
* @throws \Exception
7574
*/
76-
public function execute(QueueInterface $queue, Message $message)
75+
public function execute(QueueInterface $queue, Message $message): bool
7776
{
7877
$service = $this->objectManager->get($this->className);
7978
$this->deferMethodCallAspect->setProcessingJob(true);
@@ -91,7 +90,7 @@ public function execute(QueueInterface $queue, Message $message)
9190
/**
9291
* @return string
9392
*/
94-
public function getLabel()
93+
public function getLabel(): string
9594
{
9695
$arguments = array_map([VariableDumper::class, 'dumpValue'], $this->arguments);
9796
return sprintf('%s::%s(%s)', $this->className, $this->methodName, implode(', ', $arguments));

Classes/Queue/FakeQueue.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class FakeQueue implements QueueInterface
4949
* @param string $name
5050
* @param array $options
5151
*/
52-
public function __construct($name, array $options = [])
52+
public function __construct(string $name, array $options = [])
5353
{
5454
$this->name = $name;
5555
if (isset($options['async']) && $options['async'] === true) {
@@ -60,7 +60,7 @@ public function __construct($name, array $options = [])
6060
/**
6161
* @inheritdoc
6262
*/
63-
public function setUp()
63+
public function setUp(): void
6464
{
6565
// The FakeQueue does not require any setup but we use it to verify the options
6666
if ($this->async && !method_exists(Scripts::class, 'executeCommandAsync')) {
@@ -71,15 +71,15 @@ public function setUp()
7171
/**
7272
* @inheritdoc
7373
*/
74-
public function getName()
74+
public function getName(): string
7575
{
7676
return $this->name;
7777
}
7878

7979
/**
8080
* @inheritdoc
8181
*/
82-
public function submit($payload, array $options = [])
82+
public function submit($payload, array $options = []): string
8383
{
8484
$messageId = Algorithms::generateUUID();
8585
$message = new Message($messageId, $payload);
@@ -98,47 +98,48 @@ public function submit($payload, array $options = [])
9898
/**
9999
* @inheritdoc
100100
*/
101-
public function waitAndTake($timeout = null)
101+
public function waitAndTake(int $timeout = null): Message
102102
{
103103
throw new \BadMethodCallException('The FakeQueue does not support reserving of messages.' . chr(10) . 'It is not required to use a worker for this queue as messages are handled immediately upon submission.', 1468425275);
104104
}
105105

106106
/**
107107
* @inheritdoc
108108
*/
109-
public function waitAndReserve($timeout = null)
109+
public function waitAndReserve(int $timeout = null): Message
110110
{
111111
throw new \BadMethodCallException('The FakeQueue does not support reserving of messages.' . chr(10) . 'It is not required to use a worker for this queue as messages are handled immediately upon submission.', 1468425280);
112112
}
113113

114114
/**
115115
* @inheritdoc
116116
*/
117-
public function release($messageId, array $options = [])
117+
public function release(string $messageId, array $options = []): void
118118
{
119119
throw new \BadMethodCallException('The FakeQueue does not support releasing of failed messages.' . chr(10) . 'The "maximumNumberOfReleases" setting should be removed or set to 0 for this queue!', 1468425285);
120120
}
121121

122122
/**
123123
* @inheritdoc
124124
*/
125-
public function abort($messageId)
125+
public function abort(string $messageId): void
126126
{
127127
// The FakeQueue does not support message abortion
128128
}
129129

130130
/**
131131
* @inheritdoc
132132
*/
133-
public function finish($messageId)
133+
public function finish(string $messageId): bool
134134
{
135135
// The FakeQueue does not support message finishing
136+
return false;
136137
}
137138

138139
/**
139140
* @inheritdoc
140141
*/
141-
public function peek($limit = 1)
142+
public function peek(int $limit = 1): array
142143
{
143144
return [];
144145
}
@@ -170,7 +171,7 @@ public function countFailed(): int
170171
/**
171172
* @inheritdoc
172173
*/
173-
public function flush()
174+
public function flush(): void
174175
{
175176
// The FakeQueue does not support message flushing
176177
}

Classes/Queue/Message.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
* source code.
1212
*/
1313

14-
use Neos\Flow\Annotations as Flow;
15-
1614
/**
1715
* A DTO that wraps arbitrary payload with an identifier and a counter for failures.
1816
*/
@@ -45,7 +43,7 @@ class Message
4543
* @param mixed $payload
4644
* @param integer $numberOfReleases
4745
*/
48-
public function __construct($identifier, $payload, $numberOfReleases = 0)
46+
public function __construct(string $identifier, $payload, int $numberOfReleases = 0)
4947
{
5048
$this->identifier = $identifier;
5149
$this->payload = $payload;
@@ -55,7 +53,7 @@ public function __construct($identifier, $payload, $numberOfReleases = 0)
5553
/**
5654
* @return string
5755
*/
58-
public function getIdentifier()
56+
public function getIdentifier(): string
5957
{
6058
return $this->identifier;
6159
}
@@ -69,10 +67,10 @@ public function getPayload()
6967
}
7068

7169
/**
72-
* @return integer
70+
* @return int
7371
*/
74-
public function getNumberOfReleases()
72+
public function getNumberOfReleases(): int
7573
{
7674
return $this->numberOfReleases;
7775
}
78-
}
76+
}

Classes/Queue/QueueInterface.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
* source code.
1212
*/
1313

14-
use Neos\Flow\Annotations as Flow;
15-
1614
/**
1715
* Message queue interface
1816
*/
@@ -21,14 +19,14 @@ interface QueueInterface
2119
/**
2220
* @return void
2321
*/
24-
public function setUp();
22+
public function setUp(): void;
2523

2624
/**
2725
* The unique name of this queue
2826
*
2927
* @return string
3028
*/
31-
public function getName();
29+
public function getName(): string;
3230

3331
/**
3432
* Submit a message to the queue
@@ -37,17 +35,17 @@ public function getName();
3735
* @param array $options Simple key/value array with options, supported options depend on the queue implementation
3836
* @return string The identifier of the message under which it was queued
3937
*/
40-
public function submit($payload, array $options = []);
38+
public function submit($payload, array $options = []): string;
4139

4240
/**
4341
* Wait for a message in the queue and remove the message from the queue for processing
4442
* If a non-null value was returned, the message was not queued. Otherwise a timeout
4543
* occurred and no message was available or received.
4644
*
47-
* @param integer $timeout
45+
* @param int $timeout
4846
* @return Message The received message or NULL if a timeout occurred
4947
*/
50-
public function waitAndTake($timeout = null);
48+
public function waitAndTake(?int $timeout = null): ?Message;
5149

5250
/**
5351
* Wait for a message in the queue and reserve the message for processing
@@ -57,10 +55,10 @@ public function waitAndTake($timeout = null);
5755
* If a non-null value was returned, the message was reserved. Otherwise a timeout
5856
* occurred and no message was available or received.
5957
*
60-
* @param integer $timeout
58+
* @param int $timeout
6159
* @return Message The received message or NULL if a timeout occurred
6260
*/
63-
public function waitAndReserve($timeout = null);
61+
public function waitAndReserve(?int $timeout = null): ?Message;
6462

6563
/**
6664
* Puts a reserved message back to the queue
@@ -69,15 +67,15 @@ public function waitAndReserve($timeout = null);
6967
* @param array $options Simple key/value array with options that can be interpreted by the concrete implementation (optional)
7068
* @return void
7169
*/
72-
public function release($messageId, array $options = []);
70+
public function release(string $messageId, array $options = []): void;
7371

7472
/**
7573
* Removes a message from the active queue and marks it failed (bury)
7674
*
7775
* @param string $messageId
7876
* @return void
7977
*/
80-
public function abort($messageId);
78+
public function abort(string $messageId): void;
8179

8280
/**
8381
* Mark a message as done
@@ -86,9 +84,9 @@ public function abort($messageId);
8684
* processed successfully.
8785
*
8886
* @param string $messageId
89-
* @return boolean TRUE if the message could be removed
87+
* @return bool TRUE if the message could be removed
9088
*/
91-
public function finish($messageId);
89+
public function finish(string $messageId): bool;
9290

9391
/**
9492
* Peek for messages
@@ -99,7 +97,7 @@ public function finish($messageId);
9997
* @param integer $limit
10098
* @return Message[] The messages up to the length of limit or an empty array if no messages are present currently
10199
*/
102-
public function peek($limit = 1);
100+
public function peek(int $limit = 1): array;
103101

104102
/**
105103
* Get a count of ready messages currently in the queue.
@@ -130,5 +128,5 @@ public function countFailed(): int;
130128
*
131129
* @return void
132130
*/
133-
public function flush();
131+
public function flush(): void;
134132
}

0 commit comments

Comments
 (0)