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 | * @param string $name |
||
58 | * @param string|null $value |
||
59 | * @param int $maxAge |
||
60 | * @param string|null $domain |
||
61 | * @param string|null $path |
||
62 | * @param bool $secure |
||
63 | * @param bool $httpOnly |
||
64 | * @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided. |
||
65 | * |
||
66 | * @throws \InvalidArgumentException If name, value or max age is not valid. |
||
67 | */ |
||
68 | 57 | public function __construct( |
|
91 | |||
92 | /** |
||
93 | * Creates a new cookie without any attribute validation. |
||
94 | * |
||
95 | * @param string $name |
||
96 | * @param string|null $value |
||
97 | 2 | * @param int $maxAge |
|
98 | * @param string|null $domain |
||
99 | 2 | * @param string|null $path |
|
100 | * @param bool $secure |
||
101 | * @param bool $httpOnly |
||
102 | * @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided. |
||
103 | */ |
||
104 | public static function createWithoutValidation( |
||
105 | $name, |
||
106 | $value = null, |
||
107 | 5 | $maxAge = null, |
|
108 | $domain = null, |
||
109 | 5 | $path = null, |
|
110 | $secure = false, |
||
111 | $httpOnly = false, |
||
112 | \DateTime $expires = null |
||
113 | ) { |
||
114 | $cookie = new self('name', null, null, $domain, $path, $secure, $httpOnly, $expires); |
||
115 | $cookie->name = $name; |
||
116 | $cookie->value = $value; |
||
117 | 13 | $cookie->maxAge = $maxAge; |
|
118 | |||
119 | 13 | return $cookie; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Returns the name. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getName() |
||
128 | { |
||
129 | 8 | return $this->name; |
|
130 | } |
||
131 | 8 | ||
132 | /** |
||
133 | 5 | * Returns the value. |
|
134 | 5 | * |
|
135 | * @return string|null |
||
136 | 5 | */ |
|
137 | public function getValue() |
||
138 | { |
||
139 | return $this->value; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Checks if there is a value. |
||
144 | 3 | * |
|
145 | * @return bool |
||
146 | 3 | */ |
|
147 | public function hasValue() |
||
148 | { |
||
149 | return isset($this->value); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Sets the value. |
||
154 | 1 | * |
|
155 | * @param string|null $value |
||
156 | 1 | * |
|
157 | * @return Cookie |
||
158 | */ |
||
159 | public function withValue($value) |
||
160 | { |
||
161 | $this->validateValue($value); |
||
162 | |||
163 | $new = clone $this; |
||
164 | $new->value = $value; |
||
165 | |||
166 | 2 | return $new; |
|
167 | } |
||
168 | 2 | ||
169 | /** |
||
170 | 1 | * Returns the max age. |
|
171 | 1 | * |
|
172 | * @return int|null |
||
173 | 1 | */ |
|
174 | public function getMaxAge() |
||
175 | { |
||
176 | return $this->maxAge; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Checks if there is a max age. |
||
181 | 3 | * |
|
182 | * @return bool |
||
183 | 3 | */ |
|
184 | public function hasMaxAge() |
||
185 | { |
||
186 | return isset($this->maxAge); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Sets the max age. |
||
191 | 2 | * |
|
192 | * @param int|null $maxAge |
||
193 | 2 | * |
|
194 | * @return Cookie |
||
195 | */ |
||
196 | public function withMaxAge($maxAge) |
||
197 | { |
||
198 | $this->validateMaxAge($maxAge); |
||
199 | |||
200 | $new = clone $this; |
||
201 | $new->maxAge = $maxAge; |
||
202 | |||
203 | 1 | return $new; |
|
204 | } |
||
205 | 1 | ||
206 | 1 | /** |
|
207 | * Returns the expiration time. |
||
208 | 1 | * |
|
209 | * @return \DateTime|null |
||
210 | */ |
||
211 | public function getExpires() |
||
212 | { |
||
213 | return $this->expires; |
||
214 | } |
||
215 | |||
216 | 2 | /** |
|
217 | * Checks if there is an expiration time. |
||
218 | 2 | * |
|
219 | * @return bool |
||
220 | */ |
||
221 | public function hasExpires() |
||
222 | { |
||
223 | return isset($this->expires); |
||
224 | } |
||
225 | |||
226 | 3 | /** |
|
227 | * Sets the expires. |
||
228 | 3 | * |
|
229 | * @param \DateTime|null $expires |
||
230 | * |
||
231 | * @return Cookie |
||
232 | */ |
||
233 | public function withExpires(\DateTime $expires = null) |
||
234 | { |
||
235 | $new = clone $this; |
||
236 | 4 | $new->expires = $expires; |
|
237 | |||
238 | 4 | return $new; |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * Checks if the cookie is expired. |
||
243 | * |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function isExpired() |
||
247 | { |
||
248 | 1 | return isset($this->expires) and $this->expires < new \DateTime(); |
|
|
|||
249 | } |
||
250 | 1 | ||
251 | 1 | /** |
|
252 | * Returns the domain. |
||
253 | 1 | * |
|
254 | * @return string|null |
||
255 | */ |
||
256 | public function getDomain() |
||
257 | { |
||
258 | return $this->domain; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Checks if there is a domain. |
||
263 | * |
||
264 | * @return bool |
||
265 | 2 | */ |
|
266 | public function hasDomain() |
||
270 | |||
271 | /** |
||
272 | * Sets the domain. |
||
273 | 1 | * |
|
274 | 1 | * @param string|null $domain |
|
275 | * |
||
276 | * @return Cookie |
||
277 | 1 | */ |
|
278 | public function withDomain($domain) |
||
279 | { |
||
280 | $new = clone $this; |
||
281 | $new->domain = $this->normalizeDomain($domain); |
||
282 | |||
283 | return $new; |
||
285 | 2 | ||
286 | /** |
||
287 | 2 | * Checks whether this cookie is meant for this domain. |
|
288 | * |
||
289 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
290 | * |
||
291 | * @param string $domain |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | public function matchDomain($domain) |
||
309 | |||
310 | /** |
||
311 | * Returns the path. |
||
312 | * |
||
313 | * @return string |
||
314 | 3 | */ |
|
315 | public function getPath() |
||
319 | |||
320 | /** |
||
321 | * Sets the path. |
||
322 | * |
||
323 | * @param string|null $path |
||
324 | 2 | * |
|
325 | * @return Cookie |
||
326 | 2 | */ |
|
327 | public function withPath($path) |
||
334 | |||
335 | /** |
||
336 | 1 | * Checks whether this cookie is meant for this path. |
|
337 | * |
||
338 | 1 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
|
339 | 1 | * |
|
340 | * @param string $path |
||
341 | 1 | * |
|
342 | * @return bool |
||
343 | */ |
||
344 | public function matchPath($path) |
||
348 | |||
349 | 2 | /** |
|
350 | * Checks whether this cookie may only be sent over HTTPS. |
||
351 | 2 | * |
|
352 | * @return bool |
||
353 | */ |
||
354 | public function isSecure() |
||
358 | |||
359 | /** |
||
360 | * Sets whether this cookie should only be sent over HTTPS. |
||
361 | 1 | * |
|
362 | * @param bool $secure |
||
363 | 1 | * |
|
364 | 1 | * @return Cookie |
|
365 | */ |
||
366 | 1 | public function withSecure($secure) |
|
373 | |||
374 | /** |
||
375 | * Check whether this cookie may not be accessed through Javascript. |
||
376 | * |
||
377 | * @return bool |
||
378 | 10 | */ |
|
379 | public function isHttpOnly() |
||
383 | |||
384 | /** |
||
385 | * Sets whether this cookie may not be accessed through Javascript. |
||
386 | * |
||
387 | * @param bool $httpOnly |
||
388 | * |
||
389 | * @return Cookie |
||
390 | */ |
||
391 | public function withHttpOnly($httpOnly) |
||
398 | |||
399 | 56 | /** |
|
400 | 3 | * Checks if this cookie represents the same cookie as $cookie. |
|
401 | * |
||
402 | 53 | * This does not compare the values, only name, domain and path. |
|
403 | * |
||
404 | * @param Cookie $cookie |
||
405 | * |
||
406 | * @return bool |
||
407 | */ |
||
408 | public function match(Cookie $cookie) |
||
412 | |||
413 | 53 | /** |
|
414 | * Validates cookie attributes. |
||
415 | 53 | * |
|
416 | 49 | * @return bool |
|
417 | 6 | */ |
|
418 | public function isValid() |
||
430 | |||
431 | 50 | /** |
|
432 | 7 | * Validates the name attribute. |
|
433 | 1 | * |
|
434 | * @see http://tools.ietf.org/search/rfc2616#section-2.2 |
||
435 | 6 | * |
|
436 | 50 | * @param string $name |
|
437 | * |
||
438 | * @throws \InvalidArgumentException If the name is empty or contains invalid characters. |
||
439 | */ |
||
440 | private function validateName($name) |
||
451 | 50 | ||
452 | 6 | /** |
|
453 | 6 | * Validates a value. |
|
454 | * |
||
455 | 50 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.1 |
|
456 | * |
||
457 | * @param string|null $value |
||
458 | * |
||
459 | * @throws \InvalidArgumentException If the value contains invalid characters. |
||
460 | */ |
||
461 | private function validateValue($value) |
||
469 | |||
470 | 50 | /** |
|
471 | * Validates a Max-Age attribute. |
||
472 | 50 | * |
|
473 | 49 | * @param int|null $maxAge |
|
474 | 49 | * |
|
475 | * @throws \InvalidArgumentException If the Max-Age is not an empty or integer value. |
||
476 | 50 | */ |
|
477 | private function validateMaxAge($maxAge) |
||
485 | |||
486 | /** |
||
487 | * Remove the leading '.' and lowercase the domain as per spec in RFC 6265. |
||
488 | * |
||
489 | * @see http://tools.ietf.org/html/rfc6265#section-4.1.2.3 |
||
490 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.3 |
||
491 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.3 |
||
492 | * |
||
493 | * @param string|null $domain |
||
494 | * |
||
495 | * @return string |
||
496 | */ |
||
497 | private function normalizeDomain($domain) |
||
505 | |||
506 | /** |
||
507 | * Processes path as per spec in RFC 6265. |
||
508 | * |
||
509 | * @see http://tools.ietf.org/html/rfc6265#section-5.1.4 |
||
510 | * @see http://tools.ietf.org/html/rfc6265#section-5.2.4 |
||
511 | * |
||
512 | * @param string|null $path |
||
513 | * |
||
514 | * @return string |
||
515 | */ |
||
516 | private function normalizePath($path) |
||
526 | } |
||
527 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.