Skip to content

Commit

Permalink
Fix Psalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jul 2, 2023
1 parent 23e1958 commit 5b6879d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/Connection/Internal/Http1Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ public function parse(string $data = null): ?Response
if (!$this->bodyStarted && \in_array($this->state, [self::BODY_CHUNKS, self::BODY_IDENTITY, self::BODY_IDENTITY_EOF], true)) {
$this->bodyStarted = true;

$eventInvoker = events();
\assert($this->response !== null);

events()->responseBodyStart($this->request, $this->stream, $this->response);
$eventInvoker->responseBodyStart($this->request, $this->stream, $this->response);
}

switch ($this->state) {
Expand Down
16 changes: 9 additions & 7 deletions src/Connection/Internal/Http2ConnectionProcessor.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php declare(strict_types=1);
/** @noinspection PhpUnusedPrivateFieldInspection */

namespace Amp\Http\Client\Connection\Internal;

Expand Down Expand Up @@ -780,16 +779,17 @@ public function handleData(int $streamId, string $data): void
return;
}

$eventInvoker = events();
\assert($stream->response !== null);

if (!$stream->bodyStarted) {
$stream->bodyStarted = true;
events()->responseBodyStart($stream->request, $stream->stream, $stream->response);
$eventInvoker->responseBodyStart($stream->request, $stream->stream, $stream->response);
}

events()->responseBodyProgress($stream->request, $stream->stream, $stream->response);
$eventInvoker->responseBodyProgress($stream->request, $stream->stream, $stream->response);

$stream->body->pushAsync($data)->map(function () use ($streamId, $length): void {
$stream->body?->pushAsync($data)->map(function () use ($streamId, $length): void {
$stream = $this->streams[$streamId] ?? null;
// Stream may have closed while waiting for body data to be consumed.
if (!$stream) {
Expand Down Expand Up @@ -842,13 +842,15 @@ public function handleStreamEnd(int $streamId): void
$stream->body->complete();
$stream->body = null;

\assert($stream->response !== null);
$eventInvoker = events();

if (EventInvoker::getPhase($stream->request) === Phase::ResponseHeaders) {
events()->responseBodyStart($stream->request, $stream->stream, $stream->response);
\assert($stream->response !== null);
$eventInvoker->responseBodyStart($stream->request, $stream->stream, $stream->response);
}

events()->responseBodyEnd($stream->request, $stream->stream, $stream->response);
\assert($stream->response !== null);
$eventInvoker->responseBodyEnd($stream->request, $stream->stream, $stream->response);

// Trailers may have been received in handleHeaders(); if not, resolve with an empty set of trailers.
if ($stream->trailers !== null) {
Expand Down
5 changes: 2 additions & 3 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,8 @@ public function setBody(ReadableStream|string|null $body): void
{
$this->body = match (true) {
$body instanceof Payload => $body,
$body instanceof ReadableStream => new Payload($body),
\is_string($body), $body === null => new Payload((string) $body),
default => throw new \TypeError("Invalid body type: " . \get_debug_type($body)),
$body instanceof ReadableStream, \is_string($body) => new Payload($body),
$body === null => new Payload(''),
};
}

Expand Down
9 changes: 8 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function events(): EventListener
return EventInvoker::get();
}

/**
* @param array<EventListener> $eventListeners
* @param \Closure(Request):Response $requestHandler
*/
function processRequest(Request $request, array $eventListeners, \Closure $requestHandler): Response
{
if ($request->isStarted()) {
Expand All @@ -30,7 +34,10 @@ function processRequest(Request $request, array $eventListeners, \Closure $reque
}

$response->getTrailers()->map(fn () => events()->requestEnd($request, $response))->ignore();
$response->getTrailers()->catch(fn () => events()->requestFailed($request, $response))->ignore();
$response->getTrailers()->catch(fn (\Throwable $e) => events()->requestFailed(
$request,
$e instanceof HttpException ? $e : new HttpException('Unexpected exception: ' . $e->getMessage(), previous: $e)
))->ignore();

return $response;
}

0 comments on commit 5b6879d

Please sign in to comment.