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 namespace Neomerx\Cors\Strategies; |
||
| 30 | class Settings implements SettingsStrategyInterface |
||
| 31 | { |
||
| 32 | use LoggerAwareTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * 'All' value for allowed origins. |
||
| 36 | */ |
||
| 37 | const VALUE_ALLOW_ORIGIN_ALL = CorsResponseHeaders::VALUE_ALLOW_ORIGIN_ALL; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * 'All' values for allowed headers. |
||
| 41 | */ |
||
| 42 | const VALUE_ALLOW_ALL_HEADERS = '*'; |
||
| 43 | |||
| 44 | /** Settings key */ |
||
| 45 | const KEY_SERVER_ORIGIN = 0; |
||
| 46 | |||
| 47 | /** Settings key */ |
||
| 48 | const KEY_SERVER_ORIGIN_SCHEME = 'scheme'; |
||
| 49 | |||
| 50 | /** Settings key */ |
||
| 51 | const KEY_SERVER_ORIGIN_HOST = 'host'; |
||
| 52 | |||
| 53 | /** Settings key */ |
||
| 54 | const KEY_SERVER_ORIGIN_PORT = 'port'; |
||
| 55 | |||
| 56 | /** Settings key */ |
||
| 57 | const KEY_ALLOWED_ORIGINS = 1; |
||
| 58 | |||
| 59 | /** Settings key */ |
||
| 60 | const KEY_ALLOWED_METHODS = 2; |
||
| 61 | |||
| 62 | /** Settings key */ |
||
| 63 | const KEY_ALLOWED_HEADERS = 3; |
||
| 64 | |||
| 65 | /** Settings key */ |
||
| 66 | const KEY_EXPOSED_HEADERS = 4; |
||
| 67 | |||
| 68 | /** Settings key */ |
||
| 69 | const KEY_IS_USING_CREDENTIALS = 5; |
||
| 70 | |||
| 71 | /** Settings key */ |
||
| 72 | const KEY_FLIGHT_CACHE_MAX_AGE = 6; |
||
| 73 | |||
| 74 | /** Settings key */ |
||
| 75 | const KEY_IS_FORCE_ADD_METHODS = 7; |
||
| 76 | |||
| 77 | /** Settings key */ |
||
| 78 | const KEY_IS_FORCE_ADD_HEADERS = 8; |
||
| 79 | |||
| 80 | /** Settings key */ |
||
| 81 | const KEY_IS_CHECK_HOST = 9; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $settings; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param array $settings |
||
|
1 ignored issue
–
show
|
|||
| 90 | */ |
||
| 91 | 19 | public function __construct(array $settings = null) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @inheritdoc |
||
| 98 | */ |
||
| 99 | 8 | public function getSettings() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @inheritdoc |
||
| 106 | */ |
||
| 107 | 19 | public function setSettings(array $settings) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @inheritdoc |
||
| 114 | */ |
||
| 115 | 12 | public function getServerOrigin() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @inheritdoc |
||
| 122 | */ |
||
| 123 | 19 | public function setServerOrigin($origin) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @inheritdoc |
||
| 132 | */ |
||
| 133 | 3 | public function isPreFlightCanBeCached(RequestInterface $request) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @inheritdoc |
||
| 140 | */ |
||
| 141 | 3 | public function getPreFlightCacheMaxAge(RequestInterface $request) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @inheritdoc |
||
| 148 | */ |
||
| 149 | 10 | public function setPreFlightCacheMaxAge($cacheMaxAge) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @inheritdoc |
||
| 158 | */ |
||
| 159 | 2 | public function isForceAddAllowedMethodsToPreFlightResponse() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @inheritdoc |
||
| 166 | */ |
||
| 167 | 10 | public function setForceAddAllowedMethodsToPreFlightResponse($forceFlag) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @inheritdoc |
||
| 176 | */ |
||
| 177 | 1 | public function isForceAddAllowedHeadersToPreFlightResponse() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @inheritdoc |
||
| 184 | */ |
||
| 185 | 10 | public function setForceAddAllowedHeadersToPreFlightResponse($forceFlag) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @inheritdoc |
||
| 194 | */ |
||
| 195 | 4 | public function isRequestCredentialsSupported(RequestInterface $request) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritdoc |
||
| 202 | */ |
||
| 203 | 19 | public function setRequestCredentialsSupported($isSupported) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @inheritdoc |
||
| 212 | */ |
||
| 213 | 8 | public function isRequestOriginAllowed(ParsedUrlInterface $requestOrigin) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @inheritdoc |
||
| 229 | */ |
||
| 230 | 19 | public function setRequestAllowedOrigins(array $origins) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @inheritdoc |
||
| 243 | */ |
||
| 244 | 5 | public function isRequestMethodSupported($method) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @inheritdoc |
||
| 253 | */ |
||
| 254 | 5 | public function isRequestAllHeadersSupported($headers) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @inheritdoc |
||
| 279 | */ |
||
| 280 | 3 | public function getRequestAllowedMethods(RequestInterface $request, $requestMethod) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @inheritdoc |
||
| 287 | */ |
||
| 288 | 19 | public function setRequestAllowedMethods(array $methods) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @inheritdoc |
||
| 300 | */ |
||
| 301 | 3 | public function getRequestAllowedHeaders(RequestInterface $request, array $requestHeaders) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @inheritdoc |
||
| 308 | */ |
||
| 309 | 19 | public function setRequestAllowedHeaders(array $headers) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @inheritdoc |
||
| 322 | */ |
||
| 323 | 2 | public function getResponseExposedHeaders(RequestInterface $request) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @inheritdoc |
||
| 330 | */ |
||
| 331 | 19 | public function setResponseExposedHeaders(array $headers) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @inheritdoc |
||
| 340 | */ |
||
| 341 | 11 | public function isCheckHost() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @inheritdoc |
||
| 348 | */ |
||
| 349 | 19 | public function setCheckHost($checkFlag) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Select only enabled items from $list. |
||
| 358 | * |
||
| 359 | * @param array $list |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | 6 | protected function getEnabledItems(array $list) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @return array |
||
|
1 ignored issue
–
show
|
|||
| 378 | */ |
||
| 379 | 19 | protected function getDefaultSettings() |
|
| 475 | |||
| 476 | /** |
||
| 477 | * @param mixed $key |
||
| 478 | * @param mixed $default |
||
| 479 | * |
||
| 480 | * @return mixed |
||
| 481 | */ |
||
| 482 | 11 | private function getValue($key, $default) |
|
| 486 | } |
||
| 487 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.