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 ClientRepositoryInterface |
||
40 | */ |
||
41 | protected $clientRepository; |
||
42 | |||
43 | /** |
||
44 | * @var AccessTokenRepositoryInterface |
||
45 | */ |
||
46 | protected $accessTokenRepository; |
||
47 | |||
48 | /** |
||
49 | * @var ScopeRepositoryInterface |
||
50 | */ |
||
51 | protected $scopeRepository; |
||
52 | |||
53 | /** |
||
54 | * @var \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface |
||
55 | */ |
||
56 | protected $authCodeRepository; |
||
57 | |||
58 | /** |
||
59 | * @var \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface |
||
60 | */ |
||
61 | protected $refreshTokenRepository; |
||
62 | |||
63 | /** |
||
64 | * @var \League\OAuth2\Server\Repositories\UserRepositoryInterface |
||
65 | */ |
||
66 | protected $userRepository; |
||
67 | |||
68 | /** |
||
69 | * @var \DateInterval |
||
70 | */ |
||
71 | protected $refreshTokenTTL; |
||
72 | |||
73 | /** |
||
74 | * @param ClientRepositoryInterface $clientRepository |
||
75 | */ |
||
76 | public function setClientRepository(ClientRepositoryInterface $clientRepository) |
||
80 | |||
81 | /** |
||
82 | * @param AccessTokenRepositoryInterface $accessTokenRepository |
||
83 | */ |
||
84 | public function setAccessTokenRepository(AccessTokenRepositoryInterface $accessTokenRepository) |
||
88 | |||
89 | /** |
||
90 | * @param ScopeRepositoryInterface $scopeRepository |
||
91 | */ |
||
92 | public function setScopeRepository(ScopeRepositoryInterface $scopeRepository) |
||
96 | |||
97 | /** |
||
98 | * @param \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface $refreshTokenRepository |
||
99 | */ |
||
100 | public function setRefreshTokenRepository(RefreshTokenRepositoryInterface $refreshTokenRepository) |
||
104 | |||
105 | /** |
||
106 | * @param \League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface $authCodeRepository |
||
107 | */ |
||
108 | public function setAuthCodeRepository(AuthCodeRepositoryInterface $authCodeRepository) |
||
112 | |||
113 | /** |
||
114 | * @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository |
||
115 | */ |
||
116 | public function setUserRepository(UserRepositoryInterface $userRepository) |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function setRefreshTokenTTL(\DateInterval $refreshTokenTTL) |
||
128 | |||
129 | /** |
||
130 | * Validate the client. |
||
131 | * |
||
132 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
133 | * |
||
134 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
135 | * |
||
136 | * @return \League\OAuth2\Server\Entities\ClientEntityInterface |
||
137 | */ |
||
138 | protected function validateClient(ServerRequestInterface $request) |
||
182 | |||
183 | /** |
||
184 | * Validate scopes in the request. |
||
185 | * |
||
186 | * @param string $scopes |
||
187 | * @param string $redirectUri |
||
188 | * |
||
189 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
190 | * |
||
191 | * @return \League\OAuth2\Server\Entities\ScopeEntityInterface[] |
||
192 | */ |
||
193 | public function validateScopes( |
||
217 | |||
218 | /** |
||
219 | * Retrieve request parameter. |
||
220 | * |
||
221 | * @param string $parameter |
||
222 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
223 | * @param mixed $default |
||
224 | * |
||
225 | * @return null|string |
||
226 | */ |
||
227 | protected function getRequestParameter($parameter, ServerRequestInterface $request, $default = null) |
||
233 | |||
234 | /** |
||
235 | * Retrieve HTTP Basic Auth credentials with the Authorization header |
||
236 | * of a request. First index of the returned array is the username, |
||
237 | * second is the password (so list() will work). If the header does |
||
238 | * not exist, or is otherwise an invalid HTTP Basic header, return |
||
239 | * [null, null]. |
||
240 | * |
||
241 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
242 | * @return string[]|null[] |
||
243 | */ |
||
244 | protected function getBasicAuthCredentials(ServerRequestInterface $request) |
||
265 | |||
266 | /** |
||
267 | * Retrieve query string parameter. |
||
268 | * |
||
269 | * @param string $parameter |
||
270 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
271 | * @param mixed $default |
||
272 | * |
||
273 | * @return null|string |
||
274 | */ |
||
275 | protected function getQueryStringParameter($parameter, ServerRequestInterface $request, $default = null) |
||
279 | |||
280 | /** |
||
281 | * Retrieve cookie parameter. |
||
282 | * |
||
283 | * @param string $parameter |
||
284 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
285 | * @param mixed $default |
||
286 | * |
||
287 | * @return null|string |
||
288 | */ |
||
289 | protected function getCookieParameter($parameter, ServerRequestInterface $request, $default = null) |
||
293 | |||
294 | /** |
||
295 | * Retrieve server parameter. |
||
296 | * |
||
297 | * @param string $parameter |
||
298 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
299 | * @param mixed $default |
||
300 | * |
||
301 | * @return null|string |
||
302 | */ |
||
303 | protected function getServerParameter($parameter, ServerRequestInterface $request, $default = null) |
||
307 | |||
308 | /** |
||
309 | * Issue an access token. |
||
310 | * |
||
311 | * @param \DateInterval $accessTokenTTL |
||
312 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $client |
||
313 | * @param string $userIdentifier |
||
314 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
315 | * |
||
316 | * @return \League\OAuth2\Server\Entities\AccessTokenEntityInterface |
||
317 | */ |
||
318 | protected function issueAccessToken( |
||
338 | |||
339 | /** |
||
340 | * Issue an auth code. |
||
341 | * |
||
342 | * @param \DateInterval $authCodeTTL |
||
343 | * @param \League\OAuth2\Server\Entities\ClientEntityInterface $client |
||
344 | * @param string $userIdentifier |
||
345 | * @param string $redirectUri |
||
346 | * @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes |
||
347 | * |
||
348 | * @return \League\OAuth2\Server\Entities\AuthCodeEntityInterface |
||
349 | */ |
||
350 | protected function issueAuthCode( |
||
372 | |||
373 | /** |
||
374 | * @param \League\OAuth2\Server\Entities\AccessTokenEntityInterface $accessToken |
||
375 | * |
||
376 | * @return \League\OAuth2\Server\Entities\RefreshTokenEntityInterface |
||
377 | */ |
||
378 | protected function issueRefreshToken(AccessTokenEntityInterface $accessToken) |
||
389 | |||
390 | /** |
||
391 | * Generate a new unique identifier. |
||
392 | * |
||
393 | * @param int $length |
||
394 | * |
||
395 | * @throws \League\OAuth2\Server\Exception\OAuthServerException |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | protected function generateUniqueIdentifier($length = 40) |
||
414 | |||
415 | /** |
||
416 | * {@inheritdoc} |
||
417 | */ |
||
418 | public function canRespondToAccessTokenRequest(ServerRequestInterface $request) |
||
427 | |||
428 | /** |
||
429 | * {@inheritdoc} |
||
430 | */ |
||
431 | public function canRespondToAuthorizationRequest(ServerRequestInterface $request) |
||
435 | |||
436 | /** |
||
437 | * {@inheritdoc} |
||
438 | */ |
||
439 | public function validateAuthorizationRequest(ServerRequestInterface $request) |
||
443 | |||
444 | /** |
||
445 | * {@inheritdoc} |
||
446 | */ |
||
447 | public function completeAuthorizationRequest(AuthorizationRequest $authorizationRequest) |
||
451 | } |
||
452 |