| Conditions | 3 |
| Paths | 3 |
| Total Lines | 10 |
| Code Lines | 6 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 6 |
| CRAP Score | 3 |
| Changes | 0 | ||
| 1 | <?php |
||
| 22 | 2 | public function withoutRegion() |
|
| 23 | { |
||
| 24 | 2 | if(false !== strpos($this->value, '-')){ |
|
| 25 | 2 | $value = substr($this->value,0,strpos($this->value, '-')); |
|
| 26 | 1 | }else if(false !== strpos($this->value, '_')){ |
|
| 27 | 1 | $value = substr($this->value,0,strpos($this->value, '_')); |
|
| 28 | } |
||
| 29 | |||
| 30 | 2 | return new static($value); |
|
|
|
|||
| 31 | } |
||
| 32 | |||
| 48 |
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: