|
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\Frontend\Builder\Instruction; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
13
|
|
|
use Railt\SDL\Frontend\Builder\BaseBuilder; |
|
14
|
|
|
use Railt\SDL\Frontend\Context\ContextInterface; |
|
15
|
|
|
use Railt\SDL\IR\SymbolTable\ValueInterface; |
|
16
|
|
|
use Railt\SDL\IR\SymbolTable\VarSymbol; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class VariableBuilder |
|
20
|
|
|
*/ |
|
21
|
|
|
class VariableBuilder extends BaseBuilder |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private const VARIABLE_DEFINITIONS = [ |
|
27
|
|
|
'ConstantDefinition', |
|
28
|
|
|
'VariableDefinition', |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param RuleInterface $rule |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
|
|
public function match(RuleInterface $rule): bool |
|
36
|
|
|
{ |
|
37
|
|
|
return \in_array($rule->getName(), self::VARIABLE_DEFINITIONS, true); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param ContextInterface $ctx |
|
42
|
|
|
* @param RuleInterface $rule |
|
43
|
|
|
* @return \Generator|\Closure |
|
44
|
|
|
*/ |
|
45
|
|
|
public function reduce(ContextInterface $ctx, RuleInterface $rule): \Generator |
|
46
|
|
|
{ |
|
47
|
|
|
$isConstant = $this->isConstant($rule); |
|
48
|
|
|
$variables = []; |
|
49
|
|
|
|
|
50
|
|
|
foreach ($this->getVariableNames($rule) as $variable) { |
|
51
|
|
|
$record = $variables[] = $ctx->declare($variable); |
|
52
|
|
|
|
|
53
|
|
|
$isConstant ? $record->lock() : $record->unlock(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
yield function () use ($rule, $variables) { |
|
57
|
|
|
/** @var ValueInterface $value */ |
|
58
|
|
|
$value = yield $this->getValueNode($rule); |
|
59
|
|
|
|
|
60
|
|
|
/** @var VarSymbol $variable */ |
|
61
|
|
|
foreach ($variables as $variable) { |
|
62
|
|
|
$variable->set($value); |
|
63
|
|
|
} |
|
64
|
|
|
}; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param RuleInterface $rule |
|
69
|
|
|
* @return iterable|string[] |
|
70
|
|
|
*/ |
|
71
|
|
|
private function getVariableNames(RuleInterface $rule): iterable |
|
72
|
|
|
{ |
|
73
|
|
|
foreach ($rule->find('> #VariableName') as $name) { |
|
74
|
|
|
yield $name->first('> :T_VARIABLE')->getValue(1); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param RuleInterface $rule |
|
80
|
|
|
* @return RuleInterface |
|
81
|
|
|
*/ |
|
82
|
|
|
private function getValueNode(RuleInterface $rule): RuleInterface |
|
83
|
|
|
{ |
|
84
|
|
|
return $rule->first('> #VariableValue')->getChild(0); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param RuleInterface $rule |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
private function isConstant(RuleInterface $rule): bool |
|
92
|
|
|
{ |
|
93
|
|
|
return $rule->getName() === 'ConstantDefinition'; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
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: