Url::current()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 14
Code Lines 9

Duplication

Lines 13
Ratio 92.86 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 14
rs 8.8571
cc 5
eloc 9
nc 12
nop 0
1
<?php
2
/**
3
 * Url.php
4
 *
5
 * @author Tian.
6
 */
7
8
namespace Wechat\Utils;
9
10
/**
11
 * 链接
12
 */
13
class Url
14
{
15
16
    /**
17
     * 获取当前URL
18
     *
19
     * @return string
20
     */
21 View Code Duplication
    public static function current()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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...
22
    {
23
        $protocol = (!empty($_SERVER['HTTPS'])
24
            && $_SERVER['HTTPS'] !== 'off'
25
            || $_SERVER['SERVER_PORT'] === 443) ? 'https://' : 'http://';
26
27
        if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
28
            $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
29
        } else {
30
            $host = $_SERVER['HTTP_HOST'];
31
        }
32
33
        return $protocol . $host . $_SERVER['REQUEST_URI'];
34
    }
35
}
36