Skip to content

Commit f9fac0f

Browse files
committed
Fix or suppress Psalm 6 issues
1 parent 4c06cee commit f9fac0f

9 files changed

Lines changed: 44 additions & 15 deletions

psalm.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
</errorLevel>
2424
</MissingClosureReturnType>
2525

26+
<PossiblyUnusedMethod>
27+
<errorLevel type="suppress">
28+
<directory name="src"/>
29+
</errorLevel>
30+
</PossiblyUnusedMethod>
31+
2632
<PropertyNotSetInConstructor>
2733
<errorLevel type="suppress">
2834
<file name="src/Driver/Http1Driver.php"/>
@@ -35,5 +41,17 @@
3541
<directory name="src" />
3642
</errorLevel>
3743
</RiskyTruthyFalsyComparison>
44+
45+
<UnusedClass>
46+
<errorLevel type="suppress">
47+
<directory name="src"/>
48+
</errorLevel>
49+
</UnusedClass>
50+
51+
<UnusedClosureParam>
52+
<errorLevel type="suppress">
53+
<directory name="examples"/>
54+
</errorLevel>
55+
</UnusedClosureParam>
3856
</issueHandlers>
3957
</psalm>

src/ClientException.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*
1919
* Responses returned by request handlers after a ClientException has been thrown will be ignored, as a response has
2020
* already been generated by the error handler.
21+
*
22+
* @psalm-suppress ClassMustBeFinal
2123
*/
2224
class ClientException extends StreamException
2325
{

src/DefaultErrorHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function handleError(int $status, ?string $reason = null, ?Request $reque
1919
{
2020
self::$errorHtml ??= \file_get_contents(\dirname(__DIR__) . "/resources/error.html");
2121

22+
/** @psalm-suppress PossiblyFalseArgument */
2223
$body = self::$cache[$status] ??= \str_replace(
2324
["{code}", "{reason}"],
2425
// Using standard reason in HTML for caching purposes.

src/Driver/Http1Driver.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public function __construct(
7878
$this->deferredCancellation = new DeferredCancellation();
7979
}
8080

81+
/**
82+
* @psalm-suppress UnusedVariable
83+
*/
8184
#[\Override]
8285
public function handleClient(
8386
Client $client,
@@ -156,7 +159,10 @@ public function handleClient(
156159
throw new ClientException($this->client, "Bad Request: invalid request line", HttpStatus::BAD_REQUEST);
157160
}
158161

159-
/** @var non-empty-list<non-empty-string> $matches */
162+
/**
163+
* @psalm-suppress UnnecessaryVarAnnotation
164+
* @var non-empty-list<non-empty-string> $matches
165+
*/
160166
[$startLine, $method, $target, $protocol] = $matches;
161167
$rawHeaders = \substr($rawHeaders, \strlen($startLine));
162168

@@ -784,13 +790,12 @@ static function (int $bodySize) use (&$bodySizeLimit): void {
784790
));
785791
$this->bodyQueue = null;
786792

787-
/** @psalm-suppress TypeDoesNotContainType, RedundantCondition */
788-
($trailerDeferred ?? null)?->error($exception ??= new ClientException(
793+
/** @psalm-suppress TypeDoesNotContainNull, RedundantCondition */
794+
($trailerDeferred ?? null)?->error($exception ?? new ClientException(
789795
$this->client,
790796
"Client disconnected",
791797
HttpStatus::REQUEST_TIMEOUT
792798
));
793-
$trailerDeferred = null;
794799

795800
$this->deferredCancellation->cancel();
796801
}
@@ -852,6 +857,8 @@ protected function write(Request $request, Response $response): void
852857

853858
/**
854859
* HTTP/1.x response writer.
860+
*
861+
* @psalm-suppress UnusedVariable $chunk is used, and line-level suppression was not sufficient.
855862
*/
856863
private function send(?Future $lastWrite, Response $response, ?Request $request = null): void
857864
{
@@ -895,7 +902,6 @@ private function send(?Future $lastWrite, Response $response, ?Request $request
895902
if ($shouldClose) {
896903
$this->writableStream->end();
897904
}
898-
$need = null;
899905

900906
return;
901907
}

src/Driver/Http2Driver.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,19 @@ protected function write(Request $request, Response $response): void
270270

271271
$streamId = $this->streamIdMap[$request] ?? 1; // Default ID of 1 for upgrade requests.
272272

273+
/** @psalm-suppress RedundantCondition */
274+
\assert((bool)$streamId); // For Psalm.
275+
273276
if (!isset($this->streams[$streamId])) {
274277
return; // Client closed the stream or connection.
275278
}
276279

280+
$stream = $this->streams[$streamId];
281+
277282
if ($streamId & 1) {
278283
$this->timeoutTracker->update($streamId);
279284
}
280285

281-
$stream = $this->streams[$streamId]; // $this->streams[$streamId] may be unset in send().
282286
$deferred = new DeferredFuture;
283287
$stream->pendingWrite = $deferred->getFuture();
284288
$cancellation = $stream->deferredCancellation->getCancellation();
@@ -356,7 +360,7 @@ private function send(int $id, Response $response, Request $request, Cancellatio
356360
if ($request->getMethod() === "HEAD") {
357361
$this->streams[$id]->state |= Http2Stream::LOCAL_CLOSED;
358362
$this->writeData("", $id);
359-
$need = null;
363+
360364
return;
361365
}
362366

@@ -501,7 +505,7 @@ private function shutdown(?ClientException $reason = null): void
501505
} finally {
502506
if (!empty($this->streams)) {
503507
$reason ??= new ClientException($this->client, "Connection closed unexpectedly", Http2Parser::CANCEL);
504-
foreach ($this->streams as $id => $stream) {
508+
foreach ($this->streams as $id => $_stream) {
505509
$this->releaseStream($id, $reason);
506510
}
507511
}
@@ -925,7 +929,7 @@ public function handleConnectionWindowIncrement(int $windowSize): void
925929
#[\Override]
926930
public function handleHeaders(int $streamId, array $pseudo, array $headers, bool $streamEnded): void
927931
{
928-
foreach ($pseudo as $name => $value) {
932+
foreach ($pseudo as $name => $_value) {
929933
if (!isset(Http2Parser::KNOWN_REQUEST_PSEUDO_HEADERS[$name])) {
930934
throw new Http2StreamException(
931935
"Invalid pseudo header",
@@ -1242,7 +1246,7 @@ public function handleData(int $streamId, string $data): void
12421246
$future->ignore();
12431247

12441248
if ($stream->serverWindow <= self::MINIMUM_WINDOW) {
1245-
EventLoop::queue(function () use ($future, $stream, $streamId): void {
1249+
EventLoop::queue(function () use ($future, $streamId): void {
12461250
try {
12471251
$future->await();
12481252
} catch (\Throwable) {

src/Driver/Internal/Http2Stream.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Used in Http2Driver.
1111
*
1212
* @internal
13+
* @psalm-suppress PossiblyUnusedProperty False-positive.
1314
*/
1415
final class Http2Stream
1516
{

src/Driver/Internal/StreamTimeoutTracker.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class StreamTimeoutTracker
1414
/** @var array<int, \Closure(int): void> */
1515
private array $callbacks = [];
1616

17+
/** @psalm-suppress UnusedProperty False-positive. */
1718
private int $pingTimeout = 0;
1819

1920
/**

src/SocketHttpServer.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,6 @@ public function start(RequestHandler $requestHandler, ErrorHandler $errorHandler
293293
throw new CompositeException($exceptions);
294294
}
295295

296-
/**
297-
* @var SocketAddress $address
298-
* @var BindContext|null $bindContext
299-
*/
300296
foreach ($this->addresses as [$address, $bindContext]) {
301297
$tlsContext = $bindContext?->getTlsContext()?->withApplicationLayerProtocols(
302298
$this->httpDriverFactory->getApplicationLayerProtocols(),

test/test-server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
$server->start(new ClosureRequestHandler(static function (Request $request) {
4242
try {
4343
// Buffer entire body, but timeout after 100ms.
44-
$body = $request->getBody()->buffer(new TimeoutCancellation(0.1));
44+
$request->getBody()->buffer(new TimeoutCancellation(0.1));
4545
} catch (ClientException) {
4646
// Ignore failure to read body due to RST_STREAM frames.
4747
} catch (CancelledException) {

0 commit comments

Comments
 (0)