| Conditions | 8 |
| Paths | 10 |
| Total Lines | 23 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 7 | function getIPVisitor(array $server = []) : string |
||
| 8 | { |
||
| 9 | if (empty($server)) { |
||
| 10 | return ''; |
||
| 11 | } |
||
| 12 | |||
| 13 | if (array_key_exists('HTTP_X_FORWARDED_FOR', $server) && trim($server['HTTP_X_FORWARDED_FOR']) != '') { |
||
| 14 | $IP2Check = $server['HTTP_X_FORWARDED_FOR']; |
||
| 15 | } elseif (array_key_exists('REMOTE_ADDR', $server) && trim($server['REMOTE_ADDR'])) { |
||
| 16 | $IP2Check = $server['REMOTE_ADDR']; |
||
| 17 | } |
||
| 18 | |||
| 19 | if ($IP2Check == '') { |
||
| 20 | return ''; |
||
| 21 | } elseif (strpos($IP2Check, ',') === false) { |
||
|
|
|||
| 22 | return $IP2Check; |
||
| 23 | } else { |
||
| 24 | // Header can contain multiple IP-s of proxies that are passed through. |
||
| 25 | // Only the IP added by the last proxy (last IP in the list) can be trusted. |
||
| 26 | $client_ip = trim(end(explode(',', $IP2Check))); |
||
| 27 | return $client_ip; |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 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: