| Conditions | 4 |
| Paths | 4 |
| Total Lines | 20 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 20 |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 22 | public static function parseFileId($fileId) { |
||
| 23 | $arr = explode('_', $fileId); |
||
| 24 | if (count($arr) === 1) { |
||
| 25 | $fileId = $arr[0]; |
||
| 26 | $version = '0'; |
||
| 27 | } else if (count($arr) === 2) { |
||
| 28 | list($fileId, $instanceId) = $arr; |
||
| 29 | $version = '0'; |
||
| 30 | } else if (count($arr) === 3) { |
||
| 31 | list($fileId, $instanceId, $version) = $arr; |
||
| 32 | } else { |
||
| 33 | throw new \Exception('$fileId has not the expected format'); |
||
| 34 | } |
||
| 35 | |||
| 36 | return [ |
||
| 37 | $fileId, |
||
| 38 | $instanceId, |
||
|
|
|||
| 39 | $version, |
||
| 40 | ]; |
||
| 41 | } |
||
| 42 | |||
| 55 |
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: