Url::current()   B
last analyzed

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
namespace EntWeChat\Support;
4
5
/**
6
 * Class Url.
7
 */
8
class Url
9
{
10
    /**
11
     * Get current url.
12
     *
13
     * @return string
14
     */
15
    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...
16
    {
17
        if (defined('PHPUNIT_RUNNING')) {
18
            return 'http://localhost';
19
        }
20
21
        $protocol = (!empty($_SERVER['HTTPS'])
22
            && $_SERVER['HTTPS'] !== 'off'
23
            || (int) $_SERVER['SERVER_PORT'] === 443) ? 'https://' : 'http://';
24
25
        return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
26
    }
27
}
28