Completed
Push — master ( b645fc...1a7521 )
by Charis
03:32
created

Uri::createFromServer()   D

Complexity

Conditions 10
Paths 128

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 4.606
c 0
b 0
f 0
cc 10
eloc 15
nc 128
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Resilient\Factory;
4
5
use InvalidArgumentException;
6
use Resilient\Http\Uri as UriItem;
7
use \Psr\Http\Message\UriInterface;
8
9
class Uri
10
{
11
    /**
12
     * Create uri Instance from header.
13
     *
14
     * @access public
15
     * @static
16
     * @return Uri
17
     */
18
    public static function createFromServer($serv)
19
    {
20
        $scheme = isset($serv['HTTPS']) ? 'https://' : 'http://';
21
        $host = !empty($serv['HTTP_HOST']) ? $serv['HTTP_HOST'] : $serv['SERVER_NAME'];
22
        $port = empty($serv['SERVER_PORT']) ? $serv['SERVER_PORT'] : null;
23
24
        //Path
25
        $scriptName = parse_url($serv['SCRIPT_NAME'], PHP_URL_PATH);
26
        $scriptPath = dirname($scriptName);
0 ignored issues
show
Unused Code introduced by
$scriptPath is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
27
28
        $path = (string) parse_url('http://www.example.com/' . $serv['REQUEST_URI'], PHP_URL_PATH);
29
30
        $query = empty($serv['QUERY_STRING']) ? parse_url('http://example.com' . $serv['REQUEST_URI'], PHP_URL_QUERY) : $serv['QUERY_STRING'];
31
32
        $fragment = '';
33
34
        $user = !empty($serv['PHP_AUTH_USER']) ? $serv['PHP_AUTH_USER'] : '';
35
        $password = !empty($serv['PHP_AUTH_PW']) ? $serv['PHP_AUTH_PW'] : '';
36
37
        if (empty($user) && empty($password) && !empty($serv['HTTP_AUTHORIZATION'])) {
38
            list($user, $password) = explode(':', base64_decode(substr($serv['HTTP_AUTHORIZATION'], 6)));
39
        }
40
41
        $uri = new UriItem($scheme, $host, $port, $path, $query, $fragment, $user, $password);
42
43
        return $uri;
44
    }
45
46
47
    /**
48
     * Create Uri Instance from string http://www.example.com/url/path.html
49
     *
50
     * @access public
51
     * @static
52
     * @param string $uri
53
     * @return Uri
54
     */
55
    public static function createFromString(string $uri)
56
    {
57
        $parts = parse_url($uri);
58
        $scheme = isset($parts['scheme']) ? $parts['scheme'] : '';
59
        $user = isset($parts['user']) ? $parts['user'] : '';
60
        $pass = isset($parts['pass']) ? $parts['pass'] : '';
61
        $host = isset($parts['host']) ? $parts['host'] : '';
62
        $port = isset($parts['port']) ? $parts['port'] : null;
63
        $path = isset($parts['path']) ? $parts['path'] : '';
64
        $query = isset($parts['query']) ? $parts['query'] : '';
65
        $fragment = isset($parts['fragment']) ? $parts['fragment'] : '';
66
67
        return new UriItem($scheme, $host, $port, $path, $query, $fragment, $user, $pass);
68
    }
69
}
70