helpers.php ➔ get_server_ip()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 8
nop 0
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace YEntWeChat\Payment;
4
5
/**
6
 * Generate a signature.
7
 *
8
 * @param array  $attributes
9
 * @param string $key
10
 * @param string $encryptMethod
11
 *
12
 * @return string
13
 */
14
function generate_sign(array $attributes, $key, $encryptMethod = 'md5')
15
{
16
    ksort($attributes);
17
18
    $attributes['key'] = $key;
19
20
    return strtoupper(call_user_func_array($encryptMethod, [urldecode(http_build_query($attributes))]));
21
}
22
23
/**
24
 * Get client ip.
25
 *
26
 * @return string
27
 */
28
function get_client_ip()
0 ignored issues
show
Coding Style introduced by
get_client_ip uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
29
{
30
    if (!empty($_SERVER['REMOTE_ADDR'])) {
31
        $ip = $_SERVER['REMOTE_ADDR'];
32
    } else {
33
        // for php-cli(phpunit etc.)
34
        $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
35
    }
36
37
    return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
38
}
39
40
/**
41
 * Get current server ip.
42
 *
43
 * @return string
44
 */
45
function get_server_ip()
0 ignored issues
show
Coding Style introduced by
get_server_ip uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
46
{
47
    if (!empty($_SERVER['SERVER_ADDR'])) {
48
        $ip = $_SERVER['SERVER_ADDR'];
49
    } elseif (!empty($_SERVER['SERVER_NAME'])) {
50
        $ip = gethostbyname($_SERVER['SERVER_NAME']);
51
    } else {
52
        // for php-cli(phpunit etc.)
53
        $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
54
    }
55
56
    return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
57
}
58