Completed
Push — master ( 8dc597...ffee62 )
by Andrey
02:49 queued 19s
created

Uri   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 30
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A full() 0 3 1
A part() 0 4 2
A host() 0 3 1
1
<?php
2
namespace humanity;
3
4
class Uri {
5
6
    private $requestUri = '';
7
8
    public function __construct(){
0 ignored issues
show
Coding Style introduced by
__construct 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...
9
        $this->requestUri = $_SERVER['REQUEST_URI'];
10
        $tmp = explode('/',$this->requiestUri);
0 ignored issues
show
Bug introduced by
The property requiestUri does not seem to exist. Did you mean requestUri?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
11
        foreach($tmp as $key=>$value){
12
            $value = trim($value);
13
            if(empty($value)) unset($tmp[$key]);
14
            else $tmp[$key] = $value;
15
        }
16
        $tmp = array_values($tmp);
17
        $this->requestUri = $tmp;
0 ignored issues
show
Documentation Bug introduced by
It seems like $tmp of type array<integer,?> is incompatible with the declared type string of property $requestUri.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
18
    }
19
20
    public function full(){
21
        return '/'.implode('/',$this->requestUri);
22
    }
23
24
    public function part($number){
25
        if(isset($this->requestUri[$number])) return (string) urldecode($this->requestUri[$number]);
26
        else return '';
27
    }
28
29
    public function host(){
0 ignored issues
show
Coding Style introduced by
host 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...
30
        return parse_url('http://'.$_SERVER['HTTP_HOST'])['host'];
31
    }
32
33
}
34
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
35