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 | protected $name; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string|null |
||
| 21 | */ |
||
| 22 | protected $value; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var integer|null |
||
| 26 | */ |
||
| 27 | protected $maxAge; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \DateTime|null |
||
| 31 | */ |
||
| 32 | protected $expires; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string|null |
||
| 36 | */ |
||
| 37 | protected $domain; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $path; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var boolean |
||
| 46 | */ |
||
| 47 | protected $secure; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | protected $httpOnly; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $name |
||
| 56 | * @param string|null $value |
||
| 57 | * @param integer|\DateTime|null $expiration |
||
| 58 | * @param string|null $domain |
||
| 59 | * @param string|null $path |
||
| 60 | * @param boolean $secure |
||
| 61 | * @param boolean $httpOnly |
||
| 62 | * |
||
| 63 | * @throws \InvalidArgumentException |
||
| 64 | */ |
||
| 65 | 40 | public function __construct( |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Returns the name |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | 2 | public function getName() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Returns the value |
||
| 99 | * |
||
| 100 | * @return string|null |
||
| 101 | */ |
||
| 102 | 5 | public function getValue() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Checks if there is a value |
||
| 109 | * |
||
| 110 | * @return boolean |
||
| 111 | */ |
||
| 112 | 13 | public function hasValue() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Sets the value |
||
| 119 | * |
||
| 120 | * @param string|null $value |
||
| 121 | * |
||
| 122 | * @return self |
||
| 123 | */ |
||
| 124 | 2 | public function withValue($value) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the max age |
||
| 136 | * |
||
| 137 | * @return integer|null |
||
| 138 | */ |
||
| 139 | 8 | public function getMaxAge() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Checks if there is a max age |
||
| 146 | * |
||
| 147 | * @return boolean |
||
| 148 | */ |
||
| 149 | 5 | public function hasMaxAge() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Sets the max age |
||
| 156 | * |
||
| 157 | * @param integer|null $maxAge |
||
| 158 | * |
||
| 159 | * @return self |
||
| 160 | */ |
||
| 161 | 1 | public function withMaxAge($maxAge) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Sets both the max age and the expires attributes |
||
| 171 | * |
||
| 172 | * @param integer|\DateTime|null $expiration |
||
| 173 | * |
||
| 174 | * @return self |
||
| 175 | */ |
||
| 176 | 1 | public function withExpiration($expiration) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Returns the expiration time |
||
| 187 | * |
||
| 188 | * @return \DateTime|null |
||
| 189 | */ |
||
| 190 | 9 | public function getExpires() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Checks if there is an expiration time |
||
| 197 | * |
||
| 198 | * @return boolean |
||
| 199 | */ |
||
| 200 | 5 | public function hasExpires() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Sets the expires |
||
| 207 | * |
||
| 208 | * @param \DateTime|null $expires |
||
| 209 | * |
||
| 210 | * @return self |
||
| 211 | */ |
||
| 212 | 1 | public function withExpires(\DateTime $expires) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Checks if the cookie is expired |
||
| 222 | * |
||
| 223 | * @return boolean |
||
| 224 | */ |
||
| 225 | 5 | public function isExpired() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Returns the domain |
||
| 232 | * |
||
| 233 | * @return string|null |
||
| 234 | */ |
||
| 235 | 3 | public function getDomain() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Checks if there is a domain |
||
| 242 | * |
||
| 243 | * @return boolean |
||
| 244 | */ |
||
| 245 | 4 | public function hasDomain() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Sets the domain |
||
| 252 | * |
||
| 253 | * @param string|null $domain |
||
| 254 | * |
||
| 255 | * @return self |
||
| 256 | */ |
||
| 257 | 1 | public function withDomain($domain) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Matches a domain |
||
| 267 | * |
||
| 268 | * @param string $domain |
||
| 269 | * |
||
| 270 | * @return boolean |
||
| 271 | * |
||
| 272 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
| 273 | */ |
||
| 274 | 2 | public function matchDomain($domain) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Returns the path |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | */ |
||
| 294 | 2 | public function getPath() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Sets the path |
||
| 301 | * |
||
| 302 | * @param string|null $path |
||
| 303 | * |
||
| 304 | * @return self |
||
| 305 | */ |
||
| 306 | 1 | public function withPath($path) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * It matches a path |
||
| 316 | * |
||
| 317 | * @param string $path |
||
| 318 | * |
||
| 319 | * @return boolean |
||
| 320 | * |
||
| 321 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
| 322 | */ |
||
| 323 | 2 | public function matchPath($path) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Checks if HTTPS is required |
||
| 330 | * |
||
| 331 | * @return boolean |
||
| 332 | */ |
||
| 333 | 2 | public function isSecure() |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Sets the secure |
||
| 340 | * |
||
| 341 | * @param boolean $secure |
||
| 342 | * |
||
| 343 | * @return self |
||
| 344 | */ |
||
| 345 | 1 | public function withSecure($secure) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Checks if it is HTTP-only |
||
| 355 | * |
||
| 356 | * @return boolean |
||
| 357 | */ |
||
| 358 | 2 | public function isHttpOnly() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Sets the HTTP Only |
||
| 365 | * |
||
| 366 | * @param boolean $httpOnly |
||
| 367 | * |
||
| 368 | * @return self |
||
| 369 | */ |
||
| 370 | 1 | public function withHttpOnly($httpOnly) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Checks if it matches with another cookie |
||
| 380 | * |
||
| 381 | * @param Cookie $cookie |
||
| 382 | * |
||
| 383 | * @return boolean |
||
| 384 | */ |
||
| 385 | 10 | public function match(Cookie $cookie) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Validates the name attribute |
||
| 392 | * |
||
| 393 | * @param string $name |
||
| 394 | * |
||
| 395 | * @throws \InvalidArgumentException |
||
| 396 | * |
||
| 397 | * @see http://tools.ietf.org/search/rfc2616#section-2.2 |
||
| 398 | */ |
||
| 399 | 40 | private function validateName($name) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Validates a value |
||
| 413 | * |
||
| 414 | * @param string|null $value |
||
| 415 | * |
||
| 416 | * @throws \InvalidArgumentException |
||
| 417 | * |
||
| 418 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.1 |
||
| 419 | */ |
||
| 420 | 39 | private function validateValue($value) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Normalizes the expiration value |
||
| 431 | * |
||
| 432 | * @param integer|\DateTime|null $expiration |
||
| 433 | * |
||
| 434 | * @return \DateTime|null |
||
| 435 | */ |
||
| 436 | 39 | private function normalizeExpires($expiration) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Remove the leading '.' and lowercase the domain as per spec in RFC 6265 |
||
| 456 | * |
||
| 457 | * @param string|null $domain |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | * |
||
| 461 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.2.3 |
||
| 462 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
| 463 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.3 |
||
| 464 | */ |
||
| 465 | 39 | private function normalizeDomain($domain) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Processes path as per spec in RFC 6265 |
||
| 476 | * |
||
| 477 | * @param string|null $path |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | * |
||
| 481 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
| 482 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.4 |
||
| 483 | */ |
||
| 484 | 39 | private function normalizePath($path) |
|
| 494 | } |
||
| 495 |
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.