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 |
||
7 | class SetCookie |
||
8 | { |
||
9 | /** @var array */ |
||
10 | private static $defaults = [ |
||
11 | 'Name' => null, |
||
12 | 'Value' => null, |
||
13 | 'Domain' => null, |
||
14 | 'Path' => '/', |
||
15 | 'Max-Age' => null, |
||
16 | 'Expires' => null, |
||
17 | 'Secure' => false, |
||
18 | 'Discard' => false, |
||
19 | 'HttpOnly' => false |
||
20 | ]; |
||
21 | |||
22 | /** @var array Cookie data */ |
||
23 | private $data; |
||
24 | |||
25 | /** |
||
26 | * Create a new SetCookie object from a string |
||
27 | * |
||
28 | * @param string $cookie Set-Cookie header string |
||
29 | * |
||
30 | * @return self |
||
31 | */ |
||
32 | public static function fromString($cookie) |
||
68 | |||
69 | /** |
||
70 | * @param array $data Array of cookie data provided by a Cookie parser |
||
71 | */ |
||
72 | public function __construct(array $data = []) |
||
83 | |||
84 | public function __toString() |
||
99 | |||
100 | public function toArray() |
||
104 | |||
105 | /** |
||
106 | * Get the cookie name |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getName() |
||
114 | |||
115 | /** |
||
116 | * Set the cookie name |
||
117 | * |
||
118 | * @param string $name Cookie name |
||
119 | */ |
||
120 | public function setName($name) |
||
124 | |||
125 | /** |
||
126 | * Get the cookie value |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getValue() |
||
134 | |||
135 | /** |
||
136 | * Set the cookie value |
||
137 | * |
||
138 | * @param string $value Cookie value |
||
139 | */ |
||
140 | public function setValue($value) |
||
144 | |||
145 | /** |
||
146 | * Get the domain |
||
147 | * |
||
148 | * @return string|null |
||
149 | */ |
||
150 | public function getDomain() |
||
154 | |||
155 | /** |
||
156 | * Set the domain of the cookie |
||
157 | * |
||
158 | * @param string $domain |
||
159 | */ |
||
160 | public function setDomain($domain) |
||
164 | |||
165 | /** |
||
166 | * Get the path |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getPath() |
||
174 | |||
175 | /** |
||
176 | * Set the path of the cookie |
||
177 | * |
||
178 | * @param string $path Path of the cookie |
||
179 | */ |
||
180 | public function setPath($path) |
||
184 | |||
185 | /** |
||
186 | * Maximum lifetime of the cookie in seconds |
||
187 | * |
||
188 | * @return int|null |
||
189 | */ |
||
190 | public function getMaxAge() |
||
194 | |||
195 | /** |
||
196 | * Set the max-age of the cookie |
||
197 | * |
||
198 | * @param int $maxAge Max age of the cookie in seconds |
||
199 | */ |
||
200 | public function setMaxAge($maxAge) |
||
204 | |||
205 | /** |
||
206 | * The UNIX timestamp when the cookie Expires |
||
207 | * |
||
208 | * @return mixed |
||
209 | */ |
||
210 | public function getExpires() |
||
214 | |||
215 | /** |
||
216 | * Set the unix timestamp for which the cookie will expire |
||
217 | * |
||
218 | * @param int $timestamp Unix timestamp |
||
219 | */ |
||
220 | public function setExpires($timestamp) |
||
226 | |||
227 | /** |
||
228 | * Get whether or not this is a secure cookie |
||
229 | * |
||
230 | * @return null|bool |
||
231 | */ |
||
232 | public function getSecure() |
||
236 | |||
237 | /** |
||
238 | * Set whether or not the cookie is secure |
||
239 | * |
||
240 | * @param bool $secure Set to true or false if secure |
||
241 | */ |
||
242 | public function setSecure($secure) |
||
246 | |||
247 | /** |
||
248 | * Get whether or not this is a session cookie |
||
249 | * |
||
250 | * @return null|bool |
||
251 | */ |
||
252 | public function getDiscard() |
||
256 | |||
257 | /** |
||
258 | * Set whether or not this is a session cookie |
||
259 | * |
||
260 | * @param bool $discard Set to true or false if this is a session cookie |
||
261 | */ |
||
262 | public function setDiscard($discard) |
||
266 | |||
267 | /** |
||
268 | * Get whether or not this is an HTTP only cookie |
||
269 | * |
||
270 | * @return bool |
||
271 | */ |
||
272 | public function getHttpOnly() |
||
276 | |||
277 | /** |
||
278 | * Set whether or not this is an HTTP only cookie |
||
279 | * |
||
280 | * @param bool $httpOnly Set to true or false if this is HTTP only |
||
281 | */ |
||
282 | public function setHttpOnly($httpOnly) |
||
286 | |||
287 | /** |
||
288 | * Check if the cookie matches a path value. |
||
289 | * |
||
290 | * A request-path path-matches a given cookie-path if at least one of |
||
291 | * the following conditions holds: |
||
292 | * |
||
293 | * - The cookie-path and the request-path are identical. |
||
294 | * - The cookie-path is a prefix of the request-path, and the last |
||
295 | * character of the cookie-path is %x2F ("/"). |
||
296 | * - The cookie-path is a prefix of the request-path, and the first |
||
297 | * character of the request-path that is not included in the cookie- |
||
298 | * path is a %x2F ("/") character. |
||
299 | * |
||
300 | * @param string $requestPath Path to check against |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | public function matchesPath($requestPath) |
||
326 | |||
327 | /** |
||
328 | * Check if the cookie matches a domain value |
||
329 | * |
||
330 | * @param string $domain Domain to check against |
||
331 | * |
||
332 | * @return bool |
||
333 | */ |
||
334 | public function matchesDomain($domain) |
||
353 | |||
354 | /** |
||
355 | * Check if the cookie is expired |
||
356 | * |
||
357 | * @return bool |
||
358 | */ |
||
359 | public function isExpired() |
||
363 | |||
364 | /** |
||
365 | * Check if the cookie is valid according to RFC 6265 |
||
366 | * |
||
367 | * @return bool|string Returns true if valid or an error message if invalid |
||
368 | */ |
||
369 | public function validate() |
||
403 | } |
||
404 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: