4b4122a25b5378caf2b5a52aba7669ebf2fb7714
[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 }
19
20 public function for(ReflectionParameter $reflection, ReflectionTypeResolver $typeResolver): ParameterDefinition
21 {
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);
28
29 if ($reflection->isDefaultValueAvailable()) {
30 $defaultValue = $reflection->getDefaultValue();
31 } elseif ($reflection->isVariadic()) {
32 $defaultValue = [];
33 } else {
34 $defaultValue = null;
35 }
36
37 if ($isOptional
38 && ! $type instanceof UnresolvableType
39 && ! $type->accepts($defaultValue)
40 ) {
41 $type = UnresolvableType::forInvalidParameterDefaultValue($signature, $type, $defaultValue);
42 }
43
44 return new ParameterDefinition($name, $signature, $type, $isOptional, $isVariadic, $defaultValue, $attributes);
45 }
46 }