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 |
||
| 8 | class SetCookie |
||
| 9 | { |
||
| 10 | private $name; |
||
| 11 | private $value; |
||
| 12 | private $expires = 0; |
||
| 13 | private $maxAge = 0; |
||
| 14 | private $path; |
||
| 15 | private $domain; |
||
| 16 | private $secure = false; |
||
| 17 | private $httpOnly = false; |
||
| 18 | |||
| 19 | 31 | private function __construct($name, $value = null) |
|
| 20 | { |
||
| 21 | 31 | $this->name = $name; |
|
| 22 | 31 | $this->value = $value; |
|
| 23 | 31 | } |
|
| 24 | |||
| 25 | 19 | public function getName() |
|
| 29 | |||
| 30 | 3 | public function getValue() |
|
| 34 | |||
| 35 | 2 | public function getExpires() |
|
| 36 | { |
||
| 37 | 2 | return $this->expires; |
|
| 38 | } |
||
| 39 | |||
| 40 | public function getMaxAge() |
||
| 44 | |||
| 45 | public function getPath() |
||
| 49 | |||
| 50 | public function getDomain() |
||
| 54 | |||
| 55 | public function getSecure() |
||
| 59 | |||
| 60 | public function getHttpOnly() |
||
| 64 | |||
| 65 | 29 | public function withValue($value = null) |
|
| 73 | |||
| 74 | 13 | private function resolveExpires($expires = null) |
|
| 75 | { |
||
| 76 | 13 | if (is_null($expires)) { |
|
| 77 | return null; |
||
| 78 | } |
||
| 79 | |||
| 80 | 13 | if ($expires instanceof DateTime || $expires instanceof DateTimeInterface) { |
|
| 81 | 2 | return $expires->getTimestamp(); |
|
| 82 | } |
||
| 83 | |||
| 84 | 11 | if (is_numeric($expires)) { |
|
| 85 | return $expires; |
||
| 86 | } |
||
| 87 | |||
| 88 | 11 | return strtotime($expires); |
|
| 89 | } |
||
| 90 | |||
| 91 | 13 | public function withExpires($expires = null) |
|
| 101 | |||
| 102 | 1 | public function rememberForever() |
|
| 103 | { |
||
| 104 | 1 | return $this->withExpires(new DateTime('+5 years')); |
|
| 105 | } |
||
| 106 | |||
| 107 | 1 | public function expire() |
|
| 108 | { |
||
| 109 | 1 | return $this->withExpires(new DateTime('-5 years')); |
|
| 110 | } |
||
| 111 | |||
| 112 | 4 | public function withMaxAge($maxAge = null) |
|
| 120 | |||
| 121 | 12 | public function withPath($path = null) |
|
| 129 | |||
| 130 | 7 | public function withDomain($domain = null) |
|
| 138 | |||
| 139 | 10 | public function withSecure($secure = null) |
|
| 147 | |||
| 148 | 12 | public function withHttpOnly($httpOnly = null) |
|
| 156 | |||
| 157 | 17 | public function __toString() |
|
| 172 | |||
| 173 | 8 | public static function create($name, $value = null) |
|
| 177 | |||
| 178 | 1 | public static function createRememberedForever($name, $value = null) |
|
| 179 | { |
||
| 180 | 1 | return static::create($name, $value)->rememberForever(); |
|
| 181 | } |
||
| 182 | |||
| 183 | 1 | public static function createExpired($name) |
|
| 184 | { |
||
| 185 | 1 | return static::create($name)->expire(); |
|
| 186 | } |
||
| 187 | |||
| 188 | 29 | public static function fromSetCookieString($string) |
|
| 242 | |||
| 243 | 17 | private function appendFormattedPathPartIfSet(array $cookieStringParts) |
|
| 251 | |||
| 252 | 17 | private function appendFormattedExpiresPartIfSet(array $cookieStringParts) |
|
| 260 | |||
| 261 | 17 | private function appendFormattedMaxAgePartIfSet(array $cookieStringParts) |
|
| 269 | |||
| 270 | 17 | private function appendFormattedSecurePartIfSet(array $cookieStringParts) |
|
| 278 | |||
| 279 | 17 | private function appendFormattedHttpOnlyPartIfSet(array $cookieStringParts) |
|
| 287 | } |
||
| 288 |