| Conditions | 10 |
| Paths | 13 |
| Total Lines | 28 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 7 | function getIPVisitor(array $server = []) : string |
||
| 8 | { |
||
| 9 | if (empty($server)) { |
||
| 10 | return ''; |
||
| 11 | } |
||
| 12 | |||
| 13 | $IP2Check = ''; |
||
| 14 | if (array_key_exists('HTTP_X_FORWARDED_FOR', $server) && trim($server['HTTP_X_FORWARDED_FOR']) != '') { |
||
| 15 | $IP2Check = $server['HTTP_X_FORWARDED_FOR']; |
||
| 16 | } elseif (array_key_exists('REMOTE_ADDR', $server) && trim($server['REMOTE_ADDR'])) { |
||
| 17 | $IP2Check = $server['REMOTE_ADDR']; |
||
| 18 | } |
||
| 19 | |||
| 20 | if ($IP2Check == '') { |
||
| 21 | return ''; |
||
| 22 | } elseif (strpos($IP2Check, ',') === false) { |
||
| 23 | return $IP2Check; |
||
| 24 | } |
||
| 25 | |||
| 26 | // Header can contain multiple IP-s of proxies that are passed through. |
||
| 27 | // Only the IP added by the last proxy (last IP in the list) can be trusted. |
||
| 28 | $arrIps = explode(',', $IP2Check); |
||
| 29 | if (!is_array($arrIps) || count($arrIps) < 1) { |
||
| 30 | return ''; |
||
| 31 | } |
||
| 32 | |||
| 33 | return trim(end($arrIps)); |
||
| 34 | } |
||
| 35 | |||
| 60 |