Complex classes like SetCookie 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 SetCookie, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | 31 | class SetCookie |
|
| 24 | { |
||
| 25 | 19 | /** @var string */ |
|
| 26 | private $name; |
||
| 27 | 19 | /** @var string|null */ |
|
| 28 | private $value; |
||
| 29 | /** @var int */ |
||
| 30 | 3 | private $expires = 0; |
|
| 31 | /** @var int */ |
||
| 32 | 3 | private $maxAge = 0; |
|
| 33 | /** @var string|null */ |
||
| 34 | private $path; |
||
| 35 | 2 | /** @var string|null */ |
|
| 36 | private $domain; |
||
| 37 | 2 | /** @var bool */ |
|
| 38 | private $secure = false; |
||
| 39 | /** @var bool */ |
||
| 40 | private $httpOnly = false; |
||
| 41 | /** @var SameSite|null */ |
||
| 42 | private $sameSite; |
||
| 43 | |||
| 44 | private function __construct(string $name, ?string $value = null) |
||
| 49 | |||
| 50 | public function getName() : string |
||
| 54 | |||
| 55 | public function getValue() : ?string |
||
| 59 | |||
| 60 | public function getExpires() : int |
||
| 64 | |||
| 65 | 29 | public function getMaxAge() : int |
|
| 69 | 29 | ||
| 70 | public function getPath() : ?string |
||
| 74 | 13 | ||
| 75 | public function getDomain() : ?string |
||
| 79 | |||
| 80 | 13 | public function getSecure() : bool |
|
| 84 | 11 | ||
| 85 | public function getHttpOnly() : bool |
||
| 89 | |||
| 90 | public function getSameSite() : ?SameSite |
||
| 94 | |||
| 95 | 13 | public function withValue(?string $value = null) : self |
|
| 103 | |||
| 104 | 1 | /** @param int|\DateTimeInterface|string|null $expires */ |
|
| 105 | private function resolveExpires($expires = null) : int |
||
| 127 | 12 | ||
| 128 | /** @param int|string|\DateTimeInterface|null $expires */ |
||
| 129 | public function withExpires($expires = null) : self |
||
| 139 | 10 | ||
| 140 | public function rememberForever() : self |
||
| 144 | |||
| 145 | 10 | public function expire() : self |
|
| 149 | |||
| 150 | 12 | public function withMaxAge(?int $maxAge = null) : self |
|
| 158 | |||
| 159 | public function withPath(?string $path = null) : self |
||
| 167 | 17 | ||
| 168 | 17 | public function withDomain(?string $domain = null) : self |
|
| 176 | |||
| 177 | public function withSecure(bool $secure = true) : self |
||
| 185 | 1 | ||
| 186 | public function withHttpOnly(bool $httpOnly = true) : self |
||
| 194 | |||
| 195 | 29 | public function withSameSite(SameSite $sameSite) : self |
|
| 203 | |||
| 204 | 12 | public function withoutSameSite() : self |
|
| 212 | 11 | ||
| 213 | 12 | public function __toString() : string |
|
| 229 | |||
| 230 | 12 | public static function create(string $name, ?string $value = null) : self |
|
| 234 | 17 | ||
| 235 | public static function createRememberedForever(string $name, ?string $value = null) : self |
||
| 239 | |||
| 240 | 17 | public static function createExpired(string $name) : self |
|
| 244 | |||
| 245 | 17 | public static function fromSetCookieString(string $string) : self |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @param string[] $cookieStringParts |
||
| 305 | * |
||
| 306 | * @return string[] |
||
| 307 | */ |
||
| 308 | private function appendFormattedDomainPartIfSet(array $cookieStringParts) : array |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string[] $cookieStringParts |
||
| 319 | * |
||
| 320 | * @return string[] |
||
| 321 | */ |
||
| 322 | private function appendFormattedPathPartIfSet(array $cookieStringParts) : array |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string[] $cookieStringParts |
||
| 333 | * |
||
| 334 | * @return string[] |
||
| 335 | */ |
||
| 336 | private function appendFormattedExpiresPartIfSet(array $cookieStringParts) : array |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string[] $cookieStringParts |
||
| 347 | * |
||
| 348 | * @return string[] |
||
| 349 | */ |
||
| 350 | private function appendFormattedMaxAgePartIfSet(array $cookieStringParts) : array |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param string[] $cookieStringParts |
||
| 361 | * |
||
| 362 | * @return string[] |
||
| 363 | */ |
||
| 364 | private function appendFormattedSecurePartIfSet(array $cookieStringParts) : array |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param string[] $cookieStringParts |
||
| 375 | * |
||
| 376 | * @return string[] |
||
| 377 | */ |
||
| 378 | private function appendFormattedHttpOnlyPartIfSet(array $cookieStringParts) : array |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param string[] $cookieStringParts |
||
| 389 | * |
||
| 390 | * @return string[] |
||
| 391 | */ |
||
| 392 | private function appendFormattedSameSitePartIfSet(array $cookieStringParts) : array |
||
| 402 | } |
||
| 403 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: