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)
20 public function for(ReflectionParameter $reflection, ReflectionTypeResolver $typeResolver): ParameterDefinition
22 $name = $reflection->name;
23 $signature = Reflection::signature($reflection);
24 $type = $typeResolver->resolveType($reflection);
25 $isOptional = $reflection->isOptional();
26 $isVariadic = $reflection->isVariadic();
27 $attributes = $this->attributesFactory->for($reflection);
29 if ($reflection->isDefaultValueAvailable()) {
30 $defaultValue = $reflection->getDefaultValue();
31 } elseif ($reflection->isVariadic()) {
38 && ! $type instanceof UnresolvableType
39 && ! $type->accepts($defaultValue)
41 $type = UnresolvableType::forInvalidParameterDefaultValue($signature, $type, $defaultValue);
44 return new ParameterDefinition($name, $signature, $type, $isOptional, $isVariadic, $defaultValue, $attributes);