Conditions | 12 |
Paths | 108 |
Total Lines | 41 |
Code Lines | 24 |
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 |
||
87 | public static function extractRequestHeaders(array $server) |
||
88 | { |
||
89 | $headers = []; |
||
90 | foreach ($server as $key => $value) { |
||
91 | if (substr((string) $key, 0, 5) == 'HTTP_') { |
||
92 | $key = substr((string) $key, 5); |
||
93 | $key = strtolower((string) str_replace('_', ' ', $key ?: '')); |
||
94 | $key = str_replace(' ', '-', ucwords((string) $key)); |
||
95 | $headers[$key] = $value; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | if (isset($server['CONTENT_TYPE'])) { |
||
100 | $headers['Content-Type'] = $server['CONTENT_TYPE']; |
||
101 | } |
||
102 | if (isset($server['CONTENT_LENGTH'])) { |
||
103 | $headers['Content-Length'] = $server['CONTENT_LENGTH']; |
||
104 | } |
||
105 | |||
106 | // Enable HTTP Basic authentication workaround for PHP running in CGI mode with Apache |
||
107 | // Depending on server configuration the auth header may be in HTTP_AUTHORIZATION or |
||
108 | // REDIRECT_HTTP_AUTHORIZATION |
||
109 | $authHeader = null; |
||
110 | if (isset($headers['Authorization'])) { |
||
111 | $authHeader = $headers['Authorization']; |
||
112 | } elseif (isset($server['REDIRECT_HTTP_AUTHORIZATION'])) { |
||
113 | $authHeader = $server['REDIRECT_HTTP_AUTHORIZATION']; |
||
114 | } |
||
115 | |||
116 | // Ensure basic auth is available via headers |
||
117 | if (isset($server['PHP_AUTH_USER']) && isset($server['PHP_AUTH_PW'])) { |
||
118 | // Shift PHP_AUTH_* into headers so they are available via request |
||
119 | $headers['PHP_AUTH_USER'] = $server['PHP_AUTH_USER']; |
||
120 | $headers['PHP_AUTH_PW'] = $server['PHP_AUTH_PW']; |
||
121 | } elseif ($authHeader && preg_match('/Basic\s+(?<token>.*)$/i', (string) $authHeader, $matches)) { |
||
122 | list($name, $password) = explode(':', (string) base64_decode($matches['token'])); |
||
123 | $headers['PHP_AUTH_USER'] = $name; |
||
124 | $headers['PHP_AUTH_PW'] = $password; |
||
125 | } |
||
126 | |||
127 | return $headers; |
||
128 | } |
||
151 |