Complex classes like AuthorizationRequestLoader 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 AuthorizationRequestLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class AuthorizationRequestLoader |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var ClientRepository |
||
| 38 | */ |
||
| 39 | private $clientRepository; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | private $requestObjectAllowed = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | private $requestObjectReferenceAllowed = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var JWKSet |
||
| 53 | */ |
||
| 54 | private $keyEncryptionKeySet = null; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | private $requireRequestUriRegistration = true; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | private $requireEncryption = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var null|HttpClient |
||
| 68 | */ |
||
| 69 | private $client = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var JWSVerifier |
||
| 73 | */ |
||
| 74 | private $jwsVerifier = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var ClaimCheckerManager |
||
| 78 | */ |
||
| 79 | private $claimCheckerManager = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var JWELoader |
||
| 83 | */ |
||
| 84 | private $jweLoader = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var null|JKUFactory |
||
| 88 | */ |
||
| 89 | private $jkuFactory = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * AuthorizationRequestLoader constructor. |
||
| 93 | * |
||
| 94 | * @param ClientRepository $clientRepository |
||
| 95 | */ |
||
| 96 | public function __construct(ClientRepository $clientRepository) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function isRequestUriRegistrationRequired(): bool |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function isRequestObjectSupportEnabled(): bool |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | public function isRequestObjectReferenceSupportEnabled(): bool |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string[] |
||
| 127 | */ |
||
| 128 | public function getSupportedSignatureAlgorithms(): array |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string[] |
||
| 135 | */ |
||
| 136 | public function getSupportedKeyEncryptionAlgorithms(): array |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return string[] |
||
| 143 | */ |
||
| 144 | public function getSupportedContentEncryptionAlgorithms(): array |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param JWSVerifier $jwsVerifier |
||
| 151 | * @param ClaimCheckerManager $claimCheckerManager |
||
| 152 | */ |
||
| 153 | public function enableSignedRequestObjectSupport(JWSVerifier $jwsVerifier, ClaimCheckerManager $claimCheckerManager) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param HttpClient $client |
||
| 162 | * @param bool $requireRequestUriRegistration |
||
| 163 | */ |
||
| 164 | public function enableRequestObjectReferenceSupport(HttpClient $client, bool $requireRequestUriRegistration) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param JWELoader $jweLoader |
||
| 176 | * @param JWKSet $keyEncryptionKeySet |
||
| 177 | * @param bool $requireEncryption |
||
| 178 | * |
||
| 179 | * @throws \InvalidArgumentException |
||
| 180 | */ |
||
| 181 | public function enableEncryptedRequestObjectSupport(JWELoader $jweLoader, JWKSet $keyEncryptionKeySet, bool $requireEncryption) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param JKUFactory $jkuFactory |
||
| 196 | */ |
||
| 197 | public function enableJkuSupport(JKUFactory $jkuFactory) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | public function isEncryptedRequestSupportEnabled(): bool |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param ServerRequestInterface $request |
||
| 212 | * |
||
| 213 | * @return Authorization |
||
| 214 | * |
||
| 215 | * @throws OAuth2Message |
||
| 216 | * @throws \Exception |
||
| 217 | * @throws \Http\Client\Exception |
||
| 218 | */ |
||
| 219 | public function load(ServerRequestInterface $request): Authorization |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param array $params |
||
| 236 | * @param Client $client |
||
| 237 | * |
||
| 238 | * @throws OAuth2Message |
||
| 239 | * |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | private function createFromRequestParameter(array $params, Client &$client = null): array |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param array $params |
||
| 260 | * @param Client $client |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | * |
||
| 264 | * @throws OAuth2Message |
||
| 265 | * @throws \Exception |
||
| 266 | * @throws \Http\Client\Exception |
||
| 267 | */ |
||
| 268 | private function createFromRequestUriParameter(array $params, Client &$client = null): array |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param array $params |
||
| 287 | * |
||
| 288 | * @throws \InvalidArgumentException |
||
| 289 | */ |
||
| 290 | private function checkIssuerAndClientId(array $params) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param Client $client |
||
| 301 | * @param string $requestUri |
||
| 302 | * |
||
| 303 | * @throws OAuth2Message |
||
| 304 | */ |
||
| 305 | private function checkRequestUri(Client $client, $requestUri) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param array $params |
||
| 327 | * @param string $request |
||
| 328 | * @param Client|null $client |
||
| 329 | * |
||
| 330 | * @throws OAuth2Message |
||
| 331 | * |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | private function loadRequestObject(array $params, string $request, Client &$client = null): array |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param string $request |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | * |
||
| 376 | * @throws OAuth2Message |
||
| 377 | */ |
||
| 378 | private function tryToLoadEncryptedRequest(string $request): string |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param JWS $jws |
||
| 402 | * @param Client $client |
||
| 403 | * |
||
| 404 | * @throws \InvalidArgumentException |
||
| 405 | */ |
||
| 406 | private function checkAlgorithms(JWS $jws, Client $client) |
||
| 415 | |||
| 416 | private function checkUsedAlgorithm(string $algorithm): void |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $url |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | * |
||
| 429 | * @throws OAuth2Message |
||
| 430 | * @throws \Exception |
||
| 431 | * @throws \Http\Client\Exception |
||
| 432 | */ |
||
| 433 | private function downloadContent(string $url): string |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param array $params |
||
| 451 | * |
||
| 452 | * @throws OAuth2Message |
||
| 453 | * |
||
| 454 | * @return Client |
||
| 455 | */ |
||
| 456 | private function getClient(array $params): Client |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param Client $client |
||
| 468 | * |
||
| 469 | * @return JWKSet |
||
| 470 | */ |
||
| 471 | private function getClientKeySet(Client $client): JWKSet |
||
| 508 | } |
||
| 509 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: