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 |
||
| 10 | class Cookie implements ToArrayInterface |
||
| 11 | { |
||
| 12 | /** @var array Cookie data */ |
||
| 13 | protected $data; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string ASCII codes not valid for for use in a cookie name |
||
| 17 | * |
||
| 18 | * Cookie names are defined as 'token', according to RFC 2616, Section 2.2 |
||
| 19 | * A valid token may contain any CHAR except CTLs (ASCII 0 - 31 or 127) |
||
| 20 | * or any of the following separators |
||
| 21 | */ |
||
| 22 | protected static $invalidCharString; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Gets an array of invalid cookie characters |
||
| 26 | * |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | protected static function getInvalidCharacters() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param array $data Array of cookie data provided by a Cookie parser |
||
| 44 | */ |
||
| 45 | public function __construct(array $data = array()) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get the cookie as an array |
||
| 75 | * |
||
| 76 | * @return array |
||
| 77 | */ |
||
| 78 | public function toArray() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Get the cookie name |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function getName() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set the cookie name |
||
| 95 | * |
||
| 96 | * @param string $name Cookie name |
||
| 97 | * |
||
| 98 | * @return Cookie |
||
| 99 | */ |
||
| 100 | public function setName($name) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the cookie value |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function getValue() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Set the cookie value |
||
| 117 | * |
||
| 118 | * @param string $value Cookie value |
||
| 119 | * |
||
| 120 | * @return Cookie |
||
| 121 | */ |
||
| 122 | public function setValue($value) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the domain |
||
| 129 | * |
||
| 130 | * @return string|null |
||
| 131 | */ |
||
| 132 | public function getDomain() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Set the domain of the cookie |
||
| 139 | * |
||
| 140 | * @param string $domain |
||
| 141 | * |
||
| 142 | * @return Cookie |
||
| 143 | */ |
||
| 144 | public function setDomain($domain) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get the path |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public function getPath() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Set the path of the cookie |
||
| 161 | * |
||
| 162 | * @param string $path Path of the cookie |
||
| 163 | * |
||
| 164 | * @return Cookie |
||
| 165 | */ |
||
| 166 | public function setPath($path) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Maximum lifetime of the cookie in seconds |
||
| 173 | * |
||
| 174 | * @return int|null |
||
| 175 | */ |
||
| 176 | public function getMaxAge() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Set the max-age of the cookie |
||
| 183 | * |
||
| 184 | * @param int $maxAge Max age of the cookie in seconds |
||
| 185 | * |
||
| 186 | * @return Cookie |
||
| 187 | */ |
||
| 188 | public function setMaxAge($maxAge) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * The UNIX timestamp when the cookie expires |
||
| 195 | * |
||
| 196 | * @return mixed |
||
| 197 | */ |
||
| 198 | public function getExpires() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Set the unix timestamp for which the cookie will expire |
||
| 205 | * |
||
| 206 | * @param int $timestamp Unix timestamp |
||
| 207 | * |
||
| 208 | * @return Cookie |
||
| 209 | */ |
||
| 210 | public function setExpires($timestamp) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Version of the cookie specification. RFC 2965 is 1 |
||
| 217 | * |
||
| 218 | * @return mixed |
||
| 219 | */ |
||
| 220 | public function getVersion() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set the cookie version |
||
| 227 | * |
||
| 228 | * @param string|int $version Version to set |
||
| 229 | * |
||
| 230 | * @return Cookie |
||
| 231 | */ |
||
| 232 | public function setVersion($version) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get whether or not this is a secure cookie |
||
| 239 | * |
||
| 240 | * @return null|bool |
||
| 241 | */ |
||
| 242 | public function getSecure() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Set whether or not the cookie is secure |
||
| 249 | * |
||
| 250 | * @param bool $secure Set to true or false if secure |
||
| 251 | * |
||
| 252 | * @return Cookie |
||
| 253 | */ |
||
| 254 | public function setSecure($secure) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get whether or not this is a session cookie |
||
| 261 | * |
||
| 262 | * @return null|bool |
||
| 263 | */ |
||
| 264 | public function getDiscard() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Set whether or not this is a session cookie |
||
| 271 | * |
||
| 272 | * @param bool $discard Set to true or false if this is a session cookie |
||
| 273 | * |
||
| 274 | * @return Cookie |
||
| 275 | */ |
||
| 276 | public function setDiscard($discard) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get the comment |
||
| 283 | * |
||
| 284 | * @return string|null |
||
| 285 | */ |
||
| 286 | public function getComment() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Set the comment of the cookie |
||
| 293 | * |
||
| 294 | * @param string $comment Cookie comment |
||
| 295 | * |
||
| 296 | * @return Cookie |
||
| 297 | */ |
||
| 298 | public function setComment($comment) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Get the comment URL of the cookie |
||
| 305 | * |
||
| 306 | * @return string|null |
||
| 307 | */ |
||
| 308 | public function getCommentUrl() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Set the comment URL of the cookie |
||
| 315 | * |
||
| 316 | * @param string $commentUrl Cookie comment URL for more information |
||
| 317 | * |
||
| 318 | * @return Cookie |
||
| 319 | */ |
||
| 320 | public function setCommentUrl($commentUrl) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get an array of acceptable ports this cookie can be used with |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | public function getPorts() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Set a list of acceptable ports this cookie can be used with |
||
| 337 | * |
||
| 338 | * @param array $ports Array of acceptable ports |
||
| 339 | * |
||
| 340 | * @return Cookie |
||
| 341 | */ |
||
| 342 | public function setPorts(array $ports) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get whether or not this is an HTTP only cookie |
||
| 349 | * |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function getHttpOnly() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Set whether or not this is an HTTP only cookie |
||
| 359 | * |
||
| 360 | * @param bool $httpOnly Set to true or false if this is HTTP only |
||
| 361 | * |
||
| 362 | * @return Cookie |
||
| 363 | */ |
||
| 364 | public function setHttpOnly($httpOnly) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get an array of extra cookie data |
||
| 371 | * |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | public function getAttributes() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get a specific data point from the extra cookie data |
||
| 381 | * |
||
| 382 | * @param string $name Name of the data point to retrieve |
||
| 383 | * |
||
| 384 | * @return null|string |
||
| 385 | */ |
||
| 386 | public function getAttribute($name) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Set a cookie data attribute |
||
| 393 | * |
||
| 394 | * @param string $name Name of the attribute to set |
||
| 395 | * @param string $value Value to set |
||
| 396 | * |
||
| 397 | * @return Cookie |
||
| 398 | */ |
||
| 399 | public function setAttribute($name, $value) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Check if the cookie matches a path value |
||
| 408 | * |
||
| 409 | * @param string $path Path to check against |
||
| 410 | * |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | public function matchesPath($path) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Check if the cookie matches a domain value |
||
| 445 | * |
||
| 446 | * @param string $domain Domain to check against |
||
| 447 | * |
||
| 448 | * @return bool |
||
| 449 | */ |
||
| 450 | public function matchesDomain($domain) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Check if the cookie is compatible with a specific port |
||
| 470 | * |
||
| 471 | * @param int $port Port to check |
||
| 472 | * |
||
| 473 | * @return bool |
||
| 474 | */ |
||
| 475 | public function matchesPort($port) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Check if the cookie is expired |
||
| 482 | * |
||
| 483 | * @return bool |
||
| 484 | */ |
||
| 485 | public function isExpired() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Check if the cookie is valid according to RFC 6265 |
||
| 492 | * |
||
| 493 | * @return bool|string Returns true if valid or an error message if invalid |
||
| 494 | */ |
||
| 495 | public function validate() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Set a value and return the cookie object |
||
| 526 | * |
||
| 527 | * @param string $key Key to set |
||
| 528 | * @param string $value Value to set |
||
| 529 | * |
||
| 530 | * @return Cookie |
||
| 531 | */ |
||
| 532 | private function setData($key, $value) |
||
| 538 | } |
||
| 539 |
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: