1 | <?php |
||
13 | class SetVariablePass implements CompilerPassInterface |
||
14 | { |
||
15 | /** @const */ |
||
16 | const METHOD = 'setScopeVariables'; |
||
17 | |||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | 6 | public function process(ContainerBuilder $container) |
|
22 | { |
||
23 | 6 | if (!$container->has('psysh.shell')) { |
|
24 | 1 | return; |
|
25 | } |
||
26 | |||
27 | 5 | $variables = []; |
|
28 | |||
29 | 5 | foreach ($container->findTaggedServiceIds('psysh.variable', true) as $id => [$attributes]) { |
|
30 | 4 | $variable = $attributes['var'] ?? (\class_exists($id) ? $this->classify($id) : $id); |
|
31 | 4 | $variables[$variable] = new Reference($id); |
|
32 | } |
||
33 | |||
34 | 5 | if (empty($variables)) { |
|
35 | 1 | return; |
|
36 | } |
||
37 | |||
38 | 4 | $definition = $container->getDefinition('psysh.shell'); |
|
39 | |||
40 | 4 | if ($definition->hasMethodCall(self::METHOD)) { |
|
41 | $this->mergeMethodCall($definition, $variables); |
||
42 | |||
43 | return; |
||
44 | } |
||
45 | |||
46 | 4 | $definition->addMethodCall(self::METHOD, [$variables]); |
|
47 | 4 | } |
|
48 | |||
49 | // NameSpace\SomeName -> nameSpaceSomeName |
||
50 | 2 | private function classify(string $spec): string |
|
60 | |||
61 | private function mergeMethodCall(Definition $definition, array $variables): void |
||
80 | } |
||
81 |
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.