| Conditions | 11 |
| Paths | 17 |
| Total Lines | 43 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 19 | public static function parseArray(array $responseHeadersArray = [], bool $lowercaseKey = true): array |
||
| 20 | { |
||
| 21 | $headers = []; |
||
| 22 | |||
| 23 | foreach ($responseHeadersArray as $index => $line) { |
||
| 24 | if ('HTTP' === \substr($line, 0, 4)) { |
||
| 25 | continue; /* we'll get the status code elsewhere */ |
||
| 26 | } |
||
| 27 | $parts = \explode(': ', $line, 2); |
||
| 28 | if (!isset($parts[1])) { |
||
| 29 | continue; // invalid header (missing colon) |
||
| 30 | } |
||
| 31 | [$key, $value] = $parts; |
||
| 32 | if ($lowercaseKey) { |
||
| 33 | $key = \strtolower($key); |
||
| 34 | } |
||
| 35 | if (isset($headers[$key])) { |
||
| 36 | if (!\is_array($headers[$key])) { |
||
| 37 | $headers[$key] = [$headers[$key]]; |
||
| 38 | } |
||
| 39 | // check cookies |
||
| 40 | if ('Set-Cookie' === $key) { |
||
| 41 | $parts = \explode('=', $value, 2); |
||
| 42 | $cookieName = $parts[0]; |
||
| 43 | if (\is_array($headers[$key])) { |
||
| 44 | foreach ($headers[$key] as $cookieIndex => $existingCookie) { |
||
| 45 | // check if we already have a cookie with the same name |
||
| 46 | if (0 !== \mb_stripos($existingCookie, $cookieName)) { |
||
| 47 | continue; |
||
| 48 | } |
||
| 49 | |||
| 50 | // remove previous cookie with the same name |
||
| 51 | unset($headers[$key][$cookieIndex]); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | $headers[$key][] = \trim($value); |
||
| 56 | $headers[$key] = \array_values((array) $headers[$key]); // re-index array |
||
| 57 | } else { |
||
| 58 | $headers[$key][] = \trim($value); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | return $headers; |
||
| 62 | } |
||
| 78 |