Skip to content

Commit

Permalink
Apply fixes from StyleCI
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleCIBot committed Jan 11, 2024
1 parent c924d22 commit db6f195
Show file tree
Hide file tree
Showing 27 changed files with 89 additions and 76 deletions.
3 changes: 1 addition & 2 deletions config/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand Down Expand Up @@ -33,7 +33,6 @@
'code' => 422,
],
\Illuminate\Auth\AuthenticationException::class => [

],
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class => [
'message' => '',
Expand Down
2 changes: 1 addition & 1 deletion src/Contract/ResponseFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand Down
9 changes: 5 additions & 4 deletions src/Http/Middleware/Etag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand All @@ -19,14 +19,15 @@ class Etag
/**
* Implement Etag support.
*
* @param \Illuminate\Http\Request $request the HTTP request
* @param \Closure $next closure for the response
* @param \Illuminate\Http\Request $request the HTTP request
* @param \Closure $next closure for the response
*
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
// If this was not a get or head request, just return
if (! $request->isMethod('get') && ! $request->isMethod('head')) {
if (!$request->isMethod('get') && !$request->isMethod('head')) {
return $next($request);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/SetAcceptHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand Down
52 changes: 30 additions & 22 deletions src/Http/Middleware/ThrottleRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand Down Expand Up @@ -42,10 +42,11 @@ public function __construct(RateLimiter $limiter)
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param int|string $maxAttempts
* @param float|int $decayMinutes
* @param string $prefix
* @param \Illuminate\Http\Request $request
* @param int|string $maxAttempts
* @param float|int $decayMinutes
* @param string $prefix
*
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Illuminate\Http\Exceptions\ThrottleRequestsException
Expand Down Expand Up @@ -73,8 +74,9 @@ public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes
/**
* Resolve the number of attempts if the user is authenticated or not.
*
* @param \Illuminate\Http\Request $request
* @param int|string $maxAttempts
* @param \Illuminate\Http\Request $request
* @param int|string $maxAttempts
*
* @return int
*/
protected function resolveMaxAttempts($request, $maxAttempts)
Expand All @@ -83,7 +85,7 @@ protected function resolveMaxAttempts($request, $maxAttempts)
$maxAttempts = explode('|', $maxAttempts, 2)[$request->user() ? 1 : 0];
}

if (! is_numeric($maxAttempts) && $request->user()) {
if (!is_numeric($maxAttempts) && $request->user()) {
$maxAttempts = $request->user()->{$maxAttempts};
}

Expand All @@ -93,7 +95,8 @@ protected function resolveMaxAttempts($request, $maxAttempts)
/**
* Resolve request signature.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Http\Request $request
*
* @return string
*
* @throws \RuntimeException
Expand All @@ -110,8 +113,9 @@ protected function resolveRequestSignature($request)
/**
* Create a 'too many attempts' exception.
*
* @param string $key
* @param int $maxAttempts
* @param string $key
* @param int $maxAttempts
*
* @return \Illuminate\Http\Exceptions\ThrottleRequestsException
*/
protected function buildException($key, $maxAttempts)
Expand All @@ -132,7 +136,8 @@ protected function buildException($key, $maxAttempts)
/**
* Get the number of seconds until the next retry.
*
* @param string $key
* @param string $key
*
* @return int
*/
protected function getTimeUntilNextRetry($key)
Expand All @@ -143,9 +148,10 @@ protected function getTimeUntilNextRetry($key)
/**
* Add the limit header information to the given response.
*
* @param int $maxAttempts
* @param int $remainingAttempts
* @param int|null $retryAfter
* @param int $maxAttempts
* @param int $remainingAttempts
* @param int|null $retryAfter
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function addHeaders(Response $response, $maxAttempts, $remainingAttempts, $retryAfter = null)
Expand All @@ -160,9 +166,10 @@ protected function addHeaders(Response $response, $maxAttempts, $remainingAttemp
/**
* Get the limit headers information.
*
* @param int $maxAttempts
* @param int $remainingAttempts
* @param int|null $retryAfter
* @param int $maxAttempts
* @param int $remainingAttempts
* @param int|null $retryAfter
*
* @return array
*/
protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = null)
Expand All @@ -172,7 +179,7 @@ protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = nu
'X-RateLimit-Remaining' => $remainingAttempts,
];

