Url   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 56.52 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 0
dl 13
loc 23
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B current() 13 14 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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