1 | <?php |
||
30 | final class NodeTraverser extends PhpParserNodeTraverser |
||
31 | { |
||
32 | private $prefix; |
||
33 | |||
34 | 371 | public function __construct(string $prefix) |
|
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | 371 | public function traverse(array $nodes) |
|
51 | |||
52 | /** |
||
53 | * Wrap the statements in a namespace when necessary:. |
||
54 | * |
||
55 | * ```php |
||
56 | * #!/usr/bin/env php |
||
57 | * <?php declare(strict_types=1); |
||
58 | * |
||
59 | * // A small comment |
||
60 | * |
||
61 | * if (\true) { |
||
62 | * echo "yo"; |
||
63 | * } |
||
64 | * ``` |
||
65 | * |
||
66 | * Will result in: |
||
67 | * |
||
68 | * ```php |
||
69 | * #!/usr/bin/env php |
||
70 | * <?php declare(strict_types=1); |
||
71 | * |
||
72 | * // A small comment |
||
73 | * |
||
74 | * namespace { |
||
75 | * if (\true) { |
||
76 | * echo "yo"; |
||
77 | * } |
||
78 | * } |
||
79 | * ``` |
||
80 | * |
||
81 | * @param Node[] $nodes |
||
82 | * |
||
83 | * @return Node[] |
||
84 | */ |
||
85 | 371 | private function wrapInNamespace(array $nodes): array |
|
110 | |||
111 | /** |
||
112 | * @param Node[] $nodes |
||
113 | * |
||
114 | * @return Node[] |
||
115 | */ |
||
116 | 371 | private function replaceGroupUseStatements(array $nodes): array |
|
143 | |||
144 | /** |
||
145 | * @param GroupUse $node |
||
146 | * |
||
147 | * @return Use_[] |
||
148 | */ |
||
149 | 6 | private function createUses_(GroupUse $node): array |
|
169 | } |
||
170 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: