Test Setup Failed
Pull Request — master (#616)
by mingyoung
02:52
created

helpers.php ➔ get_server_ip()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 8
nop 0
dl 0
loc 13
ccs 0
cts 7
cp 0
crap 30
rs 8.8571
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
 * @see      https://github.com/overtrue
19
 * @see      http://overtrue.me
20
 */
21
22
namespace EasyWeChat\Payment;
23
24
/**
25
 * Generate a signature.
26
 *
27
 * @param array  $attributes
28
 * @param string $key
29
 * @param string $encryptMethod
30
 *
31
 * @return string
32
 */
33
function generate_sign(array $attributes, $key, $encryptMethod = 'md5')
34
{
35 20
    ksort($attributes);
36
37 20
    $attributes['key'] = $key;
38
39 20
    return strtoupper(call_user_func_array($encryptMethod, [urldecode(http_build_query($attributes))]));
40
}
41
42
/**
43
 * Get client ip.
44
 *
45
 * @return string
46
 */
47
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...
48
{
49 1
    if (!empty($_SERVER['REMOTE_ADDR'])) {
50
        $ip = $_SERVER['REMOTE_ADDR'];
51
    } else {
52
        // for php-cli(phpunit etc.)
53 1
        $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
54
    }
55
56 1
    return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
57
}
58
59
/**
60
 * Get current server ip.
61
 *
62
 * @return string
63
 */
64
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...
65
{
66
    if (!empty($_SERVER['SERVER_ADDR'])) {
67
        $ip = $_SERVER['SERVER_ADDR'];
68
    } elseif (!empty($_SERVER['SERVER_NAME'])) {
69
        $ip = gethostbyname($_SERVER['SERVER_NAME']);
70
    } else {
71
        // for php-cli(phpunit etc.)
72
        $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
73
    }
74
75
    return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
76
}
77