Skip to content

Commit b7320ca

Browse files
committed
Fix some of the phpstan errors.
1 parent 6e12db8 commit b7320ca

3 files changed

Lines changed: 33 additions & 10 deletions

File tree

src/Handler/Command.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function verifyCommandToken(string $commandToken): array|false
5656
* @var array<string, mixed> $payload
5757
*/
5858
$payload = json_decode($payloadJson, true);
59-
if ($payload === null) {
59+
if (!is_array($payload)) {
6060
error_log('commands.verifyCommandToken: invalid JSON decoding');
6161
return false;
6262
}
@@ -66,7 +66,8 @@ public function verifyCommandToken(string $commandToken): array|false
6666
return false;
6767
}
6868

69-
$iss = (string) $payload['iss'];
69+
/** @var string $iss */
70+
$iss = $payload['iss'];
7071

7172
if (!isset($issuers[$iss])) {
7273
error_log("commands.verifyCommandToken: unknown issuer - $iss");
@@ -81,7 +82,9 @@ public function verifyCommandToken(string $commandToken): array|false
8182
]
8283
]);
8384

84-
return json_decode($response->getBody()->getContents(), true);
85+
/** @var array<string, mixed> $decodedResponse */
86+
$decodedResponse = json_decode($response->getBody()->getContents(), true);
87+
return $decodedResponse;
8588
} catch (RequestException $e) {
8689
error_log('error verifying command token: ' . $e->getMessage());
8790
return false;
@@ -116,7 +119,9 @@ public function handleCommand(): string
116119
$this->helloResponse->send();
117120
}
118121

119-
$commandToken = (string) $this->helloRequest->fetch('command_token') ?? '';
122+
/** @var string $commandToken */
123+
$commandToken = $this->helloRequest->fetch('command_token');
124+
/** @var array<string, mixed> $claims */
120125
$claims = $this->verifyCommandToken($commandToken);
121126
// Ensure claims is an array before accessing its keys
122127
if (!$claims || !is_array($claims)) {
@@ -129,18 +134,28 @@ public function handleCommand(): string
129134
$this->helloResponse->send();
130135
}
131136

132-
$command = CommandEnum::tryFrom((string) $claims['command']) ?? '';
137+
/** @var string $commandEnum */
138+
$commandEnum = $claims['command'];
139+
$command = CommandEnum::tryFrom($commandEnum) ?? '';
133140
if (!$command) {
134141
$this->helloResponse->setStatusCode(400);
135142
return $this->helloResponse->json(['error' => 'unsupported_command']);
136143
}
137144

145+
/** @var string $iss */
146+
$iss = $claims['iss'];
147+
/** @var string $sub */
148+
$sub = $claims['sub'];
149+
/** @var string|null $tenant */
150+
$tenant = isset($claims['tenant']) ? $claims['tenant'] : null;
151+
/** @var array<string>|null $groups */
152+
$groups = isset($claims['groups']) ? $claims['groups'] : null;
138153
$commandClaims = new CommandClaims(
139-
iss: (string) $claims['iss'],
140-
sub: (string) $claims['sub'],
154+
iss: $iss,
155+
sub: $sub,
141156
command: $command,
142-
tenant: isset($claims['tenant']) ? (string) $claims['tenant'] : null,
143-
groups: isset($claims['groups']) ? (array) $claims['groups'] : null
157+
tenant: $tenant,
158+
groups: $groups
144159
);
145160

146161
$handler = $this->config->getCommandHandler();

tests/Handler/CommandTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public function testVerifyCommandTokenWithInvalidToken(): void
4949

5050
public function testVerifyCommandTokenWithValidToken(): void
5151
{
52-
$tokenPayload = base64_encode(json_encode(['iss' => 'https://issuer.hello.coop']));
52+
/** @var string tokenJsonPayload */
53+
$tokenJsonPayload = json_encode(['iss' => 'https://issuer.hello.coop']);
54+
$tokenPayload = base64_encode($tokenJsonPayload);
5355
$commandToken = 'header.' . $tokenPayload . '.signature';
5456

5557
$mockClient = $this->createMock(Client::class);

tests/Handler/InviteTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ class InviteTest extends TestCase
1111
use ServiceMocksTrait;
1212

1313
private Invite $invite;
14+
/**
15+
* @var array<string, mixed>
16+
*/
1417
private array $originalGet;
18+
/**
19+
* @var array<string, mixed>
20+
*/
1521
private array $originalCookie;
1622

1723
protected function setUp(): void

0 commit comments

Comments
 (0)