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 | * Set allowed origins. |
||
| 448 | * |
||
| 449 | * @param array $origins |
||
| 450 | * |
||
| 451 | * @return self |
||
| 452 | */ |
||
| 453 | 16 | public function setAllowedOrigins(array $origins): self |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @inheritdoc |
||
| 468 | */ |
||
| 469 | 4 | public function isRequestMethodSupported(string $method): bool |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Enable all methods allowed. |
||
| 476 | * |
||
| 477 | * @return self |
||
| 478 | */ |
||
| 479 | 16 | public function enableAllMethodsAllowed(): self |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Set allowed methods. |
||
| 488 | * |
||
| 489 | * Security Note: you have to remember CORS is not access control system and you should not expect all |
||
| 490 | * cross-origin requests will have pre-flights. For so-called 'simple' methods with so-called 'simple' |
||
| 491 | * headers request will be made without pre-flight. Thus you can not restrict such requests with CORS |
||
| 492 | * and should use other means. |
||
| 493 | * For example method 'GET' without any headers or with only 'simple' headers will not have pre-flight |
||
| 494 | * request so disabling it will not restrict access to resource(s). |
||
| 495 | * You can read more on 'simple' methods at http://www.w3.org/TR/cors/#simple-method |
||
| 496 | * |
||
| 497 | * @param array $methods |
||
| 498 | * |
||
| 499 | * @return self |
||
| 500 | */ |
||
| 501 | 16 | public function setAllowedMethods(array $methods): self |
|
| 514 | |||
| 515 | /** |
||
| 516 | * @inheritdoc |
||
| 517 | */ |
||
| 518 | 3 | public function isRequestAllHeadersSupported(array $lcHeaders): bool |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Enable all headers allowed. |
||
| 526 | * |
||
| 527 | * @return self |
||
| 528 | */ |
||
| 529 | 16 | public function enableAllHeadersAllowed(): self |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Set allowed headers. |
||
| 538 | * |
||
| 539 | * Security Note: you have to remember CORS is not access control system and you should not expect all |
||
| 540 | * cross-origin requests will have pre-flights. For so-called 'simple' methods with so-called 'simple' |
||
| 541 | * headers request will be made without pre-flight. Thus you can not restrict such requests with CORS |
||
| 542 | * and should use other means. |
||
| 543 | * For example method 'GET' without any headers or with only 'simple' headers will not have pre-flight |
||
| 544 | * request so disabling it will not restrict access to resource(s). |
||
| 545 | * You can read more on 'simple' headers at http://www.w3.org/TR/cors/#simple-header |
||
| 546 | * |
||
| 547 | * @param array $headers |
||
| 548 | * |
||
| 549 | * @return self |
||
| 550 | */ |
||
| 551 | 16 | public function setAllowedHeaders(array $headers): self |
|
| 564 | |||
| 565 | /** |
||
| 566 | * @inheritdoc |
||
| 567 | */ |
||
| 568 | 2 | public function getRequestAllowedMethods(RequestInterface $request): string |
|
| 572 | |||
| 573 | /** |
||
| 574 | * @inheritdoc |
||
| 575 | */ |
||
| 576 | 2 | public function getRequestAllowedHeaders(RequestInterface $request): string |
|
| 580 | |||
| 581 | /** |
||
| 582 | * @inheritdoc |
||
| 583 | */ |
||
| 584 | 2 | public function getResponseExposedHeaders(RequestInterface $request): string |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Set headers other than the simple ones that might be exposed to user agent. |
||
| 591 | * |
||
| 592 | * @param array $headers |
||
| 593 | * |
||
| 594 | * @return Settings |
||
| 595 | */ |
||
| 596 | 16 | public function setExposedHeaders(array $headers): self |
|
| 611 | |||
| 612 | /** |
||
| 613 | * @inheritdoc |
||
| 614 | */ |
||
| 615 | 14 | public function isCheckHost(): bool |
|
| 619 | |||
| 620 | /** |
||
| 621 | * If request 'Host' header should be checked against server's origin. |
||
| 622 | * Check of Host header is strongly encouraged by #6.3 CORS. |
||
| 623 | * Header 'Host' must present for all requests rfc2616 14.23 |
||
| 624 | * |
||
| 625 | * @return self |
||
| 626 | */ |
||
| 627 | 16 | public function enableCheckHost(): self |
|
| 633 | |||
| 634 | /** |
||
| 635 | * If request 'Host' header should be checked against server's origin. |
||
| 636 | * Check of Host header is strongly encouraged by #6.3 CORS. |
||
| 637 | * Header 'Host' must present for all requests rfc2616 14.23 |
||
| 638 | * |
||
| 639 | * @return self |
||
| 640 | */ |
||
| 641 | 16 | public function disableCheckHost(): self |
|
| 647 | } |
||
| 648 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.