Skip to content

Commit ee4ff27

Browse files
pedrosalprgithub-actions[bot]
authored andcommitted
Fix styling
1 parent 7032169 commit ee4ff27

File tree

7 files changed

+22
-41
lines changed

7 files changed

+22
-41
lines changed

src/Commands/LaravelApiProblemCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function resolveStubPath(string $stub): string
2323
{
2424
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
2525
? $customPath
26-
: __DIR__ . $stub;
26+
: __DIR__.$stub;
2727
}
2828

2929
protected function getDefaultNamespace($rootNamespace): string

src/Commands/LaravelApiProblemExceptionCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function resolveStubPath(string $stub): string
2424
{
2525
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
2626
? $customPath
27-
: __DIR__ . $stub;
27+
: __DIR__.$stub;
2828
}
2929

3030
protected function getDefaultNamespace($rootNamespace): string
@@ -34,7 +34,7 @@ protected function getDefaultNamespace($rootNamespace): string
3434

3535
protected function replaceClass($stub, $name)
3636
{
37-
$class = str_replace($this->getNamespace($name) . '\\', '', $name);
37+
$class = str_replace($this->getNamespace($name).'\\', '', $name);
3838

3939
// Do string replacement
4040
return str_replace('{{ class }}', $class, $stub);

src/Http/LaravelHttpApiProblem.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function __construct(
7272
if ($this->statusCode < 400 || $this->statusCode > 599) {
7373
$this->statusCode = 400;
7474
}
75-
if (!filter_var($this->type, FILTER_VALIDATE_URL) || empty($this->title)) {
75+
if (! filter_var($this->type, FILTER_VALIDATE_URL) || empty($this->title)) {
7676
$this->title = $this->getTitleForStatusCode($this->statusCode);
7777
$this->type = self::TYPE_ABOUT_BLANK;
7878
}
@@ -118,7 +118,7 @@ public function toArray(): array
118118
'title' => $this->title,
119119
'detail' => $this->detail,
120120
'instance' => $this->instance,
121-
'timestamp' => $this->timestamp->toJSON()
121+
'timestamp' => $this->timestamp->toJSON(),
122122
],
123123
$this->extensions
124124
);

src/LaravelApiProblem.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function __construct(
2727
if ($exception instanceof LaravelApiProblemException) {
2828
$this->apiProblemException = $exception;
2929
$this->apiProblemException();
30+
3031
return;
3132
}
3233
match (get_class($exception)) {
@@ -40,8 +41,6 @@ public function __construct(
4041

4142
/**
4243
* Render the exception as an HTTP response.
43-
*
44-
* @return JsonResponse
4544
*/
4645
public function render(): JsonResponse
4746
{
@@ -56,8 +55,6 @@ public function render(): JsonResponse
5655

5756
/**
5857
* Debug the class in array to view more details, such as: api problem and exception
59-
*
60-
* @return array
6158
*/
6259
public function toDebuggableArray(): array
6360
{
@@ -82,8 +79,6 @@ public function toDebuggableArray(): array
8279

8380
/**
8481
* Transform any exception into an http api problem with status code
85-
*
86-
* @param null|int $statusCode
8782
*/
8883
protected function default(?int $statusCode = null): void
8984
{
@@ -105,7 +100,7 @@ protected function validation(): void
105100
$extensions = [
106101
'errors' => ($this->exception instanceof ValidationException)
107102
? $this->exception->errors()
108-
: null
103+
: null,
109104
];
110105
$this->apiProblem = new LaravelHttpApiProblem(
111106
Response::HTTP_UNPROCESSABLE_ENTITY,
@@ -132,8 +127,6 @@ protected function apiProblemException(): void
132127

133128
/**
134129
* Get uri as instance
135-
*
136-
* @return string
137130
*/
138131
protected function getUriInstance(): string
139132
{
@@ -142,30 +135,25 @@ protected function getUriInstance(): string
142135

143136
/**
144137
* Get the context if it exists within the exception and return it as an extension
145-
*
146-
* @return array
147138
*/
148139
protected function getContextExceptionAsExtensions(): array
149140
{
150141
$extensions = [];
151-
if (!method_exists($this->exception, 'context')) {
142+
if (! method_exists($this->exception, 'context')) {
152143
return $extensions;
153144
}
154145
$context = $this->exception->context();
155146
if (is_array($context)) {
156147
$extensions = $context;
157-
} elseif (!empty($context)) {
148+
} elseif (! empty($context)) {
158149
$extensions = [$context];
159150
}
151+
160152
return $extensions;
161153
}
162154

163155
/**
164156
* Gets the status code from the exception code, or from the HttpException Interface, otherwise it returns an Internal Server Error
165-
*
166-
* @param null|int $code
167-
*
168-
* @return int
169157
*/
170158
protected function getStatusCode(?int $code): int
171159
{
@@ -177,17 +165,14 @@ protected function getStatusCode(?int $code): int
177165
? $this->exception->getStatusCode()
178166
: Response::HTTP_INTERNAL_SERVER_ERROR;
179167
}
168+
180169
return ($this->isStatusCodeInternalOrServerError($this->exception->getCode()))
181170
? $this->exception->getCode()
182171
: Response::HTTP_INTERNAL_SERVER_ERROR;
183172
}
184173

185174
/**
186175
* Checks if the status code is of the integer type and is in the range of Client and Server Errors
187-
*
188-
* @param null|int $statusCode
189-
*
190-
* @return bool
191176
*/
192177
protected function isStatusCodeInternalOrServerError(?int $statusCode): bool
193178
{
@@ -196,10 +181,6 @@ protected function isStatusCodeInternalOrServerError(?int $statusCode): bool
196181

197182
/**
198183
* Serialize the exception into an array
199-
*
200-
* @param \Throwable $throwable
201-
*
202-
* @return array
203184
*/
204185
private function serializeException(\Throwable $throwable): array
205186
{

tests/Feature/ApiProblemTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
declare(strict_types=1);
44

5-
use function Pest\Laravel\{getJson, postJson};
6-
use Illuminate\Auth\AuthenticationException;
75
use Illuminate\Auth\Access\AuthorizationException;
6+
use Illuminate\Auth\AuthenticationException;
87
use Illuminate\Http\Response;
98
use Illuminate\Support\Facades\Route;
109
use Pedrosalpr\LaravelApiProblem\Tests\Handlers\TestExceptionHandler;
1110
use Symfony\Component\HttpKernel\Exception\HttpException;
1211

12+
use function Pest\Laravel\getJson;
13+
use function Pest\Laravel\postJson;
14+
1315
// Antes de cada teste, substitua o manipulador de exceções do Laravel
1416
// pelo seu manipulador de exceções de teste.
1517
beforeEach(function () {
@@ -82,8 +84,6 @@
8284
]);
8385
});
8486

85-
86-
8787
test('method not allowed exception returns 405 problem json', function () {
8888
// Definir uma rota que só aceita POST
8989
Route::post('/api/only-post', fn () => ['message' => 'success']);
@@ -114,8 +114,6 @@
114114
]);
115115
});
116116

117-
118-
119117
test('http exception returns correct problem json', function () {
120118
Route::get('/api/custom-error', function () {
121119
throw new HttpException(Response::HTTP_UNAUTHORIZED, 'You do not have permission.');

tests/Handlers/TestExceptionHandler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Pedrosalpr\LaravelApiProblem\Tests\Handlers;
66

7+
use Illuminate\Auth\Access\AuthorizationException;
8+
use Illuminate\Auth\AuthenticationException;
79
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
810
use Illuminate\Http\Response;
9-
use Illuminate\Auth\AuthenticationException;
10-
use Illuminate\Auth\Access\AuthorizationException;
1111
use Illuminate\Validation\ValidationException;
1212
use Pedrosalpr\LaravelApiProblem\Http\LaravelHttpApiProblem;
1313
use Throwable;
@@ -21,12 +21,14 @@ public function render($request, Throwable $e)
2121

2222
if ($e instanceof AuthenticationException) {
2323
$problem = new LaravelHttpApiProblem(Response::HTTP_UNAUTHORIZED, $e->getMessage(), $instance);
24+
2425
return response()->json($problem->toArray(), $problem->getStatusCode())
2526
->withHeaders(['Content-Type' => $problem->getHeaderProblemJson()]);
2627
}
2728

2829
if ($e instanceof AuthorizationException) {
2930
$problem = new LaravelHttpApiProblem(Response::HTTP_FORBIDDEN, $e->getMessage(), $instance);
31+
3032
return response()->json($problem->toArray(), $problem->getStatusCode())
3133
->withHeaders(['Content-Type' => $problem->getHeaderProblemJson()]);
3234
}
@@ -36,6 +38,7 @@ public function render($request, Throwable $e)
3638
$statusCode = $e->getStatusCode();
3739
$title = Response::$statusTexts[$statusCode] ?? 'Unknown Error';
3840
$problem = new LaravelHttpApiProblem($statusCode, $e->getMessage(), $instance, title: $title);
41+
3942
return response()->json($problem->toArray(), $problem->getStatusCode())
4043
->withHeaders(['Content-Type' => $problem->getHeaderProblemJson()]);
4144
}
@@ -47,6 +50,7 @@ public function render($request, Throwable $e)
4750
instance: $instance,
4851
extensions: ['errors' => $e->errors()]
4952
);
53+
5054
return response()->json($problem->toArray(), $problem->getStatusCode())
5155
->withHeaders(['Content-Type' => $problem->getHeaderProblemJson()]);
5256
}

tests/Unit/ApiProblemTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
declare(strict_types=1);
44

55
use Pedrosalpr\LaravelApiProblem\Http\LaravelHttpApiProblem;
6-
use Pedrosalpr\LaravelApiProblem\Tests\TestCase;
7-
use function Pest\Laravel\{getJson, postJson};
86

97
test('it can be instantiated with required arguments', function () {
108
$problem = new LaravelHttpApiProblem(
@@ -64,7 +62,7 @@
6462
test('it includes extensions in the problem detail', function () {
6563
$extensions = [
6664
'trace_id' => '1a2b3c4d5e',
67-
'app_code' => 'AUTH-001'
65+
'app_code' => 'AUTH-001',
6866
];
6967

7068
$problem = new LaravelHttpApiProblem(

0 commit comments

Comments
 (0)