Complex classes like Requests_Cookie 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 Requests_Cookie, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Requests_Cookie { |
||
16 | /** |
||
17 | * Cookie name. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | public $name; |
||
22 | |||
23 | /** |
||
24 | * Cookie value. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | public $value; |
||
29 | |||
30 | /** |
||
31 | * Cookie attributes |
||
32 | * |
||
33 | * Valid keys are (currently) path, domain, expires, max-age, secure and |
||
34 | * httponly. |
||
35 | * |
||
36 | * @var Requests_Utility_CaseInsensitiveDictionary|array Array-like object |
||
37 | */ |
||
38 | public $attributes = array(); |
||
39 | |||
40 | /** |
||
41 | * Cookie flags |
||
42 | * |
||
43 | * Valid keys are (currently) creation, last-access, persistent and |
||
44 | * host-only. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | public $flags = array(); |
||
49 | |||
50 | /** |
||
51 | * Reference time for relative calculations |
||
52 | * |
||
53 | * This is used in place of `time()` when calculating Max-Age expiration and |
||
54 | * checking time validity. |
||
55 | * |
||
56 | * @var int |
||
57 | */ |
||
58 | public $reference_time = 0; |
||
59 | |||
60 | /** |
||
61 | * Create a new cookie object |
||
62 | * |
||
63 | * @param string $name |
||
64 | * @param string $value |
||
65 | * @param array|Requests_Utility_CaseInsensitiveDictionary $attributes Associative array of attribute data |
||
66 | */ |
||
67 | public function __construct($name, $value, $attributes = array(), $flags = array(), $reference_time = null) { |
||
86 | |||
87 | /** |
||
88 | * Check if a cookie is expired. |
||
89 | * |
||
90 | * Checks the age against $this->reference_time to determine if the cookie |
||
91 | * is expired. |
||
92 | * |
||
93 | * @return boolean True if expired, false if time is valid. |
||
94 | */ |
||
95 | public function is_expired() { |
||
112 | |||
113 | /** |
||
114 | * Check if a cookie is valid for a given URI |
||
115 | * |
||
116 | * @param Requests_IRI $uri URI to check |
||
117 | * @return boolean Whether the cookie is valid for the given URI |
||
118 | */ |
||
119 | public function uri_matches(Requests_IRI $uri) { |
||
130 | |||
131 | /** |
||
132 | * Check if a cookie is valid for a given domain |
||
133 | * |
||
134 | * @param string $string Domain to check |
||
135 | * @return boolean Whether the cookie is valid for the given domain |
||
136 | */ |
||
137 | public function domain_matches($string) { |
||
177 | |||
178 | /** |
||
179 | * Check if a cookie is valid for a given path |
||
180 | * |
||
181 | * From the path-match check in RFC 6265 section 5.1.4 |
||
182 | * |
||
183 | * @param string $request_path Path to check |
||
184 | * @return boolean Whether the cookie is valid for the given path |
||
185 | */ |
||
186 | public function path_matches($request_path) { |
||
222 | |||
223 | /** |
||
224 | * Normalize cookie and attributes |
||
225 | * |
||
226 | * @return boolean Whether the cookie was successfully normalized |
||
227 | */ |
||
228 | public function normalize() { |
||
244 | |||
245 | /** |
||
246 | * Parse an individual cookie attribute |
||
247 | * |
||
248 | * Handles parsing individual attributes from the cookie values. |
||
249 | * |
||
250 | * @param string $name Attribute name |
||
251 | * @param string|boolean $value Attribute value (string value, or true if empty/flag) |
||
252 | * @return mixed Value if available, or null if the attribute value is invalid (and should be skipped) |
||
253 | */ |
||
254 | protected function normalize_attribute($name, $value) { |
||
302 | |||
303 | /** |
||
304 | * Format a cookie for a Cookie header |
||
305 | * |
||
306 | * This is used when sending cookies to a server. |
||
307 | * |
||
308 | * @return string Cookie formatted for Cookie header |
||
309 | */ |
||
310 | public function format_for_header() { |
||
313 | |||
314 | /** |
||
315 | * Format a cookie for a Cookie header |
||
316 | * |
||
317 | * @codeCoverageIgnore |
||
318 | * @deprecated Use {@see Requests_Cookie::format_for_header} |
||
319 | * @return string |
||
320 | */ |
||
321 | public function formatForHeader() { |
||
324 | |||
325 | /** |
||
326 | * Format a cookie for a Set-Cookie header |
||
327 | * |
||
328 | * This is used when sending cookies to clients. This isn't really |
||
329 | * applicable to client-side usage, but might be handy for debugging. |
||
330 | * |
||
331 | * @return string Cookie formatted for Set-Cookie header |
||
332 | */ |
||
333 | public function format_for_set_cookie() { |
||
351 | |||
352 | /** |
||
353 | * Format a cookie for a Set-Cookie header |
||
354 | * |
||
355 | * @codeCoverageIgnore |
||
356 | * @deprecated Use {@see Requests_Cookie::format_for_set_cookie} |
||
357 | * @return string |
||
358 | */ |
||
359 | public function formatForSetCookie() { |
||
362 | |||
363 | /** |
||
364 | * Get the cookie value |
||
365 | * |
||
366 | * Attributes and other data can be accessed via methods. |
||
367 | */ |
||
368 | public function __toString() { |
||
371 | |||
372 | /** |
||
373 | * Parse a cookie string into a cookie object |
||
374 | * |
||
375 | * Based on Mozilla's parsing code in Firefox and related projects, which |
||
376 | * is an intentional deviation from RFC 2109 and RFC 2616. RFC 6265 |
||
377 | * specifies some of this handling, but not in a thorough manner. |
||
378 | * |
||
379 | * @param string Cookie header value (from a Set-Cookie header) |
||
380 | * @return Requests_Cookie Parsed cookie object |
||
381 | */ |
||
382 | public static function parse($string, $name = '', $reference_time = null) { |
||
425 | |||
426 | /** |
||
427 | * Parse all Set-Cookie headers from request headers |
||
428 | * |
||
429 | * @param Requests_Response_Headers $headers Headers to parse from |
||
430 | * @param Requests_IRI|null $origin URI for comparing cookie origins |
||
431 | * @param int|null $time Reference time for expiration calculation |
||
432 | * @return array |
||
433 | */ |
||
434 | public static function parse_from_headers(Requests_Response_Headers $headers, Requests_IRI $origin = null, $time = null) { |
||
489 | |||
490 | /** |
||
491 | * Parse all Set-Cookie headers from request headers |
||
492 | * |
||
493 | * @codeCoverageIgnore |
||
494 | * @deprecated Use {@see Requests_Cookie::parse_from_headers} |
||
495 | * @return string |
||
496 | */ |
||
497 | public static function parseFromHeaders(Requests_Response_Headers $headers) { |
||
500 | } |
||
501 |