Skip to content

Commit 9dbb300

Browse files
committed
refactor: more type hinting (still wip)
1 parent df2e819 commit 9dbb300

10 files changed

+91
-149
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ BC Breaks changelog
131131

132132
- Drop support for php 5.6 & 7.0
133133
- Worker now catch Throwable, not only Exceptions
134+
- WritableMessage::packAttributes now only accept timestamp or null (not false anymore)
134135

135136
**3.x -> 4.x**
136137

features/bootstrap/ConsumeContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function() {
7878
$consumer->consume($processor, $this->client, $workerContext);
7979
}
8080

81-
public function process(ReadableMessage $message)
81+
public function process(ReadableMessage $message): void
8282
{
8383
$this->consumedMessages[] = $message;
8484
}

src/Consumer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
interface Consumer extends LoggerAwareInterface
1010
{
11-
const
11+
public const
1212
DEFAULT_MAX_EXECUTION_TIME = 3600; //in seconds
1313

1414
public function consume(ProcessorInterface $processor, Client $client, WorkerContext $workerContext);

src/MessageMetadata.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,18 @@
44

55
interface MessageMetadata
66
{
7-
const
7+
public const
88
TRANSIENT = 1,
99
PERSISTENT = 2;
1010

11-
/**
12-
* @return string
13-
*/
14-
public function getRoutingKey();
11+
public function getRoutingKey(): string;
1512

16-
/**
17-
* @return string
18-
*/
19-
public function getContentType();
13+
public function getContentType(): string;
2014

21-
/**
22-
* @return array
23-
*/
24-
public function getHeaders();
15+
public function getHeaders(): array;
2516

2617
/**
2718
* @return mixed
2819
*/
29-
public function getAttribute($attributeName);
20+
public function getAttribute(string $attributeName);
3021
}

src/Messages/BodySetter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Puzzle\AMQP\Messages\Chunks\ChunkSize;
99
use Puzzle\AMQP\Messages\Bodies\StreamedFile;
1010
use Puzzle\AMQP\Messages\Bodies\StreamedBinary;
11+
use Puzzle\AMQP\WritableMessage;
1112

1213
trait BodySetter
1314
{
@@ -53,5 +54,5 @@ public function setStreamedBinary($content, ChunkSize $size)
5354
return $this;
5455
}
5556

56-
abstract public function setBody(Body $body);
57+
abstract public function setBody(Body $body): WritableMessage;
5758
}

src/Messages/Message.php

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use Puzzle\AMQP\WritableMessage;
77
use Puzzle\AMQP\Messages\Bodies\NullBody;
88
use Puzzle\Pieces\ConvertibleToString;
9+
use Puzzle\Pieces\Exceptions\JsonEncodeError;
10+
use Puzzle\Pieces\Json;
911

1012
class Message implements WritableMessage, ConvertibleToString
1113
{
12-
const
14+
public const
1315
ATTRIBUTE_CONTENT_TYPE = 'content_type';
1416

1517
use BodySetter;
@@ -22,7 +24,7 @@ class Message implements WritableMessage, ConvertibleToString
2224
$headers,
2325
$attributes;
2426

25-
public function __construct($routingKey = '')
27+
public function __construct(string $routingKey = '')
2628
{
2729
$this->body = new NullBody();
2830

@@ -35,38 +37,38 @@ public function __construct($routingKey = '')
3537
$this->changeRoutingKey($routingKey);
3638
}
3739

38-
public function changeRoutingKey($routingKey)
40+
public function changeRoutingKey(string $routingKey): void
3941
{
4042
$this->setAttribute('routing_key', $routingKey);
4143
}
4244

43-
private function initializeAttributes()
45+
private function initializeAttributes(): void
4446
{
4547
$this->attributes = array(
4648
'routing_key' => null,
4749
self::ATTRIBUTE_CONTENT_TYPE=> $this->getContentType(),
4850
'content_encoding' => 'utf8',
49-
'message_id' => function($timestamp) {
51+
'message_id' => function(int $timestamp) {
5052
return sha1($this->getRoutingKey() . $timestamp . $this->generateBodyId() . mt_rand());
5153
},
5254
'user_id' => null,
5355
'app_id' => null,
5456
'delivery_mode' => self::PERSISTENT,
5557
'priority' => null,
56-
'timestamp' => function($timestamp) {
58+
'timestamp' => function(int $timestamp) {
5759
return $timestamp;
5860
},
5961
'expiration' => null,
6062
'type' => null,
6163
'reply_to' => null,
6264
'correlation_id' => null,
63-
'headers' => function($timestamp) {
65+
'headers' => function(int $timestamp) {
6466
return $this->packHeaders($timestamp);
6567
},
6668
);
6769
}
6870

69-
private function generateBodyId()
71+
private function generateBodyId(): string
7072
{
7173
if($this->body instanceof Footprintable)
7274
{
@@ -76,19 +78,19 @@ private function generateBodyId()
7678
return uniqid(true);
7779
}
7880

79-
public function canBeDroppedSilently()
81+
public function canBeDroppedSilently(): bool
8082
{
8183
return $this->canBeDroppedSilently;
8284
}
8385

84-
public function disallowSilentDropping()
86+
public function disallowSilentDropping(): WritableMessage
8587
{
8688
$this->canBeDroppedSilently = false;
8789

8890
return $this;
8991
}
9092

91-
public function getContentType()
93+
public function getContentType(): string
9294
{
9395
if($this->userContentType === null)
9496
{
@@ -98,37 +100,37 @@ public function getContentType()
98100
return $this->userContentType;
99101
}
100102

101-
public function getRoutingKey()
103+
public function getRoutingKey(): string
102104
{
103-
return $this->getAttribute('routing_key');
105+
return (string) $this->getAttribute('routing_key');
104106
}
105107

106108
public function getBodyInTransportFormat()
107109
{
108110
return $this->body->asTransported();
109111
}
110112

111-
public function setBody(Body $body)
113+
public function setBody(Body $body): WritableMessage
112114
{
113115
$this->body = $body;
114116
$this->updateContentType();
115117

116118
return $this;
117119
}
118120

119-
private function updateContentType()
121+
private function updateContentType(): void
120122
{
121123
$this->attributes[self::ATTRIBUTE_CONTENT_TYPE] = $this->getContentType();
122124
}
123125

124-
public function addHeader($headerName, $value)
126+
public function addHeader(string $headerName, $value): WritableMessage
125127
{
126128
$this->headers[$headerName] = $value;
127129

128130
return $this;
129131
}
130132

131-
public function addHeaders(array $headers)
133+
public function addHeaders(array $headers): WritableMessage
132134
{
133135
foreach($headers as $name => $value)
134136
{
@@ -138,18 +140,18 @@ public function addHeaders(array $headers)
138140
return $this;
139141
}
140142

141-
public function setAuthor($author)
143+
public function setAuthor(string $author): WritableMessage
142144
{
143145
$this->addHeader('author', $author);
144146

145147
return $this;
146148
}
147149

148-
public function packAttributes($timestamp = false)
150+
public function packAttributes(?int $timestamp = null): array
149151
{
150152
$this->updateContentType();
151153

152-
if($timestamp === false)
154+
if($timestamp === null)
153155
{
154156
$timestamp = (new \DateTime("now"))->getTimestamp();
155157
}
@@ -166,14 +168,14 @@ public function packAttributes($timestamp = false)
166168
}, $this->attributes);
167169
}
168170

169-
private function packHeaders($timestamp)
171+
private function packHeaders(int $timestamp): array
170172
{
171173
$this->headers['message_datetime'] = date('Y-m-d H:i:s', $timestamp);
172174

173175
return $this->headers;
174176
}
175177

176-
public function setAttribute($attributeName, $value)
178+
public function setAttribute(string $attributeName, $value): WritableMessage
177179
{
178180
if($attributeName !== 'headers')
179181
{
@@ -191,14 +193,14 @@ public function setAttribute($attributeName, $value)
191193
return $this;
192194
}
193195

194-
public function getHeaders()
196+
public function getHeaders(): array
195197
{
196198
$attributes = $this->packAttributes();
197199

198200
return $attributes['headers'];
199201
}
200202

201-
public function getAttribute($attributeName)
203+
public function getAttribute(string $attributeName)
202204
{
203205
if(array_key_exists($attributeName, $this->attributes))
204206
{
@@ -208,17 +210,23 @@ public function getAttribute($attributeName)
208210
throw new InvalidArgumentException(sprintf('Property "%s" is unknown or is not a message property', $attributeName));
209211
}
210212

211-
public function __toString()
213+
public function __toString(): string
212214
{
213-
return json_encode(array(
214-
'routing_key' => $this->getRoutingKey(),
215-
'body' => (string) $this->body,
216-
'attributes' => $this->attributes,
217-
'can be dropped silently' => $this->canBeDroppedSilently
218-
));
215+
try {
216+
return (string) Json::encode([
217+
'routing_key' => $this->getRoutingKey(),
218+
'body' => (string) $this->body,
219+
'attributes' => $this->attributes,
220+
'can be dropped silently' => $this->canBeDroppedSilently
221+
]);
222+
}
223+
catch(JsonEncodeError $e)
224+
{
225+
return sprintf('Can\'t json encode the message. error: "%s"', $e->getMessage());
226+
}
219227
}
220228

221-
public function setExpiration($expirationInSeconds)
229+
public function setExpiration(int $expirationInSeconds): WritableMessage
222230
{
223231
$ttlInMs = 1000 * (int) $expirationInSeconds;
224232

@@ -227,19 +235,19 @@ public function setExpiration($expirationInSeconds)
227235
return $this;
228236
}
229237

230-
public function isCompressionAllowed()
238+
public function isCompressionAllowed(): bool
231239
{
232240
return $this->allowCompression;
233241
}
234242

235-
public function allowCompression($allow = true)
243+
public function allowCompression(bool $allow = true): WritableMessage
236244
{
237245
$this->allowCompression = (bool) $allow;
238246

239247
return $this;
240248
}
241249

242-
public function isChunked()
250+
public function isChunked(): bool
243251
{
244252
return $this->body->isChunked();
245253
}

src/ReadableMessage.php

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Puzzle\AMQP;
44

5+
use Puzzle\AMQP\Consumers\Retry;
6+
57
interface ReadableMessage extends MessageMetadata
68
{
79
/**
@@ -13,29 +15,10 @@ public function getBodyInOriginalFormat();
1315
* @return mixed
1416
*/
1517
public function getBodyAsTransported();
18+
public function getAppId(): string;
19+
public function getAttributes(): array;
20+
public function isLastRetry(int $retryOccurence = Retry::DEFAULT_RETRY_OCCURENCE): bool;
21+
public function getRoutingKeyFromHeader(): ?string;
1622

17-
/**
18-
* @return string
19-
*/
20-
public function getAppId();
21-
22-
/**
23-
* @return array
24-
*/
25-
public function getAttributes();
26-
27-
/**
28-
* @return boolean
29-
*/
30-
public function isLastRetry();
31-
32-
/**
33-
*  @return string
34-
*/
35-
public function getRoutingKeyFromHeader();
36-
37-
/**
38-
* @return \Puzzle\AMQP\WritableMessage
39-
*/
40-
public function cloneIntoWritableMessage(WritableMessage $writable, $copyRoutingKey = false);
23+
public function cloneIntoWritableMessage(WritableMessage $writable, bool $copyRoutingKey = false): WritableMessage;
4124
}

0 commit comments

Comments
 (0)