fix(di): Improve parameter resolution and validation

This commit is contained in:
kj
2025-10-10 19:15:07 -03:00
parent a1a15f492c
commit b41514a491

View File

@@ -118,7 +118,7 @@ class Synapsis
$reflectionClass = new ReflectionClass($className);
$constructor = $reflectionClass->getConstructor();
static::$instances[$className] = new $className(
...static::resolveParameterValues($constructor->getParameters())
...static::resolveParameterValues($constructor?->getParameters() ?? [])
);
}
@@ -141,13 +141,17 @@ class Synapsis
continue;
}
// Intentamos darle un valor equivalente a nulo si es una clase primitiva pare evitar
// errores innecesarios, lo
if ($parameter->getType()->isBuiltin()) {
throw new Exception('Primitive parameters expect at least a default value.');
$paramType = $parameter->getType();
if ($paramType === null) {
// If no type is declared, and it's not optional, we cannot resolve it.
throw new Exception('Untyped parameters expect at least a default value or must be optional.');
}
$values[] = static::resolveInstance($parameter->getType()->getName());
if ($paramType->isBuiltin()) {
throw new Exception("Primitive parameter '{$parameter->getName()}' expects at least a default value.");
}
$values[] = static::resolveInstance($paramType->getName());
}
return $values;
}