Skip to content

Commit 48fcb5a

Browse files
committed
Better typehints
1 parent 3f43b51 commit 48fcb5a

File tree

6 files changed

+172
-17
lines changed

6 files changed

+172
-17
lines changed

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousPrefix"/>
2828
<exclude name="SlevomatCodingStandard.ControlStructures.AssignmentInCondition.AssignmentInCondition"/>
2929
<exclude name="SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion.RequiredConstructorPropertyPromotion"/>
30+
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation"/>
3031
</rule>
3132
<rule ref="Generic.Formatting.SpaceAfterNot">
3233
<properties>

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ parameters:
3434
path: test/ClientTest.php
3535
count: 1
3636
-
37-
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertNull\(\) with bool\|int will always evaluate to false\.$#'
37+
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertNull\(\) with int\|false will always evaluate to false\.$#'
3838
path: test/ClientTest.php
3939
count: 1

spec/generate.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,9 +714,27 @@ function amqpTypeToLength(string $type, string $e): array
714714
$methodName = dashedToCamel(($class->name !== 'basic' ? $class->name . '-' : "") . $method->name);
715715

716716
if (!isset($method->direction) || $method->direction === 'CS') {
717-
if (count($clientArgumentsTypeHint) > 0) {
717+
718+
if (isset($method->synchronous) && $method->synchronous) {
719+
$returnType = "Protocol\\" . dashedToCamel("method-" . $class->name . "-" . $method->name . "-ok-frame") . ($class->id === 60 && $method->id === 70 ? "|Protocol\\MethodBasicGetEmptyFrame" : "");
720+
}
721+
if (count($clientArgumentsTypeHint) > 0 || $hasNowait || (!isset($method->synchronous) || !$method->synchronous || $hasNowait)) {
718722
$connectionContent .= " /**\n";
719-
$connectionContent .= " * ".implode("\n * ", $channelArgumentsTypeHint) . "\n";
723+
if (count($clientArgumentsTypeHint) > 0) {
724+
$connectionContent .= " * " . implode("\n * ", $channelArgumentsTypeHint) . "\n";
725+
}
726+
if ($hasNowait) {
727+
if (count($clientArgumentsTypeHint) > 0) {
728+
$connectionContent .= " *\n";
729+
}
730+
$connectionContent .= " * @return (\$nowait is false ? $returnType : false)\n";
731+
} elseif (!isset($method->synchronous) || !$method->synchronous || $hasNowait) {
732+
if (count($clientArgumentsTypeHint) > 0) {
733+
$connectionContent .= " *\n";
734+
}
735+
$connectionContent .= " * @return false\n";
736+
}
737+
720738
$connectionContent .= " */\n";
721739
}
722740
$connectionContent .= " public function " . lcfirst($methodName) . "(" . implode(", ", $clientArguments) . "): ". ((!isset($method->synchronous) || !$method->synchronous || $hasNowait) ? "bool" : "") . (isset($method->synchronous) && $method->synchronous ? (($hasNowait ? "|" : "") . "Protocol\\" . dashedToCamel("method-" . $class->name . "-" . $method->name . "-ok-frame")) : "") . ($class->id === 60 && $method->id === 70 ? "|Protocol\\MethodBasicGetEmptyFrame" : "") . "\n";
@@ -953,6 +971,11 @@ function amqpTypeToLength(string $type, string $e): array
953971
$class->id !== 30 &&
954972
(!isset($method->direction) || $method->direction === 'CS')
955973
) {
974+
975+
if (isset($method->synchronous) && $method->synchronous) {
976+
$returnType = "Protocol\\" . dashedToCamel("method-" . $class->name . "-" . $method->name . "-ok-frame") . ($class->id === 60 && $method->id === 70 ? "|Protocol\\MethodBasicGetEmptyFrame" : "");
977+
}
978+
956979
$channelMethodsContent .= " /**\n";
957980
$channelMethodsContent .= " * Calls {$class->name}.{$method->name} AMQP method.\n";
958981
if (count($channelArgumentsTypeHint) > 0) {
@@ -961,6 +984,13 @@ function amqpTypeToLength(string $type, string $e): array
961984
$channelMethodsContent .= implode("\n * ", $channelArgumentsTypeHint);
962985
$channelMethodsContent .= "\n";
963986
}
987+
if ($hasNowait) {
988+
$channelMethodsContent .= " *\n";
989+
$channelMethodsContent .= " * @return (\$nowait is false ? $returnType : false)\n";
990+
} elseif (!isset($method->synchronous) || !$method->synchronous || $hasNowait) {
991+
$channelMethodsContent .= " *\n";
992+
$channelMethodsContent .= " * @return false\n";
993+
}
964994
$channelMethodsContent .= " */\n";
965995
$channelMethodsContent .= " public function " . lcfirst($methodName) . "(" . implode(", ", $channelArguments) . "): ". ((!isset($method->synchronous) || !$method->synchronous || $hasNowait) ? "bool" : "") . (isset($method->synchronous) && $method->synchronous ? (($hasNowait ? "|" : "") . "Protocol\\" . dashedToCamel("method-" . $class->name . "-" . $method->name . "-ok-frame")) : "") . ($class->id === 60 && $method->id === 70 ? "|Protocol\\MethodBasicGetEmptyFrame" : "") . "\n";
966996
$channelMethodsContent .= " {\n";

src/ChannelInterface.php

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Bunny\Protocol\MethodBasicRecoverOkFrame;
1111
use Bunny\Protocol\MethodConfirmSelectOkFrame;
1212
use Bunny\Protocol\MethodExchangeBindOkFrame;
13+
use Bunny\Protocol\MethodExchangeDeclareOkFrame;
1314
use Bunny\Protocol\MethodExchangeDeleteOkFrame;
1415
use Bunny\Protocol\MethodExchangeUnbindOkFrame;
1516
use Bunny\Protocol\MethodQueueBindOkFrame;
@@ -81,16 +82,22 @@ public function consume(callable $callback, string $queue = '', string $consumer
8182

8283
/**
8384
* Acks given message.
85+
*
86+
* @return false
8487
*/
8588
public function ack(Message $message, bool $multiple = false): bool;
8689

8790
/**
8891
* Nacks given message.
92+
*
93+
* @return false
8994
*/
9095
public function nack(Message $message, bool $multiple = false, bool $requeue = true): bool;
9196

9297
/**
9398
* Rejects given message.
99+
*
100+
* @return false
94101
*/
95102
public function reject(Message $message, bool $requeue = true): bool;
96103

@@ -103,13 +110,17 @@ public function get(string $queue = '', bool $noAck = false): Message|null;
103110
* Published message to given exchange.
104111
*
105112
* @param array<string,mixed> $headers
113+
*
114+
* @return int|false
106115
*/
107-
public function publish(string $body, array $headers = [], string $exchange = '', string $routingKey = '', bool $mandatory = false, bool $immediate = false): bool|int;
116+
public function publish(string $body, array $headers = [], string $exchange = '', string $routingKey = '', bool $mandatory = false, bool $immediate = false): int|bool;
108117

109118
/**
110119
* Cancels given consumer subscription.
120+
*
121+
* @return ($nowait is false ? \Bunny\Protocol\MethodBasicCancelOkFrame : false)
111122
*/
112-
public function cancel(string $consumerTag, bool $nowait = false): bool|MethodBasicCancelOkFrame;
123+
public function cancel(string $consumerTag, bool $nowait = false): MethodBasicCancelOkFrame|bool;
113124

114125
/**
115126
* Changes channel to transactional mode. All messages are published to queues only after {@link txCommit()} is called.
@@ -128,78 +139,98 @@ public function txRollback(): MethodTxRollbackOkFrame;
128139

129140
/**
130141
* Changes channel to confirm mode. Broker then asynchronously sends 'basic.ack's for published messages.
142+
*
143+
* @return ($nowait is false ? \Bunny\Protocol\MethodConfirmSelectOkFrame : false)
131144
*/
132-
public function confirmSelect(?callable $callback = null, bool $nowait = false): bool|MethodConfirmSelectOkFrame;
145+
public function confirmSelect(?callable $callback = null, bool $nowait = false): MethodConfirmSelectOkFrame|bool;
133146

134147
/**
135148
* Calls basic.qos AMQP method.
136149
*/
137-
public function qos(int $prefetchSize = 0, int $prefetchCount = 0, bool $global = false): bool|MethodBasicQosOkFrame;
150+
public function qos(int $prefetchSize = 0, int $prefetchCount = 0, bool $global = false): MethodBasicQosOkFrame;
138151

139152
/**
140153
* Calls queue.declare AMQP method.
141154
*
142155
* @param array<string,mixed> $arguments
156+
*
157+
* @return ($nowait is false ? \Bunny\Protocol\MethodQueueDeclareOkFrame : false)
143158
*/
144-
public function queueDeclare(string $queue = '', bool $passive = false, bool $durable = false, bool $exclusive = false, bool $autoDelete = false, bool $nowait = false, array $arguments = []): bool|MethodQueueDeclareOkFrame;
159+
public function queueDeclare(string $queue = '', bool $passive = false, bool $durable = false, bool $exclusive = false, bool $autoDelete = false, bool $nowait = false, array $arguments = []): MethodQueueDeclareOkFrame|bool;
145160

146161
/**
147162
* Calls queue.bind AMQP method.
148163
*
149164
* @param array<string,mixed> $arguments
165+
*
166+
* @return ($nowait is false ? \Bunny\Protocol\MethodQueueBindOkFrame : false)
150167
*/
151-
public function queueBind(string $exchange, string $queue = '', string $routingKey = '', bool $nowait = false, array $arguments = []): bool|MethodQueueBindOkFrame;
168+
public function queueBind(string $exchange, string $queue = '', string $routingKey = '', bool $nowait = false, array $arguments = []): MethodQueueBindOkFrame|bool;
152169

153170
/**
154171
* Calls queue.purge AMQP method.
172+
*
173+
* @return ($nowait is false ? \Bunny\Protocol\MethodQueuePurgeOkFrame : false)
155174
*/
156-
public function queuePurge(string $queue = '', bool $nowait = false): bool|MethodQueuePurgeOkFrame;
175+
public function queuePurge(string $queue = '', bool $nowait = false): MethodQueuePurgeOkFrame|bool;
157176

158177
/**
159178
* Calls queue.delete AMQP method.
179+
*
180+
* @return ($nowait is false ? \Bunny\Protocol\MethodQueueDeleteOkFrame : false)
160181
*/
161-
public function queueDelete(string $queue = '', bool $ifUnused = false, bool $ifEmpty = false, bool $nowait = false): bool|MethodQueueDeleteOkFrame;
182+
public function queueDelete(string $queue = '', bool $ifUnused = false, bool $ifEmpty = false, bool $nowait = false): MethodQueueDeleteOkFrame|bool;
162183

163184
/**
164185
* Calls queue.unbind AMQP method.
165186
*
166187
* @param array<string,mixed> $arguments
167188
*/
168-
public function queueUnbind(string $exchange, string $queue = '', string $routingKey = '', array $arguments = []): bool|MethodQueueUnbindOkFrame;
189+
public function queueUnbind(string $exchange, string $queue = '', string $routingKey = '', array $arguments = []): MethodQueueUnbindOkFrame;
169190

170191
/**
171192
* Calls exchange.declare AMQP method.
172193
*
173194
* @param array<string,mixed> $arguments
195+
*
196+
* @return ($nowait is false ? \Bunny\Protocol\MethodExchangeDeclareOkFrame : false)
174197
*/
175-
public function exchangeDeclare(string $exchange, string $exchangeType = 'direct', bool $passive = false, bool $durable = false, bool $autoDelete = false, bool $internal = false, bool $nowait = false, array $arguments = []): bool|Protocol\MethodExchangeDeclareOkFrame;
198+
public function exchangeDeclare(string $exchange, string $exchangeType = 'direct', bool $passive = false, bool $durable = false, bool $autoDelete = false, bool $internal = false, bool $nowait = false, array $arguments = []): MethodExchangeDeclareOkFrame|bool;
176199

177200
/**
178201
* Calls exchange.delete AMQP method.
202+
*
203+
* @return ($nowait is false ? \Bunny\Protocol\MethodExchangeDeleteOkFrame : false)
179204
*/
180-
public function exchangeDelete(string $exchange, bool $ifUnused = false, bool $nowait = false): bool|MethodExchangeDeleteOkFrame;
205+
public function exchangeDelete(string $exchange, bool $ifUnused = false, bool $nowait = false): MethodExchangeDeleteOkFrame|bool;
181206

182207
/**
183208
* Calls exchange.bind AMQP method.
184209
*
185210
* @param array<string,mixed> $arguments
211+
*
212+
* @return ($nowait is false ? \Bunny\Protocol\MethodExchangeBindOkFrame : false)
186213
*/
187-
public function exchangeBind(string $destination, string $source, string $routingKey = '', bool $nowait = false, array $arguments = []): bool|MethodExchangeBindOkFrame;
214+
public function exchangeBind(string $destination, string $source, string $routingKey = '', bool $nowait = false, array $arguments = []): MethodExchangeBindOkFrame|bool;
188215

189216
/**
190217
* Calls exchange.unbind AMQP method.
191218
*
192219
* @param array<string,mixed> $arguments
220+
*
221+
* @return ($nowait is false ? \Bunny\Protocol\MethodExchangeUnbindOkFrame : false)
193222
*/
194-
public function exchangeUnbind(string $destination, string $source, string $routingKey = '', bool $nowait = false, array $arguments = []): bool|MethodExchangeUnbindOkFrame;
223+
public function exchangeUnbind(string $destination, string $source, string $routingKey = '', bool $nowait = false, array $arguments = []): MethodExchangeUnbindOkFrame|bool;
195224

196225
/**
197226
* Calls basic.recover-async AMQP method.
227+
*
228+
* @return false
198229
*/
199230
public function recoverAsync(bool $requeue = false): bool;
200231

201232
/**
202233
* Calls basic.recover AMQP method.
203234
*/
204-
public function recover(bool $requeue = false): bool|MethodBasicRecoverOkFrame;
235+
public function recover(bool $requeue = false): MethodBasicRecoverOkFrame;
205236
}

src/ChannelMethods.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ abstract public function getChannelId(): int;
2727
* Calls exchange.declare AMQP method.
2828
*
2929
* @param array<string,mixed> $arguments
30+
*
31+
* @return ($nowait is false ? Protocol\MethodExchangeDeclareOkFrame : false)
3032
*/
3133
public function exchangeDeclare(string $exchange, string $exchangeType = 'direct', bool $passive = false, bool $durable = false, bool $autoDelete = false, bool $internal = false, bool $nowait = false, array $arguments = []): bool|Protocol\MethodExchangeDeclareOkFrame
3234
{
@@ -35,6 +37,8 @@ public function exchangeDeclare(string $exchange, string $exchangeType = 'direct
3537

3638
/**
3739
* Calls exchange.delete AMQP method.
40+
*
41+
* @return ($nowait is false ? Protocol\MethodExchangeDeleteOkFrame : false)
3842
*/
3943
public function exchangeDelete(string $exchange, bool $ifUnused = false, bool $nowait = false): bool|Protocol\MethodExchangeDeleteOkFrame
4044
{
@@ -45,6 +49,8 @@ public function exchangeDelete(string $exchange, bool $ifUnused = false, bool $n
4549
* Calls exchange.bind AMQP method.
4650
*
4751
* @param array<string,mixed> $arguments
52+
*
53+
* @return ($nowait is false ? Protocol\MethodExchangeBindOkFrame : false)
4854
*/
4955
public function exchangeBind(string $destination, string $source, string $routingKey = '', bool $nowait = false, array $arguments = []): bool|Protocol\MethodExchangeBindOkFrame
5056
{
@@ -55,6 +61,8 @@ public function exchangeBind(string $destination, string $source, string $routin
5561
* Calls exchange.unbind AMQP method.
5662
*
5763
* @param array<string,mixed> $arguments
64+
*
65+
* @return ($nowait is false ? Protocol\MethodExchangeUnbindOkFrame : false)
5866
*/
5967
public function exchangeUnbind(string $destination, string $source, string $routingKey = '', bool $nowait = false, array $arguments = []): bool|Protocol\MethodExchangeUnbindOkFrame
6068
{
@@ -65,6 +73,8 @@ public function exchangeUnbind(string $destination, string $source, string $rout
6573
* Calls queue.declare AMQP method.
6674
*
6775
* @param array<string,mixed> $arguments
76+
*
77+
* @return ($nowait is false ? Protocol\MethodQueueDeclareOkFrame : false)
6878
*/
6979
public function queueDeclare(string $queue = '', bool $passive = false, bool $durable = false, bool $exclusive = false, bool $autoDelete = false, bool $nowait = false, array $arguments = []): bool|Protocol\MethodQueueDeclareOkFrame
7080
{
@@ -75,6 +85,8 @@ public function queueDeclare(string $queue = '', bool $passive = false, bool $du
7585
* Calls queue.bind AMQP method.
7686
*
7787
* @param array<string,mixed> $arguments
88+
*
89+
* @return ($nowait is false ? Protocol\MethodQueueBindOkFrame : false)
7890
*/
7991
public function queueBind(string $exchange, string $queue = '', string $routingKey = '', bool $nowait = false, array $arguments = []): bool|Protocol\MethodQueueBindOkFrame
8092
{
@@ -83,6 +95,8 @@ public function queueBind(string $exchange, string $queue = '', string $routingK
8395

8496
/**
8597
* Calls queue.purge AMQP method.
98+
*
99+
* @return ($nowait is false ? Protocol\MethodQueuePurgeOkFrame : false)
86100
*/
87101
public function queuePurge(string $queue = '', bool $nowait = false): bool|Protocol\MethodQueuePurgeOkFrame
88102
{
@@ -91,6 +105,8 @@ public function queuePurge(string $queue = '', bool $nowait = false): bool|Proto
91105

92106
/**
93107
* Calls queue.delete AMQP method.
108+
*
109+
* @return ($nowait is false ? Protocol\MethodQueueDeleteOkFrame : false)
94110
*/
95111
public function queueDelete(string $queue = '', bool $ifUnused = false, bool $ifEmpty = false, bool $nowait = false): bool|Protocol\MethodQueueDeleteOkFrame
96112
{
@@ -119,6 +135,8 @@ public function qos(int $prefetchSize = 0, int $prefetchCount = 0, bool $global
119135
* Calls basic.consume AMQP method.
120136
*
121137
* @param array<string,mixed> $arguments
138+
*
139+
* @return ($nowait is false ? Protocol\MethodBasicConsumeOkFrame : false)
122140
*/
123141
public function consume(string $queue = '', string $consumerTag = '', bool $noLocal = false, bool $noAck = false, bool $exclusive = false, bool $nowait = false, array $arguments = []): bool|Protocol\MethodBasicConsumeOkFrame
124142
{
@@ -127,6 +145,8 @@ public function consume(string $queue = '', string $consumerTag = '', bool $noLo
127145

128146
/**
129147
* Calls basic.cancel AMQP method.
148+
*
149+
* @return ($nowait is false ? Protocol\MethodBasicCancelOkFrame : false)
130150
*/
131151
public function cancel(string $consumerTag, bool $nowait = false): bool|Protocol\MethodBasicCancelOkFrame
132152
{
@@ -137,6 +157,8 @@ public function cancel(string $consumerTag, bool $nowait = false): bool|Protocol
137157
* Calls basic.publish AMQP method.
138158
*
139159
* @param array<string,mixed> $headers
160+
*
161+
* @return false
140162
*/
141163
public function publish(string $body, array $headers = [], string $exchange = '', string $routingKey = '', bool $mandatory = false, bool $immediate = false): bool
142164
{
@@ -153,6 +175,8 @@ public function get(string $queue = '', bool $noAck = false): Protocol\MethodBas
153175

154176
/**
155177
* Calls basic.ack AMQP method.
178+
*
179+
* @return false
156180
*/
157181
public function ack(int $deliveryTag = 0, bool $multiple = false): bool
158182
{
@@ -161,6 +185,8 @@ public function ack(int $deliveryTag = 0, bool $multiple = false): bool
161185

162186
/**
163187
* Calls basic.reject AMQP method.
188+
*
189+
* @return false
164190
*/
165191
public function reject(int $deliveryTag, bool $requeue = true): bool
166192
{
@@ -169,6 +195,8 @@ public function reject(int $deliveryTag, bool $requeue = true): bool
169195

170196
/**
171197
* Calls basic.recover-async AMQP method.
198+
*
199+
* @return false
172200
*/
173201
public function recoverAsync(bool $requeue = false): bool
174202
{
@@ -185,6 +213,8 @@ public function recover(bool $requeue = false): Protocol\MethodBasicRecoverOkFra
185213

186214
/**
187215
* Calls basic.nack AMQP method.
216+
*
217+
* @return false
188218
*/
189219
public function nack(int $deliveryTag = 0, bool $multiple = false, bool $requeue = true): bool
190220
{
@@ -217,6 +247,8 @@ public function txRollback(): Protocol\MethodTxRollbackOkFrame
217247

218248
/**
219249
* Calls confirm.select AMQP method.
250+
*
251+
* @return ($nowait is false ? Protocol\MethodConfirmSelectOkFrame : false)
220252
*/
221253
public function confirmSelect(bool $nowait = false): bool|Protocol\MethodConfirmSelectOkFrame
222254
{

0 commit comments

Comments
 (0)