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 |
||
| 24 | final class Authorization |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var bool|null |
||
| 28 | */ |
||
| 29 | private $authorized = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Client |
||
| 33 | */ |
||
| 34 | private $client; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var UserAccountInterface|null |
||
| 38 | */ |
||
| 39 | private $userAccount = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var null|bool |
||
| 43 | */ |
||
| 44 | private $userAccountFullyAuthenticated = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string[] |
||
| 48 | */ |
||
| 49 | private $scopes = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $data = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var TokenTypeInterface|null |
||
| 58 | */ |
||
| 59 | private $tokenType = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var ResponseTypeInterface[] |
||
| 63 | */ |
||
| 64 | private $responseTypes = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var ResponseModeInterface|null |
||
| 68 | */ |
||
| 69 | private $responseMode = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private $queryParameters = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string|null |
||
| 78 | */ |
||
| 79 | private $redirectUri = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private $consentScreenOptions = []; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private $responseParameters = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | private $responseHeaders = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var null|ResourceServerInterface |
||
| 98 | */ |
||
| 99 | private $resourceServer = null; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Authorization constructor. |
||
| 103 | * |
||
| 104 | * @param Client $client |
||
| 105 | * @param array $queryParameters |
||
| 106 | */ |
||
| 107 | private function __construct(Client $client, array $queryParameters) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param Client $client |
||
| 115 | * @param array $queryParameters |
||
| 116 | * |
||
| 117 | * @return Authorization |
||
| 118 | */ |
||
| 119 | public static function create(Client $client, array $queryParameters): Authorization |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function getQueryParams(): array |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $param |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public function hasQueryParam(string $param): bool |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $param |
||
| 144 | * |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | public function getQueryParam(string $param) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return Client |
||
| 156 | */ |
||
| 157 | public function getClient(): Client |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param TokenTypeInterface $tokenType |
||
| 164 | * |
||
| 165 | * @return Authorization |
||
| 166 | */ |
||
| 167 | public function withTokenType(TokenTypeInterface $tokenType): Authorization |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @return null|TokenTypeInterface |
||
| 177 | */ |
||
| 178 | public function getTokenType(): ?TokenTypeInterface |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param ResponseTypeInterface[] $responseTypes |
||
| 185 | * |
||
| 186 | * @return Authorization |
||
| 187 | */ |
||
| 188 | public function withResponseTypes(array $responseTypes): Authorization |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return ResponseTypeInterface[] |
||
| 199 | */ |
||
| 200 | public function getResponseTypes(): array |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param ResponseModeInterface $responseMode |
||
| 207 | * |
||
| 208 | * @return Authorization |
||
| 209 | */ |
||
| 210 | public function withResponseMode(ResponseModeInterface $responseMode): Authorization |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return null|ResponseModeInterface |
||
| 220 | */ |
||
| 221 | public function getResponseMode(): ?ResponseModeInterface |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $redirectUri |
||
| 228 | * |
||
| 229 | * @return Authorization |
||
| 230 | */ |
||
| 231 | public function withRedirectUri(string $redirectUri): Authorization |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return null|string |
||
| 241 | */ |
||
| 242 | public function getRedirectUri(): ?string |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param UserAccountInterface $userAccount |
||
| 249 | * @param bool $isFullyAuthenticated |
||
| 250 | * |
||
| 251 | * @return Authorization |
||
| 252 | */ |
||
| 253 | public function withUserAccount(UserAccountInterface $userAccount, bool $isFullyAuthenticated): Authorization |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return null|UserAccountInterface |
||
| 264 | */ |
||
| 265 | public function getUserAccount(): ?UserAccountInterface |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $responseParameter |
||
| 272 | * @param mixed $value |
||
| 273 | * |
||
| 274 | * @return Authorization |
||
| 275 | */ |
||
| 276 | public function withResponseParameter(string $responseParameter, $value): Authorization |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | public function getResponseParameters(): array |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $param |
||
| 294 | * |
||
| 295 | * @return mixed |
||
| 296 | */ |
||
| 297 | public function getResponseParameter(string $param) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $param |
||
| 306 | * |
||
| 307 | * @return bool |
||
| 308 | */ |
||
| 309 | public function hasResponseParameter(string $param): bool |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $responseHeader |
||
| 316 | * @param mixed $value |
||
| 317 | * |
||
| 318 | * @return Authorization |
||
| 319 | */ |
||
| 320 | public function withResponseHeader(string $responseHeader, $value): Authorization |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | public function getResponseHeaders(): array |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return bool|null |
||
| 338 | */ |
||
| 339 | public function isUserAccountFullyAuthenticated(): ?bool |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return string[] |
||
| 346 | */ |
||
| 347 | public function getPrompt(): array |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @return bool |
||
| 358 | */ |
||
| 359 | public function hasUiLocales(): bool |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return string[] |
||
| 366 | */ |
||
| 367 | public function getUiLocales(): array |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $prompt |
||
| 374 | * |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | public function hasPrompt(string $prompt): bool |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param array $scope |
||
| 384 | * |
||
| 385 | * @return Authorization |
||
| 386 | */ |
||
| 387 | public function withScopes(array $scope): Authorization |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return array |
||
| 397 | */ |
||
| 398 | public function getScopes(): array |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $scope |
||
| 405 | * |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | public function hasScope(string $scope): bool |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param string $scope |
||
| 415 | * |
||
| 416 | * @return Authorization |
||
| 417 | */ |
||
| 418 | public function withoutScope(string $scope): Authorization |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param string $scope |
||
| 431 | * |
||
| 432 | * @return Authorization |
||
| 433 | */ |
||
| 434 | public function addScope(string $scope): Authorization |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return bool|null |
||
| 447 | */ |
||
| 448 | public function isAuthorized(): ?bool |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return Authorization |
||
| 455 | */ |
||
| 456 | public function allow(): Authorization |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return Authorization |
||
| 466 | */ |
||
| 467 | public function deny(): Authorization |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @param string $key |
||
| 477 | * |
||
| 478 | * @return bool |
||
| 479 | */ |
||
| 480 | public function hasData(string $key): bool |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param string $key |
||
| 487 | * |
||
| 488 | * @return mixed |
||
| 489 | */ |
||
| 490 | public function getData(string $key) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param string $key |
||
| 499 | * @param mixed $data |
||
| 500 | * |
||
| 501 | * @return Authorization |
||
| 502 | */ |
||
| 503 | public function withData(string $key, $data): Authorization |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @return null|ResourceServerInterface |
||
| 513 | */ |
||
| 514 | public function getResourceServer(): ?ResourceServerInterface |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param ResourceServerInterface $resourceServer |
||
| 521 | * |
||
| 522 | * @return Authorization |
||
| 523 | */ |
||
| 524 | public function withResourceServer(ResourceServerInterface $resourceServer): Authorization |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @return bool |
||
| 534 | */ |
||
| 535 | public function hasOfflineAccess(): bool |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param string $option |
||
| 553 | * @param mixed $value |
||
| 554 | * |
||
| 555 | * @return Authorization |
||
| 556 | */ |
||
| 557 | public function withConsentScreenOption(string $option, $value): Authorization |
||
| 564 | |||
| 565 | /** |
||
| 566 | * @param string $option |
||
| 567 | * |
||
| 568 | * @return Authorization |
||
| 569 | */ |
||
| 570 | public function withoutConsentScreenOption(string $option): Authorization |
||
| 581 | } |
||
| 582 |