Skip to content

Commit 288c943

Browse files
loks0nclaude
andauthored
Chore: Bump PHPStan to level 7 (#246)
Adds generic array annotations and return types throughout src/ and tests/ to satisfy level 6, then hardens a handful of unchecked `string|false` / `resource|false` call sites for level 7. Behaviour is unchanged on the happy path; the only runtime-visible changes are in previously-broken error paths (e.g. opendir() returning false, getHeader() on array-valued headers). Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 60bcad8 commit 288c943

22 files changed

Lines changed: 159 additions & 130 deletions

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
parameters:
22
scanDirectories:
33
- vendor/swoole/ide-helper
4-
level: 5
4+
level: 7
55
paths:
66
- src
77
- tests

src/Http/Adapter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
abstract class Adapter
88
{
9-
abstract public function onStart(callable $callback);
10-
abstract public function onRequest(callable $callback);
11-
abstract public function start();
9+
abstract public function onStart(callable $callback): void;
10+
abstract public function onRequest(callable $callback): void;
11+
abstract public function start(): void;
1212
abstract public function getContainer(): Container;
1313
}

src/Http/Adapter/FPM/Request.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function setURI(string $uri): static
189189
* Method for querying upload files data. If $key is not found empty array will be returned.
190190
*
191191
* @param string $key
192-
* @return array
192+
* @return array<string, mixed>
193193
*/
194194
public function getFiles(string $key): array
195195
{
@@ -275,7 +275,13 @@ public function getHeader(string $key, string $default = ''): string
275275
{
276276
$headers = $this->generateHeaders();
277277

278-
return (isset($headers[$key])) ? $headers[$key] : $default;
278+
if (!isset($headers[$key])) {
279+
return $default;
280+
}
281+
282+
$value = $headers[$key];
283+
284+
return \is_array($value) ? \implode(', ', $value) : $value;
279285
}
280286

281287
/**
@@ -316,7 +322,7 @@ public function removeHeader(string $key): static
316322
*
317323
* Generate PHP input stream and parse it as an array in order to handle different content type of requests
318324
*
319-
* @return array
325+
* @return array<string, mixed>
320326
*/
321327
protected function generateInput(): array
322328
{
@@ -331,7 +337,7 @@ protected function generateInput(): array
331337
$length = (empty($length)) ? \strlen($contentType) : $length;
332338
$contentType = \substr($contentType, 0, $length);
333339

334-
$this->rawPayload = \file_get_contents('php://input');
340+
$this->rawPayload = \file_get_contents('php://input') ?: '';
335341

336342
switch ($contentType) {
337343
case 'application/json':
@@ -361,7 +367,7 @@ protected function generateInput(): array
361367
*
362368
* Parse request headers as an array for easy querying using the getHeader method
363369
*
364-
* @return array
370+
* @return array<string, string|array<int, string>>
365371
*/
366372
protected function generateHeaders(): array
367373
{

src/Http/Adapter/FPM/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function sendHeader(string $key, mixed $value): void
7474
*
7575
* @param string $name
7676
* @param string $value
77-
* @param array $options
77+
* @param array<string, mixed> $options
7878
* @return void
7979
*/
8080
protected function sendCookie(string $name, string $value, array $options): void

src/Http/Adapter/FPM/Server.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Server extends Adapter
99
{
1010
public function __construct(private Container $container) {}
1111

12-
public function onRequest(callable $callback)
12+
public function onRequest(callable $callback): void
1313
{
1414
$request = new Request();
1515
$response = new Response();
@@ -20,7 +20,7 @@ public function onRequest(callable $callback)
2020
\call_user_func($callback, $request, $response);
2121
}
2222

23-
public function onStart(callable $callback)
23+
public function onStart(callable $callback): void
2424
{
2525
\call_user_func($callback, $this);
2626
}
@@ -30,7 +30,7 @@ public function getContainer(): Container
3030
return $this->container;
3131
}
3232

33-
public function start()
33+
public function start(): void
3434
{
3535
return;
3636
}

src/Http/Adapter/Swoole/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(SwooleRequest $request)
3131
*/
3232
public function getRawPayload(): string
3333
{
34-
return $this->swoole->rawContent();
34+
return $this->swoole->rawContent() ?: '';
3535
}
3636

3737
/**

src/Http/Adapter/Swoole/Server.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ class Server extends Adapter
1515
protected const REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';
1616
protected Container $container;
1717

18+
/**
19+
* @param array<string, mixed> $settings
20+
*/
1821
public function __construct(string $host, ?string $port = null, array $settings = [], int $mode = SWOOLE_PROCESS, ?Container $container = null)
1922
{
2023
$this->server = new SwooleServer($host, (int) $port, $mode);
2124
$this->server->set($settings);
2225
$this->container = $container ?? new Container();
2326
}
2427

25-
public function onRequest(callable $callback)
28+
public function onRequest(callable $callback): void
2629
{
2730
$this->server->on('request', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
2831
$requestContainer = new Container($this->container);
@@ -49,7 +52,7 @@ public function getServer(): SwooleServer
4952
return $this->server;
5053
}
5154

52-
public function onStart(callable $callback)
55+
public function onStart(callable $callback): void
5356
{
5457
$this->server->on('start', function () use ($callback) {
5558
go(function () use ($callback) {
@@ -58,8 +61,8 @@ public function onStart(callable $callback)
5861
});
5962
}
6063

61-
public function start()
64+
public function start(): void
6265
{
63-
return $this->server->start();
66+
$this->server->start();
6467
}
6568
}

src/Http/Adapter/SwooleCoroutine/Server.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class Server extends Adapter
1919
/** @var callable|null */
2020
protected $onStartCallback = null;
2121

22+
/**
23+
* @param array<string, mixed> $settings
24+
*/
2225
public function __construct(
2326
string $host,
2427
?string $port = null,
@@ -30,7 +33,7 @@ public function __construct(
3033
$this->container = $container ?? new Container();
3134
}
3235

33-
public function onRequest(callable $callback)
36+
public function onRequest(callable $callback): void
3437
{
3538
$this->server->handle('/', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
3639
$requestContainer = new Container($this->container);
@@ -57,12 +60,12 @@ public function getServer(): SwooleServer
5760
return $this->server;
5861
}
5962

60-
public function onStart(callable $callback)
63+
public function onStart(callable $callback): void
6164
{
6265
$this->onStartCallback = $callback;
6366
}
6467

65-
public function start()
68+
public function start(): void
6669
{
6770
if ($this->onStartCallback) {
6871
\call_user_func($this->onStartCallback, $this);

src/Http/Files.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ public function load(string $directory, ?string $root = null): void
9595

9696
$handle = opendir(strval($directory));
9797

98+
if ($handle === false) {
99+
throw new Exception("Failed to open directory: {$directory}");
100+
}
101+
98102
while ($path = readdir($handle)) {
99103
$extension = pathinfo($path, PATHINFO_EXTENSION);
100104

src/Http/Http.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ public function getResources(array $list): array
464464
/**
465465
* Set a resource on the given scope.
466466
*
467-
* @param string[] $injections
467+
* @param list<string> $injections
468468
*/
469469
public function setResource(string $name, callable $callback, array $injections = []): void
470470
{
@@ -474,7 +474,7 @@ public function setResource(string $name, callable $callback, array $injections
474474
/**
475475
* Set a request-scoped resource on the current request's container.
476476
*
477-
* @param string[] $injections
477+
* @param list<string> $injections
478478
*/
479479
protected function setRequestResource(string $name, callable $callback, array $injections = []): void
480480
{
@@ -516,7 +516,7 @@ public static function isStage(): bool
516516
*
517517
* Get all application routes
518518
*
519-
* @return array
519+
* @return array<string, Route[]>
520520
*/
521521
public static function getRoutes(): array
522522
{
@@ -628,7 +628,7 @@ public static function onRequest(): Hook
628628
return $hook;
629629
}
630630

631-
public function start()
631+
public function start(): void
632632
{
633633

634634
$this->server->onRequest(
@@ -779,9 +779,9 @@ public function execute(Route $route, Request $request, Response $response): sta
779779
* Get Arguments
780780
*
781781
* @param Hook $hook
782-
* @param array $values
783-
* @param array $requestParams
784-
* @return array
782+
* @param array<string, mixed> $values
783+
* @param array<string, mixed> $requestParams
784+
* @return array<int, mixed>
785785
*
786786
* @throws Exception
787787
*/
@@ -1002,7 +1002,7 @@ private function runInternal(Request $request, Response $response): static
10021002
* Creates an validator instance and validate given value with given rules.
10031003
*
10041004
* @param string $key
1005-
* @param array $param
1005+
* @param array<string, mixed> $param
10061006
* @param mixed $value
10071007
* @return void
10081008
*

0 commit comments

Comments
 (0)