Skip to content

Commit

Permalink
fix phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Dec 16, 2024
1 parent fc25141 commit 2bc425b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/Gallery/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function createAlbum(AddAlbumRequest $request): string
/**
* Create a tag album.
*
* @param AddAlbumRequest $request
* @param AddTagAlbumRequest $request
*
* @return string
*/
Expand All @@ -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
*/
Expand Down
26 changes: 12 additions & 14 deletions app/Http/Resources/OpenApi/DataToResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand All @@ -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);

Expand Down

0 comments on commit 2bc425b

Please sign in to comment.