Completed
Pull Request — master (#553)
by
unknown
02:35
created

Url::encode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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
 * Url.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\Support;
23
24
/**
25
 * Class Url.
26
 */
27
class Url
28
{
29
    /**
30
     * Get current url.
31
     *
32
     * @return string
33
     */
34
    public static function current()
0 ignored issues
show
Coding Style introduced by
current 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...
35
    {
36
        $protocol = (!empty($_SERVER['HTTPS'])
37
                        && $_SERVER['HTTPS'] !== 'off'
38
                        || (int) $_SERVER['SERVER_PORT'] === 443) ? 'https://' : 'http://';
39
40
        return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
41
    }
42
43
    /**
44
     * URL-encodes string.
45
     *
46
     * @param $url
47
     *
48
     * @return string
49
     */
50
    public static function encode($url)
51
    {
52
        return urlencode($url);
53
    }
54
}
55