if (! is_null($retryAfter)) {
if (!is_null($retryAfter)) {
$headers['Retry-After'] = $retryAfter;
$headers['X-RateLimit-Reset'] = $this->availableAt($retryAfter);
}
Expand All @@ -183,9 +190,10 @@ protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = nu
/**
* Calculate the number of remaining attempts.
*
* @param string $key
* @param int $maxAttempts
* @param int|null $retryAfter
* @param string $key
* @param int $maxAttempts
* @param int|null $retryAfter
*
* @return int
*/
protected function calculateRemainingAttempts($key, $maxAttempts, $retryAfter = null)
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/LumenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand Down
2 changes: 1 addition & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand Down
12 changes: 6 additions & 6 deletions src/Support/Facades/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand All @@ -21,11 +21,11 @@

/**
* @method static \Jiannei\Response\Laravel\Support\Format data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null)
* @method static array|null get()
* @method static array paginator(AbstractPaginator|AbstractCursorPaginator|Paginator $resource)
* @method static array resourceCollection(ResourceCollection $collection)
* @method static array jsonResource(JsonResource $resource)
* @method static JsonResponse response()
* @method static array|null get()
* @method static array paginator(AbstractPaginator|AbstractCursorPaginator|Paginator $resource)
* @method static array resourceCollection(ResourceCollection $collection)
* @method static array jsonResource(JsonResource $resource)
* @method static JsonResponse response()
*
* @see \Jiannei\Response\Laravel\Support\Format
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Facades/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand Down
13 changes: 7 additions & 6 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand Down Expand Up @@ -60,8 +60,9 @@ public function get(): ?array
/**
* Core format.
*
* @param null $data
* @param null $error
* @param null $data
* @param null $error
*
* @return Format
*/
public function data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null): static
Expand Down Expand Up @@ -132,7 +133,7 @@ protected function formatMessage(int $code, string $message = ''): ?string
$localizationKey = implode('.', [Config::get('response.locale', 'enums'), $code]);

return match (true) {
! $message && Lang::has($localizationKey) => Lang::get($localizationKey),
!$message && Lang::has($localizationKey) => Lang::get($localizationKey),
default => $message
};
}
Expand Down Expand Up @@ -235,7 +236,7 @@ protected function formatDataFields(array $data): array
{
return tap($data, function (&$item) {
foreach ($this->config as $key => $config) {
if (! Arr::has($item, $key)) {
if (!Arr::has($item, $key)) {
continue;
}

Expand All @@ -248,7 +249,7 @@ protected function formatDataFields(array $data): array
$key = $alias;
}

if (! $show) {
if (!$show) {
$item = Arr::except($item, $key);
}
}
Expand Down
19 changes: 11 additions & 8 deletions src/Support/Traits/ExceptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the jiannei/laravel-response.
*
* (c) Jiannei <[email protected]>
* (c) Jiannei <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
Expand All @@ -27,7 +27,8 @@ trait ExceptionTrait
/**
* Custom Normal Exception response.
*
* @param Throwable|Exception $e
* @param Throwable|Exception $e
*
* @return JsonResponse
*/
protected function prepareJsonResponse($request, $e)
Expand All @@ -36,7 +37,7 @@ protected function prepareJsonResponse($request, $e)
// 或者是 ajax 请求,header 中包含 X-Requested-With:XMLHttpRequest;
$exceptionConfig = Config::get('response.exception.'.get_class($e));

if ($exceptionConfig === false) {
if (false === $exceptionConfig) {
return parent::prepareJsonResponse($request, $e);
}

Expand Down Expand Up @@ -78,14 +79,15 @@ protected function buildFailedValidationResponse(Request $request, array $errors
/**
* Custom Failed Validation Response for Laravel.
*
* @param Request $request
* @param Request $request
*
* @return JsonResponse
*/
protected function invalidJson($request, ValidationException $exception)
{
$exceptionConfig = Config::get('response.exception.'.ValidationException::class);

return $exceptionConfig !== false ? Response::fail(
return false !== $exceptionConfig ? Response::fail(
$exception->validator->errors()->first(),
Arr::get($exceptionConfig, 'code', 422),
$exception->errors()
Expand All @@ -95,14 +97,15 @@ protected function invalidJson($request, ValidationException $exception)
/**
* Custom Failed Authentication Response for Laravel.
*
* @param Request $request
* @return \Illuminate\Http\RedirectResponse | JsonResponse
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse|JsonResponse
*/
protected function unauthenticated($request, AuthenticationException $exception)
{
$exceptionConfig = Config::get('response.exception.'.AuthenticationException::class);

return $exceptionConfig !== false && $request->expectsJson()
return false !== $exceptionConfig && $request->expectsJson()
? Response::errorUnauthorized($exceptionConfig['message'] ?? $exception->getMessage())
: parent::unauthenticated($request, $exception);
}
Expand Down
Loading

0 comments on commit db6f195

Please sign in to comment.