Conditions | 4 |
Paths | 6 |
Total Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 20 |
Changes | 0 |
1 | <?php |
||
21 | public function pass(Expr $expr, Context $context) |
||
22 | { |
||
23 | $compiler = $context->getExpressionCompiler(); |
||
24 | |||
25 | if ($expr instanceof Expr\AssignOp) { |
||
26 | $right = $compiler->compile($expr->expr); |
||
27 | } elseif ($expr instanceof Expr\BinaryOp) { |
||
28 | $right = $compiler->compile($expr->right); |
||
29 | } |
||
30 | |||
31 | if ($right->getValue() == 1) { |
||
|
|||
32 | $context->notice( |
||
33 | 'division_by_one', |
||
34 | "You are trying to divide by one", |
||
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: