From 1707a9aaeead5f51307d9f3104908cbf6bdfeed5 Mon Sep 17 00:00:00 2001 From: Sean Tymon Date: Wed, 15 Jan 2025 09:15:56 +0000 Subject: [PATCH] cleanup --- src/Converters/FromClosure.php | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/src/Converters/FromClosure.php b/src/Converters/FromClosure.php index 692b732..8d15cf9 100644 --- a/src/Converters/FromClosure.php +++ b/src/Converters/FromClosure.php @@ -25,17 +25,9 @@ public static function convert(Closure $closure): ObjectSchema $schema = new ObjectSchema(); // TODO: handle descriptions - // $doc = $reflection->getDocComment(); foreach ($reflection->getParameters() as $parameter) { - $propertySchema = self::getPropertySchema($parameter); - - // No type hint, skip - if ($propertySchema === null) { - continue; - } - - $schema->properties($propertySchema); + $schema->properties(self::getSchemaFromReflectionParameter($parameter)); } return $schema; @@ -44,19 +36,15 @@ public static function convert(Closure $closure): ObjectSchema /** * Create a schema from a given type. */ - protected static function getPropertySchema(ReflectionParameter $parameter): ?Schema + protected static function getSchemaFromReflectionParameter(ReflectionParameter $parameter): Schema { $type = $parameter->getType(); - if ($type === null) { - return null; - } - $schema = self::getSchemaFromReflectionType($type); $schema->title($parameter->getName()); - if ($type->allowsNull()) { + if ($type === null || $type->allowsNull()) { $schema->nullable(); } @@ -89,21 +77,21 @@ protected static function getPropertySchema(ReflectionParameter $parameter): ?Sc * Resolve the schema instance from the given reflection type. */ protected static function getSchemaFromReflectionType( - ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType $type, + ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null $type, ): Schema { - $matchedTypes = match (true) { + $schemaTypes = match (true) { $type instanceof ReflectionUnionType, $type instanceof ReflectionIntersectionType => array_map( fn(ReflectionNamedType $t): SchemaType => self::resolveSchemaType($t), $type->getTypes(), ), - $type instanceof ReflectionNamedType && $type->getName() === 'mixed' => SchemaType::cases(), - $type instanceof ReflectionNamedType => [self::resolveSchemaType($type)], - default => throw new SchemaException('Unknown type: ' . $type), + // If the parameter is not typed or explicitly typed as mixed, we use all schema types + in_array($type?->getName(), ['mixed', null], true) => SchemaType::cases(), + default => [self::resolveSchemaType($type)], }; - return count($matchedTypes) === 1 - ? $matchedTypes[0]->instance() - : new UnionSchema($matchedTypes); + return count($schemaTypes) === 1 + ? $schemaTypes[0]->instance() + : new UnionSchema($schemaTypes); } /**