From 2bc425bbe5e34ffad129f5efeb56199eadc2b411 Mon Sep 17 00:00:00 2001 From: ildyria Date: Mon, 16 Dec 2024 14:17:24 +0100 Subject: [PATCH] fix phpstan --- .../Controllers/Gallery/AlbumController.php | 4 +-- app/Http/Resources/OpenApi/DataToResponse.php | 26 +++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/Gallery/AlbumController.php b/app/Http/Controllers/Gallery/AlbumController.php index ab5a4892cdf..a7b7ae7c44a 100644 --- a/app/Http/Controllers/Gallery/AlbumController.php +++ b/app/Http/Controllers/Gallery/AlbumController.php @@ -102,7 +102,7 @@ public function createAlbum(AddAlbumRequest $request): string /** * Create a tag album. * - * @param AddAlbumRequest $request + * @param AddTagAlbumRequest $request * * @return string */ @@ -114,7 +114,7 @@ public function createTagAlbum(AddTagAlbumRequest $request, CreateTagAlbum $crea /** * Update the info of an Album. * - * @param AddAlbumRequest $request + * @param UpdateAlbumRequest $request * * @return EditableBaseAlbumResource */ diff --git a/app/Http/Resources/OpenApi/DataToResponse.php b/app/Http/Resources/OpenApi/DataToResponse.php index 3b6628ae56b..33d11686114 100644 --- a/app/Http/Resources/OpenApi/DataToResponse.php +++ b/app/Http/Resources/OpenApi/DataToResponse.php @@ -38,11 +38,11 @@ public function shouldHandle(Type $type): bool */ public function toSchema(Type $type): ?OpenApiType { + /** @phpstan-ignore-next-line */ $reflect = new \ReflectionClass($type->name); $props = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC); $ret = new OpenApiObjectType(); - /** @phpstan-ignore-next-line */ collect($props)->each(function ($prop) use ($ret) { $toConvertType = $this->convertReflected($prop->getType()); $ret->addProperty($prop->name, $this->openApiTransformer->transform($toConvertType)); @@ -71,7 +71,7 @@ public function reference(ObjectType $type): Reference * @throws \InvalidArgumentException * @throws LycheeLogicException */ - private function convertReflected(\ReflectionNamedType|\ReflectionUnionType|null $type): Type + private function convertReflected(\ReflectionNamedType|\ReflectionUnionType|\ReflectionType|null $type): Type { if ($type === null) { return new NullType(); @@ -85,26 +85,24 @@ private function convertReflected(\ReflectionNamedType|\ReflectionUnionType|null throw new LycheeLogicException('Intersection types are not supported.'); } - /** @phpstan-ignore-next-line @disregard */ - if ($type->isBuiltin()) { - return $this->handleBuiltin($type->getName()); + if (!$type instanceof \ReflectionNamedType) { + throw new LycheeLogicException('Unexpected reflection type.'); } - if ($type->getName() === 'Spatie\LaravelData\Data') { - throw new LycheeLogicException('Spatie\LaravelData\Data should not be used as return type.'); - } - - if ($type->getName() === 'Illuminate\Support\Collection') { - // Refactor me later. - return new ArrayType(); + $name = $type->getName(); + if ($type->isBuiltin()) { + return $this->handleBuiltin($name); } - return new ObjectType($type->getName()); + return match ($name) { + 'Spatie\LaravelData\Data' => throw new LycheeLogicException('Spatie\LaravelData\Data should not be used as return type.'), + 'Illuminate\Support\Collection' => new ArrayType(), // refactor me later. + default => new ObjectType($name), + }; } private function handleUnionType(\ReflectionUnionType $union): Type { - /** @phpstan-ignore-next-line @disregard */ $types = collect($union->getTypes())->map(fn ($type) => $this->convertReflected($type))->all(); $unionType = new Union($types);