| Conditions | 6 |
| Paths | 1 |
| Total Lines | 27 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 8 | function foldersResolvePath(array $folders, $sep = '::', $file_exists = 'file_exists') { |
||
| 9 | return function(ResolvePathArgs $args, $next) use ($folders, $sep, $file_exists) { |
||
| 10 | if (strpos($args->path, $sep) === false) { |
||
| 11 | return $next($args); |
||
| 12 | } |
||
| 13 | |||
| 14 | list($folder, $name) = explode($sep, $args->path); |
||
| 15 | if (!isset($folders[$folder])) { |
||
| 16 | return $next($args); |
||
| 17 | } |
||
| 18 | $folder_struct = $folders[$folder]; |
||
| 19 | |||
| 20 | foreach ($folder_struct['prefixes'] as $prefix) { |
||
| 21 | $path = $next($args->withPath( |
||
| 22 | Plates\Util\joinPath([$prefix, $name]) |
||
| 23 | )); |
||
| 24 | |||
| 25 | // no need to check if file exists if we only have prefix |
||
| 26 | if (count($folder_struct['prefixes']) == 1 || $file_exists($path)) { |
||
| 27 | return $path; |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | // none of the paths matched, just return what we have. |
||
| 32 | return $path; |
||
|
|
|||
| 33 | }; |
||
| 34 | } |
||
| 35 |
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: