Complex classes like SetCookieHeader 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 SetCookieHeader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class SetCookieHeader implements HeaderInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Expires date format. |
||
| 25 | */ |
||
| 26 | const DATE_FORMAT = 'D, d M Y H:i:s \G\M\T'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $parts = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $name; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $value; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | private $httpOnly = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | private $secure = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor. |
||
| 55 | * |
||
| 56 | * @param string|null $name |
||
| 57 | * @param string|null $value |
||
| 58 | * @param int|null $version |
||
| 59 | * @param int|null $maxAge |
||
| 60 | * @param string|null $domain |
||
| 61 | * @param string|null $path |
||
| 62 | * @param DateTime|null $expires |
||
| 63 | * @param bool|false $httpOnly |
||
| 64 | * @param bool|false $secure |
||
| 65 | */ |
||
| 66 | public function __construct( |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Factory create SetCookieHeader from string representation. |
||
| 111 | * |
||
| 112 | * @param string $headerLine |
||
| 113 | * |
||
| 114 | * @return $this |
||
| 115 | */ |
||
| 116 | public static function fromString($headerLine) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Factory create header. |
||
| 128 | * |
||
| 129 | * @param string $fieldValue |
||
| 130 | * |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | private static function create($fieldValue) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get header field name. |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function getFieldName() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Set name. |
||
| 163 | * |
||
| 164 | * @param string $name |
||
| 165 | * |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | public function setName($name) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get name. |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function getName() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set value. |
||
| 188 | * |
||
| 189 | * @param string $value |
||
| 190 | * |
||
| 191 | * @return $this |
||
| 192 | */ |
||
| 193 | public function setValue($value) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get value. |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getValue() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set cookie version. |
||
| 213 | * |
||
| 214 | * @param int $version |
||
| 215 | * |
||
| 216 | * @return $this |
||
| 217 | * |
||
| 218 | * @throws InvalidHeaderValueException |
||
| 219 | */ |
||
| 220 | public function setVersion($version) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get cookie version. |
||
| 233 | * |
||
| 234 | * @return int|null |
||
| 235 | */ |
||
| 236 | public function getVersion() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set cookie max age. |
||
| 243 | * |
||
| 244 | * @param int $deltaSeconds |
||
| 245 | * |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function setMaxAge($deltaSeconds) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get max age. |
||
| 257 | * |
||
| 258 | * @return int|null |
||
| 259 | */ |
||
| 260 | public function getMaxAge() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Set cookie domain. |
||
| 267 | * |
||
| 268 | * @param string $domain |
||
| 269 | * |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | public function setDomain($domain) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get cookie domain. |
||
| 279 | * |
||
| 280 | * @return string|null |
||
| 281 | */ |
||
| 282 | public function getDomain() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set cookie path. |
||
| 289 | * |
||
| 290 | * @param string $path |
||
| 291 | * |
||
| 292 | * @return $this |
||
| 293 | */ |
||
| 294 | public function setPath($path) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get cookie path. |
||
| 301 | * |
||
| 302 | * @return string|null |
||
| 303 | */ |
||
| 304 | public function getPath() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Set cookie as http only. |
||
| 311 | * |
||
| 312 | * @param bool $flag |
||
| 313 | * |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | public function setHttpOnly($flag = true) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Check is cookie http only. |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | public function isHttpOnly() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Set secure cookie. |
||
| 335 | * |
||
| 336 | * @param bool $flag |
||
| 337 | * |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | public function setSecure($flag = true) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Check is cookie secure. |
||
| 349 | * |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | public function isSecure() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Set cookie expires. |
||
| 359 | * |
||
| 360 | * @param DateTime $date |
||
| 361 | * |
||
| 362 | * @return $this |
||
| 363 | */ |
||
| 364 | public function setExpires(DateTime $date) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get expires. |
||
| 374 | * |
||
| 375 | * @return DateTime|null |
||
| 376 | */ |
||
| 377 | public function getExpires() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Check that cookie has expires. |
||
| 384 | * |
||
| 385 | * @param DateTime|null $now |
||
| 386 | * |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | public function isExpired(DateTime $now = null) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get header field value. |
||
| 398 | * |
||
| 399 | * @return mixed |
||
| 400 | */ |
||
| 401 | public function getFieldValue() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Set cookie directive. |
||
| 435 | * |
||
| 436 | * @param string $key |
||
| 437 | * @param string $value |
||
| 438 | * |
||
| 439 | * @return $this |
||
| 440 | */ |
||
| 441 | public function setDirective($key, $value) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Cast header to string. |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | public function __toString() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Set cookie part. |
||
| 482 | * |
||
| 483 | * @param string $key |
||
| 484 | * @param string $value |
||
| 485 | * |
||
| 486 | * @return $this |
||
| 487 | */ |
||
| 488 | private function set($key, $value) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Get cookie part by key. |
||
| 498 | * |
||
| 499 | * @param string $key |
||
| 500 | * |
||
| 501 | * @return string|int|null |
||
| 502 | */ |
||
| 503 | private function get($key) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Check is parts has key. |
||
| 510 | * |
||
| 511 | * @param string $key |
||
| 512 | * |
||
| 513 | * @return bool |
||
| 514 | */ |
||
| 515 | private function has($key) |
||
| 519 | } |
||
| 520 |