Conditions | 2 |
Paths | 2 |
Total Lines | 27 |
Code Lines | 17 |
Lines | 0 |
Ratio | 0 % |
1 | <?php |
||
31 | public function testParseAndDump($code, $expectedDump) |
||
32 | { |
||
33 | $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, new \PhpParser\Lexer\Emulative( |
||
34 | array( |
||
35 | 'usedAttributes' => array( |
||
36 | 'comments', |
||
37 | 'startLine', |
||
38 | 'endLine', |
||
39 | 'startTokenPos', |
||
40 | 'endTokenPos' |
||
41 | ) |
||
42 | ) |
||
43 | )); |
||
44 | $ast = $parser->parse($code); |
||
45 | |||
46 | $expressionCompiler = $this->getContext()->getExpressionCompiler(); |
||
47 | |||
48 | foreach ($ast as $node) { |
||
49 | $compiledExpression = $expressionCompiler ->compile($node); |
||
50 | self::assertInstanceOfCompiledExpression($compiledExpression); |
||
51 | } |
||
52 | |||
53 | self::assertSame( |
||
54 | json_encode($compiledExpression->__debugInfo()), |
||
|
|||
55 | trim($expectedDump) |
||
56 | ); |
||
57 | } |
||
58 | } |
||
59 |
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: