Conditions | 8 |
Paths | 48 |
Total Lines | 51 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
108 | public static function cleanEnvironment(array $variables) |
||
109 | { |
||
110 | // IIS will sometimes generate this. |
||
111 | if (!empty($variables['_SERVER']['HTTP_X_ORIGINAL_URL'])) { |
||
112 | $variables['_SERVER']['REQUEST_URI'] = $variables['_SERVER']['HTTP_X_ORIGINAL_URL']; |
||
113 | } |
||
114 | |||
115 | // Override REQUEST_METHOD |
||
116 | if (isset($variables['_SERVER']['X-HTTP-Method-Override'])) { |
||
117 | $variables['_SERVER']['REQUEST_METHOD'] = $variables['_SERVER']['X-HTTP-Method-Override']; |
||
118 | } |
||
119 | |||
120 | // Prevent injection of url= querystring argument by prioritising any leading url argument |
||
121 | if (isset($variables['_SERVER']['QUERY_STRING']) && |
||
122 | preg_match('/^(?<url>url=[^&?]*)(?<query>.*[&?]url=.*)$/', $variables['_SERVER']['QUERY_STRING'], $results) |
||
123 | ) { |
||
124 | $queryString = $results['query'].'&'.$results['url']; |
||
125 | parse_str($queryString, $variables['_GET']); |
||
126 | } |
||
127 | |||
128 | // Decode url from REQUEST_URI if not passed via $_GET['url'] |
||
129 | if (!isset($variables['_GET']['url'])) { |
||
130 | $url = $variables['_SERVER']['REQUEST_URI']; |
||
131 | |||
132 | // Querystring args need to be explicitly parsed |
||
133 | if (strpos($url, '?') !== false) { |
||
134 | list($url, $queryString) = explode('?', $url, 2); |
||
135 | parse_str($queryString); |
||
136 | } |
||
137 | |||
138 | // Ensure $_GET['url'] is set |
||
139 | $variables['_GET']['url'] = urldecode($url); |
||
140 | } |
||
141 | |||
142 | // Remove base folders from the URL if webroot is hosted in a subfolder |
||
143 | if (substr(strtolower($variables['_GET']['url']), 0, strlen(BASE_URL)) === strtolower(BASE_URL)) { |
||
144 | $variables['_GET']['url'] = substr($variables['_GET']['url'], strlen(BASE_URL)); |
||
145 | } |
||
146 | |||
147 | // Merge $_FILES into $_POST |
||
148 | $variables['_POST'] = array_merge((array)$variables['_POST'], (array)$variables['_FILES']); |
||
149 | |||
150 | // Merge $_POST, $_GET, and $_COOKIE into $_REQUEST |
||
151 | $variables['_REQUEST'] = array_merge( |
||
152 | (array)$variables['_GET'], |
||
153 | (array)$variables['_POST'], |
||
154 | (array)$variables['_COOKIE'] |
||
155 | ); |
||
156 | |||
157 | return $variables; |
||
158 | } |
||
159 | } |
||
160 |