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 int|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 bool |
||
| 46 | */ |
||
| 47 | protected $secure; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected $httpOnly; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $name |
||
| 56 | * @param string|null $value |
||
| 57 | * @param int|\DateTime|null $expiration |
||
| 58 | * @param string|null $domain |
||
| 59 | * @param string|null $path |
||
| 60 | * @param bool $secure |
||
| 61 | * @param bool $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 bool |
||
| 111 | */ |
||
| 112 | 13 | public function hasValue() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Sets the value. |
||
| 119 | * |
||
| 120 | * @param string|null $value |
||
| 121 | * |
||
| 122 | * @return Cookie |
||
| 123 | */ |
||
| 124 | 2 | public function withValue($value) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the max age. |
||
| 136 | * |
||
| 137 | * @return int|null |
||
| 138 | */ |
||
| 139 | 8 | public function getMaxAge() |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Checks if there is a max age. |
||
| 146 | * |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | 5 | public function hasMaxAge() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Sets the max age. |
||
| 156 | * |
||
| 157 | * @param int|null $maxAge |
||
| 158 | * |
||
| 159 | * @return Cookie |
||
| 160 | */ |
||
| 161 | 1 | public function withMaxAge($maxAge) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Sets both the max age and the expires attributes. |
||
| 171 | * |
||
| 172 | * @param int|\DateTime|null $expiration |
||
| 173 | * |
||
| 174 | * @return Cookie |
||
| 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 bool |
||
| 199 | */ |
||
| 200 | 5 | public function hasExpires() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Sets the expires. |
||
| 207 | * |
||
| 208 | * @param \DateTime|null $expires |
||
| 209 | * |
||
| 210 | * @return Cookie |
||
| 211 | */ |
||
| 212 | 1 | public function withExpires(\DateTime $expires) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Checks if the cookie is expired. |
||
| 222 | * |
||
| 223 | * @return bool |
||
| 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 bool |
||
| 244 | */ |
||
| 245 | 4 | public function hasDomain() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Sets the domain. |
||
| 252 | * |
||
| 253 | * @param string|null $domain |
||
| 254 | * |
||
| 255 | * @return Cookie |
||
| 256 | */ |
||
| 257 | 1 | public function withDomain($domain) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Checks whether this cookie is meant for this domain. |
||
| 267 | * |
||
| 268 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
| 269 | * |
||
| 270 | * @param string $domain |
||
| 271 | * |
||
| 272 | * @return bool |
||
| 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 Cookie |
||
| 305 | */ |
||
| 306 | 1 | public function withPath($path) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Checks whether this cookie is meant for this path. |
||
| 316 | * |
||
| 317 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
| 318 | * |
||
| 319 | * @param string $path |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | 2 | public function matchPath($path) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Checks whether this cookie may only be sent over HTTPS. |
||
| 330 | * |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | 2 | public function isSecure() |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Sets whether this cookie should only be sent over HTTPS. |
||
| 340 | * |
||
| 341 | * @param bool $secure |
||
| 342 | * |
||
| 343 | * @return Cookie |
||
| 344 | */ |
||
| 345 | 1 | public function withSecure($secure) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Check whether this cookie may not be accessed through Javascript. |
||
| 355 | * |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | 2 | public function isHttpOnly() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Sets whether this cookie may not be accessed through Javascript. |
||
| 365 | * |
||
| 366 | * @param bool $httpOnly |
||
| 367 | * |
||
| 368 | * @return Cookie |
||
| 369 | */ |
||
| 370 | 1 | public function withHttpOnly($httpOnly) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Checks if this cookie represents the same cookie as $cookie. |
||
| 380 | * |
||
| 381 | * This does not compare the values, only name, domain and path. |
||
| 382 | * |
||
| 383 | * @param Cookie $cookie |
||
| 384 | * |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | 10 | public function match(Cookie $cookie) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Validates the name attribute. |
||
| 394 | * |
||
| 395 | * @see http://tools.ietf.org/search/rfc2616#section-2.2 |
||
| 396 | * |
||
| 397 | * @param string $name |
||
| 398 | * |
||
| 399 | * @throws \InvalidArgumentException If the name is empty or contains invalid characters. |
||
| 400 | */ |
||
| 401 | 40 | private function validateName($name) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Validates a value. |
||
| 415 | * |
||
| 416 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.1 |
||
| 417 | * |
||
| 418 | * @param string|null $value |
||
| 419 | * |
||
| 420 | * @throws \InvalidArgumentException If the value contains invalid characters. |
||
| 421 | */ |
||
| 422 | 39 | private function validateValue($value) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Normalizes the expiration value. |
||
| 433 | * |
||
| 434 | * @param int|\DateTime|null $expiration |
||
| 435 | * |
||
| 436 | * @return \DateTime|null |
||
| 437 | */ |
||
| 438 | 39 | private function normalizeExpires($expiration) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Remove the leading '.' and lowercase the domain as per spec in RFC 6265. |
||
| 458 | * |
||
| 459 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.2.3 |
||
| 460 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
| 461 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.3 |
||
| 462 | * |
||
| 463 | * @param string|null $domain |
||
| 464 | * |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | 39 | private function normalizeDomain($domain) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Processes path as per spec in RFC 6265. |
||
| 478 | * |
||
| 479 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
| 480 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.4 |
||
| 481 | * |
||
| 482 | * @param string|null $path |
||
| 483 | * |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | 39 | private function normalizePath($path) |
|
| 496 | } |
||
| 497 |
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.