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 |
||
| 12 | final class Cookie |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $name; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string|null |
||
| 21 | */ |
||
| 22 | private $value; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var int|null |
||
| 26 | */ |
||
| 27 | private $maxAge; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string|null |
||
| 31 | */ |
||
| 32 | private $domain; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $path; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | private $secure; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private $httpOnly; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Expires attribute is HTTP 1.0 only and should be avoided. |
||
| 51 | * |
||
| 52 | * @var \DateTime|null |
||
| 53 | */ |
||
| 54 | private $expires; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Validation state |
||
| 58 | * |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | private $valid; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param string $name |
||
| 65 | * @param string|null $value |
||
| 66 | * @param int $maxAge |
||
| 67 | * @param string|null $domain |
||
| 68 | 57 | * @param string|null $path |
|
| 69 | * @param bool $secure |
||
| 70 | * @param bool $httpOnly |
||
| 71 | * @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided. |
||
| 72 | * @param bool $requireValidation deprecated since version 1.5. Will be removed in 2.0 |
||
| 73 | * |
||
| 74 | * @throws \InvalidArgumentException If name, value or max age is not valid. Attributes validation during instantiation is deprecated since 1.5 and will be removed in 2.0. |
||
| 75 | */ |
||
| 76 | public function __construct( |
||
| 104 | |||
| 105 | public function createWithoutValidation( |
||
| 117 | 13 | ||
| 118 | /** |
||
| 119 | 13 | * Returns the name. |
|
| 120 | * |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public function getName() |
||
| 127 | |||
| 128 | /** |
||
| 129 | 8 | * Returns the value. |
|
| 130 | * |
||
| 131 | 8 | * @return string|null |
|
| 132 | */ |
||
| 133 | 5 | public function getValue() |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Checks if there is a value. |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | public function hasValue() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Sets the value. |
||
| 150 | * |
||
| 151 | * @param string|null $value |
||
| 152 | * |
||
| 153 | * @return Cookie |
||
| 154 | 1 | */ |
|
| 155 | public function withValue($value) |
||
| 164 | |||
| 165 | /** |
||
| 166 | 2 | * Returns the max age. |
|
| 167 | * |
||
| 168 | 2 | * @return int|null |
|
| 169 | */ |
||
| 170 | 1 | public function getMaxAge() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Checks if there is a max age. |
||
| 177 | * |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function hasMaxAge() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Sets the max age. |
||
| 187 | * |
||
| 188 | * @param int|null $maxAge |
||
| 189 | * |
||
| 190 | * @return Cookie |
||
| 191 | 2 | */ |
|
| 192 | public function withMaxAge($maxAge) |
||
| 201 | |||
| 202 | /** |
||
| 203 | 1 | * Returns the expiration time. |
|
| 204 | * |
||
| 205 | 1 | * @return \DateTime|null |
|
| 206 | 1 | */ |
|
| 207 | public function getExpires() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Checks if there is an expiration time. |
||
| 214 | * |
||
| 215 | * @return bool |
||
| 216 | 2 | */ |
|
| 217 | public function hasExpires() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Sets the expires. |
||
| 224 | * |
||
| 225 | * @param \DateTime|null $expires |
||
| 226 | 3 | * |
|
| 227 | * @return Cookie |
||
| 228 | 3 | */ |
|
| 229 | public function withExpires(\DateTime $expires = null) |
||
| 236 | 4 | ||
| 237 | /** |
||
| 238 | 4 | * Checks if the cookie is expired. |
|
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | public function isExpired() |
||
| 246 | |||
| 247 | /** |
||
| 248 | 1 | * Returns the domain. |
|
| 249 | * |
||
| 250 | 1 | * @return string|null |
|
| 251 | 1 | */ |
|
| 252 | public function getDomain() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Checks if there is a domain. |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | public function hasDomain() |
||
| 266 | |||
| 267 | /** |
||
| 268 | 2 | * Sets the domain. |
|
| 269 | 2 | * |
|
| 270 | * @param string|null $domain |
||
| 271 | * |
||
| 272 | * @return Cookie |
||
| 273 | 1 | */ |
|
| 274 | 1 | public function withDomain($domain) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Checks whether this cookie is meant for this domain. |
||
| 284 | * |
||
| 285 | 2 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
|
| 286 | * |
||
| 287 | 2 | * @param string $domain |
|
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | public function matchDomain($domain) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Returns the path. |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getPath() |
||
| 315 | |||
| 316 | 3 | /** |
|
| 317 | * Sets the path. |
||
| 318 | * |
||
| 319 | * @param string|null $path |
||
| 320 | * |
||
| 321 | * @return Cookie |
||
| 322 | */ |
||
| 323 | public function withPath($path) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Checks whether this cookie is meant for this path. |
||
| 333 | * |
||
| 334 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
| 335 | * |
||
| 336 | 1 | * @param string $path |
|
| 337 | * |
||
| 338 | 1 | * @return bool |
|
| 339 | 1 | */ |
|
| 340 | public function matchPath($path) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Checks whether this cookie may only be sent over HTTPS. |
||
| 347 | * |
||
| 348 | * @return bool |
||
| 349 | 2 | */ |
|
| 350 | public function isSecure() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Sets whether this cookie should only be sent over HTTPS. |
||
| 357 | * |
||
| 358 | * @param bool $secure |
||
| 359 | * |
||
| 360 | * @return Cookie |
||
| 361 | 1 | */ |
|
| 362 | public function withSecure($secure) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Check whether this cookie may not be accessed through Javascript. |
||
| 372 | * |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | public function isHttpOnly() |
||
| 379 | |||
| 380 | 10 | /** |
|
| 381 | * Sets whether this cookie may not be accessed through Javascript. |
||
| 382 | * |
||
| 383 | * @param bool $httpOnly |
||
| 384 | * |
||
| 385 | * @return Cookie |
||
| 386 | */ |
||
| 387 | public function withHttpOnly($httpOnly) |
||
| 394 | 57 | ||
| 395 | 1 | /** |
|
| 396 | * Checks if this cookie represents the same cookie as $cookie. |
||
| 397 | * |
||
| 398 | * This does not compare the values, only name, domain and path. |
||
| 399 | 56 | * |
|
| 400 | 3 | * @param Cookie $cookie |
|
| 401 | * |
||
| 402 | 53 | * @return bool |
|
| 403 | */ |
||
| 404 | public function match(Cookie $cookie) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Validates cookie attributes |
||
| 411 | * |
||
| 412 | * @return bool |
||
| 413 | 53 | */ |
|
| 414 | public function isValid() |
||
| 429 | 50 | ||
| 430 | /** |
||
| 431 | 50 | * Validates the name attribute. |
|
| 432 | 7 | * |
|
| 433 | 1 | * @see http://tools.ietf.org/search/rfc2616#section-2.2 |
|
| 434 | * |
||
| 435 | 6 | * @param string $name |
|
| 436 | 50 | * |
|
| 437 | * @throws \InvalidArgumentException If the name is empty or contains invalid characters. |
||
| 438 | */ |
||
| 439 | private function validateName($name) |
||
| 450 | |||
| 451 | 50 | /** |
|
| 452 | 6 | * Validates a value. |
|
| 453 | 6 | * |
|
| 454 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.1 |
||
| 455 | 50 | * |
|
| 456 | * @param string|null $value |
||
| 457 | * |
||
| 458 | * @throws \InvalidArgumentException If the value contains invalid characters. |
||
| 459 | */ |
||
| 460 | private function validateValue($value) |
||
| 468 | 50 | ||
| 469 | /** |
||
| 470 | 50 | * Validates a Max-Age attribute. |
|
| 471 | * |
||
| 472 | 50 | * @param int|null $maxAge |
|
| 473 | 49 | * |
|
| 474 | 49 | * @throws \InvalidArgumentException If the Max-Age is not an empty or integer value. |
|
| 475 | */ |
||
| 476 | 50 | private function validateMaxAge($maxAge) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Remove the leading '.' and lowercase the domain as per spec in RFC 6265. |
||
| 487 | * |
||
| 488 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.2.3 |
||
| 489 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
| 490 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.3 |
||
| 491 | * |
||
| 492 | * @param string|null $domain |
||
| 493 | * |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | private function normalizeDomain($domain) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Processes path as per spec in RFC 6265. |
||
| 507 | * |
||
| 508 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
| 509 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.4 |
||
| 510 | * |
||
| 511 | * @param string|null $path |
||
| 512 | * |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | private function normalizePath($path) |
||
| 525 | } |
||
| 526 |
If you suppress an error, we recommend checking for the error condition explicitly: