Skip to content

Commit 57dba89

Browse files
committed
style: 调整代码格式与注释规范
1 parent d134a91 commit 57dba89

File tree

6 files changed

+45
-61
lines changed

6 files changed

+45
-61
lines changed

src/Http/Middleware/Etag.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ class Etag
1919
/**
2020
* Implement Etag support.
2121
*
22-
* @param \Illuminate\Http\Request $request the HTTP request
23-
* @param \Closure $next closure for the response
24-
*
22+
* @param \Illuminate\Http\Request $request the HTTP request
23+
* @param \Closure $next closure for the response
2524
* @return mixed
2625
*/
2726
public function handle(Request $request, Closure $next)
2827
{
2928
// If this was not a get or head request, just return
30-
if (!$request->isMethod('get') && !$request->isMethod('head')) {
29+
if (! $request->isMethod('get') && ! $request->isMethod('head')) {
3130
return $next($request);
3231
}
3332

src/Http/Middleware/ThrottleRequests.php

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ public function __construct(RateLimiter $limiter)
4242
/**
4343
* Handle an incoming request.
4444
*
45-
* @param \Illuminate\Http\Request $request
46-
* @param int|string $maxAttempts
47-
* @param float|int $decayMinutes
48-
* @param string $prefix
49-
*
45+
* @param \Illuminate\Http\Request $request
46+
* @param int|string $maxAttempts
47+
* @param float|int $decayMinutes
48+
* @param string $prefix
5049
* @return \Symfony\Component\HttpFoundation\Response
5150
*
5251
* @throws \Illuminate\Http\Exceptions\ThrottleRequestsException
@@ -74,9 +73,8 @@ public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes
7473
/**
7574
* Resolve the number of attempts if the user is authenticated or not.
7675
*
77-
* @param \Illuminate\Http\Request $request
78-
* @param int|string $maxAttempts
79-
*
76+
* @param \Illuminate\Http\Request $request
77+
* @param int|string $maxAttempts
8078
* @return int
8179
*/
8280
protected function resolveMaxAttempts($request, $maxAttempts)
@@ -85,7 +83,7 @@ protected function resolveMaxAttempts($request, $maxAttempts)
8583
$maxAttempts = explode('|', $maxAttempts, 2)[$request->user() ? 1 : 0];
8684
}
8785

88-
if (!is_numeric($maxAttempts) && $request->user()) {
86+
if (! is_numeric($maxAttempts) && $request->user()) {
8987
$maxAttempts = $request->user()->{$maxAttempts};
9088
}
9189

@@ -95,8 +93,7 @@ protected function resolveMaxAttempts($request, $maxAttempts)
9593
/**
9694
* Resolve request signature.
9795
*
98-
* @param \Illuminate\Http\Request $request
99-
*
96+
* @param \Illuminate\Http\Request $request
10097
* @return string
10198
*
10299
* @throws \RuntimeException
@@ -113,9 +110,8 @@ protected function resolveRequestSignature($request)
113110
/**
114111
* Create a 'too many attempts' exception.
115112
*
116-
* @param string $key
117-
* @param int $maxAttempts
118-
*
113+
* @param string $key
114+
* @param int $maxAttempts
119115
* @return \Illuminate\Http\Exceptions\ThrottleRequestsException
120116
*/
121117
protected function buildException($key, $maxAttempts)
@@ -136,8 +132,7 @@ protected function buildException($key, $maxAttempts)
136132
/**
137133
* Get the number of seconds until the next retry.
138134
*
139-
* @param string $key
140-
*
135+
* @param string $key
141136
* @return int
142137
*/
143138
protected function getTimeUntilNextRetry($key)
@@ -148,10 +143,9 @@ protected function getTimeUntilNextRetry($key)
148143
/**
149144
* Add the limit header information to the given response.
150145
*
151-
* @param int $maxAttempts
152-
* @param int $remainingAttempts
153-
* @param int|null $retryAfter
154-
*
146+
* @param int $maxAttempts
147+
* @param int $remainingAttempts
148+
* @param int|null $retryAfter
155149
* @return \Symfony\Component\HttpFoundation\Response
156150
*/
157151
protected function addHeaders(Response $response, $maxAttempts, $remainingAttempts, $retryAfter = null)
@@ -166,10 +160,9 @@ protected function addHeaders(Response $response, $maxAttempts, $remainingAttemp
166160
/**
167161
* Get the limit headers information.
168162
*
169-
* @param int $maxAttempts
170-
* @param int $remainingAttempts
171-
* @param int|null $retryAfter
172-
*
163+
* @param int $maxAttempts
164+
* @param int $remainingAttempts
165+
* @param int|null $retryAfter
173166
* @return array
174167
*/
175168
protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = null)
@@ -179,7 +172,7 @@ protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = nu
179172
'X-RateLimit-Remaining' => $remainingAttempts,
180173
];
181174

182-
if (!is_null($retryAfter)) {
175+
if (! is_null($retryAfter)) {
183176
$headers['Retry-After'] = $retryAfter;
184177
$headers['X-RateLimit-Reset'] = $this->availableAt($retryAfter);
185178
}
@@ -190,10 +183,9 @@ protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = nu
190183
/**
191184
* Calculate the number of remaining attempts.
192185
*
193-
* @param string $key
194-
* @param int $maxAttempts
195-
* @param int|null $retryAfter
196-
*
186+
* @param string $key
187+
* @param int $maxAttempts
188+
* @param int|null $retryAfter
197189
* @return int
198190
*/
199191
protected function calculateRemainingAttempts($key, $maxAttempts, $retryAfter = null)

src/Support/Facades/Format.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
/**
2323
* @method static \Jiannei\Response\Laravel\Support\Format data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null)
24-
* @method static array|null get()
25-
* @method static array paginator(AbstractPaginator|AbstractCursorPaginator|Paginator $resource)
26-
* @method static array resourceCollection(ResourceCollection $collection)
27-
* @method static array jsonResource(JsonResource $resource)
28-
* @method static JsonResponse response()
24+
* @method static array|null get()
25+
* @method static array paginator(AbstractPaginator|AbstractCursorPaginator|Paginator $resource)
26+
* @method static array resourceCollection(ResourceCollection $collection)
27+
* @method static array jsonResource(JsonResource $resource)
28+
* @method static JsonResponse response()
2929
*
3030
* @see \Jiannei\Response\Laravel\Support\Format
3131
*/

src/Support/Format.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ class Format implements ResponseFormat
3434

3535
protected int $statusCode = 200;
3636

37-
public function __construct(protected array $config = [])
38-
{
39-
}
37+
public function __construct(protected array $config = []) {}
4038

4139
/**
4240
* Return a new JSON response from the application.
@@ -60,9 +58,8 @@ public function get(): ?array
6058
/**
6159
* Core format.
6260
*
63-
* @param null $data
64-
* @param null $error
65-
*
61+
* @param null $data
62+
* @param null $error
6663
* @return Format
6764
*/
6865
public function data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null): static
@@ -135,7 +132,7 @@ protected function formatMessage(int $code, string $message = ''): ?string
135132
$localizationKey = implode('.', [Config::get('response.locale', 'enums'), $code]);
136133

137134
return match (true) {
138-
!$message && Lang::has($localizationKey) => Lang::get($localizationKey),
135+
! $message && Lang::has($localizationKey) => Lang::get($localizationKey),
139136
default => $message
140137
};
141138
}
@@ -240,7 +237,7 @@ protected function formatDataFields(array $data): array
240237
{
241238
return tap($data, function (&$item) {
242239
foreach ($this->config as $key => $config) {
243-
if (!Arr::has($item, $key)) {
240+
if (! Arr::has($item, $key)) {
244241
continue;
245242
}
246243

@@ -253,7 +250,7 @@ protected function formatDataFields(array $data): array
253250
$key = $alias;
254251
}
255252

256-
if (!$show) {
253+
if (! $show) {
257254
$item = Arr::except($item, $key);
258255
}
259256
}

src/Support/Traits/ExceptionTrait.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ trait ExceptionTrait
2727
/**
2828
* Custom Normal Exception response.
2929
*
30-
* @param Throwable|Exception $e
31-
*
30+
* @param Throwable|Exception $e
3231
* @return JsonResponse
3332
*/
3433
protected function prepareJsonResponse($request, $e)
@@ -37,7 +36,7 @@ protected function prepareJsonResponse($request, $e)
3736
// 或者是 ajax 请求,header 中包含 X-Requested-With:XMLHttpRequest;
3837
$exceptionConfig = Config::get('response.exception.'.get_class($e));
3938

40-
if (false === $exceptionConfig) {
39+
if ($exceptionConfig === false) {
4140
return parent::prepareJsonResponse($request, $e);
4241
}
4342

@@ -79,15 +78,14 @@ protected function buildFailedValidationResponse(Request $request, array $errors
7978
/**
8079
* Custom Failed Validation Response for Laravel.
8180
*
82-
* @param Request $request
83-
*
81+
* @param Request $request
8482
* @return JsonResponse
8583
*/
8684
protected function invalidJson($request, ValidationException $exception)
8785
{
8886
$exceptionConfig = Config::get('response.exception.'.ValidationException::class);
8987

90-
return false !== $exceptionConfig ? Response::fail(
88+
return $exceptionConfig !== false ? Response::fail(
9189
$exception->validator->errors()->first(),
9290
Arr::get($exceptionConfig, 'code', 422),
9391
$exception->errors()
@@ -97,15 +95,14 @@ protected function invalidJson($request, ValidationException $exception)
9795
/**
9896
* Custom Failed Authentication Response for Laravel.
9997
*
100-
* @param Request $request
101-
*
98+
* @param Request $request
10299
* @return \Illuminate\Http\RedirectResponse|JsonResponse
103100
*/
104101
protected function unauthenticated($request, AuthenticationException $exception)
105102
{
106103
$exceptionConfig = Config::get('response.exception.'.AuthenticationException::class);
107104

108-
return false !== $exceptionConfig && $request->expectsJson()
105+
return $exceptionConfig !== false && $request->expectsJson()
109106
? Response::errorUnauthorized($exceptionConfig['message'] ?? $exception->getMessage())
110107
: parent::unauthenticated($request, $exception);
111108
}

src/Support/Traits/JsonResponseTrait.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ trait JsonResponseTrait
1919
/**
2020
* Respond with an accepted response and associate a location and/or content if provided.
2121
*
22-
* @param array $data
22+
* @param array $data
2323
*/
2424
public function accepted($data = [], string $message = '', string $location = ''): JsonResponse
2525
{
@@ -33,7 +33,7 @@ public function accepted($data = [], string $message = '', string $location = ''
3333
/**
3434
* Respond with a created response and associate a location if provided.
3535
*
36-
* @param null $data
36+
* @param null $data
3737
*/
3838
public function created($data = [], string $message = '', string $location = ''): JsonResponse
3939
{
@@ -112,7 +112,7 @@ public function errorMethodNotAllowed(string $message = ''): JsonResponse
112112
/**
113113
* Return an fail response.
114114
*
115-
* @param null $errors
115+
* @param null $errors
116116
*/
117117
public function fail(string $message = '', int|\BackedEnum $code = 500, $errors = null): JsonResponse
118118
{
@@ -122,8 +122,7 @@ public function fail(string $message = '', int|\BackedEnum $code = 500, $errors
122122
/**
123123
* Return a success response.
124124
*
125-
* @param mixed $data
126-
*
125+
* @param mixed $data
127126
* @return JsonResponse
128127
*/
129128
public function success($data = [], string $message = '', int|\BackedEnum $code = 200)

0 commit comments

Comments
 (0)