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