Complex classes like headers often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use headers, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | final class headers |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Parse response headers string from a HTTP request into an array of headers. e.g. |
||
| 23 | * [ 'Location' => 'http://www.example.com', ... ] |
||
| 24 | * When multiple headers with the same name are present, all values will form an array, in the order in which |
||
| 25 | * they are present in the source. |
||
| 26 | * @param string|string[] $headers The headers string to parse. |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | 6 | public static function parse( $headers ) { |
|
| 61 | 6 | ||
| 62 | /** |
||
| 63 | * Return an array with values from a Comma seperated header like Cache-Control or Accept |
||
| 64 | * e.g. 'max-age=300,public,no-store' |
||
| 65 | * results in |
||
| 66 | * [ 0 => [ 'max-age' => '300' ], 1 => [ 'public' => 'public' ], 2 => ['no-store' => 'no-store'] ] |
||
| 67 | * @param string $header |
||
| 68 | * @return array |
||
| 69 | 1 | */ |
|
| 70 | 1 | public static function parseHeader($header) |
|
| 90 | 4 | ||
| 91 | 4 | /** |
|
| 92 | 4 | * Merge multiple occurances of a comma seperated header |
|
| 93 | 4 | * @param array $headers |
|
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | public static function mergeHeaders( $headers ) |
||
| 109 | 1 | ||
| 110 | 1 | /** |
|
| 111 | 1 | * Parse response headers to determine if and how long you may cache the response. Doesn't understand ETags. |
|
| 112 | 4 | * @param string|string[] $headers Headers string or array as returned by parse() |
|
| 113 | * @param bool $private Whether to store a private cache (true) or public cache image (false). Default is public. |
||
| 114 | * @return int The number of seconds you may cache this result starting from now. |
||
| 115 | 4 | */ |
|
| 116 | public static function parseCacheTime( $headers, $private=false ) |
||
| 131 | 3 | ||
| 132 | 1 | /** |
|
| 133 | 1 | * Parses Accept-* header and returns best matching value from the $acceptable list |
|
| 134 | 1 | * Takes into account the Q value and wildcards. Does not take into account other parameters |
|
| 135 | 1 | * currently ( e.g. text/html;level=1 ) |
|
| 136 | 2 | * @param array|string $header The Accept-* header (Accept:, Accept-Lang:, Accept-Encoding: etc.) |
|
| 137 | 2 | * @param array $acceptable List of acceptable values, in order of preference |
|
| 138 | 1 | * @return string |
|
| 139 | 1 | */ |
|
| 140 | 1 | public static function accept( $header, $acceptable ) |
|
| 152 | |||
| 153 | private static function addHeader($headers, $name, $value) |
||
| 169 | 4 | ||
| 170 | 5 | private static function getCacheControlTime( $header, $private ) |
|
| 203 | |||
| 204 | private static function orderByQuality($header) |
||
| 219 | |||
| 220 | private static function pregEscape($string) { |
||
| 225 | |||
| 226 | private static function isAcceptable($name, $acceptable) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Return the last value sent for a specific header, uses the output of parse(). |
||
| 235 | * @param (mixed) $headers An array with multiple header strings or a single string. |
||
| 236 | * @return array|mixed |
||
| 237 | */ |
||
| 238 | private static function getLastHeader($headers) { |
||
| 244 | |||
| 245 | |||
| 246 | } |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.