Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 9f948f3

Browse files
committed
Refactors for better type safety
Updates the methods of the `ErrorResponseGeneratorTrait` to accept the renderer and debug flag as arguments instead of relying on internal state; this ensures that they are of the type expected. Updated both implementations to pass the new arguments.
1 parent f99aa57 commit 9f948f3

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

src/Middleware/ErrorResponseGenerator.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,21 @@ public function __invoke(
4040
$response = $response->withStatus(Utils::getStatusCode($e, $response));
4141

4242
if ($this->renderer) {
43-
return $this->prepareTemplatedResponse($e, $response, [
44-
'response' => $response,
45-
'request' => $request,
46-
'uri' => (string) $request->getUri(),
47-
'status' => $response->getStatusCode(),
48-
'reason' => $response->getReasonPhrase(),
49-
]);
43+
return $this->prepareTemplatedResponse(
44+
$e,
45+
$this->renderer,
46+
[
47+
'response' => $response,
48+
'request' => $request,
49+
'uri' => (string) $request->getUri(),
50+
'status' => $response->getStatusCode(),
51+
'reason' => $response->getReasonPhrase(),
52+
],
53+
$this->debug,
54+
$response
55+
);
5056
}
5157

52-
return $this->prepareDefaultResponse($e, $response);
58+
return $this->prepareDefaultResponse($e, $this->debug, $response);
5359
}
5460
}

src/Response/ErrorResponseGeneratorTrait.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,29 @@ trait ErrorResponseGeneratorTrait
4747

4848
private function prepareTemplatedResponse(
4949
Throwable $e,
50-
ResponseInterface $response,
51-
array $templateData
50+
TemplateRendererInterface $renderer,
51+
array $templateData,
52+
bool $debug,
53+
ResponseInterface $response
5254
) : ResponseInterface {
53-
if ($this->debug) {
55+
if ($debug) {
5456
$templateData['error'] = $e;
5557
}
5658

5759
$response->getBody()
58-
->write($this->renderer->render($this->template, $templateData));
60+
->write($renderer->render($this->template, $templateData));
5961

6062
return $response;
6163
}
6264

63-
private function prepareDefaultResponse(Throwable $e, ResponseInterface $response) : ResponseInterface
64-
{
65+
private function prepareDefaultResponse(
66+
Throwable $e,
67+
bool $debug,
68+
ResponseInterface $response
69+
) : ResponseInterface {
6570
$message = 'An unexpected error occurred';
6671

67-
if ($this->debug) {
72+
if ($debug) {
6873
$message .= "; stack trace:\n\n" . $this->prepareStackTrace($e);
6974
}
7075

src/Response/ServerRequestErrorResponseGenerator.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,19 @@ public function __invoke(Throwable $e) : ResponseInterface
5151
$response = $response->withStatus(Utils::getStatusCode($e, $response));
5252

5353
if ($this->renderer) {
54-
return $this->prepareTemplatedResponse($e, $response, [
55-
'response' => $response,
56-
'status' => $response->getStatusCode(),
57-
'reason' => $response->getReasonPhrase(),
58-
]);
54+
return $this->prepareTemplatedResponse(
55+
$e,
56+
$this->renderer,
57+
[
58+
'response' => $response,
59+
'status' => $response->getStatusCode(),
60+
'reason' => $response->getReasonPhrase(),
61+
],
62+
$this->debug,
63+
$response
64+
);
5965
}
6066

61-
return $this->prepareDefaultResponse($e, $response);
67+
return $this->prepareDefaultResponse($e, $this->debug, $response);
6268
}
6369
}

0 commit comments

Comments
 (0)