282fa1bc5092a422bf2aad41191e320265965c59
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CuyZ\Valinor\Definition\Repository\Cache\Compiler;
6
7 use CuyZ\Valinor\Definition\MethodDefinition;
8 use CuyZ\Valinor\Definition\ParameterDefinition;
9
10 use function var_export;
11
12 /** @internal */
13 final class MethodDefinitionCompiler
14 {
15 private TypeCompiler $typeCompiler;
16
17 private ParameterDefinitionCompiler $parameterCompiler;
18
19 public function __construct(TypeCompiler $typeCompiler, AttributesCompiler $attributesCompiler)
20 {
21 $this->typeCompiler = $typeCompiler;
22 $this->parameterCompiler = new ParameterDefinitionCompiler($typeCompiler, $attributesCompiler);
23 }
24
25 public function compile(MethodDefinition $method): string
26 {
27 $parameters = array_map(
28 fn (ParameterDefinition $parameter) => $this->parameterCompiler->compile($parameter),
29 iterator_to_array($method->parameters())
30 );
31
32 $parameters = implode(', ', $parameters);
33 $isStatic = var_export($method->isStatic(), true);
34 $isPublic = var_export($method->isPublic(), true);
35 $returnType = $this->typeCompiler->compile($method->returnType());
36
37 return <<<PHP
38 new \CuyZ\Valinor\Definition\MethodDefinition(
39 '{$method->name()}',
40 '{$method->signature()}',
41 new \CuyZ\Valinor\Definition\Parameters($parameters),
42 $isStatic,
43 $isPublic,
44 $returnType
45 )
46 PHP;
47 }
48 }