Passed
Pull Request — master (#43)
by Yasin
01:37
created

FactoryUri::createFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
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
    public static function createFromString($uri)
34
    {
35
        $data = parse_url(self::validateUrl($uri));
36
37
        $scheme = self::validateString((string) self::checkData($data, 'scheme', ''));
38
        $user = self::validateString(self::checkData($data, 'user', ''));
0 ignored issues
show
Bug introduced by
self::checkData($data, 'user', '') of type array is incompatible with the type string expected by parameter $var of One\FactoryUri::validateString(). ( Ignorable by Annotation )

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

38
        $user = self::validateString(/** @scrutinizer ignore-type */ self::checkData($data, 'user', ''));
Loading history...
39
        $pass = self::validateString(self::checkData($data, 'pass', ''));
40
        $host = self::validateString(self::checkData($data, 'host', ''));
41
        $port = self::validateInteger((int) self::checkData($data, 'port', null));
42
        $path = self::validateString(self::checkData($data, 'path', ''));
43
        $query = self::validateString(self::checkData($data, 'query', ''));
44
        $fragment = self::validateString(self::checkData($data, 'fragment', ''));
45
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
46
    }
47
48
    /**
49
     * functionality to check whether a variable is set or not.
50
     *
51
     * @param array $parts
52
     * @return array
53
     */
54
    private static function checkData($data, $key, $default = '')
55
    {
56
        return isset($data[$key]) ? $data[$key] : $default;
57
    }
58
59
    /**
60
     * function for Create Uri From Server
61
     *
62
     */
63
    public static function createFromServer()
64
    {
65
        $scheme = self::validateString((string) self::checkData($_SERVER, 'HTTPS', 'http://'));
66
        $host = self::validateString(self::checkData($_SERVER, 'HTTP_HOST', isset($_SERVER['SERVER_NAME'])));
0 ignored issues
show
Bug introduced by
self::checkData($_SERVER, 'HTTP_HOST', IssetNode) of type array is incompatible with the type string expected by parameter $var of One\FactoryUri::validateString(). ( Ignorable by Annotation )

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

66
        $host = self::validateString(/** @scrutinizer ignore-type */ self::checkData($_SERVER, 'HTTP_HOST', isset($_SERVER['SERVER_NAME'])));
Loading history...
67
        $port = self::validateInteger((int) self::checkData($_SERVER, 'SERVER_PORT', null));
68
        $user = self::validateString(self::checkData($_SERVER, 'PHP_AUTH_USER', ''));
69
        $pass = self::validateString(self::checkData($_SERVER, 'PHP_AUTH_PW', ''));
70
        /*		$path = self::validateString((string) parse_url('http://www.foobar.com/' . self::checkData($_SERVER, 'REQUEST_URI', ''), PHP_URL_PATH));*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
71
        $path = self::validateString((string) parse_url('http://www.foobar.com/' . self::checkData($_SERVER, 'REQUEST_URI', ''), PHP_URL_PATH));
0 ignored issues
show
Bug introduced by
Are you sure self::checkData($_SERVER, 'REQUEST_URI', '') of type array can be used in concatenation? ( Ignorable by Annotation )

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

71
        $path = self::validateString((string) parse_url('http://www.foobar.com/' . /** @scrutinizer ignore-type */ self::checkData($_SERVER, 'REQUEST_URI', ''), PHP_URL_PATH));
Loading history...
72
73
        $query = self::validateString(self::checkData($_SERVER, 'QUERY_STRING', ''));
74
        $fragment = '';
75
        if (empty($user) && empty($pass) && !empty($_SERVER['HTTP_AUTHORIZATION'])) {
76
            list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
77
        }
78
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
79
    }
80
81
    /**
82
     * Create Uri Object
83
     *
84
     * @param String $string
85
     * @param string $scheme
86
     * @param string $host
87
     * @param int $port
88
     * @param string $user
89
     * @param string $password
90
     * @param string $path
91
     * @param string $query
92
     * @param string $fragment
93
     * @return Uri Object
94
     */
95
    private static function createUri($scheme, $host, $port, $user, $password, $path, $query, $fragment)
96
    {
97
        return new Uri(
98
            $scheme,
99
            $host,
100
            $port,
101
            $path,
102
            $query,
103
            $fragment,
104
            $user,
105
            $password
106
        );
107
    }
108
109
    /**
110
     * Make Sure Url in string with correct url format
111
     *
112
     * @param String $string
113
     * @return string
114
     */
115
    private static function validateUrl($url)
116
    {
117
        if (filter_var($url, FILTER_VALIDATE_URL) === false) {
118
            throw new \Exception("Invalid url : $url");
119
        }
120
        return $url;
121
    }
122
123
    /**
124
     * functionality validity for int variables
125
     *
126
     * @param int $var
127
     * @return int
128
     */
129
    private static function validateInteger($var)
130
    {
131
        if (filter_var($var, FILTER_VALIDATE_INT) === false) {
132
            throw new \Exception("The variable must be a integer :" . $var);
133
        }
134
        return $var;
135
    }
136
137
    /**
138
     * functionality validity for string variables
139
     *
140
     * @param String $var
141
     * @return String
142
     */
143
    private static function validateString($var)
144
    {
145
        if (is_string($var) === false) {
0 ignored issues
show
introduced by
The condition is_string($var) === false is always false.
Loading history...
146
            throw new \Exception("The variable must be a string :" . $var);
147
        }
148
        return $var;
149
    }
150
}
151