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) |
||
188 | |||
189 | /** |
||
190 | * Validate redirect Uri |
||
191 | * |
||
192 | * @param $redirectUri |
||
193 | * @param $client |
||
194 | * @param $request |
||
195 | * @throws OAuthServerException |
||
196 | */ |
||
197 | public function checkRedirectUri($redirectUri, $client, $request){ |
||
229 | |||
230 | /** |
||
231 | * Get domain from the URL |
||
232 | * |
||
233 | * @param $redirectUri |
||
234 | * @param int $sub_domain_depth (2 - domain.com, 3 - subdomain.domain.com, etc.) |
||
235 | * @return null|string |
||
236 | */ |
||
237 | protected function getDomain($redirectUri, $sub_domain_depth = null) |
||
251 | |||
252 | |||
253 | /** |
||
254 | * Validate scopes in the request. |
||
255 | * |
||
256 | * @param string $scopes |
||
257 | * @param string $redirectUri |
||
258 | * |
||
259 | * @throws OAuthServerException |
||
260 | * |
||
261 | * @return ScopeEntityInterface[] |
||
262 | */ |
||
263 | public function validateScopes( |
||
288 | |||
289 | /** |
||
290 | * Retrieve request parameter. |
||
291 | * |
||
292 | * @param string $parameter |
||
293 | * @param ServerRequestInterface $request |
||
294 | * @param mixed $default |
||
295 | * |
||
296 | * @return null|string |
||
297 | */ |
||
298 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
||
304 | |||
305 | /** |
||
306 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
307 | * of a request. First index of the returned array is the username, |
||
308 | * second is the password (so list() will work). If the header does |
||
309 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
310 | * [null, null]. |
||
311 | * |
||
312 | * @param ServerRequestInterface $request |
||
313 | * |
||
314 | * @return string[]|null[] |
||
315 | */ |
||
316 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
||
337 | |||
338 | /** |
||
339 | * Retrieve query string parameter. |
||
340 | * |
||
341 | * @param string $parameter |
||
342 | * @param ServerRequestInterface $request |
||
343 | * @param mixed $default |
||
344 | * |
||
345 | * @return null|string |
||
346 | */ |
||
347 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
||
351 | |||
352 | /** |
||
353 | * Retrieve cookie parameter. |
||
354 | * |
||
355 | * @param string $parameter |
||
356 | * @param ServerRequestInterface $request |
||
357 | * @param mixed $default |
||
358 | * |
||
359 | * @return null|string |
||
360 | */ |
||
361 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
||
365 | |||
366 | /** |
||
367 | * Retrieve server parameter. |
||
368 | * |
||
369 | * @param string $parameter |
||
370 | * @param ServerRequestInterface $request |
||
371 | * @param mixed $default |
||
372 | * |
||
373 | * @return null|string |
||
374 | */ |
||
375 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
||
379 | |||
380 | /** |
||
381 | * Issue an access token. |
||
382 | * |
||
383 | * @param \DateInterval $accessTokenTTL |
||
384 | * @param ClientEntityInterface $client |
||
385 | * @param string $userIdentifier |
||
386 | * @param ScopeEntityInterface[] $scopes |
||
387 | * |
||
388 | * @throws OAuthServerException |
||
389 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
390 | * |
||
391 | * @return AccessTokenEntityInterface |
||
392 | */ |
||
393 | protected function issueAccessToken( |
||
424 | |||
425 | /** |
||
426 | * Issue an auth code. |
||
427 | * |
||
428 | * @param \DateInterval $authCodeTTL |
||
429 | * @param ClientEntityInterface $client |
||
430 | * @param string $userIdentifier |
||
431 | * @param string $redirectUri |
||
432 | * @param ScopeEntityInterface[] $scopes |
||
433 | * |
||
434 | * @throws OAuthServerException |
||
435 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
436 | * |
||
437 | * @return AuthCodeEntityInterface |
||
438 | */ |
||
439 | protected function issueAuthCode( |
||
472 | |||
473 | /** |
||
474 | * @param AccessTokenEntityInterface $accessToken |
||
475 | * |
||
476 | * @throws OAuthServerException |
||
477 | * @throws UniqueTokenIdentifierConstraintViolationException |
||
478 | * |
||
479 | * @return RefreshTokenEntityInterface |
||
480 | */ |
||
481 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
||
502 | |||
503 | /** |
||
504 | * Generate a new unique identifier. |
||
505 | * |
||
506 | * @param int $length |
||
507 | * |
||
508 | * @throws OAuthServerException |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | protected function generateUniqueIdentifier($length = 40) |
||
527 | |||
528 | /** |
||
529 | * {@inheritdoc} |
||
530 | */ |
||
531 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
||
540 | |||
541 | /** |
||
542 | * {@inheritdoc} |
||
543 | */ |
||
544 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
548 | |||
549 | /** |
||
550 | * {@inheritdoc} |
||
551 | */ |
||
552 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
556 | |||
557 | /** |
||
558 | * {@inheritdoc} |
||
559 | */ |
||
560 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
564 | } |
||
565 |
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: