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