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 |
||
| 23 | class Authorization |
||
|
|
|||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private $authorized; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Client |
||
| 32 | */ |
||
| 33 | private $client; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var UserAccount|null |
||
| 37 | */ |
||
| 38 | private $userAccount = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var DataBag |
||
| 42 | */ |
||
| 43 | private $metadata; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var null|bool |
||
| 47 | */ |
||
| 48 | private $userAccountFullyAuthenticated = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $data = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var null|array |
||
| 57 | */ |
||
| 58 | private $claims = null; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var TokenType|null |
||
| 62 | */ |
||
| 63 | private $tokenType = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var ResponseType] |
||
| 67 | */ |
||
| 68 | private $responseType = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var ResponseMode|null |
||
| 72 | */ |
||
| 73 | private $responseMode = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | private $queryParameters = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string|null |
||
| 82 | */ |
||
| 83 | private $redirectUri = null; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | private $consentScreenOptions = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $responseParameters = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | private $responseHeaders = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var null|ResourceServer |
||
| 102 | */ |
||
| 103 | private $resourceServer = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Authorization constructor. |
||
| 107 | * |
||
| 108 | * @param Client $client |
||
| 109 | * @param array $queryParameters |
||
| 110 | */ |
||
| 111 | private function __construct(Client $client, array $queryParameters) |
||
| 112 | { |
||
| 113 | $this->client = $client; |
||
| 114 | $this->queryParameters = $queryParameters; |
||
| 115 | $this->metadata = DataBag::create([]); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param Client $client |
||
| 120 | * @param array $queryParameters |
||
| 121 | * |
||
| 122 | * @return Authorization |
||
| 123 | */ |
||
| 124 | public static function create(Client $client, array $queryParameters): self |
||
| 125 | { |
||
| 126 | return new self($client, $queryParameters); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return array |
||
| 131 | */ |
||
| 132 | public function getQueryParams(): array |
||
| 133 | { |
||
| 134 | return $this->queryParameters; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $param |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | public function hasQueryParam(string $param): bool |
||
| 143 | { |
||
| 144 | return array_key_exists($param, $this->queryParameters); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $param |
||
| 149 | * |
||
| 150 | * @return mixed |
||
| 151 | */ |
||
| 152 | public function getQueryParam(string $param) |
||
| 153 | { |
||
| 154 | if (!$this->hasQueryParam($param)) { |
||
| 155 | throw new \InvalidArgumentException(sprintf('Invalid parameter "%s".', $param)); |
||
| 156 | } |
||
| 157 | |||
| 158 | return $this->queryParameters[$param]; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return Client |
||
| 163 | */ |
||
| 164 | public function getClient(): Client |
||
| 165 | { |
||
| 166 | return $this->client; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param TokenType $tokenType |
||
| 171 | * |
||
| 172 | * @return Authorization |
||
| 173 | */ |
||
| 174 | public function withTokenType(TokenType $tokenType): self |
||
| 175 | { |
||
| 176 | $this->tokenType = $tokenType; |
||
| 177 | |||
| 178 | return $this; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return null|TokenType |
||
| 183 | */ |
||
| 184 | public function getTokenType(): ? TokenType |
||
| 185 | { |
||
| 186 | return $this->tokenType; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param array $claims |
||
| 191 | * |
||
| 192 | * @return Authorization |
||
| 193 | */ |
||
| 194 | public function withClaims(array $claims): self |
||
| 195 | { |
||
| 196 | $this->claims = $claims; |
||
| 197 | |||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return null|array |
||
| 203 | */ |
||
| 204 | public function getClaims(): ?array |
||
| 205 | { |
||
| 206 | return $this->claims; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param ResponseType $responseType |
||
| 211 | * |
||
| 212 | * @return Authorization |
||
| 213 | */ |
||
| 214 | public function withResponseType(ResponseType $responseType): self |
||
| 215 | { |
||
| 216 | $this->responseType = $responseType; |
||
| 217 | |||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return ResponseType |
||
| 223 | */ |
||
| 224 | public function getResponseType(): ResponseType |
||
| 225 | { |
||
| 226 | return $this->responseType; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param ResponseMode $responseMode |
||
| 231 | * |
||
| 232 | * @return Authorization |
||
| 233 | */ |
||
| 234 | public function withResponseMode(ResponseMode $responseMode): self |
||
| 235 | { |
||
| 236 | $this->responseMode = $responseMode; |
||
| 237 | |||
| 238 | return $this; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return null|ResponseMode |
||
| 243 | */ |
||
| 244 | public function getResponseMode(): ? ResponseMode |
||
| 245 | { |
||
| 246 | return $this->responseMode; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $redirectUri |
||
| 251 | * |
||
| 252 | * @return Authorization |
||
| 253 | */ |
||
| 254 | public function withRedirectUri(string $redirectUri): self |
||
| 255 | { |
||
| 256 | $this->redirectUri = $redirectUri; |
||
| 257 | |||
| 258 | return $this; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return null|string |
||
| 263 | */ |
||
| 264 | public function getRedirectUri(): ? string |
||
| 265 | { |
||
| 266 | return $this->redirectUri; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param UserAccount $userAccount |
||
| 271 | * @param bool $isFullyAuthenticated |
||
| 272 | * |
||
| 273 | * @return Authorization |
||
| 274 | */ |
||
| 275 | public function withUserAccount(UserAccount $userAccount, bool $isFullyAuthenticated): self |
||
| 276 | { |
||
| 277 | $this->userAccount = $userAccount; |
||
| 278 | $this->userAccountFullyAuthenticated = $isFullyAuthenticated; |
||
| 279 | |||
| 280 | return $this; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return null|UserAccount |
||
| 285 | */ |
||
| 286 | public function getUserAccount(): ? UserAccount |
||
| 287 | { |
||
| 288 | return $this->userAccount; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $responseParameter |
||
| 293 | * @param mixed $value |
||
| 294 | * |
||
| 295 | * @return Authorization |
||
| 296 | */ |
||
| 297 | public function withResponseParameter(string $responseParameter, $value): self |
||
| 298 | { |
||
| 299 | $this->responseParameters[$responseParameter] = $value; |
||
| 300 | |||
| 301 | return $this; |
||
| 302 | } |
||
| 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 |
||
| 343 | { |
||
| 344 | $this->responseHeaders[$responseHeader] = $value; |
||
| 345 | |||
| 346 | return $this; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | public function getResponseHeaders(): array |
||
| 353 | { |
||
| 354 | return $this->responseHeaders; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @return bool|null |
||
| 359 | */ |
||
| 360 | public function isUserAccountFullyAuthenticated(): ? bool |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return string[] |
||
| 367 | */ |
||
| 368 | public function getPrompt(): array |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public function hasUiLocales(): bool |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @return string[] |
||
| 387 | */ |
||
| 388 | public function getUiLocales(): array |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $prompt |
||
| 395 | * |
||
| 396 | * @return bool |
||
| 397 | */ |
||
| 398 | public function hasPrompt(string $prompt): bool |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | public function isAuthorized(): bool |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return Authorization |
||
| 413 | */ |
||
| 414 | public function allow(): self |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return Authorization |
||
| 423 | */ |
||
| 424 | public function deny(): self |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $key |
||
| 433 | * |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | public function hasData(string $key): bool |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param string $key |
||
| 443 | * |
||
| 444 | * @return mixed |
||
| 445 | */ |
||
| 446 | public function getData(string $key) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param string $key |
||
| 457 | * @param mixed $data |
||
| 458 | * |
||
| 459 | * @return Authorization |
||
| 460 | */ |
||
| 461 | public function withData(string $key, $data): self |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return null|ResourceServer |
||
| 470 | */ |
||
| 471 | public function getResourceServer(): ? ResourceServer |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param ResourceServer $resourceServer |
||
| 478 | * |
||
| 479 | * @return Authorization |
||
| 480 | */ |
||
| 481 | public function withResourceServer(ResourceServer $resourceServer): self |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @param string $option |
||
| 490 | * @param mixed $value |
||
| 491 | * |
||
| 492 | * @return Authorization |
||
| 493 | */ |
||
| 494 | public function withConsentScreenOption(string $option, $value): self |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param string $option |
||
| 503 | * |
||
| 504 | * @return Authorization |
||
| 505 | */ |
||
| 506 | public function withoutConsentScreenOption(string $option): self |
||
| 516 | |||
| 517 | public function hasScope(): bool |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | public function getScope(): string |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param string $key |
||
| 532 | * @param mixed|null $data |
||
| 533 | * |
||
| 534 | * @return Authorization |
||
| 535 | */ |
||
| 536 | public function withMetadata(string $key, $data): self |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param string $key |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | public function hasMetadata(string $key): bool |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @param string $key |
||
| 555 | * |
||
| 556 | * @return bool |
||
| 557 | */ |
||
| 558 | public function getMetadata(string $key): bool |
||
| 566 | |||
| 567 | public function getMetadatas(): DataBag |
||
| 571 | } |
||
| 572 |