Complex classes like 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 Cookie, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Cookie { |
||
| 25 | protected $name; |
||
| 26 | protected $value; |
||
| 27 | protected $expires; |
||
| 28 | protected $path; |
||
| 29 | protected $domain; |
||
| 30 | protected $isSessionKey = true; |
||
| 31 | // TO IMPLEMENT protected $secure |
||
| 32 | // TO IMPLEMENT? protected $maxAge (add onto expires) |
||
| 33 | // TO IMPLEMENT? protected $version |
||
| 34 | // TO IMPLEMENT? protected $comment |
||
| 35 | |||
| 36 | function __construct( $name, $value, $attr ) { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Sets a cookie. Used before a request to set up any individual |
||
| 43 | * cookies. Used internally after a request to parse the |
||
| 44 | * Set-Cookie headers. |
||
| 45 | * |
||
| 46 | * @param string $value The value of the cookie |
||
| 47 | * @param array $attr Possible key/values: |
||
| 48 | * expires A date string |
||
| 49 | * path The path this cookie is used on |
||
| 50 | * domain Domain this cookie is used on |
||
| 51 | * @throws InvalidArgumentException |
||
| 52 | */ |
||
| 53 | public function set( $value, $attr ) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Return the true if the cookie is valid is valid. Otherwise, |
||
| 78 | * false. The uses a method similar to IE cookie security |
||
| 79 | * described here: |
||
| 80 | * http://kuza55.blogspot.com/2008/02/understanding-cookie-security.html |
||
| 81 | * A better method might be to use a blacklist like |
||
| 82 | * http://publicsuffix.org/ |
||
| 83 | * |
||
| 84 | * @todo fixme fails to detect 3-letter top-level domains |
||
| 85 | * @todo fixme fails to detect 2-letter top-level domains for single-domain use (probably |
||
| 86 | * not a big problem in practice, but there are test cases) |
||
| 87 | * |
||
| 88 | * @param string $domain The domain to validate |
||
| 89 | * @param string $originDomain (optional) the domain the cookie originates from |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public static function validateCookieDomain( $domain, $originDomain = null ) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Serialize the cookie jar into a format useful for HTTP Request headers. |
||
| 154 | * |
||
| 155 | * @param string $path The path that will be used. Required. |
||
| 156 | * @param string $domain The domain that will be used. Required. |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function serializeToHttpRequest( $path, $domain ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param string $domain |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | protected function canServeDomain( $domain ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $path |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | protected function canServePath( $path ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | protected function isUnExpired() { |
||
| 208 | } |
||
| 209 |