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. |
||
| 67 | */ |
||
| 68 | 61 | public function __construct( |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Creates a new cookie without any attribute validation. |
||
| 94 | * |
||
| 95 | * @param string $name |
||
| 96 | * @param string|null $value |
||
| 97 | * @param int $maxAge |
||
| 98 | * @param string|null $domain |
||
| 99 | * @param string|null $path |
||
| 100 | * @param bool $secure |
||
| 101 | * @param bool $httpOnly |
||
| 102 | * @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided. |
||
| 103 | */ |
||
| 104 | 3 | public static function createWithoutValidation( |
|
| 105 | $name, |
||
| 106 | $value = null, |
||
| 107 | $maxAge = null, |
||
| 108 | $domain = null, |
||
| 109 | $path = null, |
||
| 110 | $secure = false, |
||
| 111 | $httpOnly = false, |
||
| 112 | \DateTime $expires = null |
||
| 113 | ) { |
||
| 114 | 3 | $cookie = new self('name', null, null, $domain, $path, $secure, $httpOnly, $expires); |
|
| 115 | 3 | $cookie->name = $name; |
|
| 116 | 3 | $cookie->value = $value; |
|
| 117 | 3 | $cookie->maxAge = $maxAge; |
|
| 118 | |||
| 119 | 3 | return $cookie; |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Returns the name. |
||
| 124 | * |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | 2 | public function getName() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Returns the value. |
||
| 134 | * |
||
| 135 | * @return string|null |
||
| 136 | */ |
||
| 137 | 5 | public function getValue() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Checks if there is a value. |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 13 | public function hasValue() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Sets the value. |
||
| 154 | * |
||
| 155 | * @param string|null $value |
||
| 156 | * |
||
| 157 | * @return Cookie |
||
| 158 | */ |
||
| 159 | 8 | public function withValue($value) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Returns the max age. |
||
| 171 | * |
||
| 172 | * @return int|null |
||
| 173 | */ |
||
| 174 | 3 | public function getMaxAge() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Checks if there is a max age. |
||
| 181 | * |
||
| 182 | * @return bool |
||
| 183 | */ |
||
| 184 | 1 | public function hasMaxAge() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Sets the max age. |
||
| 191 | * |
||
| 192 | * @param int|null $maxAge |
||
| 193 | * |
||
| 194 | * @return Cookie |
||
| 195 | */ |
||
| 196 | 2 | public function withMaxAge($maxAge) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Returns the expiration time. |
||
| 208 | * |
||
| 209 | * @return \DateTime|null |
||
| 210 | */ |
||
| 211 | 3 | public function getExpires() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Checks if there is an expiration time. |
||
| 218 | * |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | 2 | public function hasExpires() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Sets the expires. |
||
| 228 | * |
||
| 229 | * @param \DateTime|null $expires |
||
| 230 | * |
||
| 231 | * @return Cookie |
||
| 232 | */ |
||
| 233 | 1 | public function withExpires(\DateTime $expires = null) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Checks if the cookie is expired. |
||
| 243 | * |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | 2 | public function isExpired() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Returns the domain. |
||
| 253 | * |
||
| 254 | * @return string|null |
||
| 255 | */ |
||
| 256 | 3 | public function getDomain() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Checks if there is a domain. |
||
| 263 | * |
||
| 264 | * @return bool |
||
| 265 | */ |
||
| 266 | 4 | public function hasDomain() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Sets the domain. |
||
| 273 | * |
||
| 274 | * @param string|null $domain |
||
| 275 | * |
||
| 276 | * @return Cookie |
||
| 277 | */ |
||
| 278 | 1 | public function withDomain($domain) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Checks whether this cookie is meant for this domain. |
||
| 288 | * |
||
| 289 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
| 290 | * |
||
| 291 | * @param string $domain |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | 2 | public function matchDomain($domain) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Returns the path. |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | 2 | public function getPath() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Sets the path. |
||
| 322 | * |
||
| 323 | * @param string|null $path |
||
| 324 | * |
||
| 325 | * @return Cookie |
||
| 326 | */ |
||
| 327 | 1 | public function withPath($path) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Checks whether this cookie is meant for this path. |
||
| 337 | * |
||
| 338 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
| 339 | * |
||
| 340 | * @param string $path |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | 3 | public function matchPath($path) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Checks whether this cookie may only be sent over HTTPS. |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | 2 | public function isSecure() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Sets whether this cookie should only be sent over HTTPS. |
||
| 361 | * |
||
| 362 | * @param bool $secure |
||
| 363 | * |
||
| 364 | * @return Cookie |
||
| 365 | */ |
||
| 366 | 1 | public function withSecure($secure) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Check whether this cookie may not be accessed through Javascript. |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | 2 | public function isHttpOnly() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Sets whether this cookie may not be accessed through Javascript. |
||
| 386 | * |
||
| 387 | * @param bool $httpOnly |
||
| 388 | * |
||
| 389 | * @return Cookie |
||
| 390 | */ |
||
| 391 | 1 | public function withHttpOnly($httpOnly) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Checks if this cookie represents the same cookie as $cookie. |
||
| 401 | * |
||
| 402 | * This does not compare the values, only name, domain and path. |
||
| 403 | * |
||
| 404 | * @param Cookie $cookie |
||
| 405 | * |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | 10 | public function match(Cookie $cookie) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Validates cookie attributes. |
||
| 415 | * |
||
| 416 | * @return bool |
||
| 417 | */ |
||
| 418 | 4 | public function isValid() |
|
| 419 | { |
||
| 420 | try { |
||
| 421 | 4 | $this->validateName($this->name); |
|
| 422 | 3 | $this->validateValue($this->value); |
|
| 423 | 2 | $this->validateMaxAge($this->maxAge); |
|
| 424 | 4 | } catch (\InvalidArgumentException $e) { |
|
| 425 | 3 | return false; |
|
| 426 | } |
||
| 427 | |||
| 428 | 1 | return true; |
|
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Validates the name attribute. |
||
| 433 | * |
||
| 434 | * @see http://tools.ietf.org/search/rfc2616#section-2.2 |
||
| 435 | * |
||
| 436 | * @param string $name |
||
| 437 | * |
||
| 438 | * @throws \InvalidArgumentException If the name is empty or contains invalid characters. |
||
| 439 | */ |
||
| 440 | 61 | private function validateName($name) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Validates a value. |
||
| 454 | * |
||
| 455 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.1 |
||
| 456 | * |
||
| 457 | * @param string|null $value |
||
| 458 | * |
||
| 459 | * @throws \InvalidArgumentException If the value contains invalid characters. |
||
| 460 | */ |
||
| 461 | 57 | private function validateValue($value) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Validates a Max-Age attribute. |
||
| 472 | * |
||
| 473 | * @param int|null $maxAge |
||
| 474 | * |
||
| 475 | * @throws \InvalidArgumentException If the Max-Age is not an empty or integer value. |
||
| 476 | */ |
||
| 477 | 54 | private function validateMaxAge($maxAge) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Remove the leading '.' and lowercase the domain as per spec in RFC 6265. |
||
| 488 | * |
||
| 489 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.2.3 |
||
| 490 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
| 491 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.3 |
||
| 492 | * |
||
| 493 | * @param string|null $domain |
||
| 494 | * |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | 54 | private function normalizeDomain($domain) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Processes path as per spec in RFC 6265. |
||
| 508 | * |
||
| 509 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
| 510 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.4 |
||
| 511 | * |
||
| 512 | * @param string|null $path |
||
| 513 | * |
||
| 514 | * @return string |
||
| 515 | */ |
||
| 516 | 54 | private function normalizePath($path) |
|
| 526 | } |
||
| 527 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.