Completed
Push — master ( 9e0ead...983405 )
by Carlos
11:10 queued 08:26
created

Url::current()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.2
cc 4
eloc 5
nc 6
nop 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
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Support;
22
23
/**
24
 * Class Url.
25
 */
26
class Url
27
{
28
    /**
29
     * Get current url.
30
     *
31
     * @return string
32
     */
33
    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...
34
    {
35
        $protocol = (!empty($_SERVER['HTTPS'])
36
                        && $_SERVER['HTTPS'] !== 'off'
37
                        || $_SERVER['SERVER_PORT'] === 443) ? 'https://' : 'http://';
38
39
        return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
40
    }
41
}
42