Completed
Push — master ( 923933...545f71 )
by Carlos
03:11
created

helpers.php ➔ get_server_ip()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 4
c 2
b 1
f 0
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 3
cp 0
crap 6
rs 9.6666
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
    // for php-cli(phpunit etc.)
49 1
    if (empty($_SERVER['REMOTE_ADDR'])) {
50 1
        return gethostbyname(gethostname());
51
    }
52
53
    return $_SERVER['REMOTE_ADDR'];
54
}
55
56
/**
57
 * Get current server ip.
58
 *
59
 * @return string
60
 */
61
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...
62
{
63
    // for php-cli(phpunit etc.)
64
    if (empty($_SERVER['SERVER_ADDR'])) {
65
        return gethostbyname(gethostname());
66
    }
67
68
    return $_SERVER['SERVER_ADDR'];
69
}
70