Complex classes like AbstractGrant 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 AbstractGrant, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | abstract class AbstractGrant implements GrantTypeInterface |
||
| 37 | { |
||
| 38 | use EmitterAwareTrait, CryptTrait; |
||
| 39 | |||
| 40 | const SCOPE_DELIMITER_STRING = ' '; |
||
| 41 | |||
| 42 | const MAX_RANDOM_TOKEN_GENERATION_ATTEMPTS = 10; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var ClientRepositoryInterface |
||
| 46 | */ |
||
| 47 | protected $clientRepository; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var AccessTokenRepositoryInterface |
||
| 51 | */ |
||
| 52 | protected $accessTokenRepository; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var ScopeRepositoryInterface |
||
| 56 | */ |
||
| 57 | protected $scopeRepository; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var AuthCodeRepositoryInterface |
||
| 61 | */ |
||
| 62 | protected $authCodeRepository; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var RefreshTokenRepositoryInterface |
||
| 66 | */ |
||
| 67 | protected $refreshTokenRepository; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var UserRepositoryInterface |
||
| 71 | */ |
||
| 72 | protected $userRepository; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var \DateInterval |
||
| 76 | */ |
||
| 77 | protected $refreshTokenTTL; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \League\OAuth2\Server\CryptKey |
||
| 81 | */ |
||
| 82 | protected $privateKey; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param ClientRepositoryInterface $clientRepository |
||
| 86 | */ |
||
| 87 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
| 94 | */ |
||
| 95 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param ScopeRepositoryInterface $scopeRepository |
||
| 102 | */ |
||
| 103 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param RefreshTokenRepositoryInterface $refreshTokenRepository |
||
| 110 | */ |
||
| 111 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param AuthCodeRepositoryInterface $authCodeRepository |
||
| 118 | */ |
||
| 119 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param UserRepositoryInterface $userRepository |
||
| 126 | */ |
||
| 127 | public function setUserRepository(UserRepositoryInterface $userRepository) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function setRefreshTokenTTL(\DateInterval $refreshTokenTTL) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set the private key |
||
| 142 | * |
||
| 143 | * @param \League\OAuth2\Server\CryptKey $key |
||
| 144 | */ |
||
| 145 | public function setPrivateKey(CryptKey $key) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Validate the client. |
||
| 152 | * |
||
| 153 | * @param ServerRequestInterface $request |
||
| 154 | * |
||
| 155 | * @throws OAuthServerException |
||
| 156 | * |
||
| 157 | * @return ClientEntityInterface |
||
| 158 | */ |
||
| 159 | protected function validateClient(ServerRequestInterface $request) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get domain from the URL |
||
| 221 | * |
||
| 222 | * @param $redirectUri |
||
| 223 | * @param int $sub_domain_depth (2 - domain.com, 3 - subdomain.domain.com, etc.) |
||
| 224 | * @return null|string |
||
| 225 | */ |
||
| 226 | protected function getDomain($redirectUri, $sub_domain_depth = null) |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * Validate scopes in the request. |
||
| 244 | * |
||
| 245 | * @param string $scopes |
||
| 246 | * @param string $redirectUri |
||
| 247 | * |
||
| 248 | * @throws OAuthServerException |
||
| 249 | * |
||
| 250 | * @return ScopeEntityInterface[] |
||
| 251 | */ |
||
| 252 | public function validateScopes( |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Retrieve request parameter. |
||
| 280 | * |
||
| 281 | * @param string $parameter |
||
| 282 | * @param ServerRequestInterface $request |
||
| 283 | * @param mixed $default |
||
| 284 | * |
||
| 285 | * @return null|string |
||
| 286 | */ |
||
| 287 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
| 296 | * of a request. First index of the returned array is the username, |
||
| 297 | * second is the password (so list() will work). If the header does |
||
| 298 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
| 299 | * [null, null]. |
||
| 300 | * |
||
| 301 | * @param ServerRequestInterface $request |
||
| 302 | * |
||
| 303 | * @return string[]|null[] |
||
| 304 | */ |
||
| 305 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Retrieve query string parameter. |
||
| 329 | * |
||
| 330 | * @param string $parameter |
||
| 331 | * @param ServerRequestInterface $request |
||
| 332 | * @param mixed $default |
||
| 333 | * |
||
| 334 | * @return null|string |
||
| 335 | */ |
||
| 336 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Retrieve cookie parameter. |
||
| 343 | * |
||
| 344 | * @param string $parameter |
||
| 345 | * @param ServerRequestInterface $request |
||
| 346 | * @param mixed $default |
||
| 347 | * |
||
| 348 | * @return null|string |
||
| 349 | */ |
||
| 350 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Retrieve server parameter. |
||
| 357 | * |
||
| 358 | * @param string $parameter |
||
| 359 | * @param ServerRequestInterface $request |
||
| 360 | * @param mixed $default |
||
| 361 | * |
||
| 362 | * @return null|string |
||
| 363 | */ |
||
| 364 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Issue an access token. |
||
| 371 | * |
||
| 372 | * @param \DateInterval $accessTokenTTL |
||
| 373 | * @param ClientEntityInterface $client |
||
| 374 | * @param string $userIdentifier |
||
| 375 | * @param ScopeEntityInterface[] $scopes |
||
| 376 | * |
||
| 377 | * @throws OAuthServerException |
||
| 378 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 379 | * |
||
| 380 | * @return AccessTokenEntityInterface |
||
| 381 | */ |
||
| 382 | protected function issueAccessToken( |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Issue an auth code. |
||
| 416 | * |
||
| 417 | * @param \DateInterval $authCodeTTL |
||
| 418 | * @param ClientEntityInterface $client |
||
| 419 | * @param string $userIdentifier |
||
| 420 | * @param string $redirectUri |
||
| 421 | * @param ScopeEntityInterface[] $scopes |
||
| 422 | * |
||
| 423 | * @throws OAuthServerException |
||
| 424 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 425 | * |
||
| 426 | * @return AuthCodeEntityInterface |
||
| 427 | */ |
||
| 428 | protected function issueAuthCode( |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param AccessTokenEntityInterface $accessToken |
||
| 464 | * |
||
| 465 | * @throws OAuthServerException |
||
| 466 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
| 467 | * |
||
| 468 | * @return RefreshTokenEntityInterface |
||
| 469 | */ |
||
| 470 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Generate a new unique identifier. |
||
| 494 | * |
||
| 495 | * @param int $length |
||
| 496 | * |
||
| 497 | * @throws OAuthServerException |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | protected function generateUniqueIdentifier($length = 40) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * {@inheritdoc} |
||
| 519 | */ |
||
| 520 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * {@inheritdoc} |
||
| 532 | */ |
||
| 533 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * {@inheritdoc} |
||
| 540 | */ |
||
| 541 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * {@inheritdoc} |
||
| 548 | */ |
||
| 549 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
| 553 | } |
||
| 554 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: