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 |
||
32 | abstract class AbstractGrant implements GrantTypeInterface |
||
33 | { |
||
34 | use EmitterAwareTrait, CryptTrait; |
||
35 | |||
36 | const SCOPE_DELIMITER_STRING = ' '; |
||
37 | |||
38 | /** |
||
39 | * @var ServerRequestInterface |
||
40 | */ |
||
41 | protected $request; |
||
42 | |||
43 | /** |
||
44 | * @var ClientRepositoryInterface |
||
45 | */ |
||
46 | protected $clientRepository; |
||
47 | |||
48 | /** |
||
49 | * @var AccessTokenRepositoryInterface |
||
50 | */ |
||
51 | protected $accessTokenRepository; |
||
52 | |||
53 | /** |
||
54 | * @var ScopeRepositoryInterface |
||
55 | */ |
||
56 | protected $scopeRepository; |
||
57 | |||
58 | /** |
||
59 | * @var \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface |
||
60 | */ |
||
61 | protected $authCodeRepository; |
||
62 | |||
63 | /** |
||
64 | * @var \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface |
||
65 | */ |
||
66 | protected $refreshTokenRepository; |
||
67 | |||
68 | /** |
||
69 | * @var \League\OAuth2\Server\Repositories\UserRepositoryInterface |
||
70 | */ |
||
71 | protected $userRepository; |
||
72 | |||
73 | /** |
||
74 | * @var \DateInterval |
||
75 | */ |
||
76 | protected $refreshTokenTTL; |
||
77 | |||
78 | /** |
||
79 | * @param ClientRepositoryInterface $clientRepository |
||
80 | */ |
||
81 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
||
85 | |||
86 | /** |
||
87 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
88 | */ |
||
89 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
||
93 | |||
94 | /** |
||
95 | * @param ScopeRepositoryInterface $scopeRepository |
||
96 | */ |
||
97 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
||
101 | |||
102 | /** |
||
103 | * @param \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface $refreshTokenRepository |
||
104 | */ |
||
105 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
||
109 | |||
110 | /** |
||
111 | * @param \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface $authCodeRepository |
||
112 | */ |
||
113 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
||
117 | |||
118 | /** |
||
119 | * @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository |
||
120 | */ |
||
121 | public function setUserRepository(UserRepositoryInterface $userRepository) |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function setRefreshTokenTTL(\DateInterval $refreshTokenTTL) |
||
133 | |||
134 | /** |
||
135 | * Validate the client. |
||
136 | * |
||
137 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
138 | * |
||
139 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
140 | * |
||
141 | * @return \League\OAuth2\Server\Entities\ClientEntityInterface |
||
142 | */ |
||
143 | protected function validateClient(ServerRequestInterface $request) |
||
192 | |||
193 | /** |
||
194 | * Validate scopes in the request. |
||
195 | * |
||
196 | * @param string $scopes |
||
197 | * @param string $redirectUri |
||
198 | * |
||
199 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
200 | * |
||
201 | * @return \League\OAuth2\Server\Entities\ScopeEntityInterface[] |
||
202 | */ |
||
203 | public function validateScopes( |
||
227 | |||
228 | /** |
||
229 | * Retrieve request parameter. |
||
230 | * |
||
231 | * @param string $parameter |
||
232 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
233 | * @param mixed $default |
||
234 | * |
||
235 | * @return null|string |
||
236 | */ |
||
237 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
||
243 | |||
244 | /** |
||
245 | * Retrieve query string parameter. |
||
246 | * |
||
247 | * @param string $parameter |
||
248 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
249 | * @param mixed $default |
||
250 | * |
||
251 | * @return null|string |
||
252 | */ |
||
253 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
||
257 | |||
258 | /** |
||
259 | * Retrieve cookie parameter. |
||
260 | * |
||
261 | * @param string $parameter |
||
262 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
263 | * @param mixed $default |
||
264 | * |
||
265 | * @return null|string |
||
266 | */ |
||
267 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
||
271 | |||
272 | /** |
||
273 | * Retrieve server parameter. |
||
274 | * |
||
275 | * @param string $parameter |
||
276 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
277 | * @param mixed $default |
||
278 | * |
||
279 | * @return null|string |
||
280 | */ |
||
281 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
||
285 | |||
286 | /** |
||
287 | * Issue an access token. |
||
288 | * |
||
289 | * @param \DateInterval $accessTokenTTL |
||
290 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $client |
||
291 | * @param string $userIdentifier |
||
292 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
293 | * |
||
294 | * @return \League\OAuth2\Server\Entities\AccessTokenEntityInterface |
||
295 | */ |
||
296 | protected function issueAccessToken( |
||
316 | |||
317 | /** |
||
318 | * Issue an auth code. |
||
319 | * |
||
320 | * @param \DateInterval $authCodeTTL |
||
321 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $client |
||
322 | * @param string $userIdentifier |
||
323 | * @param string $redirectUri |
||
324 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
325 | * |
||
326 | * @return \League\OAuth2\Server\Entities\AuthCodeEntityInterface |
||
327 | */ |
||
328 | protected function issueAuthCode( |
||
350 | |||
351 | /** |
||
352 | * @param \League\OAuth2\Server\Entities\AccessTokenEntityInterface $accessToken |
||
353 | * |
||
354 | * @return \League\OAuth2\Server\Entities\RefreshTokenEntityInterface |
||
355 | */ |
||
356 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
||
367 | |||
368 | /** |
||
369 | * Generate a new unique identifier. |
||
370 | * |
||
371 | * @param int $length |
||
372 | * |
||
373 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | protected function generateUniqueIdentifier($length = 40) |
||
392 | |||
393 | /** |
||
394 | * {@inheritdoc} |
||
395 | */ |
||
396 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
||
405 | |||
406 | /** |
||
407 | * {@inheritdoc} |
||
408 | */ |
||
409 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
413 | |||
414 | /** |
||
415 | * {@inheritdoc} |
||
416 | */ |
||
417 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
421 | |||
422 | /** |
||
423 | * {@inheritdoc} |
||
424 | */ |
||
425 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
429 | } |
||
430 |