Skip to content

Commit e69026c

Browse files
committed
pref: enhance exception handling and add PHPStan analysis
1 parent 00edadb commit e69026c

File tree

14 files changed

+183
-92
lines changed

14 files changed

+183
-92
lines changed

README-EN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
99
![Test](https://github.com/Jiannei/laravel-response/workflows/Test/badge.svg)
1010
[![Pest](https://img.shields.io/badge/Tests-Pest-green?style=flat)](https://pestphp.com)
11+
[![PHPStan](https://img.shields.io/badge/PHPStan-Level%206-brightgreen?style=flat)](https://phpstan.org)
12+
[![Code Coverage](https://img.shields.io/badge/Coverage-100%25-brightgreen?style=flat)](https://github.com/Jiannei/laravel-response)
1113
[![Pint](https://img.shields.io/badge/Code%20Style-Pint-orange?style=flat)](https://laravel.com/docs/pint)
1214
[![Latest Stable Version](https://poser.pugx.org/jiannei/laravel-response/v)](https://packagist.org/packages/jiannei/laravel-response)
1315
[![Total Downloads](https://poser.pugx.org/jiannei/laravel-response/downloads)](https://packagist.org/packages/jiannei/laravel-response)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
99
![Test](https://github.com/Jiannei/laravel-response/workflows/Test/badge.svg)
1010
[![Pest](https://img.shields.io/badge/Tests-Pest-green?style=flat)](https://pestphp.com)
11+
[![PHPStan](https://img.shields.io/badge/PHPStan-Level%206-brightgreen?style=flat)](https://phpstan.org)
12+
[![Code Coverage](https://img.shields.io/badge/Coverage-100%25-brightgreen?style=flat)](https://github.com/Jiannei/laravel-response)
1113
[![Pint](https://img.shields.io/badge/Code%20Style-Pint-orange?style=flat)](https://laravel.com/docs/pint)
1214
[![Latest Stable Version](https://poser.pugx.org/jiannei/laravel-response/v)](https://packagist.org/packages/jiannei/laravel-response)
1315
[![Total Downloads](https://poser.pugx.org/jiannei/laravel-response/downloads)](https://packagist.org/packages/jiannei/laravel-response)

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"orchestra/testbench": "^10.0.0",
1717
"pestphp/pest": "^3.5",
1818
"jiannei/laravel-enum": "dev-main",
19-
"laravel/pint": "^1.18.1"
19+
"laravel/pint": "^1.18.1",
20+
"larastan/larastan": "^3.0"
2021
},
2122
"autoload": {
2223
"psr-4": {
@@ -40,7 +41,10 @@
4041
},
4142
"scripts": {
4243
"test": "vendor/bin/pest",
43-
"style": "vendor/bin/pint"
44+
"test-coverage": "vendor/bin/pest --coverage",
45+
"test-coverage-html": "vendor/bin/pest --coverage-html coverage",
46+
"style": "vendor/bin/pint",
47+
"analyse": "vendor/bin/phpstan analyse"
4448
},
4549
"minimum-stability": "dev",
4650
"prefer-stable" : true,

phpstan.neon

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
includes:
2+
- vendor/larastan/larastan/extension.neon
3+
4+
parameters:
5+
level: 9
6+
paths:
7+
- src
8+
ignoreErrors:
9+
- '#Call to function is_(string|int)\(\) with .* will always evaluate to true#'
10+
- '#Result of && is always false#'
11+
- '#Possibly invalid array key type mixed#'

phpunit.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@
99
<directory suffix="Test.php">./tests</directory>
1010
</testsuite>
1111
</testsuites>
12+
<source>
13+
<include>
14+
<directory suffix=".php">./src</directory>
15+
</include>
16+
</source>
1217
</phpunit>

src/Contract/ResponseFormat.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public function response(): JsonResponse;
2424

2525
/**
2626
* Get formatted data.
27+
*
28+
* @return array<string, mixed>|null
2729
*/
2830
public function get(): ?array;
2931

@@ -32,20 +34,27 @@ public function get(): ?array;
3234
*
3335
* @return $this
3436
*/
35-
public function data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null): static;
37+
public function data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, mixed $error = null): static;
3638

3739
/**
3840
* Format paginator data.
41+
*
42+
* @param AbstractPaginator<int|string, mixed>|AbstractCursorPaginator<int|string, mixed>|Paginator<int|string, mixed> $resource
43+
* @return array<string, mixed>
3944
*/
4045
public function paginator(AbstractPaginator|AbstractCursorPaginator|Paginator $resource): array;
4146

4247
/**
4348
* Format collection resource data.
49+
*
50+
* @return array<string, mixed>
4451
*/
4552
public function resourceCollection(ResourceCollection $collection): array;
4653

4754
/**
4855
* Format JsonResource Data.
56+
*
57+
* @return array<string, mixed>
4958
*/
5059
public function jsonResource(JsonResource $resource): array;
5160
}

src/Http/Middleware/SetAcceptHeader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ class SetAcceptHeader
2020
/**
2121
* Handle an incoming request.
2222
*
23-
* @return \Illuminate\Http\Response
23+
* @param \Closure(\Illuminate\Http\Request): mixed $next
24+
* @return mixed
2425
*/
2526
public function handle(Request $request, Closure $next, string $type = 'json')
2627
{
27-
Str::contains($request->header('Accept'), $contentType = "application/$type") or
28+
Str::contains($request->header('Accept') ?? '', $contentType = "application/$type") or
2829
$request->headers->set('Accept', $contentType);
2930

3031
return $next($request);

src/Http/Middleware/ThrottleRequests.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes
6060
throw $this->buildException($key, $maxAttempts);
6161
}
6262

63-
$this->limiter->hit($key, $decayMinutes * 60);
63+
$this->limiter->hit($key, (int) ($decayMinutes * 60));
6464

6565
$response = $next($request);
6666

@@ -74,13 +74,12 @@ public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes
7474
* Resolve the number of attempts if the user is authenticated or not.
7575
*
7676
* @param \Illuminate\Http\Request $request
77-
* @param int|string $maxAttempts
7877
* @return int
7978
*/
80-
protected function resolveMaxAttempts($request, $maxAttempts)
79+
protected function resolveMaxAttempts($request, int|string $maxAttempts)
8180
{
82-
if (Str::contains($maxAttempts, '|')) {
83-
$maxAttempts = explode('|', $maxAttempts, 2)[$request->user() ? 1 : 0];
81+
if (Str::contains((string) $maxAttempts, '|')) {
82+
$maxAttempts = explode('|', (string) $maxAttempts, 2)[$request->user() ? 1 : 0];
8483
}
8584

8685
if (! is_numeric($maxAttempts) && $request->user()) {
@@ -101,10 +100,14 @@ protected function resolveMaxAttempts($request, $maxAttempts)
101100
protected function resolveRequestSignature($request)
102101
{
103102
if ($user = $request->user()) {
104-
return sha1($user->getAuthIdentifier());
103+
$identifier = $user->getAuthIdentifier();
104+
105+
return sha1(is_scalar($identifier) ? (string) $identifier : '');
105106
}
106107

107-
return sha1($request->fingerprint());
108+
$fingerprint = $request->fingerprint();
109+
110+
return sha1((string) $fingerprint);
108111
}
109112

110113
/**
@@ -163,7 +166,7 @@ protected function addHeaders(Response $response, $maxAttempts, $remainingAttemp
163166
* @param int $maxAttempts
164167
* @param int $remainingAttempts
165168
* @param int|null $retryAfter
166-
* @return array
169+
* @return array<string, mixed>
167170
*/
168171
protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = null)
169172
{

src/Providers/LaravelServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class_exists($formatter) && is_subclass_of($formatter, ResponseFormat::class) =>
3838
});
3939
}
4040

41-
protected function setupConfig()
41+
protected function setupConfig(): void
4242
{
4343
$path = dirname(__DIR__, 2).'/config/response.php';
4444

src/Providers/LumenServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class LumenServiceProvider extends LaravelServiceProvider
1515
{
16-
protected function setupConfig()
16+
protected function setupConfig(): void
1717
{
1818
$path = dirname(__DIR__, 2).'/config/response.php';
1919

0 commit comments

Comments
 (0)