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 | * @param string $name |
||
| 58 | * @param string|null $value |
||
| 59 | * @param int $maxAge |
||
| 60 | * @param string|null $domain |
||
| 61 | * @param string|null $path |
||
| 62 | * @param bool $secure |
||
| 63 | * @param bool $httpOnly |
||
| 64 | * @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided. |
||
| 65 | * |
||
| 66 | * @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. |
||
| 67 | */ |
||
| 68 | 73 | public function __construct( |
|
| 96 | |||
| 97 | 15 | public static function createWithoutValidation( |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Returns the name. |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | 2 | public function getName() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Returns the value. |
||
| 127 | * |
||
| 128 | * @return string|null |
||
| 129 | */ |
||
| 130 | 5 | public function getValue() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Checks if there is a value. |
||
| 137 | * |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | 13 | public function hasValue() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Sets the value. |
||
| 147 | * |
||
| 148 | * @param string|null $value |
||
| 149 | * |
||
| 150 | * @return Cookie |
||
| 151 | */ |
||
| 152 | 8 | public function withValue($value) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Returns the max age. |
||
| 164 | * |
||
| 165 | * @return int|null |
||
| 166 | */ |
||
| 167 | 3 | public function getMaxAge() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Checks if there is a max age. |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | 1 | public function hasMaxAge() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Sets the max age. |
||
| 184 | * |
||
| 185 | * @param int|null $maxAge |
||
| 186 | * |
||
| 187 | * @return Cookie |
||
| 188 | */ |
||
| 189 | 2 | public function withMaxAge($maxAge) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the expiration time. |
||
| 201 | * |
||
| 202 | * @return \DateTime|null |
||
| 203 | */ |
||
| 204 | 3 | public function getExpires() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Checks if there is an expiration time. |
||
| 211 | * |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | 2 | public function hasExpires() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Sets the expires. |
||
| 221 | * |
||
| 222 | * @param \DateTime|null $expires |
||
| 223 | * |
||
| 224 | * @return Cookie |
||
| 225 | */ |
||
| 226 | 1 | public function withExpires(\DateTime $expires = null) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Checks if the cookie is expired. |
||
| 236 | * |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | 2 | public function isExpired() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Returns the domain. |
||
| 246 | * |
||
| 247 | * @return string|null |
||
| 248 | */ |
||
| 249 | 3 | public function getDomain() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Checks if there is a domain. |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | 4 | public function hasDomain() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Sets the domain. |
||
| 266 | * |
||
| 267 | * @param string|null $domain |
||
| 268 | * |
||
| 269 | * @return Cookie |
||
| 270 | */ |
||
| 271 | 1 | public function withDomain($domain) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Checks whether this cookie is meant for this domain. |
||
| 281 | * |
||
| 282 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
| 283 | * |
||
| 284 | * @param string $domain |
||
| 285 | * |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | 2 | public function matchDomain($domain) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Returns the path. |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | 2 | public function getPath() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Sets the path. |
||
| 315 | * |
||
| 316 | * @param string|null $path |
||
| 317 | * |
||
| 318 | * @return Cookie |
||
| 319 | */ |
||
| 320 | 1 | public function withPath($path) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Checks whether this cookie is meant for this path. |
||
| 330 | * |
||
| 331 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
| 332 | * |
||
| 333 | * @param string $path |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | 3 | public function matchPath($path) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Checks whether this cookie may only be sent over HTTPS. |
||
| 344 | * |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | 2 | public function isSecure() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Sets whether this cookie should only be sent over HTTPS. |
||
| 354 | * |
||
| 355 | * @param bool $secure |
||
| 356 | * |
||
| 357 | * @return Cookie |
||
| 358 | */ |
||
| 359 | 1 | public function withSecure($secure) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Check whether this cookie may not be accessed through Javascript. |
||
| 369 | * |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | 2 | public function isHttpOnly() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Sets whether this cookie may not be accessed through Javascript. |
||
| 379 | * |
||
| 380 | * @param bool $httpOnly |
||
| 381 | * |
||
| 382 | * @return Cookie |
||
| 383 | */ |
||
| 384 | 1 | public function withHttpOnly($httpOnly) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Checks if this cookie represents the same cookie as $cookie. |
||
| 394 | * |
||
| 395 | * This does not compare the values, only name, domain and path. |
||
| 396 | * |
||
| 397 | * @param Cookie $cookie |
||
| 398 | * |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | 10 | public function match(Cookie $cookie) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Validates cookie attributes. |
||
| 408 | * |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | 16 | public function isValid() |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Validates the name attribute. |
||
| 426 | * |
||
| 427 | * @see http://tools.ietf.org/search/rfc2616#section-2.2 |
||
| 428 | * |
||
| 429 | * @param string $name |
||
| 430 | * |
||
| 431 | * @throws \InvalidArgumentException If the name is empty or contains invalid characters. |
||
| 432 | */ |
||
| 433 | 73 | private function validateName($name) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Validates a value. |
||
| 447 | * |
||
| 448 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.1 |
||
| 449 | * |
||
| 450 | * @param string|null $value |
||
| 451 | * |
||
| 452 | * @throws \InvalidArgumentException If the value contains invalid characters. |
||
| 453 | */ |
||
| 454 | 69 | private function validateValue($value) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Validates a Max-Age attribute. |
||
| 465 | * |
||
| 466 | * @param int|null $maxAge |
||
| 467 | * |
||
| 468 | * @throws \InvalidArgumentException If the Max-Age is not an empty or integer value. |
||
| 469 | */ |
||
| 470 | 66 | private function validateMaxAge($maxAge) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Remove the leading '.' and lowercase the domain as per spec in RFC 6265. |
||
| 481 | * |
||
| 482 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.2.3 |
||
| 483 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
| 484 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.3 |
||
| 485 | * |
||
| 486 | * @param string|null $domain |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | 66 | private function normalizeDomain($domain) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Processes path as per spec in RFC 6265. |
||
| 501 | * |
||
| 502 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
| 503 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.4 |
||
| 504 | * |
||
| 505 | * @param string|null $path |
||
| 506 | * |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | 66 | private function normalizePath($path) |
|
| 519 | } |
||
| 520 |
If you suppress an error, we recommend checking for the error condition explicitly: