| Conditions | 20 |
| Paths | 51 |
| Total Lines | 96 |
| Code Lines | 60 |
| 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 |
||
| 190 | public static function createFromServerArray(array $serverArray): RequestBuilder |
||
| 191 | { |
||
| 192 | $headers = []; |
||
| 193 | $method = null; |
||
| 194 | $url = null; |
||
| 195 | $httpVersion = '1.1'; |
||
| 196 | |||
| 197 | $protocol = 'http'; |
||
| 198 | $hostName = 'localhost'; |
||
| 199 | |||
| 200 | foreach ($serverArray as $key => $value) { |
||
| 201 | switch ($key) { |
||
| 202 | case 'SERVER_PROTOCOL': |
||
| 203 | if ('HTTP/1.0' === $value) { |
||
| 204 | $httpVersion = '1.0'; |
||
| 205 | } elseif ('HTTP/2.0' === $value) { |
||
| 206 | $httpVersion = '2.0'; |
||
| 207 | } |
||
| 208 | break; |
||
| 209 | case 'REQUEST_METHOD': |
||
| 210 | $method = $value; |
||
| 211 | break; |
||
| 212 | case 'REQUEST_URI': |
||
| 213 | $url = $value; |
||
| 214 | break; |
||
| 215 | |||
| 216 | // These sometimes show up without a HTTP_ prefix |
||
| 217 | case 'CONTENT_TYPE': |
||
| 218 | $headers['Content-Type'] = $value; |
||
| 219 | break; |
||
| 220 | case 'CONTENT_LENGTH': |
||
| 221 | $headers['Content-Length'] = $value; |
||
| 222 | break; |
||
| 223 | |||
| 224 | // mod_php on apache will put credentials in these variables. |
||
| 225 | // (fast)cgi does not usually do this, however. |
||
| 226 | case 'PHP_AUTH_USER': |
||
| 227 | if (isset($serverArray['PHP_AUTH_PW'])) { |
||
| 228 | $headers['Authorization'] = 'Basic '.base64_encode($value.':'.$serverArray['PHP_AUTH_PW']); |
||
| 229 | } |
||
| 230 | break; |
||
| 231 | |||
| 232 | // Similarly, mod_php may also screw around with digest auth. |
||
| 233 | case 'PHP_AUTH_DIGEST': |
||
| 234 | $headers['Authorization'] = 'Digest '.$value; |
||
| 235 | break; |
||
| 236 | |||
| 237 | // Apache may prefix the HTTP_AUTHORIZATION header with |
||
| 238 | // REDIRECT_, if mod_rewrite was used. |
||
| 239 | case 'REDIRECT_HTTP_AUTHORIZATION': |
||
| 240 | $headers['Authorization'] = $value; |
||
| 241 | break; |
||
| 242 | |||
| 243 | case 'HTTP_HOST': |
||
| 244 | $hostName = $value; |
||
| 245 | $headers['Host'] = $value; |
||
| 246 | break; |
||
| 247 | |||
| 248 | case 'HTTPS': |
||
| 249 | if (!empty($value) && 'off' !== $value) { |
||
| 250 | $protocol = 'https'; |
||
| 251 | } |
||
| 252 | break; |
||
| 253 | |||
| 254 | default: |
||
| 255 | if ('HTTP_' === substr($key, 0, 5)) { |
||
| 256 | // It's a HTTP header |
||
| 257 | |||
| 258 | // Normalizing it to be prettier |
||
| 259 | $header = strtolower(substr($key, 5)); |
||
| 260 | |||
| 261 | // Transforming dashes into spaces, and uppercasing |
||
| 262 | // every first letter. |
||
| 263 | $header = ucwords(str_replace('_', ' ', $header)); |
||
| 264 | |||
| 265 | // Turning spaces into dashes. |
||
| 266 | $header = str_replace(' ', '-', $header); |
||
| 267 | $headers[$header] = $value; |
||
| 268 | } |
||
| 269 | break; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | if (null === $url) { |
||
| 274 | throw new \InvalidArgumentException('The _SERVER array must have a REQUEST_URI key'); |
||
| 275 | } |
||
| 276 | |||
| 277 | if (null === $method) { |
||
| 278 | throw new \InvalidArgumentException('The _SERVER array must have a REQUEST_METHOD key'); |
||
| 279 | } |
||
| 280 | $r = new RequestBuilder($method, $url, $headers); |
||
| 281 | $r->setHttpVersion($httpVersion); |
||
| 282 | $r->setRawServerData($serverArray); |
||
| 283 | $r->setAbsoluteUrl($protocol.'://'.$hostName.$url); |
||
| 284 | |||
| 285 | return $r; |
||
| 286 | } |
||
| 289 |