1 | <?php |
||
29 | final class NodeTraverser extends PhpParserNodeTraverser |
||
30 | { |
||
31 | private $prefix; |
||
32 | |||
33 | 338 | public function __construct(string $prefix) |
|
39 | |||
40 | /** |
||
41 | * @inheritdoc |
||
42 | */ |
||
43 | 338 | public function traverse(array $nodes) |
|
50 | |||
51 | /** |
||
52 | * Wrap the statements in a namespace when necessary:. |
||
53 | * |
||
54 | * ```php |
||
55 | * #!/usr/bin/env php |
||
56 | * <?php declare(strict_types=1); |
||
57 | * |
||
58 | * // A small comment |
||
59 | * |
||
60 | * if (\true) { |
||
61 | * echo "yo"; |
||
62 | * } |
||
63 | * ``` |
||
64 | * |
||
65 | * Will result in: |
||
66 | * |
||
67 | * ```php |
||
68 | * #!/usr/bin/env php |
||
69 | * <?php declare(strict_types=1); |
||
70 | * |
||
71 | * // A small comment |
||
72 | * |
||
73 | * namespace { |
||
74 | * if (\true) { |
||
75 | * echo "yo"; |
||
76 | * } |
||
77 | * } |
||
78 | * ``` |
||
79 | * |
||
80 | * @param Node[] $nodes |
||
81 | * |
||
82 | * @return Node[] |
||
83 | */ |
||
84 | 338 | private function wrapInNamespace(array $nodes): array |
|
109 | |||
110 | /** |
||
111 | * @param Node[] $nodes |
||
112 | * |
||
113 | * @return Node[] |
||
114 | */ |
||
115 | 338 | private function replaceGroupUseStatements(array $nodes): array |
|
142 | |||
143 | /** |
||
144 | * @param GroupUse $node |
||
145 | * |
||
146 | * @return Use_[] |
||
147 | */ |
||
148 | 6 | private function createUses_(GroupUse $node): array |
|
168 | } |
||
169 |
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: