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 |
||
25 | class SetCookie |
||
26 | { |
||
27 | /** @var string */ |
||
28 | private $name; |
||
29 | /** @var string|null */ |
||
30 | private $value; |
||
31 | /** @var int */ |
||
32 | private $expires = 0; |
||
33 | /** @var int */ |
||
34 | private $maxAge = 0; |
||
35 | /** @var string|null */ |
||
36 | private $path; |
||
37 | /** @var string|null */ |
||
38 | private $domain; |
||
39 | /** @var bool */ |
||
40 | private $secure = false; |
||
41 | /** @var bool */ |
||
42 | private $httpOnly = false; |
||
43 | /** @var SameSite|null */ |
||
44 | private $sameSite; |
||
45 | |||
46 | 35 | private function __construct(string $name, ?string $value = null) |
|
47 | { |
||
48 | 35 | $this->name = $name; |
|
49 | 35 | $this->value = $value; |
|
50 | 35 | } |
|
51 | |||
52 | 19 | public function getName(): string |
|
53 | { |
||
54 | 19 | return $this->name; |
|
55 | } |
||
56 | |||
57 | 3 | public function getValue(): ?string |
|
58 | { |
||
59 | 3 | return $this->value; |
|
60 | } |
||
61 | |||
62 | 2 | public function getExpires(): int |
|
63 | { |
||
64 | 2 | return $this->expires; |
|
65 | } |
||
66 | |||
67 | public function getMaxAge(): int |
||
68 | { |
||
69 | return $this->maxAge; |
||
70 | } |
||
71 | |||
72 | public function getPath(): ?string |
||
73 | { |
||
74 | return $this->path; |
||
75 | } |
||
76 | |||
77 | public function getDomain(): ?string |
||
78 | { |
||
79 | return $this->domain; |
||
80 | } |
||
81 | |||
82 | public function getSecure(): bool |
||
83 | { |
||
84 | return $this->secure; |
||
85 | } |
||
86 | |||
87 | public function getHttpOnly(): bool |
||
88 | { |
||
89 | return $this->httpOnly; |
||
90 | } |
||
91 | |||
92 | 1 | public function getSameSite(): ?SameSite |
|
96 | |||
97 | 32 | public function withValue(?string $value = null): self |
|
105 | |||
106 | /** @param int|DateTimeInterface|string|null $expires */ |
||
107 | 16 | private function resolveExpires($expires = null): int |
|
108 | { |
||
109 | 16 | if ($expires === null) { |
|
110 | return 0; |
||
111 | } |
||
112 | |||
113 | 16 | if ($expires instanceof DateTimeInterface) { |
|
114 | 2 | return (int) $expires->getTimestamp(); |
|
115 | } |
||
116 | |||
117 | 14 | if (is_numeric($expires)) { |
|
118 | return (int) $expires; |
||
119 | } |
||
120 | |||
121 | 14 | $time = strtotime($expires); |
|
122 | |||
123 | 14 | if (! is_int($time)) { |
|
124 | 1 | throw new InvalidArgumentException(sprintf('Invalid expires "%s" provided', $expires)); |
|
125 | } |
||
126 | |||
127 | 13 | return $time; |
|
128 | } |
||
129 | |||
130 | /** @param int|string|DateTimeInterface|null $expires */ |
||
131 | 16 | public function withExpires($expires = null): self |
|
132 | { |
||
133 | 16 | $expires = $this->resolveExpires($expires); |
|
134 | |||
135 | 15 | $clone = clone $this; |
|
136 | |||
137 | 15 | $clone->expires = $expires; |
|
138 | |||
139 | 15 | return $clone; |
|
140 | } |
||
141 | |||
142 | 1 | public function rememberForever(): self |
|
146 | |||
147 | 1 | public function expire(): self |
|
151 | |||
152 | 6 | public function withMaxAge(?int $maxAge = null): self |
|
153 | { |
||
154 | 6 | $clone = clone $this; |
|
155 | |||
156 | 6 | $clone->maxAge = (int) $maxAge; |
|
157 | |||
158 | 6 | return $clone; |
|
159 | } |
||
160 | |||
161 | 15 | public function withPath(?string $path = null): self |
|
162 | { |
||
163 | 15 | $clone = clone $this; |
|
164 | |||
165 | 15 | $clone->path = $path; |
|
166 | |||
167 | 15 | return $clone; |
|
168 | } |
||
169 | |||
170 | 10 | public function withDomain(?string $domain = null): self |
|
171 | { |
||
172 | 10 | $clone = clone $this; |
|
173 | |||
174 | 10 | $clone->domain = $domain; |
|
175 | |||
176 | 10 | return $clone; |
|
177 | } |
||
178 | |||
179 | 12 | public function withSecure(bool $secure = true): self |
|
180 | { |
||
181 | 12 | $clone = clone $this; |
|
182 | |||
183 | 12 | $clone->secure = $secure; |
|
184 | |||
185 | 12 | return $clone; |
|
186 | } |
||
187 | |||
188 | 15 | public function withHttpOnly(bool $httpOnly = true): self |
|
196 | |||
197 | 3 | public function withSameSite(SameSite $sameSite): self |
|
198 | { |
||
199 | 3 | $clone = clone $this; |
|
200 | |||
201 | 3 | $clone->sameSite = $sameSite; |
|
202 | |||
203 | 3 | return $clone; |
|
204 | } |
||
205 | |||
206 | 1 | public function withoutSameSite(): self |
|
207 | { |
||
208 | 1 | $clone = clone $this; |
|
209 | |||
210 | 1 | $clone->sameSite = null; |
|
211 | |||
212 | 1 | return $clone; |
|
213 | } |
||
214 | |||
215 | 20 | public function __toString(): string |
|
216 | { |
||
217 | 20 | $cookieStringParts = [ |
|
218 | 20 | urlencode($this->name) . '=' . urlencode((string) $this->value), |
|
219 | ]; |
||
220 | |||
221 | 20 | $cookieStringParts = $this->appendFormattedDomainPartIfSet($cookieStringParts); |
|
222 | 20 | $cookieStringParts = $this->appendFormattedPathPartIfSet($cookieStringParts); |
|
223 | 20 | $cookieStringParts = $this->appendFormattedExpiresPartIfSet($cookieStringParts); |
|
224 | 20 | $cookieStringParts = $this->appendFormattedMaxAgePartIfSet($cookieStringParts); |
|
225 | 20 | $cookieStringParts = $this->appendFormattedSecurePartIfSet($cookieStringParts); |
|
226 | 20 | $cookieStringParts = $this->appendFormattedHttpOnlyPartIfSet($cookieStringParts); |
|
227 | 20 | $cookieStringParts = $this->appendFormattedSameSitePartIfSet($cookieStringParts); |
|
228 | |||
229 | 20 | return implode('; ', $cookieStringParts); |
|
230 | } |
||
231 | |||
232 | 10 | public static function create(string $name, ?string $value = null): self |
|
236 | |||
237 | 1 | public static function createRememberedForever(string $name, ?string $value = null): self |
|
238 | { |
||
239 | 1 | return static::create($name, $value)->rememberForever(); |
|
240 | } |
||
241 | |||
242 | /** |
||
243 | * @deprecated Do not use this method. Will be removed in v4.0. |
||
244 | * |
||
245 | * If you want to remove a cookie, create it normally and call ->expire() |
||
246 | * on the SetCookie object. |
||
247 | */ |
||
248 | public static function createExpired(string $name): self |
||
249 | { |
||
250 | return static::create($name)->expire(); |
||
251 | } |
||
252 | |||
253 | 32 | public static function fromSetCookieString(string $string): self |
|
254 | { |
||
255 | 32 | $rawAttributes = StringUtil::splitOnAttributeDelimiter($string); |
|
256 | |||
309 | |||
310 | /** |
||
311 | * @param string[] $cookieStringParts |
||
312 | * |
||
313 | * @return string[] |
||
314 | */ |
||
315 | 20 | private function appendFormattedDomainPartIfSet(array $cookieStringParts): array |
|
323 | |||
324 | /** |
||
325 | * @param string[] $cookieStringParts |
||
326 | * |
||
327 | * @return string[] |
||
328 | */ |
||
329 | 20 | private function appendFormattedPathPartIfSet(array $cookieStringParts): array |
|
337 | |||
338 | /** |
||
339 | * @param string[] $cookieStringParts |
||
340 | * |
||
341 | * @return string[] |
||
342 | */ |
||
343 | 20 | private function appendFormattedExpiresPartIfSet(array $cookieStringParts): array |
|
351 | |||
352 | /** |
||
353 | * @param string[] $cookieStringParts |
||
354 | * |
||
355 | * @return string[] |
||
356 | */ |
||
357 | 20 | private function appendFormattedMaxAgePartIfSet(array $cookieStringParts): array |
|
365 | |||
366 | /** |
||
367 | * @param string[] $cookieStringParts |
||
368 | * |
||
369 | * @return string[] |
||
370 | */ |
||
371 | 20 | private function appendFormattedSecurePartIfSet(array $cookieStringParts): array |
|
379 | |||
380 | /** |
||
381 | * @param string[] $cookieStringParts |
||
382 | * |
||
383 | * @return string[] |
||
384 | */ |
||
385 | 20 | private function appendFormattedHttpOnlyPartIfSet(array $cookieStringParts): array |
|
393 | |||
394 | /** |
||
395 | * @param string[] $cookieStringParts |
||
396 | * |
||
397 | * @return string[] |
||
398 | */ |
||
399 | 20 | private function appendFormattedSameSitePartIfSet(array $cookieStringParts): array |
|
409 | } |
||
410 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.