Complex classes like Settings 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 Settings, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
35 | class Settings implements AnalysisStrategyInterface |
||
36 | { |
||
37 | use LoggerAwareTrait; |
||
38 | |||
39 | /** |
||
40 | * @var string[] |
||
41 | */ |
||
42 | const SIMPLE_LC_RESPONSE_HEADERS = [ |
||
43 | SimpleResponseHeaders::LC_ACCEPT_LANGUAGE, |
||
44 | SimpleResponseHeaders::LC_CACHE_CONTROL, |
||
45 | SimpleResponseHeaders::LC_CONTENT_LANGUAGE, |
||
46 | SimpleResponseHeaders::LC_CONTENT_TYPE, |
||
47 | SimpleResponseHeaders::LC_EXPIRES, |
||
48 | SimpleResponseHeaders::LC_LAST_MODIFIED, |
||
49 | SimpleResponseHeaders::LC_PRAGMA, |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $serverOriginScheme; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $serverOriginHost; |
||
61 | |||
62 | /** |
||
63 | * @var null|int |
||
64 | */ |
||
65 | private $serverOriginPort; |
||
66 | |||
67 | /** |
||
68 | * @var bool |
||
69 | */ |
||
70 | private $isPreFlightCanBeCached; |
||
71 | |||
72 | /** |
||
73 | * @var int |
||
74 | */ |
||
75 | private $preFlightCacheMaxAge; |
||
76 | |||
77 | /** |
||
78 | * @var bool |
||
79 | */ |
||
80 | private $isForceAddMethods; |
||
81 | |||
82 | /** |
||
83 | * @var bool |
||
84 | */ |
||
85 | private $isForceAddHeaders; |
||
86 | |||
87 | /** |
||
88 | * @var bool |
||
89 | */ |
||
90 | private $isUseCredentials; |
||
91 | |||
92 | /** |
||
93 | * @var bool |
||
94 | */ |
||
95 | private $areAllOriginsAllowed; |
||
96 | |||
97 | /** |
||
98 | * @var array |
||
99 | */ |
||
100 | private $allowedOrigins; |
||
101 | |||
102 | /** |
||
103 | * @var bool |
||
104 | */ |
||
105 | private $areAllMethodsAllowed; |
||
106 | |||
107 | /** |
||
108 | * @var array |
||
109 | */ |
||
110 | private $allowedLcMethods; |
||
111 | |||
112 | /** |
||
113 | * @var string |
||
114 | */ |
||
115 | private $allowedMethodsList; |
||
116 | |||
117 | /** |
||
118 | * @var bool |
||
119 | */ |
||
120 | private $areAllHeadersAllowed; |
||
121 | |||
122 | /** |
||
123 | * @var array |
||
124 | */ |
||
125 | private $allowedLcHeaders; |
||
126 | |||
127 | /** |
||
128 | * @var string |
||
129 | */ |
||
130 | private $allowedHeadersList; |
||
131 | |||
132 | /** |
||
133 | * @var string |
||
134 | */ |
||
135 | private $exposedHeadersList; |
||
136 | |||
137 | /** |
||
138 | * @var bool |
||
139 | */ |
||
140 | private $isCheckHost; |
||
141 | |||
142 | /** |
||
143 | * Sort of default constructor, though made separate to be used optionally when no cached data available. |
||
144 | * |
||
145 | * @param string $scheme |
||
146 | * @param string $host |
||
147 | * @param int $port |
||
148 | * |
||
149 | * @return self |
||
|
|||
150 | */ |
||
151 | 16 | public function init(string $scheme, string $host, int $port): self |
|
168 | |||
169 | /** |
||
170 | * Get internal data state. Can be used for data caching. |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | 16 | public function getData(): array |
|
197 | |||
198 | /** |
||
199 | * Set internal data state. Can be used for setting cached data. |
||
200 | * |
||
201 | * @param array $data |
||
202 | * |
||
203 | * @return self |
||
204 | */ |
||
205 | 16 | public function setData(array $data): self |
|
230 | |||
231 | /** |
||
232 | * @inheritdoc |
||
233 | */ |
||
234 | 2 | public function getServerOriginScheme(): string |
|
238 | |||
239 | /** |
||
240 | * @inheritdoc |
||
241 | */ |
||
242 | 14 | public function getServerOriginHost(): string |
|
246 | |||
247 | /** |
||
248 | * @inheritdoc |
||
249 | */ |
||
250 | 15 | public function getServerOriginPort(): ?int |
|
254 | |||
255 | /** |
||
256 | * Set server Origin URL. |
||
257 | * |
||
258 | * @param string $scheme |
||
259 | * @param string $host |
||
260 | * @param int $port |
||
261 | * |
||
262 | * @return self |
||
263 | */ |
||
264 | 16 | public function setServerOrigin(string $scheme, string $host, int $port): self |
|
282 | |||
283 | /** |
||
284 | * @inheritdoc |
||
285 | */ |
||
286 | 2 | public function isPreFlightCanBeCached(RequestInterface $request): bool |
|
290 | |||
291 | /** |
||
292 | * @inheritdoc |
||
293 | */ |
||
294 | 2 | public function getPreFlightCacheMaxAge(RequestInterface $request): int |
|
298 | |||
299 | /** |
||
300 | * Set pre-flight cache max period in seconds. |
||
301 | * |
||
302 | * @param int $cacheMaxAge |
||
303 | * |
||
304 | * @return self |
||
305 | */ |
||
306 | 16 | public function setPreFlightCacheMaxAge(int $cacheMaxAge): self |
|
315 | |||
316 | /** |
||
317 | * @inheritdoc |
||
318 | */ |
||
319 | 1 | public function isForceAddAllowedMethodsToPreFlightResponse(): bool |
|
323 | |||
324 | /** |
||
325 | * If allowed headers should be added when request headers are 'simple' and |
||
326 | * non of them is 'Content-Type' (see #6.2.10 CORS). |
||
327 | * |
||
328 | * @see http://www.w3.org/TR/cors/#resource-preflight-requests |
||
329 | * |
||
330 | * @return self |
||
331 | */ |
||
332 | 1 | public function enableAddAllowedMethodsToPreFlightResponse(): self |
|
338 | |||
339 | /** |
||
340 | * If allowed headers should be added when request headers are 'simple' and |
||
341 | * non of them is 'Content-Type' (see #6.2.10 CORS). |
||
342 | * |
||
343 | * @see http://www.w3.org/TR/cors/#resource-preflight-requests |
||
344 | * |
||
345 | * @return self |
||
346 | */ |
||
347 | 16 | public function disableAddAllowedMethodsToPreFlightResponse(): self |
|
353 | |||
354 | /** |
||
355 | * @inheritdoc |
||
356 | */ |
||
357 | 1 | public function isForceAddAllowedHeadersToPreFlightResponse(): bool |
|
361 | |||
362 | /** |
||
363 | * If allowed headers should be added when request headers are 'simple' and |
||
364 | * non of them is 'Content-Type' (see #6.2.10 CORS). |
||
365 | * |
||
366 | * @see http://www.w3.org/TR/cors/#resource-preflight-requests |
||
367 | * |
||
368 | * @return self |
||
369 | */ |
||
370 | 1 | public function enableAddAllowedHeadersToPreFlightResponse(): self |
|
376 | |||
377 | /** |
||
378 | * If allowed headers should be added when request headers are 'simple' and |
||
379 | * non of them is 'Content-Type' (see #6.2.10 CORS). |
||
380 | * |
||
381 | * @see http://www.w3.org/TR/cors/#resource-preflight-requests |
||
382 | * |
||
383 | * @return self |
||
384 | */ |
||
385 | 16 | public function disableAddAllowedHeadersToPreFlightResponse(): self |
|
391 | |||
392 | /** |
||
393 | * @inheritdoc |
||
394 | */ |
||
395 | 4 | public function isRequestCredentialsSupported(RequestInterface $request): bool |
|
399 | |||
400 | /** |
||
401 | * If access with credentials is supported by the resource. |
||
402 | * |
||
403 | * @return self |
||
404 | */ |
||
405 | 16 | public function setCredentialsSupported(): self |
|
411 | |||
412 | /** |
||
413 | * If access with credentials is supported by the resource. |
||
414 | * |
||
415 | * @return self |
||
416 | */ |
||
417 | 16 | public function setCredentialsNotSupported(): self |
|
423 | |||
424 | /** |
||
425 | * @inheritdoc |
||
426 | */ |
||
427 | 10 | public function isRequestOriginAllowed(string $requestOrigin): bool |
|
433 | |||
434 | /** |
||
435 | * Enable all origins allowed. |
||
436 | * |
||
437 | * @return self |
||
438 | */ |
||
439 | 16 | public function enableAllOriginsAllowed(): self |
|
445 | |||
446 | /** |
||
447 | * Disable all origins allowed. |
||
448 | * |
||
449 | * @return self |
||
450 | */ |
||
451 | 16 | public function disableAllOriginsAllowed(): self |
|
457 | |||
458 | /** |
||
459 | * Set allowed origins. |
||
460 | * |
||
461 | * @param array $origins |
||
462 | * |
||
463 | * @return self |
||
464 | */ |
||
465 | 16 | public function setAllowedOrigins(array $origins): self |
|
475 | |||
476 | /** |
||
477 | * @inheritdoc |
||
478 | */ |
||
479 | 4 | public function isRequestMethodSupported(string $method): bool |
|
483 | |||
484 | /** |
||
485 | * Enable all methods allowed. |
||
486 | * |
||
487 | * @return self |
||
488 | */ |
||
489 | 16 | public function enableAllMethodsAllowed(): self |
|
495 | |||
496 | /** |
||
497 | * Disable all methods allowed. |
||
498 | * |
||
499 | * @return self |
||
500 | */ |
||
501 | 16 | public function disableAllMethodsAllowed(): self |
|
507 | |||
508 | /** |
||
509 | * Set allowed methods. |
||
510 | * |
||
511 | * Security Note: you have to remember CORS is not access control system and you should not expect all |
||
512 | * cross-origin requests will have pre-flights. For so-called 'simple' methods with so-called 'simple' |
||
513 | * headers request will be made without pre-flight. Thus you can not restrict such requests with CORS |
||
514 | * and should use other means. |
||
515 | * For example method 'GET' without any headers or with only 'simple' headers will not have pre-flight |
||
516 | * request so disabling it will not restrict access to resource(s). |
||
517 | * You can read more on 'simple' methods at http://www.w3.org/TR/cors/#simple-method |
||
518 | * |
||
519 | * @param array $methods |
||
520 | * |
||
521 | * @return self |
||
522 | */ |
||
523 | 16 | public function setAllowedMethods(array $methods): self |
|
534 | |||
535 | /** |
||
536 | * @inheritdoc |
||
537 | */ |
||
538 | 3 | public function isRequestAllHeadersSupported(array $lcHeaders): bool |
|
543 | |||
544 | /** |
||
545 | * Enable all headers allowed. |
||
546 | * |
||
547 | * @return self |
||
548 | */ |
||
549 | 16 | public function enableAllHeadersAllowed(): self |
|
555 | |||
556 | /** |
||
557 | * Disable all headers allowed. |
||
558 | * |
||
559 | * @return self |
||
560 | */ |
||
561 | 16 | public function disableAllHeadersAllowed(): self |
|
567 | |||
568 | /** |
||
569 | * Set allowed headers. |
||
570 | * |
||
571 | * Security Note: you have to remember CORS is not access control system and you should not expect all |
||
572 | * cross-origin requests will have pre-flights. For so-called 'simple' methods with so-called 'simple' |
||
573 | * headers request will be made without pre-flight. Thus you can not restrict such requests with CORS |
||
574 | * and should use other means. |
||
575 | * For example method 'GET' without any headers or with only 'simple' headers will not have pre-flight |
||
576 | * request so disabling it will not restrict access to resource(s). |
||
577 | * You can read more on 'simple' headers at http://www.w3.org/TR/cors/#simple-header |
||
578 | * |
||
579 | * @param array $headers |
||
580 | * |
||
581 | * @return self |
||
582 | */ |
||
583 | 16 | public function setAllowedHeaders(array $headers): self |
|
594 | |||
595 | /** |
||
596 | * @inheritdoc |
||
597 | */ |
||
598 | 2 | public function getRequestAllowedMethods(RequestInterface $request): string |
|
602 | |||
603 | /** |
||
604 | * @inheritdoc |
||
605 | */ |
||
606 | 2 | public function getRequestAllowedHeaders(RequestInterface $request): string |
|
610 | |||
611 | /** |
||
612 | * @inheritdoc |
||
613 | */ |
||
614 | 2 | public function getResponseExposedHeaders(RequestInterface $request): string |
|
618 | |||
619 | /** |
||
620 | * Set headers other than the simple ones that might be exposed to user agent. |
||
621 | * |
||
622 | * @param array $headers |
||
623 | * |
||
624 | * @return Settings |
||
625 | */ |
||
626 | 16 | public function setExposedHeaders(array $headers): self |
|
641 | |||
642 | /** |
||
643 | * @inheritdoc |
||
644 | */ |
||
645 | 14 | public function isCheckHost(): bool |
|
649 | |||
650 | /** |
||
651 | * If request 'Host' header should be checked against server's origin. |
||
652 | * Check of Host header is strongly encouraged by #6.3 CORS. |
||
653 | * Header 'Host' must present for all requests rfc2616 14.23 |
||
654 | * |
||
655 | * @return self |
||
656 | */ |
||
657 | 16 | public function enableCheckHost(): self |
|
663 | |||
664 | /** |
||
665 | * If request 'Host' header should be checked against server's origin. |
||
666 | * Check of Host header is strongly encouraged by #6.3 CORS. |
||
667 | * Header 'Host' must present for all requests rfc2616 14.23 |
||
668 | * |
||
669 | * @return self |
||
670 | */ |
||
671 | 16 | public function disableCheckHost(): self |
|
677 | } |
||
678 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.