Skip to content

Commit e2a626d

Browse files
authored
Merge pull request #20 from minenok/laravel-10
laravel 10 and strict types
2 parents c431ee1 + 1816514 commit e2a626d

26 files changed

+162
-305
lines changed

composer.json

+44-38
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,47 @@
11
{
2-
"name": "lanin/laravel-api-exceptions",
3-
"description": "All in one solution for exception for JSON REST APIs on Laravel and Lumen.",
4-
"keywords": ["laravel", "framework", "api", "exceptions", "json"],
5-
"license": "MIT",
6-
"authors": [
7-
{
8-
"name": "Maxim Lanin",
9-
"email": "[email protected]",
10-
"homepage": "https://blog.lanin.me"
11-
}
12-
],
13-
"require": {
14-
"php": "^7.2|^8.0",
15-
"illuminate/support": "^7.0|^8.0|^9.0"
16-
},
17-
"require-dev": {
18-
"orchestra/testbench": "^5.0",
19-
"phpunit/phpunit": "^8.0",
20-
"mockery/mockery": "^1.0"
21-
},
22-
"autoload": {
23-
"psr-4": {
24-
"Lanin\\Laravel\\ApiExceptions\\": "src/"
25-
}
26-
},
27-
"autoload-dev": {
28-
"psr-4": {
29-
"Lanin\\Laravel\\ApiExceptions\\Tests\\": "tests/"
30-
}
31-
},
32-
"minimum-stability": "dev",
33-
"prefer-stable": true,
34-
"extra": {
35-
"laravel": {
36-
"providers": [
37-
"Lanin\\Laravel\\ApiExceptions\\ApiExceptionsServiceProvider"
38-
]
39-
}
2+
"name": "lanin/laravel-api-exceptions",
3+
"description": "All in one solution for exception for JSON REST APIs on Laravel and Lumen.",
4+
"keywords": [
5+
"laravel",
6+
"framework",
7+
"api",
8+
"exceptions",
9+
"json"
10+
],
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Maxim Lanin",
15+
"email": "[email protected]",
16+
"homepage": "https://blog.lanin.me"
4017
}
18+
],
19+
"require": {
20+
"php": ">=8.1",
21+
"illuminate/support": "^10.0"
22+
},
23+
"require-dev": {
24+
"orchestra/testbench": "^8.3",
25+
"phpunit/phpunit": ">=8.0",
26+
"mockery/mockery": "^1.0"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"Lanin\\Laravel\\ApiExceptions\\": "src/"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Lanin\\Laravel\\ApiExceptions\\Tests\\": "tests/"
36+
}
37+
},
38+
"minimum-stability": "dev",
39+
"prefer-stable": true,
40+
"extra": {
41+
"laravel": {
42+
"providers": [
43+
"Lanin\\Laravel\\ApiExceptions\\ApiExceptionsServiceProvider"
44+
]
45+
}
46+
}
4147
}

phpunit.xml

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
11-
>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php"
3+
colors="true" processIsolation="false" stopOnFailure="false"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache"
5+
backupStaticProperties="false">
6+
<coverage>
7+
<include>
8+
<directory suffix=".php">./src/</directory>
9+
</include>
10+
</coverage>
1211
<testsuites>
1312
<testsuite name="Package Test Suite">
1413
<directory suffix=".php">./tests/</directory>
1514
</testsuite>
1615
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">./src/</directory>
20-
</whitelist>
21-
</filter>
22-
</phpunit>
16+
</phpunit>

src/ApiException.php

+19-48
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Lanin\Laravel\ApiExceptions;
46

57
use Illuminate\Contracts\Support\Arrayable;
@@ -10,59 +12,32 @@
1012

1113
abstract class ApiException extends IdException implements Jsonable, \JsonSerializable, Arrayable
1214
{
13-
protected $headers = [];
14-
15-
/**
16-
* @param int $statusCode
17-
* @param string $id
18-
* @param string $message
19-
* @param \Throwable|null $previous
20-
* @param array $headers
21-
*/
22-
public function __construct($statusCode = 0, $id = '', $message = '', ?\Throwable $previous = null, array $headers = [])
23-
{
24-
$this->headers = $headers;
25-
15+
public function __construct(
16+
int $statusCode = 0,
17+
string $id = '',
18+
string $message = '',
19+
?\Throwable $previous = null,
20+
protected array $headers = [],
21+
) {
2622
parent::__construct($id, $message, $previous, $statusCode);
2723
}
2824

29-
/**
30-
* Return headers array.
31-
*
32-
* @return array
33-
*/
34-
public function getHeaders()
25+
public function getHeaders(): array
3526
{
3627
return $this->headers;
3728
}
3829

39-
/**
40-
* Convert the object into something JSON serializable.
41-
*
42-
* @return array
43-
*/
44-
public function jsonSerialize()
30+
public function jsonSerialize(): mixed
4531
{
4632
return $this->toArray();
4733
}
4834

49-
/**
50-
* Convert exception to JSON.
51-
*
52-
* @param int $options
53-
* @return array
54-
*/
55-
public function toJson($options = 0)
35+
public function toJson($options = 0): string
5636
{
5737
return json_encode($this->toArray());
5838
}
5939

60-
/**
61-
* Convert exception to array.
62-
*
63-
* @return array
64-
*/
65-
public function toArray()
40+
public function toArray(): array
6641
{
6742
$e = $this;
6843

@@ -76,7 +51,7 @@ public function toArray()
7651

7752
if ($e instanceof ApiException) {
7853
$meta = $this->getMeta();
79-
if (! empty($meta)) {
54+
if (!empty($meta)) {
8055
$return['meta'] = $meta;
8156
}
8257
}
@@ -88,20 +63,16 @@ public function toArray()
8863
return $return;
8964
}
9065

91-
/**
92-
* Prepare exception for report.
93-
*
94-
* @return string
95-
*/
96-
public function toReport()
66+
public function toReport(): self
9767
{
9868
return $this;
9969
}
10070

10171
/**
10272
* Add extra info to the output.
103-
*
104-
* @return mixed
10573
*/
106-
public function getMeta() {}
74+
public function getMeta(): array
75+
{
76+
return [];
77+
}
10778
}

src/ApiExceptionsServiceProvider.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Lanin\Laravel\ApiExceptions;
46

57
use Illuminate\Support\ServiceProvider;

src/BadRequestApiException.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Lanin\Laravel\ApiExceptions;
46

57
use Lanin\Laravel\ApiExceptions\Contracts\DontReport;
68

79
class BadRequestApiException extends ApiException implements DontReport
810
{
9-
/**
10-
* @param string $message
11-
* @param \Throwable|null $previous
12-
*/
13-
public function __construct($message = '', ?\Throwable $previous = null)
11+
public function __construct(string $message = '', ?\Throwable $previous = null)
1412
{
1513
if (empty($message)) {
1614
$message = 'The server cannot process the request due to its malformed syntax.';

src/ConflictApiException.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Lanin\Laravel\ApiExceptions;
46

57
use Lanin\Laravel\ApiExceptions\Contracts\DontReport;
68

79
class ConflictApiException extends ApiException implements DontReport
810
{
9-
/**
10-
* @param string $message
11-
* @param \Throwable $previous
12-
*/
13-
public function __construct($message = '', ?\Throwable $previous = null)
11+
public function __construct(string $message = '', ?\Throwable $previous = null)
1412
{
1513
if (empty($message)) {
1614
$message = 'Request could not be processed because of conflict.';

src/Contracts/DontReport.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Lanin\Laravel\ApiExceptions\Contracts;
46

57
interface DontReport
68
{
7-
8-
}
9+
}

src/Contracts/ShowsPrevious.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Lanin\Laravel\ApiExceptions\Contracts;
46

57
interface ShowsPrevious
68
{
7-
8-
}
9+
}

src/Contracts/ShowsTrace.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Lanin\Laravel\ApiExceptions\Contracts;
46

57
interface ShowsTrace
68
{
7-
8-
}
9+
}

src/ExceptionHandlerTrait.php

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Lanin\Laravel\ApiExceptions;
46

57
use Illuminate\Auth\Access\AuthorizationException;
68
use Illuminate\Auth\AuthenticationException;
79
use Illuminate\Database\Eloquent\ModelNotFoundException;
10+
use Illuminate\Http\JsonResponse;
11+
use Illuminate\Http\Request;
812
use Illuminate\Validation\ValidationException;
913
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
1014
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -14,22 +18,17 @@ trait ExceptionHandlerTrait
1418
/**
1519
* Report or log an exception.
1620
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
17-
*
18-
* @param \Throwable $e
19-
* @return void
20-
* @throws \Throwable
2121
*/
22-
public function report(\Throwable $e)
22+
public function report(\Throwable $e): void
2323
{
2424
parent::report($e instanceof ApiException ? $e->toReport() : $e);
2525
}
2626

2727
/**
2828
* Render an exception into an HTTP response.
2929
*
30-
* @param \Illuminate\Http\Request $request
31-
* @param \Throwable $e
32-
* @return \Illuminate\Http\Response
30+
* @param Request $request
31+
* @return JsonResponse
3332
*/
3433
public function render($request, \Throwable $e)
3534
{
@@ -42,11 +41,8 @@ public function render($request, \Throwable $e)
4241

4342
/**
4443
* Define exception.
45-
*
46-
* @param \Throwable $e
47-
* @return ApiException
4844
*/
49-
protected function resolveException(\Throwable $e)
45+
protected function resolveException(\Throwable $e): ApiException
5046
{
5147
switch (true) {
5248
case $e instanceof ApiException:
@@ -77,11 +73,8 @@ protected function resolveException(\Throwable $e)
7773

7874
/**
7975
* Format error message for API response.
80-
*
81-
* @param ApiException $exception
82-
* @return mixed
8376
*/
84-
protected function formatApiResponse(ApiException $exception)
77+
protected function formatApiResponse(ApiException $exception): array
8578
{
8679
return $exception->toArray();
8780
}

0 commit comments

Comments
 (0)