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 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | private static $defaults = [ |
||
14 | 'Name' => null, |
||
15 | 'Value' => null, |
||
16 | 'Domain' => null, |
||
17 | 'Path' => '/', |
||
18 | 'Max-Age' => null, |
||
19 | 'Expires' => null, |
||
20 | 'Secure' => false, |
||
21 | 'Discard' => false, |
||
22 | 'HttpOnly' => false |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * @var array Cookie data |
||
27 | */ |
||
28 | private $data; |
||
29 | |||
30 | /** |
||
31 | * Create a new SetCookie object from a string. |
||
32 | * |
||
33 | * @param string $cookie Set-Cookie header string |
||
34 | */ |
||
35 | public static function fromString(string $cookie): self |
||
71 | |||
72 | /** |
||
73 | * @param array $data Array of cookie data provided by a Cookie parser |
||
74 | */ |
||
75 | public function __construct(array $data = []) |
||
92 | |||
93 | public function __toString() |
||
108 | |||
109 | public function toArray(): array |
||
113 | |||
114 | /** |
||
115 | * Get the cookie name. |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getName() |
||
123 | |||
124 | /** |
||
125 | * Set the cookie name. |
||
126 | * |
||
127 | * @param string $name Cookie name |
||
128 | */ |
||
129 | public function setName($name): void |
||
133 | |||
134 | /** |
||
135 | * Get the cookie value. |
||
136 | * |
||
137 | * @return string|null |
||
138 | */ |
||
139 | public function getValue() |
||
143 | |||
144 | /** |
||
145 | * Set the cookie value. |
||
146 | * |
||
147 | * @param string $value Cookie value |
||
148 | */ |
||
149 | public function setValue($value): void |
||
153 | |||
154 | /** |
||
155 | * Get the domain. |
||
156 | * |
||
157 | * @return string|null |
||
158 | */ |
||
159 | public function getDomain() |
||
163 | |||
164 | /** |
||
165 | * Set the domain of the cookie. |
||
166 | * |
||
167 | * @param string $domain |
||
168 | */ |
||
169 | public function setDomain($domain): void |
||
173 | |||
174 | /** |
||
175 | * Get the path. |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | public function getPath() |
||
183 | |||
184 | /** |
||
185 | * Set the path of the cookie. |
||
186 | * |
||
187 | * @param string $path Path of the cookie |
||
188 | */ |
||
189 | public function setPath($path): void |
||
193 | |||
194 | /** |
||
195 | * Maximum lifetime of the cookie in seconds. |
||
196 | * |
||
197 | * @return int|null |
||
198 | */ |
||
199 | public function getMaxAge() |
||
203 | |||
204 | /** |
||
205 | * Set the max-age of the cookie. |
||
206 | * |
||
207 | * @param int $maxAge Max age of the cookie in seconds |
||
208 | */ |
||
209 | public function setMaxAge($maxAge): void |
||
213 | |||
214 | /** |
||
215 | * The UNIX timestamp when the cookie Expires. |
||
216 | * |
||
217 | * @return string|int|null |
||
218 | */ |
||
219 | public function getExpires() |
||
223 | |||
224 | /** |
||
225 | * Set the unix timestamp for which the cookie will expire. |
||
226 | * |
||
227 | * @param int|string $timestamp Unix timestamp or any English textual datetime description. |
||
228 | */ |
||
229 | public function setExpires($timestamp): void |
||
235 | |||
236 | /** |
||
237 | * Get whether or not this is a secure cookie. |
||
238 | * |
||
239 | * @return bool|null |
||
240 | */ |
||
241 | public function getSecure() |
||
245 | |||
246 | /** |
||
247 | * Set whether or not the cookie is secure. |
||
248 | * |
||
249 | * @param bool $secure Set to true or false if secure |
||
250 | */ |
||
251 | public function setSecure($secure): void |
||
255 | |||
256 | /** |
||
257 | * Get whether or not this is a session cookie. |
||
258 | * |
||
259 | * @return bool|null |
||
260 | */ |
||
261 | public function getDiscard() |
||
265 | |||
266 | /** |
||
267 | * Set whether or not this is a session cookie. |
||
268 | * |
||
269 | * @param bool $discard Set to true or false if this is a session cookie |
||
270 | */ |
||
271 | public function setDiscard($discard): void |
||
275 | |||
276 | /** |
||
277 | * Get whether or not this is an HTTP only cookie. |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | public function getHttpOnly() |
||
285 | |||
286 | /** |
||
287 | * Set whether or not this is an HTTP only cookie. |
||
288 | * |
||
289 | * @param bool $httpOnly Set to true or false if this is HTTP only |
||
290 | */ |
||
291 | public function setHttpOnly($httpOnly): void |
||
295 | |||
296 | /** |
||
297 | * Check if the cookie matches a path value. |
||
298 | * |
||
299 | * A request-path path-matches a given cookie-path if at least one of |
||
300 | * the following conditions holds: |
||
301 | * |
||
302 | * - The cookie-path and the request-path are identical. |
||
303 | * - The cookie-path is a prefix of the request-path, and the last |
||
304 | * character of the cookie-path is %x2F ("/"). |
||
305 | * - The cookie-path is a prefix of the request-path, and the first |
||
306 | * character of the request-path that is not included in the cookie- |
||
307 | * path is a %x2F ("/") character. |
||
308 | * |
||
309 | * @param string $requestPath Path to check against |
||
310 | */ |
||
311 | public function matchesPath(string $requestPath): bool |
||
333 | |||
334 | /** |
||
335 | * Check if the cookie matches a domain value. |
||
336 | * |
||
337 | * @param string $domain Domain to check against |
||
338 | */ |
||
339 | public function matchesDomain(string $domain): bool |
||
363 | |||
364 | /** |
||
365 | * Check if the cookie is expired. |
||
366 | */ |
||
367 | public function isExpired(): bool |
||
371 | |||
372 | /** |
||
373 | * Check if the cookie is valid according to RFC 6265. |
||
374 | * |
||
375 | * @return bool|string Returns true if valid or an error message if invalid |
||
376 | */ |
||
377 | public function validate() |
||
410 | } |
||
411 |
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: