Complex classes like CookieJar 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 CookieJar, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class CookieJar implements CookieJarInterface |
||
| 11 | { |
||
| 12 | /** @var SetCookie[] Loaded cookie data */ |
||
| 13 | private $cookies = []; |
||
| 14 | |||
| 15 | /** @var bool */ |
||
| 16 | private $strictMode; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param bool $strictMode Set to true to throw exceptions when invalid |
||
| 20 | * cookies are added to the cookie jar. |
||
| 21 | * @param array $cookieArray Array of SetCookie objects or a hash of |
||
| 22 | * arrays that can be used with the SetCookie |
||
| 23 | * constructor |
||
| 24 | */ |
||
| 25 | public function __construct($strictMode = false, $cookieArray = []) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Create a new Cookie jar from an associative array and domain. |
||
| 39 | * |
||
| 40 | * @param array $cookies Cookies to create the jar from |
||
| 41 | * @param string $domain Domain to set the cookies to |
||
| 42 | * |
||
| 43 | * @return self |
||
| 44 | */ |
||
| 45 | public static function fromArray(array $cookies, $domain) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @deprecated |
||
| 62 | */ |
||
| 63 | public static function getCookieValue($value) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Evaluate if this cookie should be persisted to storage |
||
| 70 | * that survives between requests. |
||
| 71 | * |
||
| 72 | * @param SetCookie $cookie Being evaluated. |
||
| 73 | * @param bool $allowSessionCookies If we should persist session cookies |
||
| 74 | * @return bool |
||
| 75 | */ |
||
| 76 | public static function shouldPersist( |
||
| 88 | |||
| 89 | public function toArray() |
||
| 95 | |||
| 96 | public function clear($domain = null, $path = null, $name = null) |
||
| 127 | |||
| 128 | public function clearSessionCookies() |
||
| 137 | |||
| 138 | public function setCookie(SetCookie $cookie) |
||
| 198 | |||
| 199 | public function count() |
||
| 203 | |||
| 204 | public function getIterator() |
||
| 208 | |||
| 209 | public function extractCookies( |
||
| 223 | |||
| 224 | public function withCookieHeader(RequestInterface $request) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * If a cookie already exists and the server asks to set it again with a |
||
| 250 | * null value, the cookie must be deleted. |
||
| 251 | * |
||
| 252 | * @param SetCookie $cookie |
||
| 253 | */ |
||
| 254 | private function removeCookieIfEmpty(SetCookie $cookie) |
||
| 265 | } |
||
| 266 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.