3 declare(strict_types=1);
5 namespace CuyZ\Valinor\Definition\Repository\Reflection;
7 use CuyZ\Valinor\Definition\ParameterDefinition;
8 use CuyZ\Valinor\Definition\Repository\AttributesRepository;
9 use CuyZ\Valinor\Type\Types\UnresolvableType;
10 use CuyZ\Valinor\Utility\Reflection\Reflection;
11 use ReflectionParameter;
14 final class ReflectionParameterDefinitionBuilder
16 public function __construct(private AttributesRepository $attributesFactory) {}
18 public function for(ReflectionParameter $reflection, ReflectionTypeResolver $typeResolver): ParameterDefinition
20 $name = $reflection->name;
21 $signature = Reflection::signature($reflection);
22 $type = $typeResolver->resolveType($reflection);
23 $isOptional = $reflection->isOptional();
24 $isVariadic = $reflection->isVariadic();
25 $attributes = $this->attributesFactory->for($reflection);
27 if ($reflection->isDefaultValueAvailable()) {
28 $defaultValue = $reflection->getDefaultValue();
29 } elseif ($reflection->isVariadic()) {
36 && ! $type instanceof UnresolvableType
37 && ! $type->accepts($defaultValue)
39 $type = UnresolvableType::forInvalidParameterDefaultValue($signature, $type, $defaultValue);
42 return new ParameterDefinition($name, $signature, $type, $isOptional, $isVariadic, $defaultValue, $attributes);