| Conditions | 4 |
| Paths | 6 |
| Total Lines | 22 |
| Code Lines | 13 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 21 | public function pass(Expr $expr, Context $context) |
||
| 22 | { |
||
| 23 | $compiler = $context->getExpressionCompiler(); |
||
| 24 | |||
| 25 | if ($expr instanceof Expr\AssignOp) { |
||
| 26 | $left = $compiler->compile($expr->var); |
||
| 27 | } elseif ($expr instanceof Expr\BinaryOp) { |
||
| 28 | $left = $compiler->compile($expr->left); |
||
| 29 | } |
||
| 30 | |||
| 31 | if ($left->getValue() == 0) { |
||
|
|
|||
| 32 | $context->notice( |
||
| 33 | 'division_from_zero', |
||
| 34 | "You are trying to divide from zero", |
||
| 35 | $expr |
||
| 36 | ); |
||
| 37 | |||
| 38 | return true; |
||
| 39 | } |
||
| 40 | |||
| 41 | return false; |
||
| 42 | } |
||
| 43 | |||
| 57 |
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: