Completed
Push — master ( 313b19...3586a2 )
by Carlos
127:14 queued 118:54
created

helpers.php ➔ get_client_ip()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 11
ccs 3
cts 5
cp 0.6
crap 3.576
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * helpers.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Payment;
22
23
/**
24
 * Generate a signature.
25
 *
26
 * @param array  $attributes
27
 * @param string $key
28
 * @param string $encryptMethod
29
 *
30
 * @return string
31
 */
32
function generate_sign(array $attributes, $key, $encryptMethod = 'md5')
33
{
34 20
    ksort($attributes);
35
36 20
    $attributes['key'] = $key;
37
38 20
    return strtoupper(call_user_func_array($encryptMethod, [urldecode(http_build_query($attributes))]));
39
}
40
41
/**
42
 * Get client ip.
43
 *
44
 * @return string
45
 */
46
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...
47
{
48 1
    if (!empty($_SERVER['REMOTE_ADDR'])) {
49
        $ip = $_SERVER['REMOTE_ADDR'];
50
    } else {
51
        // for php-cli(phpunit etc.)
52 1
        $ip = gethostbyname(gethostname());
53
    }
54
55 1
    return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
56
}
57
58
/**
59
 * Get current server ip.
60
 *
61
 * @return string
62
 */
63
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...
64
{
65
    if (!empty($_SERVER['SERVER_ADDR'])) {
66
        $ip = $_SERVER['SERVER_ADDR'];
67
    } elseif (!empty($_SERVER['SERVER_NAME'])) {
68
        $ip = gethostbyname($_SERVER['SERVER_NAME']);
69
    } else {
70
        // for php-cli(phpunit etc.)
71
        $ip = gethostbyname(gethostname());
72
    }
73
74
    return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
75
}
76