Completed
Push — master ( 79f205...ceb60c )
by Lorenzo
02:17
created

ip.php ➔ getIPVisitor()   D

Complexity

Conditions 10
Paths 13

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 16
nc 13
nop 1
dl 0
loc 28
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * @param array $server
5
 * @return string
6
 */
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
36
/**
37
 * anonimizeIp masquerade last 3 digit of IP address
38
 * @param  string $ip
39
 * @return string  masked IP
40
 */
41
function anonimizeIp(string $ip) : string
42
{
43
    if ($ip === null || strlen($ip) < 2 || strrpos($ip, ".") === false) {
44
        return $ip;
45
    }
46
    return substr($ip, 0, strrpos($ip, ".") + 1) . '0';
47
}
48
49
/**
50
 * getHost Get the Internet host name corresponding to a given IP address
51
 * @param  string $ip the IP to resolve.
52
 * @return string    Returns the host name of the Internet host specified by ip_address on success,
53
 *                   the unmodified ip_address on failure,
54
 *                   or FALSE on malformed input.
55
 */
56
function getHost($ip)
57
{
58
    return gethostbyaddr($ip);
59
}
60