1 | <?php |
||
20 | class VariableBuilder extends BaseBuilder |
||
21 | { |
||
22 | /** |
||
23 | * @var string[] |
||
24 | */ |
||
25 | private const VARIABLE_DEFINITIONS = [ |
||
26 | 'ConstantDefinition', |
||
27 | 'VariableDefinition', |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * @param RuleInterface $rule |
||
32 | * @return bool |
||
33 | */ |
||
34 | public function match(RuleInterface $rule): bool |
||
35 | { |
||
36 | return \in_array($rule->getName(), self::VARIABLE_DEFINITIONS, true); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param ContextInterface $ctx |
||
41 | * @param RuleInterface $rule |
||
42 | * @return \Generator|\Closure |
||
43 | */ |
||
44 | public function reduce(ContextInterface $ctx, RuleInterface $rule): \Generator |
||
45 | { |
||
46 | yield function() use ($ctx, $rule): \Generator { |
||
47 | /** @var ValueInterface $value */ |
||
48 | [$isConstant, $value] = [$this->isConstant($rule), yield $this->getValueNode($rule)]; |
||
49 | |||
50 | foreach ($rule->find('> #VariableName') as $name) { |
||
51 | $variable = $name->first('> :T_VARIABLE')->getValue(1); |
||
52 | |||
53 | $record = $ctx->declare($variable)->set($value); |
||
54 | |||
55 | $isConstant ? $record->lock() : $record->unlock(); |
||
56 | } |
||
57 | }; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param RuleInterface $rule |
||
62 | * @return RuleInterface |
||
63 | */ |
||
64 | private function getValueNode(RuleInterface $rule): RuleInterface |
||
65 | { |
||
66 | return $rule->first('> #VariableValue')->getChild(0); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param RuleInterface $rule |
||
71 | * @return bool |
||
72 | */ |
||
73 | private function isConstant(RuleInterface $rule): bool |
||
77 | } |
||
78 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.