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 TokenType|null |
||
| 57 | */ |
||
| 58 | private $tokenType = null; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var ResponseType] |
||
| 62 | */ |
||
| 63 | private $responseType = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var ResponseMode|null |
||
| 67 | */ |
||
| 68 | private $responseMode = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $queryParameters = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string|null |
||
| 77 | */ |
||
| 78 | private $redirectUri = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private $consentScreenOptions = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | private $responseParameters = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $responseHeaders = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var null|ResourceServer |
||
| 97 | */ |
||
| 98 | private $resourceServer = null; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Authorization constructor. |
||
| 102 | * |
||
| 103 | * @param Client $client |
||
| 104 | * @param array $queryParameters |
||
| 105 | */ |
||
| 106 | private function __construct(Client $client, array $queryParameters) |
||
| 107 | { |
||
| 108 | $this->client = $client; |
||
| 109 | $this->queryParameters = $queryParameters; |
||
| 110 | $this->metadata = DataBag::create([]); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param Client $client |
||
| 115 | * @param array $queryParameters |
||
| 116 | * |
||
| 117 | * @return Authorization |
||
| 118 | */ |
||
| 119 | public static function create(Client $client, array $queryParameters): self |
||
| 120 | { |
||
| 121 | return new self($client, $queryParameters); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function getQueryParams(): array |
||
| 128 | { |
||
| 129 | return $this->queryParameters; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $param |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public function hasQueryParam(string $param): bool |
||
| 138 | { |
||
| 139 | return array_key_exists($param, $this->queryParameters); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $param |
||
| 144 | * |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | public function getQueryParam(string $param) |
||
| 148 | { |
||
| 149 | if (!$this->hasQueryParam($param)) { |
||
| 150 | throw new \InvalidArgumentException(sprintf('Invalid parameter "%s".', $param)); |
||
| 151 | } |
||
| 152 | |||
| 153 | return $this->queryParameters[$param]; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return Client |
||
| 158 | */ |
||
| 159 | public function getClient(): Client |
||
| 160 | { |
||
| 161 | return $this->client; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param TokenType $tokenType |
||
| 166 | * |
||
| 167 | * @return Authorization |
||
| 168 | */ |
||
| 169 | public function withTokenType(TokenType $tokenType): self |
||
| 170 | { |
||
| 171 | $this->tokenType = $tokenType; |
||
| 172 | |||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return null|TokenType |
||
| 178 | */ |
||
| 179 | public function getTokenType(): ? TokenType |
||
| 180 | { |
||
| 181 | return $this->tokenType; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param ResponseType $responseType |
||
| 186 | * |
||
| 187 | * @return Authorization |
||
| 188 | */ |
||
| 189 | public function withResponseType(ResponseType $responseType): self |
||
| 190 | { |
||
| 191 | $this->responseType = $responseType; |
||
| 192 | |||
| 193 | return $this; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return ResponseType |
||
| 198 | */ |
||
| 199 | public function getResponseType(): ResponseType |
||
| 200 | { |
||
| 201 | return $this->responseType; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param ResponseMode $responseMode |
||
| 206 | * |
||
| 207 | * @return Authorization |
||
| 208 | */ |
||
| 209 | public function withResponseMode(ResponseMode $responseMode): self |
||
| 210 | { |
||
| 211 | $this->responseMode = $responseMode; |
||
| 212 | |||
| 213 | return $this; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @return null|ResponseMode |
||
| 218 | */ |
||
| 219 | public function getResponseMode(): ? ResponseMode |
||
| 220 | { |
||
| 221 | return $this->responseMode; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $redirectUri |
||
| 226 | * |
||
| 227 | * @return Authorization |
||
| 228 | */ |
||
| 229 | public function withRedirectUri(string $redirectUri): self |
||
| 230 | { |
||
| 231 | $this->redirectUri = $redirectUri; |
||
| 232 | |||
| 233 | return $this; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return null|string |
||
| 238 | */ |
||
| 239 | public function getRedirectUri(): ? string |
||
| 240 | { |
||
| 241 | return $this->redirectUri; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param UserAccount $userAccount |
||
| 246 | * @param bool $isFullyAuthenticated |
||
| 247 | * |
||
| 248 | * @return Authorization |
||
| 249 | */ |
||
| 250 | public function withUserAccount(UserAccount $userAccount, bool $isFullyAuthenticated): self |
||
| 251 | { |
||
| 252 | $this->userAccount = $userAccount; |
||
| 253 | $this->userAccountFullyAuthenticated = $isFullyAuthenticated; |
||
| 254 | |||
| 255 | return $this; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return null|UserAccount |
||
| 260 | */ |
||
| 261 | public function getUserAccount(): ? UserAccount |
||
| 262 | { |
||
| 263 | return $this->userAccount; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $responseParameter |
||
| 268 | * @param mixed $value |
||
| 269 | * |
||
| 270 | * @return Authorization |
||
| 271 | */ |
||
| 272 | public function withResponseParameter(string $responseParameter, $value): self |
||
| 273 | { |
||
| 274 | $this->responseParameters[$responseParameter] = $value; |
||
| 275 | |||
| 276 | return $this; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | public function getResponseParameters(): array |
||
| 283 | { |
||
| 284 | return $this->responseParameters; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $param |
||
| 289 | * |
||
| 290 | * @return mixed |
||
| 291 | */ |
||
| 292 | public function getResponseParameter(string $param) |
||
| 293 | { |
||
| 294 | if (!$this->hasResponseParameter($param)) { |
||
| 295 | throw new \InvalidArgumentException(sprintf('Invalid response parameter "%s".', $param)); |
||
| 296 | } |
||
| 297 | |||
| 298 | return $this->getResponseParameters()[$param]; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $param |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | public function hasResponseParameter(string $param): bool |
||
| 307 | { |
||
| 308 | return array_key_exists($param, $this->getResponseParameters()); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $responseHeader |
||
| 313 | * @param mixed $value |
||
| 314 | * |
||
| 315 | * @return Authorization |
||
| 316 | */ |
||
| 317 | public function withResponseHeader(string $responseHeader, $value): self |
||
| 318 | { |
||
| 319 | $this->responseHeaders[$responseHeader] = $value; |
||
| 320 | |||
| 321 | return $this; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return array |
||
| 326 | */ |
||
| 327 | public function getResponseHeaders(): array |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return bool|null |
||
| 334 | */ |
||
| 335 | public function isUserAccountFullyAuthenticated(): ? bool |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return string[] |
||
| 342 | */ |
||
| 343 | public function getPrompt(): array |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return bool |
||
| 354 | */ |
||
| 355 | public function hasUiLocales(): bool |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @return string[] |
||
| 362 | */ |
||
| 363 | public function getUiLocales(): array |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param string $prompt |
||
| 370 | * |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | public function hasPrompt(string $prompt): bool |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | public function isAuthorized(): bool |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return Authorization |
||
| 388 | */ |
||
| 389 | public function allow(): self |
||
| 390 | { |
||
| 391 | $this->authorized = true; |
||
| 392 | |||
| 393 | return $this; |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return Authorization |
||
| 398 | */ |
||
| 399 | public function deny(): self |
||
| 400 | { |
||
| 401 | $this->authorized = false; |
||
| 402 | |||
| 403 | return $this; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $key |
||
| 408 | * |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | public function hasData(string $key): bool |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $key |
||
| 418 | * |
||
| 419 | * @return mixed |
||
| 420 | */ |
||
| 421 | public function getData(string $key) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param string $key |
||
| 432 | * @param mixed $data |
||
| 433 | * |
||
| 434 | * @return Authorization |
||
| 435 | */ |
||
| 436 | public function withData(string $key, $data): self |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return null|ResourceServer |
||
| 445 | */ |
||
| 446 | public function getResourceServer(): ? ResourceServer |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param ResourceServer $resourceServer |
||
| 453 | * |
||
| 454 | * @return Authorization |
||
| 455 | */ |
||
| 456 | public function withResourceServer(ResourceServer $resourceServer): self |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param string $option |
||
| 465 | * @param mixed $value |
||
| 466 | * |
||
| 467 | * @return Authorization |
||
| 468 | */ |
||
| 469 | public function withConsentScreenOption(string $option, $value): self |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @param string $option |
||
| 478 | * |
||
| 479 | * @return Authorization |
||
| 480 | */ |
||
| 481 | public function withoutConsentScreenOption(string $option): self |
||
| 491 | |||
| 492 | public function hasScope(): bool |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @return string |
||
| 499 | */ |
||
| 500 | public function getScope(): string |
||
| 504 | |||
| 505 | public function getMetadata(): DataBag |
||
| 509 | } |
||
| 510 |