1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\SDL\Compiler\Builder\Dependent; |
11
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\RuleInterface; |
13
|
|
|
use Railt\Reflection\Contracts\Definition; |
14
|
|
|
use Railt\Reflection\Contracts\Type as TypeInterface; |
15
|
|
|
use Railt\Reflection\Definition\Dependent\EnumValueDefinition; |
16
|
|
|
use Railt\Reflection\Definition\EnumDefinition; |
17
|
|
|
use Railt\Reflection\Type; |
18
|
|
|
use Railt\SDL\Compiler\Ast\Dependent\EnumValueDefinitionNode; |
19
|
|
|
use Railt\SDL\Compiler\Ast\TypeHintNode; |
20
|
|
|
use Railt\SDL\Compiler\Builder\Builder; |
21
|
|
|
use Railt\SDL\Compiler\Builder\Virtual\TypeHint; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class EnumValueBuilder |
25
|
|
|
*/ |
26
|
|
|
class EnumValueBuilder extends Builder |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param RuleInterface|EnumValueDefinitionNode $rule |
30
|
|
|
* @param Definition|EnumDefinition $parent |
31
|
|
|
* @return Definition |
32
|
|
|
*/ |
33
|
|
|
public function build(RuleInterface $rule, Definition $parent): Definition |
34
|
|
|
{ |
35
|
|
|
$value = new EnumValueDefinition($parent, $rule->getValueName()); |
|
|
|
|
36
|
|
|
$value->withOffset($rule->getOffset()); |
37
|
|
|
$value->withDescription($rule->getDescription()); |
|
|
|
|
38
|
|
|
|
39
|
|
|
$this->when->runtime(function () use ($rule, $value) { |
40
|
|
|
if ($hint = $rule->getTypeHint()) { |
|
|
|
|
41
|
|
|
$value->withValue($this->valueOf($this->virtualTypeHint($value, $hint), $rule->getValue())); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($rule->getDirectives() as $ast) { |
|
|
|
|
45
|
|
|
$value->withDirective($this->dependent($ast, $value)); |
46
|
|
|
} |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
return $value; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param EnumValueDefinition $value |
54
|
|
|
* @param TypeHintNode $ast |
55
|
|
|
* @return TypeHint |
56
|
|
|
*/ |
57
|
|
|
private function virtualTypeHint(EnumValueDefinition $value, TypeHintNode $ast): TypeHint |
58
|
|
|
{ |
59
|
|
|
$virtual = new class($value->getDocument()) extends TypeHint |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
public static function getType(): TypeInterface |
62
|
|
|
{ |
63
|
|
|
return Type::of(Type::ENUM_VALUE); |
64
|
|
|
} |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
$virtual->withOffset($ast->getOffset()); |
68
|
|
|
$virtual->withTypeDefinition($ast->getTypeName()); |
69
|
|
|
$virtual->withModifiers($ast->getModifiers()); |
70
|
|
|
|
71
|
|
|
return $virtual; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: