Passed
Pull Request — master (#43)
by Charis
01:42
created

FactoryUri::createFromServer()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 2
nop 0
dl 0
loc 14
rs 9.9
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);
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryUri::createFromString() is not static, but was called statically. ( Ignorable by Annotation )

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

28
            return self::/** @scrutinizer ignore-call */ createFromString($string);
Loading history...
29
        }
30
        return self::createFromServer();
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryUri::createFromServer() is not static, but was called statically. ( Ignorable by Annotation )

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

30
        return self::/** @scrutinizer ignore-call */ createFromServer();
Loading history...
31
    }
32
33
    /**
34
     * function for Create Uri From String
35
     *
36
     * @param String $string
37
     */
38
    public function createFromString($string)
39
    {
40
        $data = self::parseUrl(self::validateUrl($string));
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryUri::validateUrl() is not static, but was called statically. ( Ignorable by Annotation )

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

40
        $data = self::parseUrl(self::/** @scrutinizer ignore-call */ validateUrl($string));
Loading history...
Bug Best Practice introduced by
The method One\FactoryUri::parseUrl() is not static, but was called statically. ( Ignorable by Annotation )

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

40
        /** @scrutinizer ignore-call */ 
41
        $data = self::parseUrl(self::validateUrl($string));
Loading history...
41
        $scheme = self::validateString(self::checkData($data, 'scheme', ''));
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryUri::checkData() is not static, but was called statically. ( Ignorable by Annotation )

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

41
        $scheme = self::validateString(self::/** @scrutinizer ignore-call */ checkData($data, 'scheme', ''));
Loading history...
Bug Best Practice introduced by
The method One\FactoryUri::validateString() is not static, but was called statically. ( Ignorable by Annotation )

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

41
        /** @scrutinizer ignore-call */ 
42
        $scheme = self::validateString(self::checkData($data, 'scheme', ''));
Loading history...
Bug introduced by
self::checkData($data, 'scheme', '') 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

41
        $scheme = self::validateString(/** @scrutinizer ignore-type */ self::checkData($data, 'scheme', ''));
Loading history...
42
        $host = self::validateString(self::checkData($data, 'host', ''));
43
        $port = 85;
44
        //$port = self::validateInteger($data['port']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
45
        $user = self::validateString(self::checkData($data, 'user', ''));
46
        $pass = self::validateString(self::checkData($data, 'pass', ''));
47
        $path = self::validateString(self::checkData($data, 'path', ''));
48
        $query = self::validateString(self::checkData($data, 'query', ''));
49
        $fragment = self::validateString(self::checkData($data, 'fragment', ''));
50
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryUri::createUri() is not static, but was called statically. ( Ignorable by Annotation )

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

50
        return self::/** @scrutinizer ignore-call */ createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
Loading history...
51
    }
52
53
    /**
54
     * functionality to check whether a variable is set or not.
55
     *
56
     * @param array $parts
57
     * @return array
58
     */
59
    private function checkData($data, $key, $default = '')
60
    {
61
        return isset($data[$key]) ? $data[$key] : $default;
62
    }
63
64
    /**
65
     * function for Create Uri From Server
66
     *
67
     */
68
    public function createFromServer()
69
    {
70
        $scheme = self::validateString(self::checkData($_SERVER, 'HTTPS', 'http://'));
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryUri::checkData() is not static, but was called statically. ( Ignorable by Annotation )

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

70
        $scheme = self::validateString(self::/** @scrutinizer ignore-call */ checkData($_SERVER, 'HTTPS', 'http://'));
Loading history...
Bug Best Practice introduced by
The method One\FactoryUri::validateString() is not static, but was called statically. ( Ignorable by Annotation )

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

70
        /** @scrutinizer ignore-call */ 
71
        $scheme = self::validateString(self::checkData($_SERVER, 'HTTPS', 'http://'));
Loading history...
Bug introduced by
self::checkData($_SERVER, 'HTTPS', 'http://') 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

70
        $scheme = self::validateString(/** @scrutinizer ignore-type */ self::checkData($_SERVER, 'HTTPS', 'http://'));
Loading history...
71
        $host = self::validateString(self::checkData($_SERVER, 'HTTP_HOST', isset($_SERVER['SERVER_NAME'])));
72
        $port = self::validateInteger(self::checkData($_SERVER, 'SERVER_PORT', null));
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryUri::validateInteger() is not static, but was called statically. ( Ignorable by Annotation )

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

72
        /** @scrutinizer ignore-call */ 
73
        $port = self::validateInteger(self::checkData($_SERVER, 'SERVER_PORT', null));
Loading history...
Bug introduced by
self::checkData($_SERVER, 'SERVER_PORT', null) of type array is incompatible with the type integer expected by parameter $var of One\FactoryUri::validateInteger(). ( Ignorable by Annotation )

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

72
        $port = self::validateInteger(/** @scrutinizer ignore-type */ self::checkData($_SERVER, 'SERVER_PORT', null));
Loading history...
73
        $user = self::validateString(self::checkData($_SERVER, 'PHP_AUTH_USER', ''));
74
        $pass = self::validateString(self::checkData($_SERVER, 'PHP_AUTH_PW', ''));
75
        $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

75
        $path = self::validateString((string) parse_url('http://www.foobar.com/' . /** @scrutinizer ignore-type */ self::checkData($_SERVER, 'REQUEST_URI', ''), PHP_URL_PATH));
Loading history...
76
        $query = self::validateString(self::checkData($_SERVER, 'QUERY_STRING', ''));
77
        $fragment = '';
78
        if (empty($user) && empty($pass) && !empty($_SERVER['HTTP_AUTHORIZATION'])) {
79
            list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
80
        }
81
        return self::createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
0 ignored issues
show
Bug Best Practice introduced by
The method One\FactoryUri::createUri() is not static, but was called statically. ( Ignorable by Annotation )

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

81
        return self::/** @scrutinizer ignore-call */ createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
Loading history...
82
    }
83
84
    /**
85
     * Create Uri Object
86
     *
87
     * @param String $string
88
     * @param string $scheme
89
     * @param string $host
90
     * @param int $port
91
     * @param string $user
92
     * @param string $password
93
     * @param string $path
94
     * @param string $query
95
     * @param string $fragment
96
     * @return Uri Object
97
     */
98
    public function createUri($scheme, $host, $port, $user, $password, $path, $query, $fragment)
99
    {
100
        return new Uri(
101
            $scheme,
102
            $host,
103
            $port,
104
            $path,
105
            $query,
106
            $fragment,
107
            $user,
108
            $password
109
        );
110
    }
111
112
    /**
113
     * Make Sure Url in string with correct url format
114
     *
115
     * @param String $string
116
     * @return string
117
     */
118
    private function validateUrl($string)
119
    {
120
        if (filter_var($string, FILTER_VALIDATE_URL) == false) {
121
            throw new \Exception("Invalid url : $string");
122
        }
123
        return $string;
124
    }
125
126
    /**
127
     * Make Sure Url in string with correct url format
128
     *
129
     * @param String
130
     * @return array
131
     */
132
    private function parseUrl($string)
133
    {
134
        return parse_url($string);
135
    }
136
137
    /**
138
     * functionality validity for int variables
139
     *
140
     * @param int $var
141
     * @return int
142
     */
143
    private function validateInteger($var)
144
    {
145
        if (filter_var($var, FILTER_VALIDATE_INT) == false) {
146
            throw new \Exception("The variable must be a integer :" . $var);
147
        }
148
        return $var;
149
    }
150
151
    /**
152
     * functionality validity for string variables
153
     *
154
     * @param String $var
155
     * @return String
156
     */
157
    private function validateString($var)
158
    {
159
        if (is_string($var) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
introduced by
The condition is_string($var) == false is always false.
Loading history...
160
            throw new \Exception("The variable must be a string :" . $var);
161
        }
162
        return $var;
163
    }
164
}
165