Test Failed
Push — master ( f4a193...311c78 )
by Carlos
03:06
created

Url::current()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 7
nop 0
dl 0
loc 12
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
 * 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
        if (defined('PHPUNIT_RUNNING')) {
37
            return 'http://localhost';
38
        }
39
40
        $protocol = (!empty($_SERVER['HTTPS'])
41
                        && $_SERVER['HTTPS'] !== 'off'
42
                        || (int) $_SERVER['SERVER_PORT'] === 443) ? 'https://' : 'http://';
43
44
        return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
45
    }
46
}
47