Passed
Push — master ( 68b778...1b1614 )
by Thierry
07:15 queued 04:29
created

URI::detect()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 123
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 55
c 5
b 0
f 0
dl 0
loc 123
rs 0
cc 28
nc 25938
nop 0

How to fix   Long Method    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
/**
4
 * URI.php - Jaxon request URI detector
5
 *
6
 * Detect and parse the URI of the Jaxon request being processed.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
14
15
namespace Jaxon\Utils\Http;
16
17
class URI
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class URI
Loading history...
18
{
19
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $aURL should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sKey should have a doc-comment as per coding-style.
Loading history...
20
     * Get the URL from the $_SERVER var
21
     *
22
     * @var array       &$aURL      The URL data
23
     * @var string      $sKey       The key in the $_SERVER array
24
     *
25
     * @return void
26
     */
27
    private function getHostFromServer(array &$aURL, $sKey)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
28
    {
29
        if(empty($aURL['host']) && !empty($_SERVER[$sKey]))
30
        {
31
            if(strpos($_SERVER[$sKey], ':') > 0)
32
            {
33
                list($aURL['host'], $aURL['port']) = explode(':', $_SERVER[$sKey]);
34
            }
35
            else
36
            {
37
                $aURL['host'] = $_SERVER[$sKey];
38
            }
39
        }
40
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
41
42
    /**
43
     * Detect the URI of the current request
44
     *
45
     * @return string        The URI
46
     */
47
    public function detect()
48
    {
49
        $aURL = [];
50
        // Try to get the request URL
51
        if(!empty($_SERVER['REQUEST_URI']))
52
        {
53
            $_SERVER['REQUEST_URI'] = str_replace(['"', "'", '<', '>'], ['%22', '%27', '%3C', '%3E'], $_SERVER['REQUEST_URI']);
54
            $aURL = parse_url($_SERVER['REQUEST_URI']);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 19 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55
            if(!is_array($aURL))
0 ignored issues
show
introduced by
The condition is_array($aURL) is always true.
Loading history...
56
            {
57
                $aURL = [];
58
            }
59
        }
60
61
        // Fill in the empty values
62
        if(empty($aURL['scheme']))
63
        {
64
            if(!empty($_SERVER['HTTP_SCHEME']))
65
            {
66
                $aURL['scheme'] = $_SERVER['HTTP_SCHEME'];
67
            }
68
            else
69
            {
70
                $aURL['scheme'] = ((!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http');
71
            }
72
        }
73
74
        $this->getHostFromServer($aURL, 'HTTP_X_FORWARDED_HOST');
75
        $this->getHostFromServer($aURL, 'HTTP_HOST');
76
        $this->getHostFromServer($aURL, 'SERVER_NAME');
77
        if(empty($aURL['host']))
78
        {
79
            throw new \Jaxon\Exception\URI();
80
        }
81
82
        if(empty($aURL['port']) && !empty($_SERVER['SERVER_PORT']))
83
        {
84
            $aURL['port'] = $_SERVER['SERVER_PORT'];
85
        }
86
87
        if(!empty($aURL['path']) && strlen(basename($aURL['path'])) == 0)
88
        {
89
            unset($aURL['path']);
90
        }
91
92
        if(empty($aURL['path']))
93
        {
94
            if(!empty($_SERVER['PATH_INFO']))
95
            {
96
                $sPath = parse_url($_SERVER['PATH_INFO']);
97
            }
98
            else
99
            {
100
                $sPath = parse_url($_SERVER['PHP_SELF']);
101
            }
102
            if(isset($sPath['path']))
103
            {
104
                $aURL['path'] = str_replace(['"', "'", '<', '>'], ['%22', '%27', '%3C', '%3E'], $sPath['path']);
105
            }
106
            unset($sPath);
107
        }
108
109
        if(empty($aURL['query']))
110
        {
111
            $aURL['query'] = empty($_SERVER['QUERY_STRING']) ? '' : $_SERVER['QUERY_STRING'];
112
        }
113
114
        if(!empty($aURL['query']))
115
        {
116
            $aURL['query'] = '?' . $aURL['query'];
117
        }
118
119
        // Build the URL: Start with scheme, user and pass
120
        $sURL = $aURL['scheme'] . '://';
121
        if(!empty($aURL['user']))
122
        {
123
            $sURL .= $aURL['user'];
124
            if(!empty($aURL['pass']))
125
            {
126
                $sURL .= ':' . $aURL['pass'];
127
            }
128
            $sURL .= '@';
129
        }
130
131
        // Add the host
132
        $sURL .= $aURL['host'];
133
134
        // Add the port if needed
135
        if(!empty($aURL['port']) &&
136
            (($aURL['scheme'] == 'http' && $aURL['port'] != 80) ||
137
            ($aURL['scheme'] == 'https' && $aURL['port'] != 443)))
138
        {
139
            $sURL .= ':' . $aURL['port'];
140
        }
141
142
        // Add the path and the query string
143
        $sURL .= $aURL['path'] . $aURL['query'];
144
145
        // Clean up
146
        unset($aURL);
147
148
        $aURL = explode("?", $sURL);
149
150
        if(1 < count($aURL))
151
        {
152
            $aQueries = explode("&", $aURL[1]);
153
154
            foreach($aQueries as $sKey => $sQuery)
155
            {
156
                if("jxnGenerate" == substr($sQuery, 0, 11))
157
                {
158
                                    unset($aQueries[$sKey]);
159
                }
160
            }
161
162
            $sQueries = implode("&", $aQueries);
163
164
            $aURL[1] = $sQueries;
165
166
            $sURL = implode("?", $aURL);
167
        }
168
169
        return $sURL;
170
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
171
}
172