Skip to content

Commit

Permalink
fix(HasExceptionPipes, HasPipes): Refactor object type check for clarity
Browse files Browse the repository at this point in the history
- Refactor type checking for objects in both HasExceptionPipes and HasPipes traits.
- Change use of 'and' to '&&' for better readability.
- Improve code clarity by explicitly handling object instances before assigning class names.
- Ensure functionality remains consistent while enhancing maintainability.
  • Loading branch information
guanguans committed Nov 7, 2024
1 parent 2b0e938 commit cde3c20
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/Concerns/ConcreteCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*/
trait ConcreteCast
{
public function castToNull(): self
{
return $this->castTo('null');
}

public function castToInteger(): self
{
return $this->castTo('integer');
Expand Down
8 changes: 7 additions & 1 deletion src/Concerns/HasExceptionPipes.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public function removeExceptionPipes(string ...$findExceptionPipes): self
return $this->extendExceptionPipes(
static fn (Collection $exceptionPipes): Collection => $exceptionPipes
->reject(static function ($exceptionPipe) use ($findExceptionPipes): bool {
\is_object($exceptionPipe) and !$exceptionPipe instanceof \Closure and $exceptionPipe = \get_class($exceptionPipe);
if (\is_object($exceptionPipe) && !$exceptionPipe instanceof \Closure) {
$exceptionPipe = \get_class($exceptionPipe);
}

if (!\is_string($exceptionPipe)) {
return false;
Expand Down Expand Up @@ -104,6 +106,10 @@ private function spliceExceptionPipes(string $findExceptionPipe, array $exceptio
private function findByExceptionPipe(string $findExceptionPipe): int
{
foreach ($this->exceptionPipes as $idx => $exceptionPipe) {
if (\is_object($exceptionPipe) && !$exceptionPipe instanceof \Closure) {
$exceptionPipe = \get_class($exceptionPipe);
}

if (\is_string($exceptionPipe) && Str::of($exceptionPipe)->startsWith($findExceptionPipe)) {
return $idx;
}
Expand Down
8 changes: 7 additions & 1 deletion src/Concerns/HasPipes.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public function removePipes(string ...$findPipes): self
return $this->extendPipes(
static fn (Collection $pipes): Collection => $pipes
->reject(static function ($pipe) use ($findPipes): bool {
\is_object($pipe) and !$pipe instanceof \Closure and $pipe = \get_class($pipe);
if (\is_object($pipe) && !$pipe instanceof \Closure) {
$pipe = \get_class($pipe);
}

if (!\is_string($pipe)) {
return false;
Expand Down Expand Up @@ -107,6 +109,10 @@ private function splicePipes(string $findPipe, array $pipes, bool $before): self
private function findByPipe(string $findPipe): int
{
foreach ($this->pipes as $idx => $pipe) {
if (\is_object($pipe) && !$pipe instanceof \Closure) {
$pipe = \get_class($pipe);
}

if (\is_string($pipe) && Str::of($pipe)->startsWith($findPipe)) {
return $idx;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Symfony\Component\HttpKernel\Exception\HttpException;

it('can return JsonResponse', function (): void {
expect($this->apiResponse()->exception(new HttpException(Response::HTTP_BAD_REQUEST, $this->faker()->title())))
expect($this->apiResponse()->dump()->exception(new HttpException(Response::HTTP_BAD_REQUEST, $this->faker()->title())))
->toBeInstanceOf(JsonResponse::class)
->isClientError()->toBeTrue()
->and($this->apiResponse()->exception(new \RuntimeException($this->faker()->title(), 0)))
Expand Down
45 changes: 45 additions & 0 deletions tests/Concerns/ConcreteCastTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/** @noinspection PhpInternalEntityUsedInspection */
/** @noinspection AnonymousFunctionStaticInspection */
/** @noinspection StaticClosureCanBeUsedInspection */

declare(strict_types=1);

/**
* Copyright (c) 2024 guanguans<ityaozm@gmail.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/guanguans/laravel-api-response
*/

it('can cast type', function ($data): void {
expect($this->apiResponse()->castToNull()->success($data)->getData(true)['data'])->toBeNull()
->and($this->apiResponse()->castToNull()->success($data)->getData()->data)->toBeNull()
->and($this->apiResponse()->castToInteger()->success($data)->getData(true)['data'])->toBeInt()
->and($this->apiResponse()->castToInteger()->success($data)->getData()->data)->toBeInt()
->and($this->apiResponse()->castToFloat()->success($data)->getData(true)['data'])->toBeNumeric() // 0, float
->and($this->apiResponse()->castToFloat()->success($data)->getData()->data)->toBeNumeric() // 0, float
->and($this->apiResponse()->castToString()->success($data)->getData(true)['data'])->toBeString()
->and($this->apiResponse()->castToString()->success($data)->getData()->data)->toBeString()
->and($this->apiResponse()->castToBoolean()->success($data)->getData(true)['data'])->toBeBool()
->and($this->apiResponse()->castToBoolean()->success($data)->getData()->data)->toBeBool()
->and($this->apiResponse()->castToArray()->success($data)->getData(true)['data'])->toBeArray()
// ->and($this->apiResponse()->castToArray()->success($data)->getData()->data)->toBeArray()
// ->and($this->apiResponse()->castToObject()->success($data)->getData(true)['data'])->toBeObject()
->and($this->apiResponse()->castToObject()->success($data)->getData()->data)->toBeObject();
})->group(__DIR__, __FILE__)->with([
null,
1,
1.1,
'string',
true,
fn (): array => [],
fn (): array => ['foo', 'bar', 'baz'],
fn (): array => ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'],
(object) [],
(object) ['foo', 'bar', 'baz'],
(object) ['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'],
]);
7 changes: 2 additions & 5 deletions tests/Concerns/HasExceptionPipesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Guanguans\LaravelApiResponse\ExceptionPipes\AuthenticationExceptionPipe;
use Guanguans\LaravelApiResponse\ExceptionPipes\SetCodeExceptionPipe;
use Guanguans\LaravelApiResponse\ExceptionPipes\SetHeadersExceptionPipe;
use Guanguans\LaravelApiResponse\ExceptionPipes\SetMessageExceptionPipe;
use Guanguans\LaravelApiResponse\Exceptions\InvalidArgumentException;
use Illuminate\Support\Collection;
use function Spatie\Snapshots\assertMatchesObjectSnapshot;
Expand All @@ -31,8 +30,7 @@
it('can use exception pipes', function (): void {
expect($this->apiResponse())
->unshiftExceptionPipes()
->removeExceptionPipes(SetMessageExceptionPipe::with())
->pushExceptionPipes(SetMessageExceptionPipe::with())
->pushExceptionPipes()
->beforeExceptionPipes(
AuthenticationExceptionPipe::with(),
static fn (\Throwable $throwable, \Closure $next): array => $next($throwable),
Expand All @@ -47,8 +45,7 @@
static fn (\Throwable $throwable, \Closure $next): array => $next($throwable),
)
->afterExceptionPipes(
// SetHeadersExceptionPipe::with(),
SetMessageExceptionPipe::with(),
SetHeadersExceptionPipe::with(),
static fn (\Throwable $throwable, \Closure $next): array => $next($throwable),
)
->removeExceptionPipes(
Expand Down
9 changes: 8 additions & 1 deletion tests/Concerns/HasPipesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
MessagePipe::with(),
static fn (array $structure, \Closure $next): JsonResponse => $next($structure),
static fn (array $structure, \Closure $next): JsonResponse => $next($structure),
new class {
public function handle(array $structure, \Closure $next): JsonResponse
{
return $next($structure);
}
}
)
->beforePipes(
CallableDataPipe::with(),
Expand All @@ -51,7 +57,8 @@
)
->removePipes(
CallableDataPipe::with(),
CallableDataPipe::with()
CallableDataPipe::with(),
'class@anonymous'
)
->tapPipes(static function (Collection $pipes): void {
assertMatchesObjectSnapshot($pipes);
Expand Down
12 changes: 12 additions & 0 deletions tests/Pipes/PipesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@
->exception(new HttpException(500000))->toBeInstanceOf(JsonResponse::class)
->exception(new HttpException(600))->toBeInstanceOf(JsonResponse::class);
})->group(__DIR__, __FILE__);

it('can will throw InvalidArgumentException', function ($data): void {
$this->apiResponse()->castToFloat()->success($data);
})->group(__DIR__, __FILE__)->throws(InvalidArgumentException::class)->with([
'Infinity',
'-Infinity',
'NaN',
]);

it('can will throw Guanguans\LaravelApiResponse\Exceptions\InvalidArgumentException', function (): void {
$this->apiResponse()->castTo('resource')->success($this->faker()->name());
})->group(__DIR__, __FILE__)->throws(Guanguans\LaravelApiResponse\Exceptions\InvalidArgumentException::class, 'resource');
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- Guanguans\LaravelApiResponse\ExceptionPipes\ValidationExceptionPipe
- { }
- { }
- 'Guanguans\LaravelApiResponse\ExceptionPipes\SetMessageExceptionPipe:Whoops, looks like something went wrong.'
- { }
- { }
- Guanguans\LaravelApiResponse\ExceptionPipes\SetMessageExceptionPipe
- { }

0 comments on commit cde3c20

Please sign in to comment.