Complex classes like SetCookie 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 SetCookie, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class SetCookie |
||
| 8 | { |
||
| 9 | /** @var array */ |
||
| 10 | private static $defaults = [ |
||
| 11 | 'Name' => null, |
||
| 12 | 'Value' => null, |
||
| 13 | 'Domain' => null, |
||
| 14 | 'Path' => '/', |
||
| 15 | 'Max-Age' => null, |
||
| 16 | 'Expires' => null, |
||
| 17 | 'Secure' => false, |
||
| 18 | 'Discard' => false, |
||
| 19 | 'HttpOnly' => false |
||
| 20 | ]; |
||
| 21 | |||
| 22 | /** @var array Cookie data */ |
||
| 23 | private $data; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Create a new SetCookie object from a string |
||
| 27 | * |
||
| 28 | * @param string $cookie Set-Cookie header string |
||
| 29 | * |
||
| 30 | * @return self |
||
| 31 | */ |
||
| 32 | public static function fromString($cookie) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param array $data Array of cookie data provided by a Cookie parser |
||
| 72 | */ |
||
| 73 | public function __construct(array $data = []) |
||
| 84 | |||
| 85 | public function __toString() |
||
| 100 | |||
| 101 | public function toArray() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the cookie name |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function getName() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set the cookie name |
||
| 118 | * |
||
| 119 | * @param string $name Cookie name |
||
| 120 | */ |
||
| 121 | public function setName($name) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get the cookie value |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | public function getValue() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set the cookie value |
||
| 138 | * |
||
| 139 | * @param string $value Cookie value |
||
| 140 | */ |
||
| 141 | public function setValue($value) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the domain |
||
| 148 | * |
||
| 149 | * @return string|null |
||
| 150 | */ |
||
| 151 | public function getDomain() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Set the domain of the cookie |
||
| 158 | * |
||
| 159 | * @param string $domain |
||
| 160 | */ |
||
| 161 | public function setDomain($domain) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Get the path |
||
| 168 | * |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getPath() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Set the path of the cookie |
||
| 178 | * |
||
| 179 | * @param string $path Path of the cookie |
||
| 180 | */ |
||
| 181 | public function setPath($path) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Maximum lifetime of the cookie in seconds |
||
| 188 | * |
||
| 189 | * @return int|null |
||
| 190 | */ |
||
| 191 | public function getMaxAge() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Set the max-age of the cookie |
||
| 198 | * |
||
| 199 | * @param int $maxAge Max age of the cookie in seconds |
||
| 200 | */ |
||
| 201 | public function setMaxAge($maxAge) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * The UNIX timestamp when the cookie Expires |
||
| 208 | * |
||
| 209 | * @return mixed |
||
| 210 | */ |
||
| 211 | public function getExpires() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Set the unix timestamp for which the cookie will expire |
||
| 218 | * |
||
| 219 | * @param int $timestamp Unix timestamp |
||
| 220 | */ |
||
| 221 | public function setExpires($timestamp) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get whether or not this is a secure cookie |
||
| 230 | * |
||
| 231 | * @return null|bool |
||
| 232 | */ |
||
| 233 | public function getSecure() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Set whether or not the cookie is secure |
||
| 240 | * |
||
| 241 | * @param bool $secure Set to true or false if secure |
||
| 242 | */ |
||
| 243 | public function setSecure($secure) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get whether or not this is a session cookie |
||
| 250 | * |
||
| 251 | * @return null|bool |
||
| 252 | */ |
||
| 253 | public function getDiscard() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Set whether or not this is a session cookie |
||
| 260 | * |
||
| 261 | * @param bool $discard Set to true or false if this is a session cookie |
||
| 262 | */ |
||
| 263 | public function setDiscard($discard) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get whether or not this is an HTTP only cookie |
||
| 270 | * |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | public function getHttpOnly() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Set whether or not this is an HTTP only cookie |
||
| 280 | * |
||
| 281 | * @param bool $httpOnly Set to true or false if this is HTTP only |
||
| 282 | */ |
||
| 283 | public function setHttpOnly($httpOnly) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Check if the cookie matches a path value. |
||
| 290 | * |
||
| 291 | * A request-path path-matches a given cookie-path if at least one of |
||
| 292 | * the following conditions holds: |
||
| 293 | * |
||
| 294 | * - The cookie-path and the request-path are identical. |
||
| 295 | * - The cookie-path is a prefix of the request-path, and the last |
||
| 296 | * character of the cookie-path is %x2F ("/"). |
||
| 297 | * - The cookie-path is a prefix of the request-path, and the first |
||
| 298 | * character of the request-path that is not included in the cookie- |
||
| 299 | * path is a %x2F ("/") character. |
||
| 300 | * |
||
| 301 | * @param string $requestPath Path to check against |
||
| 302 | * |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | public function matchesPath($requestPath) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Check if the cookie matches a domain value |
||
| 330 | * |
||
| 331 | * @param string $domain Domain to check against |
||
| 332 | * |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | public function matchesDomain($domain) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Check if the cookie is expired |
||
| 357 | * |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | public function isExpired() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Check if the cookie is valid according to RFC 6265 |
||
| 367 | * |
||
| 368 | * @return bool|string Returns true if valid or an error message if invalid |
||
| 369 | */ |
||
| 370 | public function validate() |
||
| 404 | } |
||
| 405 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: