Complex classes like Authorization 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 Authorization, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Authorization |
||
|
|
|||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var bool|null |
||
| 26 | */ |
||
| 27 | private $authorized = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Client |
||
| 31 | */ |
||
| 32 | private $client; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var UserAccount|null |
||
| 36 | */ |
||
| 37 | private $userAccount = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var null|bool |
||
| 41 | */ |
||
| 42 | private $userAccountFullyAuthenticated = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | private $data = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var null|array |
||
| 51 | */ |
||
| 52 | private $claims = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var TokenType|null |
||
| 56 | */ |
||
| 57 | private $tokenType = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var ResponseType] |
||
| 61 | */ |
||
| 62 | private $responseType = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var ResponseMode|null |
||
| 66 | */ |
||
| 67 | private $responseMode = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $queryParameters = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string|null |
||
| 76 | */ |
||
| 77 | private $redirectUri = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $consentScreenOptions = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | private $responseParameters = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private $responseHeaders = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var null|ResourceServer |
||
| 96 | */ |
||
| 97 | private $resourceServer = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Authorization constructor. |
||
| 101 | * |
||
| 102 | * @param Client $client |
||
| 103 | * @param array $queryParameters |
||
| 104 | */ |
||
| 105 | private function __construct(Client $client, array $queryParameters) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param Client $client |
||
| 113 | * @param array $queryParameters |
||
| 114 | * |
||
| 115 | * @return Authorization |
||
| 116 | */ |
||
| 117 | public static function create(Client $client, array $queryParameters): self |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | public function getQueryParams(): array |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $param |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function hasQueryParam(string $param): bool |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param string $param |
||
| 142 | * |
||
| 143 | * @return mixed |
||
| 144 | */ |
||
| 145 | public function getQueryParam(string $param) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return Client |
||
| 156 | */ |
||
| 157 | public function getClient(): Client |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param TokenType $tokenType |
||
| 164 | * |
||
| 165 | * @return Authorization |
||
| 166 | */ |
||
| 167 | public function withTokenType(TokenType $tokenType): self |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return null|TokenType |
||
| 177 | */ |
||
| 178 | public function getTokenType(): ? TokenType |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param array $claims |
||
| 185 | * |
||
| 186 | * @return Authorization |
||
| 187 | */ |
||
| 188 | public function withClaims(array $claims): self |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return null|array |
||
| 198 | */ |
||
| 199 | public function getClaims(): ?array |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param ResponseType $responseType |
||
| 206 | * |
||
| 207 | * @return Authorization |
||
| 208 | */ |
||
| 209 | public function withResponseType(ResponseType $responseType): self |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return ResponseType |
||
| 219 | */ |
||
| 220 | public function getResponseType(): ResponseType |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param ResponseMode $responseMode |
||
| 227 | * |
||
| 228 | * @return Authorization |
||
| 229 | */ |
||
| 230 | public function withResponseMode(ResponseMode $responseMode): self |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return null|ResponseMode |
||
| 240 | */ |
||
| 241 | public function getResponseMode(): ? ResponseMode |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $redirectUri |
||
| 248 | * |
||
| 249 | * @return Authorization |
||
| 250 | */ |
||
| 251 | public function withRedirectUri(string $redirectUri): self |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return null|string |
||
| 261 | */ |
||
| 262 | public function getRedirectUri(): ? string |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param UserAccount $userAccount |
||
| 269 | * @param bool $isFullyAuthenticated |
||
| 270 | * |
||
| 271 | * @return Authorization |
||
| 272 | */ |
||
| 273 | public function withUserAccount(UserAccount $userAccount, bool $isFullyAuthenticated): self |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return null|UserAccount |
||
| 284 | */ |
||
| 285 | public function getUserAccount(): ? UserAccount |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $responseParameter |
||
| 292 | * @param mixed $value |
||
| 293 | * |
||
| 294 | * @return Authorization |
||
| 295 | */ |
||
| 296 | public function withResponseParameter(string $responseParameter, $value): self |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | public function getResponseParameters(): array |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $param |
||
| 314 | * |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | public function getResponseParameter(string $param) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param string $param |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public function hasResponseParameter(string $param): bool |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $responseHeader |
||
| 338 | * @param mixed $value |
||
| 339 | * |
||
| 340 | * @return Authorization |
||
| 341 | */ |
||
| 342 | public function withResponseHeader(string $responseHeader, $value): self |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | public function getResponseHeaders(): array |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @return bool|null |
||
| 360 | */ |
||
| 361 | public function isUserAccountFullyAuthenticated(): ? bool |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @return string[] |
||
| 368 | */ |
||
| 369 | public function getPrompt(): array |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | public function hasUiLocales(): bool |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return string[] |
||
| 388 | */ |
||
| 389 | public function getUiLocales(): array |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param string $prompt |
||
| 396 | * |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | public function hasPrompt(string $prompt): bool |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @return bool|null |
||
| 406 | */ |
||
| 407 | public function isAuthorized(): ? bool |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return Authorization |
||
| 414 | */ |
||
| 415 | public function allow(): self |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @return Authorization |
||
| 425 | */ |
||
| 426 | public function deny(): self |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $key |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | public function hasData(string $key): bool |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $key |
||
| 446 | * |
||
| 447 | * @return mixed |
||
| 448 | */ |
||
| 449 | public function getData(string $key) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param string $key |
||
| 460 | * @param mixed $data |
||
| 461 | * |
||
| 462 | * @return Authorization |
||
| 463 | */ |
||
| 464 | public function withData(string $key, $data): self |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @return null|ResourceServer |
||
| 474 | */ |
||
| 475 | public function getResourceServer(): ? ResourceServer |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param ResourceServer $resourceServer |
||
| 482 | * |
||
| 483 | * @return Authorization |
||
| 484 | */ |
||
| 485 | public function withResourceServer(ResourceServer $resourceServer): self |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @param string $option |
||
| 495 | * @param mixed $value |
||
| 496 | * |
||
| 497 | * @return Authorization |
||
| 498 | */ |
||
| 499 | public function withConsentScreenOption(string $option, $value): self |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param string $option |
||
| 509 | * |
||
| 510 | * @return Authorization |
||
| 511 | */ |
||
| 512 | public function withoutConsentScreenOption(string $option): self |
||
| 523 | |||
| 524 | public function hasScope(): bool |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | public function getScope(): string |
||
| 536 | } |
||
| 537 |