Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tymondesigns committed Jan 15, 2025
1 parent e11c7f1 commit 1707a9a
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions src/Converters/FromClosure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Check failure on line 43 in src/Converters/FromClosure.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $type of static method Cortex\JsonSchema\Converters\FromClosure::getSchemaFromReflectionType() expects ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null, ReflectionType|null given.

$schema->title($parameter->getName());

if ($type->allowsNull()) {
if ($type === null || $type->allowsNull()) {
$schema->nullable();
}

Expand Down Expand Up @@ -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),

Check failure on line 84 in src/Converters/FromClosure.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $callback of function array_map expects (callable(ReflectionType): mixed)|null, Closure(ReflectionNamedType): Cortex\JsonSchema\Enums\SchemaType given.
$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);
}

/**
Expand Down

0 comments on commit 1707a9a

Please sign in to comment.