Passed
Push — psr-implementation ( 1cb923...69db7b )
by Yasin
02:05
created

FactoryUri::createUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 8
dl 0
loc 11
rs 9.9666
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
namespace One;
3
4
/**
5
 * FactoryUri Class
6
 *
7
 * @method create
8
 * @method createArticle
9
 * @method validateArray
10
 * @method validateUrl
11
 * @method validateInteger
12
 * @method validateString
13
 * @method checkData
14
 *
15
 */
16
class FactoryUri
17
{
18
19
    /**
20
     * function Create Uri
21
     *
22
     * @param String $string
23
     * @return object Uri
24
     */
25
    public static function create($string = null)
26
    {
27
        if (!empty($string)) {
28
            return self::createFromString($string);
29
        }
30
        return self::createFromServer();
31
    }
32
33
    /**
34
     * function for Create Uri From Server
35
     *
36
     */
37
    public static function createFromString($uri)
38
    {
39
        $data = parse_url($uri);
40
        $scheme = self::validateString((string) self::checkData($data, 'scheme', ''));
41
        $user = self::validateString((string) self::checkData($data, 'user', ''));
42
        $pass = self::validateString((string) self::checkData($data, 'pass', ''));
43
        $host = self::validateString((string) self::checkData($data, 'host', ''));
44
        $port = self::checkData($data, 'port', null);
45
        $path = self::validateString((string) self::checkData($data, 'path', ''));
46
        $query = self::validateString((string) self::checkData($data, 'query', ''));
47
        $fragment = self::validateString((string) self::checkData($data, 'fragment', ''));
48
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
0 ignored issues
show
Bug introduced by
$port of type array is incompatible with the type integer expected by parameter $port of One\FactoryUri::createUri(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        return self::createUri($scheme, $host, /** @scrutinizer ignore-type */ $port, $user, $pass, $path, $query, $fragment);
Loading history...
49
    }
50
51
    /**
52
     * functionality to check whether a variable is set or not.
53
     *
54
     * @return array
55
     */
56
    private static function checkData($data, $key, $default = '')
57
    {
58
        return isset($data[$key]) ? $data[$key] : $default;
59
    }
60
61
    /**
62
     * function for Create Uri From Server
63
     *
64
     */
65
    public static function createFromServer()
66
    {
67
        $scheme = self::validateString((string) self::checkData($_SERVER, 'HTTPS', 'http://'));
68
        $host = self::validateString((string) self::checkData($_SERVER, 'HTTP_HOST', isset($_SERVER['SERVER_NAME'])));
69
        $port = self::checkData($_SERVER, 'SERVER_PORT', null);
70
        $user = self::validateString((string) self::checkData($_SERVER, 'PHP_AUTH_USER', ''));
71
        $pass = self::validateString((string) self::checkData($_SERVER, 'PHP_AUTH_PW', ''));
72
        $path = (string) self::checkData($_SERVER, 'REQUEST_URI', '');
73
        $path = self::validateString(parse_url('http://www.foobar.com/' . $path, PHP_URL_PATH));
74
        $query = self::validateString((string) self::checkData($_SERVER, 'QUERY_STRING', ''));
75
        $fragment = '';
76
        if (empty($user) && empty($pass) && !empty($_SERVER['HTTP_AUTHORIZATION'])) {
77
            list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
78
        }
79
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
0 ignored issues
show
Bug introduced by
$port of type array is incompatible with the type integer expected by parameter $port of One\FactoryUri::createUri(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        return self::createUri($scheme, $host, /** @scrutinizer ignore-type */ $port, $user, $pass, $path, $query, $fragment);
Loading history...
80
    }
81
82
    /**
83
     * Create Uri Object
84
     *
85
     * @param String $string
86
     * @param string $scheme
87
     * @param string $host
88
     * @param int $port
89
     * @param string $user
90
     * @param string $password
91
     * @param string $path
92
     * @param string $query
93
     * @param string $fragment
94
     * @return Uri Object
95
     */
96
    private static function createUri($scheme, $host, $port, $user, $password, $path, $query, $fragment)
97
    {
98
        return new Uri(
99
            $scheme,
100
            $host,
101
            $port,
102
            $path,
103
            $query,
104
            $fragment,
105
            $user,
106
            $password
107
        );
108
    }
109
110
    /**
111
     * functionality validity for string variables
112
     *
113
     * @param String $var
114
     * @return String
115
     */
116
    private static function validateString($var)
117
    {
118
        if (gettype($var) === "string") {
119
            return $var;
120
        }
121
        throw new \Exception("The variable type must String :" . $var);
122
    }
123
}
124