Complex classes like Cookie 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 Cookie, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | final class Cookie |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $name; |
||
18 | |||
19 | /** |
||
20 | * @var string|null |
||
21 | */ |
||
22 | private $value; |
||
23 | |||
24 | /** |
||
25 | * @var int|null |
||
26 | */ |
||
27 | private $maxAge; |
||
28 | |||
29 | /** |
||
30 | * @var string|null |
||
31 | */ |
||
32 | private $domain; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $path; |
||
38 | |||
39 | /** |
||
40 | * @var bool |
||
41 | */ |
||
42 | private $secure; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $httpOnly; |
||
48 | |||
49 | /** |
||
50 | * Expires attribute is HTTP 1.0 only and should be avoided. |
||
51 | * |
||
52 | * @var \DateTime|null |
||
53 | */ |
||
54 | private $expires; |
||
55 | |||
56 | /** |
||
57 | * Validation state. |
||
58 | * |
||
59 | * @var bool |
||
60 | */ |
||
61 | private $valid; |
||
62 | |||
63 | /** |
||
64 | * @param string $name |
||
65 | * @param string|null $value |
||
66 | * @param int $maxAge |
||
67 | * @param string|null $domain |
||
68 | * @param string|null $path |
||
69 | * @param bool $secure |
||
70 | * @param bool $httpOnly |
||
71 | * @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided. |
||
72 | * @param bool $requireValidation deprecated since version 1.5. Will be removed in 2.0 |
||
73 | * |
||
74 | * @throws \InvalidArgumentException If name, value or max age is not valid. Attributes validation during instantiation is deprecated since 1.5 and will be removed in 2.0. |
||
75 | */ |
||
76 | 73 | public function __construct( |
|
77 | $name, |
||
78 | $value = null, |
||
79 | $maxAge = null, |
||
80 | $domain = null, |
||
81 | $path = null, |
||
82 | $secure = false, |
||
83 | $httpOnly = false, |
||
84 | \DateTime $expires = null, |
||
85 | $requireValidation = true |
||
86 | ) { |
||
87 | 73 | if ($requireValidation) { |
|
88 | 58 | @trigger_error('Attributes validation during instantiation is deprecated since 1.5 and will be removed in 2.0', E_USER_DEPRECATED); |
|
|
|||
89 | 58 | $this->validateName($name); |
|
90 | 54 | $this->validateValue($value); |
|
91 | 51 | $this->validateMaxAge($maxAge); |
|
92 | 51 | $this->valid = true; |
|
93 | 51 | } |
|
94 | |||
95 | 66 | $this->name = $name; |
|
96 | 66 | $this->value = $value; |
|
97 | 66 | $this->maxAge = $maxAge; |
|
98 | 66 | $this->expires = $expires; |
|
99 | 66 | $this->domain = $this->normalizeDomain($domain); |
|
100 | 66 | $this->path = $this->normalizePath($path); |
|
101 | 66 | $this->secure = (bool) $secure; |
|
102 | 66 | $this->httpOnly = (bool) $httpOnly; |
|
103 | 66 | } |
|
104 | |||
105 | 15 | public static function createWithoutValidation( |
|
106 | $name, |
||
107 | $value = null, |
||
108 | $maxAge = null, |
||
109 | $domain = null, |
||
110 | $path = null, |
||
111 | $secure = false, |
||
112 | $httpOnly = false, |
||
113 | \DateTime $expires = null |
||
114 | ) { |
||
115 | 15 | return new self($name, $value, $maxAge, $domain, $path, $secure, $httpOnly, $expires, false); |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Returns the name. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | 2 | public function getName() |
|
124 | { |
||
125 | 2 | return $this->name; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Returns the value. |
||
130 | * |
||
131 | * @return string|null |
||
132 | */ |
||
133 | 5 | public function getValue() |
|
134 | { |
||
135 | 5 | return $this->value; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * Checks if there is a value. |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | 13 | public function hasValue() |
|
147 | |||
148 | /** |
||
149 | * Sets the value. |
||
150 | * |
||
151 | * @param string|null $value |
||
152 | * |
||
153 | * @return Cookie |
||
154 | */ |
||
155 | 8 | public function withValue($value) |
|
156 | { |
||
157 | 8 | $this->validateValue($value); |
|
158 | |||
159 | 5 | $new = clone $this; |
|
160 | 5 | $new->value = $value; |
|
161 | |||
162 | 5 | return $new; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Returns the max age. |
||
167 | * |
||
168 | * @return int|null |
||
169 | */ |
||
170 | 3 | public function getMaxAge() |
|
171 | { |
||
172 | 3 | return $this->maxAge; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Checks if there is a max age. |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | 1 | public function hasMaxAge() |
|
184 | |||
185 | /** |
||
186 | * Sets the max age. |
||
187 | * |
||
188 | * @param int|null $maxAge |
||
189 | * |
||
190 | * @return Cookie |
||
191 | */ |
||
192 | 2 | public function withMaxAge($maxAge) |
|
201 | |||
202 | /** |
||
203 | * Returns the expiration time. |
||
204 | * |
||
205 | * @return \DateTime|null |
||
206 | */ |
||
207 | 3 | public function getExpires() |
|
211 | |||
212 | /** |
||
213 | * Checks if there is an expiration time. |
||
214 | * |
||
215 | * @return bool |
||
216 | */ |
||
217 | 2 | public function hasExpires() |
|
221 | |||
222 | /** |
||
223 | * Sets the expires. |
||
224 | * |
||
225 | * @param \DateTime|null $expires |
||
226 | * |
||
227 | * @return Cookie |
||
228 | */ |
||
229 | 1 | public function withExpires(\DateTime $expires = null) |
|
236 | |||
237 | /** |
||
238 | * Checks if the cookie is expired. |
||
239 | * |
||
240 | * @return bool |
||
241 | */ |
||
242 | 2 | public function isExpired() |
|
246 | |||
247 | /** |
||
248 | * Returns the domain. |
||
249 | * |
||
250 | * @return string|null |
||
251 | */ |
||
252 | 3 | public function getDomain() |
|
256 | |||
257 | /** |
||
258 | * Checks if there is a domain. |
||
259 | * |
||
260 | * @return bool |
||
261 | */ |
||
262 | 4 | public function hasDomain() |
|
266 | |||
267 | /** |
||
268 | * Sets the domain. |
||
269 | * |
||
270 | * @param string|null $domain |
||
271 | * |
||
272 | * @return Cookie |
||
273 | */ |
||
274 | 1 | public function withDomain($domain) |
|
281 | |||
282 | /** |
||
283 | * Checks whether this cookie is meant for this domain. |
||
284 | * |
||
285 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
286 | * |
||
287 | * @param string $domain |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | 2 | public function matchDomain($domain) |
|
305 | |||
306 | /** |
||
307 | * Returns the path. |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | 2 | public function getPath() |
|
315 | |||
316 | /** |
||
317 | * Sets the path. |
||
318 | * |
||
319 | * @param string|null $path |
||
320 | * |
||
321 | * @return Cookie |
||
322 | */ |
||
323 | 1 | public function withPath($path) |
|
330 | |||
331 | /** |
||
332 | * Checks whether this cookie is meant for this path. |
||
333 | * |
||
334 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
335 | * |
||
336 | * @param string $path |
||
337 | * |
||
338 | * @return bool |
||
339 | */ |
||
340 | 3 | public function matchPath($path) |
|
344 | |||
345 | /** |
||
346 | * Checks whether this cookie may only be sent over HTTPS. |
||
347 | * |
||
348 | * @return bool |
||
349 | */ |
||
350 | 2 | public function isSecure() |
|
354 | |||
355 | /** |
||
356 | * Sets whether this cookie should only be sent over HTTPS. |
||
357 | * |
||
358 | * @param bool $secure |
||
359 | * |
||
360 | * @return Cookie |
||
361 | */ |
||
362 | 1 | public function withSecure($secure) |
|
369 | |||
370 | /** |
||
371 | * Check whether this cookie may not be accessed through Javascript. |
||
372 | * |
||
373 | * @return bool |
||
374 | */ |
||
375 | 2 | public function isHttpOnly() |
|
379 | |||
380 | /** |
||
381 | * Sets whether this cookie may not be accessed through Javascript. |
||
382 | * |
||
383 | * @param bool $httpOnly |
||
384 | * |
||
385 | * @return Cookie |
||
386 | */ |
||
387 | 1 | public function withHttpOnly($httpOnly) |
|
394 | |||
395 | /** |
||
396 | * Checks if this cookie represents the same cookie as $cookie. |
||
397 | * |
||
398 | * This does not compare the values, only name, domain and path. |
||
399 | * |
||
400 | * @param Cookie $cookie |
||
401 | * |
||
402 | * @return bool |
||
403 | */ |
||
404 | 10 | public function match(Cookie $cookie) |
|
408 | |||
409 | /** |
||
410 | * Validates cookie attributes. |
||
411 | * |
||
412 | * @return bool |
||
413 | */ |
||
414 | 16 | public function isValid() |
|
429 | |||
430 | /** |
||
431 | * Validates the name attribute. |
||
432 | * |
||
433 | * @see http://tools.ietf.org/search/rfc2616#section-2.2 |
||
434 | * |
||
435 | * @param string $name |
||
436 | * |
||
437 | * @throws \InvalidArgumentException If the name is empty or contains invalid characters. |
||
438 | */ |
||
439 | 73 | private function validateName($name) |
|
450 | |||
451 | /** |
||
452 | * Validates a value. |
||
453 | * |
||
454 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.1 |
||
455 | * |
||
456 | * @param string|null $value |
||
457 | * |
||
458 | * @throws \InvalidArgumentException If the value contains invalid characters. |
||
459 | */ |
||
460 | 66 | private function validateValue($value) |
|
468 | |||
469 | /** |
||
470 | * Validates a Max-Age attribute. |
||
471 | * |
||
472 | * @param int|null $maxAge |
||
473 | * |
||
474 | * @throws \InvalidArgumentException If the Max-Age is not an empty or integer value. |
||
475 | */ |
||
476 | 60 | private function validateMaxAge($maxAge) |
|
484 | |||
485 | /** |
||
486 | * Remove the leading '.' and lowercase the domain as per spec in RFC 6265. |
||
487 | * |
||
488 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.2.3 |
||
489 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
490 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.3 |
||
491 | * |
||
492 | * @param string|null $domain |
||
493 | * |
||
494 | * @return string |
||
495 | */ |
||
496 | 66 | private function normalizeDomain($domain) |
|
504 | |||
505 | /** |
||
506 | * Processes path as per spec in RFC 6265. |
||
507 | * |
||
508 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
509 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.4 |
||
510 | * |
||
511 | * @param string|null $path |
||
512 | * |
||
513 | * @return string |
||
514 | */ |
||
515 | 66 | private function normalizePath($path) |
|
525 | } |
||
526 |
If you suppress an error, we recommend checking for the error condition explicitly: