Skip to content

Commit 3630bb2

Browse files
committed
FIX QA errors
Fix errors reported by PHPStan and PHP_CodeSniffer.
1 parent bef862f commit 3630bb2

10 files changed

Lines changed: 114 additions & 43 deletions

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
1818
"phpcompatibility/php-compatibility": "^9.3",
1919
"phpstan/extension-installer": "^1.1",
20-
"phpstan/phpstan-strict-rules": "^1.1"
20+
"phpstan/phpstan-strict-rules": "^1.1",
21+
"phpstan/phpstan-phpunit": "^1.0"
2122
},
2223
"license": "MIT",
2324
"authors": [

phpcs.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@
1414

1515
<file>src/</file>
1616
<file>tests/</file>
17+
18+
<rule ref="Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore">
19+
<exclude-pattern>tests/*</exclude-pattern>
20+
</rule>
1721
</ruleset>

src/Messenger/Stamp/AzureBrokerPropertiesStamp.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ public function __construct(
107107
public static function createFromResponse(ResponseInterface $response): self
108108
{
109109
$header = $response->getHeaders()['brokerproperties'][0] ?? '';
110+
111+
/**
112+
* @var null|array{
113+
* ContentType?: string,
114+
* CorrelationId?: string,
115+
* SessionID?: string,
116+
* DeliveryCount?: int,
117+
* LockedUntilUtc?: string,
118+
* LockToken?: string,
119+
* MessageId?: string,
120+
* Label?: string,
121+
* ReplyTo?: string,
122+
* EnqueuedTimeUtc?: string,
123+
* SequenceNumber?: int,
124+
* TimeToLive?: int,
125+
* To?: string,
126+
* ScheduledEnqueueTimeUtc?: string,
127+
* ReplyToSessionId?: string,
128+
* PartitionKey?: string
129+
* } $properties
130+
*/
110131
$properties = json_decode($header, true);
111132

112133
return new self(
@@ -137,6 +158,7 @@ public static function createFromResponse(ResponseInterface $response): self
137158

138159
/**
139160
* Encode the broker properties in JSON for sending to Azure Service Bus
161+
* @throws \JsonException
140162
* @internal
141163
*/
142164
public function encode(): string
@@ -170,7 +192,7 @@ public function encode(): string
170192
return !is_null($property);
171193
});
172194

173-
return json_encode($properties, JSON_FORCE_OBJECT);
195+
return json_encode($properties, JSON_FORCE_OBJECT | JSON_THROW_ON_ERROR);
174196
}
175197

176198
public function getContentType(): ?string

src/Messenger/Transport/AzureHttpClientConfigurationBuilder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ final class AzureHttpClientConfigurationBuilder
1515
* @param array{
1616
* transport_name: string,
1717
* entity_path: string,
18-
* subscription?: string|null,
19-
* token_expiry: int
18+
* subscription: string|null,
19+
* token_expiry: int,
2020
* receive_mode: string
2121
* } $options
2222
* @return array{endpoint: string, options: mixed[]}
@@ -31,8 +31,8 @@ public function buildSenderConfiguration(string $dsn, array $options): array
3131
* @param array{
3232
* transport_name: string,
3333
* entity_path: string,
34-
* subscription?: string|null,
35-
* token_expiry: int
34+
* subscription: string|null,
35+
* token_expiry: int,
3636
* receive_mode: string
3737
* } $options
3838
* @return array{endpoint: string, options: mixed[]}
@@ -46,8 +46,8 @@ public function buildReceiverConfiguration(string $dsn, array $options): array
4646
* @param array{
4747
* transport_name: string,
4848
* entity_path: string,
49-
* subscription?: string|null,
50-
* token_expiry: int
49+
* subscription: string|null,
50+
* token_expiry: int,
5151
* receive_mode: string
5252
* } $options
5353
* @return array{endpoint: string, options: mixed[]}

src/Messenger/Transport/AzureTransport.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ public function send(Envelope $envelope): Envelope
125125
/** @var null|AzureBrokerPropertiesStamp $brokerProperties */
126126
$brokerProperties = $envelope->last(AzureBrokerPropertiesStamp::class);
127127
if (null !== $brokerProperties) {
128-
$additionalHeaders['BrokerProperties'] = $brokerProperties->encode();
128+
try {
129+
$additionalHeaders['BrokerProperties'] = $brokerProperties->encode();
130+
} catch (\JsonException $e) {
131+
throw new TransportException('Could not encode the "BrokerProperties" header.', 1644511135, $e);
132+
}
129133
}
130134

131135
// Decode message

src/Messenger/Transport/AzureTransportFactory.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,7 @@ public function supports(string $dsn, array $options): bool
4343
}
4444

4545
/**
46-
* @param array{
47-
* transport_name: string,
48-
* entity_path?: string,
49-
* subscription?: string|null,
50-
* token_expiry?: int
51-
* receive_mode?: string
52-
* } $options
46+
* @param mixed[] $options
5347
*/
5448
public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
5549
{
@@ -72,24 +66,27 @@ public function createTransport(string $dsn, array $options, SerializerInterface
7266

7367
/**
7468
* Validate options and set default values
75-
* @param array{
76-
* transport_name: string,
77-
* entity_path?: string,
78-
* subscription?: string|null,
79-
* token_expiry?: int
80-
* receive_mode?: string
81-
* } $options
69+
* @param mixed[] $options
8270
* @return array{
8371
* transport_name: string,
8472
* entity_path: string,
85-
* subscription?: string|null,
86-
* token_expiry: int
73+
* subscription: string|null,
74+
* token_expiry: int,
8775
* receive_mode: string
8876
* }
8977
*/
9078
private function validateOptions(array $options): array
9179
{
9280
// Set default values
81+
/**
82+
* @var array{
83+
* transport_name: string,
84+
* entity_path: string|null,
85+
* subscription: string|null,
86+
* token_expiry: int,
87+
* receive_mode: string
88+
* } $options
89+
*/
9390
$options = array_merge(self::DEFAULT_OPTIONS, $options);
9491

9592
// Missing topic or queue name

tests/Messenger/Stamp/AzureBrokerPropertiesStampTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function testCreateFromResponse(): void
104104
$scheduledEnqueueTimeUtc = '1970-01-01 00:00:00';
105105
$replyToSessionId = 'test-reply-to-session-id';
106106
$partitionKey = 'test-partition-key';
107-
107+
108108
$httpClient = new MockHttpClient([
109109
new MockResponse('', [
110110
'response_headers' => [
@@ -168,16 +168,17 @@ public function testEncodeTo(): void
168168
{
169169
$stamp = new AzureBrokerPropertiesStamp(
170170
'test-content-type',
171-
null,
172-
null,
173-
null,
174-
new \DateTime('1970-01-01 00:00:00')
171+
null,
172+
null,
173+
null,
174+
new \DateTime('1970-01-01 00:00:00')
175175
);
176176

177177
$json = $stamp->encode();
178178
self::assertJson($json);
179179

180180
$properties = json_decode($json, true);
181+
self::assertIsArray($properties);
181182
self::assertArrayHasKey('ContentType', $properties);
182183
self::assertSame('test-content-type', $properties['ContentType']);
183184
self::assertArrayNotHasKey('CorrelationId', $properties);

tests/Messenger/Transport/AzureHttpClientConfigurationBuilderTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ final class AzureHttpClientConfigurationBuilderTest extends TestCase
1616
*/
1717
public function testDefaultConfiguration(bool $isSender, array $configuration): void
1818
{
19-
self::assertIsArray($configuration);
2019
self::assertArrayHasKey('endpoint', $configuration);
2120
self::assertArrayHasKey('options', $configuration);
2221

2322
// It MUST end with a trailing slash
23+
self::assertStringEndsWith('/', $configuration['endpoint']);
2424
self::assertSame('https://namespace.servicebus.windows.net/entity/', $configuration['endpoint']);
2525

2626
if ($isSender) {
@@ -91,7 +91,8 @@ public function testThrowsOnInvalidDsn(): void
9191
'transport_name' => 'test-transport',
9292
'entity_path' => 'entity',
9393
'token_expiry' => 3600,
94-
'receive_mode' => 'peek-lock'
94+
'receive_mode' => 'peek-lock',
95+
'subscription' => null,
9596
]);
9697
}
9798
}

tests/Messenger/Transport/AzureTransportFactoryTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public function testThrowsOnMissingEntityPath(): void
3838
'azure://KeyName:Key@namespace',
3939
[
4040
'transport_name' => 'test-transport',
41-
'entity_path' => null,
4241
],
4342
self::createMock(SerializerInterface::class)
4443
);

tests/Messenger/Transport/AzureTransportTest.php

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ public function testGetHasStamps(int $statusCode, string $receiveMode, bool $has
116116
self::assertCount(1, $result);
117117

118118
$envelope = $result[0];
119-
self::assertInstanceOf(Envelope::class, $envelope);
120-
121119
$azureReceivedStamp = $envelope->last(AzureReceivedStamp::class);
122120
self::assertInstanceOf(AzureReceivedStamp::class, $azureReceivedStamp);
123121

@@ -133,7 +131,7 @@ public function testGetHasStamps(int $statusCode, string $receiveMode, bool $has
133131
}
134132

135133
/**
136-
* @return array{int, string, bool}
134+
* @return array{int, string, bool}[]
137135
*/
138136
public function provideValidGetCases(): array
139137
{
@@ -178,7 +176,10 @@ public function testAckRejectDoesNotDeleteOnReceiveAndDeleteMode(string $methodN
178176
);
179177

180178
$envelope = new Envelope(new class {});
181-
call_user_func([$transport, $methodName], $envelope);
179+
180+
/** @var callable $method */
181+
$method = [$transport, $methodName];
182+
call_user_func($method, $envelope);
182183
}
183184

184185
/**
@@ -209,7 +210,10 @@ function (string $method, string $url) use ($expectedUrl): ResponseInterface {
209210
$envelope = new Envelope(new class {}, [
210211
new AzureReceivedStamp('message', $expectedUrl),
211212
]);
212-
call_user_func([$transport, $methodName], $envelope);
213+
214+
/** @var callable $method */
215+
$method = [$transport, $methodName];
216+
call_user_func($method, $envelope);
213217
}
214218

215219
/**
@@ -232,7 +236,10 @@ public function testAckRejectWithoutDeleteLocationOrBrokerProperties(string $met
232236
$envelope = new Envelope(new class {}, [
233237
new AzureReceivedStamp('message', null),
234238
]);
235-
call_user_func([$transport, $methodName], $envelope);
239+
240+
/** @var callable $method */
241+
$method = [$transport, $methodName];
242+
call_user_func($method, $envelope);
236243
}
237244

238245
/**
@@ -255,7 +262,10 @@ public function testAckRejectWithoutBrokerPropertiesMessageIdentifier(string $me
255262
$envelope = new Envelope(new class {}, [
256263
new AzureBrokerPropertiesStamp(),
257264
]);
258-
call_user_func([$transport, $methodName], $envelope);
265+
266+
/** @var callable $method */
267+
$method = [$transport, $methodName];
268+
call_user_func($method, $envelope);
259269
}
260270

261271
/**
@@ -278,7 +288,10 @@ public function testAckRejectWithoutBrokerPropertiesLockToken(
278288

279289

280290
$envelope = new Envelope(new class {}, [$stamp]);
281-
call_user_func([$transport, $methodName], $envelope);
291+
292+
/** @var callable $method */
293+
$method = [$transport, $methodName];
294+
call_user_func($method, $envelope);
282295
}
283296

284297
/**
@@ -362,7 +375,10 @@ function (string $method, string $url) use ($expectedUrl): ResponseInterface {
362375
$messageId
363376
),
364377
]);
365-
call_user_func([$transport, $methodName], $envelope);
378+
379+
/** @var callable $method */
380+
$method = [$transport, $methodName];
381+
call_user_func($method, $envelope);
366382
}
367383

368384
/**
@@ -389,7 +405,10 @@ public function testAckRejectThrowsOnHttpError(string $methodName): void
389405
$envelope = new Envelope(new class {}, [
390406
new AzureReceivedStamp('message', 'https://delete-location'),
391407
]);
392-
call_user_func([$transport, $methodName], $envelope);
408+
409+
/** @var callable $method */
410+
$method = [$transport, $methodName];
411+
call_user_func($method, $envelope);
393412
}
394413

395414
/**
@@ -436,6 +455,29 @@ public function testSendWithBrokerPropertiesStampGetsConvertedToHttpHeader(): vo
436455
$transport->send($envelope);
437456
}
438457

458+
/**
459+
* JSON encoding exception of the BrokerProperties must throw a transport exception
460+
*/
461+
public function testSendWithBrokerPropertiesJsonErrorThrowsTransportException(): void
462+
{
463+
self::expectException(TransportException::class);
464+
self::expectExceptionCode(1644511135);
465+
466+
$envelope = new Envelope(new class {}, [
467+
// Send "Malformed UTF-8 characters"
468+
new AzureBrokerPropertiesStamp("\xB1\x31"),
469+
]);
470+
471+
$transport = new AzureTransport(
472+
self::createMock(SerializerInterface::class),
473+
new MockHttpClient(),
474+
new MockHttpClient(),
475+
AzureTransport::RECEIVE_MODE_RECEIVE_AND_DELETE
476+
);
477+
478+
$transport->send($envelope);
479+
}
480+
439481
/**
440482
* The encoded enveloppe must have a "body" key
441483
*/

0 commit comments

Comments
 (0)