Skip to content

Commit 3d585cf

Browse files
committed
refactor: clean up
1 parent aa17283 commit 3d585cf

File tree

7 files changed

+18
-13
lines changed

7 files changed

+18
-13
lines changed

packages/http/src/IsRequest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function accepts(ContentType ...$contentTypes): bool
141141
$header = $this->headers->get(name: 'accept') ?? '';
142142

143143
/** @var array{mediaType:string,subType:string} */
144-
$mediaTypes = [];
144+
$acceptedMediaTypes = [];
145145

146146
foreach (str($header)->explode(separator: ',') as $acceptedType) {
147147
$acceptedType = str($acceptedType)->trim();
@@ -150,20 +150,20 @@ public function accepts(ContentType ...$contentTypes): bool
150150
continue;
151151
}
152152

153-
$mediaTypes[] = [
153+
$acceptedMediaTypes[] = [
154154
'mediaType' => $acceptedType->before('/')->toString(),
155155
'subType' => $acceptedType->afterFirst('/')->beforeLast(';q')->toString(),
156156
];
157157
}
158158

159-
if (count($mediaTypes) === 0) {
159+
if (count($acceptedMediaTypes) === 0) {
160160
return true;
161161
}
162162

163163
foreach ($contentTypes as $contentType) {
164-
[$mediaType, $subType] = explode('/', $contentType->value);
164+
[$mediaType, $subType] = explode('/', string: $contentType->value);
165165

166-
foreach ($mediaTypes as $acceptedType) {
166+
foreach ($acceptedMediaTypes as $acceptedType) {
167167
if (
168168
($acceptedType['mediaType'] === '*' || $acceptedType['mediaType'] === $mediaType)
169169
&& ($acceptedType['subType'] === '*' || $acceptedType['subType'] === $subType)

packages/router/src/Exceptions/ConvertsToResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ interface ConvertsToResponse
1414
/**
1515
* Gets a response to be sent to the client.
1616
*/
17-
public function toResponse(): Response;
17+
public function convertToResponse(): Response;
1818
}

packages/router/src/Exceptions/JsonExceptionRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function canRender(Throwable $throwable, Request $request): bool
3737
public function render(Throwable $throwable): Response
3838
{
3939
return match (true) {
40-
$throwable instanceof ConvertsToResponse => $throwable->toResponse(),
40+
$throwable instanceof ConvertsToResponse => $throwable->convertToResponse(),
4141
$throwable instanceof HttpRequestFailed => $this->renderErrorResponse($throwable->status, $throwable),
4242
$throwable instanceof ValidationFailed => $this->renderValidationErrorResponse($throwable),
4343
$throwable instanceof AccessWasDenied => $this->renderErrorResponse(Status::FORBIDDEN),

packages/router/src/HandleRouteExceptionMiddleware.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@ public function __invoke(Request $request, HttpMiddlewareCallable $next): Respon
3030

3131
/**
3232
* Some exceptions are not necessary to be thrown as-is, so we catch them here and convert them to equivalent responses.
33-
* - `ConvertsToResponse` is straightforward, this exception is made to return its own response.
3433
* - `RouteBindingFailed` would require to be handled manually in renderers, it's better DX to simply return a 404.
3534
*/
3635
private function forward(Request $request, HttpMiddlewareCallable $next): Response
3736
{
3837
try {
3938
return $next($request);
40-
} catch (ConvertsToResponse $convertsToResponse) {
41-
return $convertsToResponse->toResponse();
4239
} catch (RouteBindingFailed) {
4340
return new NotFound();
4441
}

packages/validation/src/Exceptions/ValidationFailed.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
namespace Tempest\Validation\Exceptions;
66

77
use Exception;
8+
use Tempest\Http\Response;
9+
use Tempest\Http\Responses\Invalid;
10+
use Tempest\Router\Exceptions\ConvertsToResponse;
811
use Tempest\Validation\FailingRule;
912

10-
final class ValidationFailed extends Exception
13+
final class ValidationFailed extends Exception implements ConvertsToResponse
1114
{
1215
/**
1316
* @template TKey of array-key
@@ -27,4 +30,9 @@ public function __construct(
2730
default => sprintf('Validation failed for %s.', is_object($subject) ? $subject::class : $subject),
2831
});
2932
}
33+
34+
public function convertToResponse(): Response
35+
{
36+
return new Invalid($this->subject, $this->failingRules, $this->targetClass);
37+
}
3038
}

tests/Integration/Http/Fixtures/ExceptionThatConvertsToRedirectResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
final class ExceptionThatConvertsToRedirectResponse extends Exception implements ConvertsToResponse
1414
{
15-
public function toResponse(): Response
15+
public function convertToResponse(): Response
1616
{
1717
return new Redirect('https://tempestphp.com');
1818
}

tests/Integration/Route/Fixtures/ExceptionThatConvertsToRedirectResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
final class ExceptionThatConvertsToRedirectResponse extends Exception implements ConvertsToResponse
1414
{
15-
public function toResponse(): Response
15+
public function convertToResponse(): Response
1616
{
1717
return new Redirect('https://tempestphp.com');
1818
}

0 commit comments

Comments
 (0)