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

FactoryUri::checkData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 3
dl 0
loc 3
rs 10
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
    public function createFromString($uri)
34
    {
35
        $data = parse_url(self::validateUrl($uri));
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

35
        $data = parse_url(self::/** @scrutinizer ignore-call */ validateUrl($uri));
Loading history...
36
        $scheme = self::validateString(self::checkData($data, 'scheme', ''));
0 ignored issues
show
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

36
        $scheme = self::validateString(/** @scrutinizer ignore-type */ self::checkData($data, 'scheme', ''));
Loading history...
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

36
        $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

36
        /** @scrutinizer ignore-call */ 
37
        $scheme = self::validateString(self::checkData($data, 'scheme', ''));
Loading history...
37
        $user = self::validateString(self::checkData($data, 'user', ''));
38
        $pass = self::validateString(self::checkData($data, 'pass', ''));
39
        $host = self::validateString(self::checkData($data, 'host', ''));
40
        $port = self::checkData($data, 'port', null);
41
        $path = self::validateString(self::checkData($data, 'path', ''));
42
        $query = self::validateString(self::checkData($data, 'query', ''));
43
        $fragment = self::validateString(self::checkData($data, 'fragment', ''));
44
        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

44
        return self::createUri($scheme, $host, /** @scrutinizer ignore-type */ $port, $user, $pass, $path, $query, $fragment);
Loading history...
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

44
        return self::/** @scrutinizer ignore-call */ createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
Loading history...
45
    }
46
47
    /**
48
     * functionality to check whether a variable is set or not.
49
     *
50
     * @param array $parts
51
     * @return array
52
     */
53
    private function checkData($data, $key, $default = '')
54
    {
55
        return isset($data[$key]) ? $data[$key] : $default;
56
    }
57
58
    /**
59
     * function for Create Uri From Server
60
     *
61
     */
62
    public function createFromServer()
63
    {
64
        $scheme = self::validateString(self::checkData($_SERVER, 'HTTPS', 'http://'));
0 ignored issues
show
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

64
        /** @scrutinizer ignore-call */ 
65
        $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

64
        $scheme = self::validateString(/** @scrutinizer ignore-type */ self::checkData($_SERVER, 'HTTPS', 'http://'));
Loading history...
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

64
        $scheme = self::validateString(self::/** @scrutinizer ignore-call */ checkData($_SERVER, 'HTTPS', 'http://'));
Loading history...
65
        $host = self::validateString(self::checkData($_SERVER, 'HTTP_HOST', isset($_SERVER['SERVER_NAME'])));
66
        $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

66
        /** @scrutinizer ignore-call */ 
67
        $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

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

69
        $path = self::validateString((string) parse_url('http://www.foobar.com/' . /** @scrutinizer ignore-type */ self::checkData($_SERVER, 'REQUEST_URI', ''), PHP_URL_PATH));
Loading history...
70
        $query = self::validateString(self::checkData($_SERVER, 'QUERY_STRING', ''));
71
        $fragment = '';
72
        if (empty($user) && empty($pass) && !empty($_SERVER['HTTP_AUTHORIZATION'])) {
73
            list($user, $password) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
74
        }
75
        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

75
        return self::/** @scrutinizer ignore-call */ createUri($scheme, $host, $port, $user, $pass, $path, $query, $fragment);
Loading history...
76
    }
77
78
    /**
79
     * Create Uri Object
80
     *
81
     * @param String $string
82
     * @param string $scheme
83
     * @param string $host
84
     * @param int $port
85
     * @param string $user
86
     * @param string $password
87
     * @param string $path
88
     * @param string $query
89
     * @param string $fragment
90
     * @return Uri Object
91
     */
92
    private function createUri($scheme, $host, $port, $user, $password, $path, $query, $fragment)
93
    {
94
        return new Uri(
95
            $scheme,
96
            $host,
97
            $port,
98
            $path,
99
            $query,
100
            $fragment,
101
            $user,
102
            $password
103
        );
104
    }
105
106
    /**
107
     * Make Sure Url in string with correct url format
108
     *
109
     * @param String $string
110
     * @return string
111
     */
112
    private function validateUrl($string)
113
    {
114
        if (filter_var($string, FILTER_VALIDATE_URL) == false) {
115
            throw new \Exception("Invalid url : $string");
116
        }
117
        return $string;
118
    }
119
120
    /**
121
     * Make Sure Url in string with correct url format
122
     *
123
     * @param String
124
     * @return array
125
     */
126
    private function parseUrl($string)
0 ignored issues
show
Unused Code introduced by
The method parseUrl() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
127
    {
128
        return parse_url($string);
129
    }
130
131
    /**
132
     * functionality validity for int variables
133
     *
134
     * @param int $var
135
     * @return int
136
     */
137
    private function validateInteger($var)
138
    {
139
        if (filter_var($var, FILTER_VALIDATE_INT) == false) {
140
            throw new \Exception("The variable must be a integer :" . $var);
141
        }
142
        return $var;
143
    }
144
145
    /**
146
     * functionality validity for string variables
147
     *
148
     * @param String $var
149
     * @return String
150
     */
151
    private function validateString($var)
152
    {
153
        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...
154
            throw new \Exception("The variable must be a string :" . $var);
155
        }
156
        return $var;
157
    }
158
}
159