| 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 | 103 | public function pass(Expr $expr, Context $context) |
|
| 22 | { |
||
| 23 | 103 | $compiler = $context->getExpressionCompiler(); |
|
| 24 | |||
| 25 | 103 | if ($expr instanceof Expr\AssignOp) { |
|
| 26 | 31 | $right = $compiler->compile($expr->expr); |
|
| 27 | 103 | } elseif ($expr instanceof Expr\BinaryOp) { |
|
| 28 | 74 | $right = $compiler->compile($expr->right); |
|
| 29 | 74 | } |
|
| 30 | |||
| 31 | 103 | if ($right->getValue() == 1) { |
|
|
|
|||
| 32 | 42 | $context->notice( |
|
| 33 | 42 | 'division_by_one', |
|
| 34 | 42 | "You are trying to divide by one", |
|
| 35 | $expr |
||
| 36 | 42 | ); |
|
| 37 | |||
| 38 | 42 | return true; |
|
| 39 | } |
||
| 40 | |||
| 41 | 62 | 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: