cc0d212072902c7583fa556ef9f4752ef4d02561
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CuyZ\Valinor\Definition\Repository\Reflection;
6
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;
12
13 /** @internal */
14 final class ReflectionParameterDefinitionBuilder
15 {
16 public function __construct(private AttributesRepository $attributesFactory) {}
17
18 public function for(ReflectionParameter $reflection, ReflectionTypeResolver $typeResolver): ParameterDefinition
19 {
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);
26
27 if ($reflection->isDefaultValueAvailable()) {
28 $defaultValue = $reflection->getDefaultValue();
29 } elseif ($reflection->isVariadic()) {
30 $defaultValue = [];
31 } else {
32 $defaultValue = null;
33 }
34
35 if ($isOptional
36 && ! $type instanceof UnresolvableType
37 && ! $type->accepts($defaultValue)
38 ) {
39 $type = UnresolvableType::forInvalidParameterDefaultValue($signature, $type, $defaultValue);
40 }
41
42 return new ParameterDefinition($name, $signature, $type, $isOptional, $isVariadic, $defaultValue, $attributes);
43 }
44 }