From b41514a491247dfb839ad6854ede07653c80becc Mon Sep 17 00:00:00 2001 From: kj Date: Fri, 10 Oct 2025 19:15:07 -0300 Subject: [PATCH] fix(di): Improve parameter resolution and validation --- src/Libs/Synapsis.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Libs/Synapsis.php b/src/Libs/Synapsis.php index 8ffcf21..96035b2 100644 --- a/src/Libs/Synapsis.php +++ b/src/Libs/Synapsis.php @@ -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; }