| Conditions | 17 |
| Paths | 320 |
| Total Lines | 73 |
| Code Lines | 33 |
| 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 |
||
| 28 | public function getHeaders() |
||
| 29 | { |
||
| 30 | $headers = array(); |
||
| 31 | $contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true); |
||
| 32 | foreach ($this->parameters as $key => $value) { |
||
| 33 | if (0 === strpos($key, 'HTTP_')) { |
||
| 34 | $headers[substr($key, 5)] = $value; |
||
| 35 | } |
||
| 36 | // CONTENT_* are not prefixed with HTTP_ |
||
| 37 | elseif (isset($contentHeaders[$key])) { |
||
| 38 | $headers[$key] = $value; |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | if (isset($this->parameters['PHP_AUTH_USER'])) { |
||
| 43 | $headers['PHP_AUTH_USER'] = $this->parameters['PHP_AUTH_USER']; |
||
| 44 | $headers['PHP_AUTH_PW'] = isset($this->parameters['PHP_AUTH_PW']) ? $this->parameters['PHP_AUTH_PW'] : ''; |
||
| 45 | } else { |
||
| 46 | /* |
||
| 47 | * php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default |
||
| 48 | * For this workaround to work, add these lines to your .htaccess file: |
||
| 49 | * RewriteCond %{HTTP:Authorization} ^(.+)$ |
||
| 50 | * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] |
||
| 51 | * |
||
| 52 | * A sample .htaccess file: |
||
| 53 | * RewriteEngine On |
||
| 54 | * RewriteCond %{HTTP:Authorization} ^(.+)$ |
||
| 55 | * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] |
||
| 56 | * RewriteCond %{REQUEST_FILENAME} !-f |
||
| 57 | * RewriteRule ^(.*)$ app.php [QSA,L] |
||
| 58 | */ |
||
| 59 | |||
| 60 | $authorizationHeader = null; |
||
| 61 | if (isset($this->parameters['HTTP_AUTHORIZATION'])) { |
||
| 62 | $authorizationHeader = $this->parameters['HTTP_AUTHORIZATION']; |
||
| 63 | } elseif (isset($this->parameters['REDIRECT_HTTP_AUTHORIZATION'])) { |
||
| 64 | $authorizationHeader = $this->parameters['REDIRECT_HTTP_AUTHORIZATION']; |
||
| 65 | } |
||
| 66 | |||
| 67 | if (null !== $authorizationHeader) { |
||
| 68 | if (0 === stripos($authorizationHeader, 'basic ')) { |
||
| 69 | // Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic |
||
| 70 | $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)), 2); |
||
| 71 | if (count($exploded) == 2) { |
||
| 72 | list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded; |
||
| 73 | } |
||
| 74 | } elseif (empty($this->parameters['PHP_AUTH_DIGEST']) && (0 === stripos($authorizationHeader, 'digest '))) { |
||
| 75 | // In some circumstances PHP_AUTH_DIGEST needs to be set |
||
| 76 | $headers['PHP_AUTH_DIGEST'] = $authorizationHeader; |
||
| 77 | $this->parameters['PHP_AUTH_DIGEST'] = $authorizationHeader; |
||
| 78 | } elseif (0 === stripos($authorizationHeader, 'bearer ')) { |
||
| 79 | /* |
||
| 80 | * XXX: Since there is no PHP_AUTH_BEARER in PHP predefined variables, |
||
| 81 | * I'll just set $headers['AUTHORIZATION'] here. |
||
| 82 | * http://php.net/manual/en/reserved.variables.server.php |
||
| 83 | */ |
||
| 84 | $headers['AUTHORIZATION'] = $authorizationHeader; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | if (isset($headers['AUTHORIZATION'])) { |
||
| 90 | return $headers; |
||
| 91 | } |
||
| 92 | |||
| 93 | // PHP_AUTH_USER/PHP_AUTH_PW |
||
| 94 | if (isset($headers['PHP_AUTH_USER'])) { |
||
| 95 | $headers['AUTHORIZATION'] = 'Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']); |
||
| 96 | } elseif (isset($headers['PHP_AUTH_DIGEST'])) { |
||
| 97 | $headers['AUTHORIZATION'] = $headers['PHP_AUTH_DIGEST']; |
||
| 98 | } |
||
| 99 | |||
| 100 | return $headers; |
||
| 101 | } |
||
| 103 |