Complex classes like TokenGuard 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 TokenGuard, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class TokenGuard |
||
22 | { |
||
23 | /** |
||
24 | * The resource server instance. |
||
25 | * |
||
26 | * @var \League\OAuth2\Server\ResourceServer |
||
27 | */ |
||
28 | protected $server; |
||
29 | |||
30 | /** |
||
31 | * The user provider implementation. |
||
32 | * |
||
33 | * @var \Rinvex\Oauth\OAuthUserProvider |
||
34 | */ |
||
35 | protected $provider; |
||
36 | |||
37 | /** |
||
38 | * The encrypter implementation. |
||
39 | * |
||
40 | * @var \Illuminate\Contracts\Encryption\Encrypter |
||
41 | */ |
||
42 | protected $encrypter; |
||
43 | |||
44 | /** |
||
45 | * Create a new token guard instance. |
||
46 | * |
||
47 | * @param \League\OAuth2\Server\ResourceServer $server |
||
48 | * @param \Rinvex\Oauth\OAuthUserProvider $provider |
||
49 | * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter |
||
50 | * |
||
51 | * @return void |
||
|
|||
52 | */ |
||
53 | public function __construct(ResourceServer $server, OAuthUserProvider $provider, Encrypter $encrypter) |
||
59 | |||
60 | /** |
||
61 | * Determine if the requested user type matches the client's user type. |
||
62 | * |
||
63 | * @param \Illuminate\Http\Request $request |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | protected function hasValidUserType(Request $request) |
||
77 | |||
78 | /** |
||
79 | * Get the user for the incoming request. |
||
80 | * |
||
81 | * @param \Illuminate\Http\Request $request |
||
82 | * |
||
83 | * @return mixed |
||
84 | */ |
||
85 | public function user(Request $request) |
||
93 | |||
94 | /** |
||
95 | * Get the client for the incoming request. |
||
96 | * |
||
97 | * @param \Illuminate\Http\Request $request |
||
98 | * |
||
99 | * @return mixed |
||
100 | */ |
||
101 | public function client(Request $request) |
||
119 | |||
120 | /** |
||
121 | * Authenticate the incoming request via the Bearer token. |
||
122 | * |
||
123 | * @param \Illuminate\Http\Request $request |
||
124 | * |
||
125 | * @return mixed |
||
126 | */ |
||
127 | protected function authenticateViaBearerToken($request) |
||
165 | |||
166 | /** |
||
167 | * Authenticate and get the incoming PSR-7 request via the Bearer token. |
||
168 | * |
||
169 | * @param \Illuminate\Http\Request $request |
||
170 | * |
||
171 | * @return \Psr\Http\Message\ServerRequestInterface |
||
172 | */ |
||
173 | protected function getPsrRequestViaBearerToken($request) |
||
193 | |||
194 | /** |
||
195 | * Authenticate the incoming request via the token cookie. |
||
196 | * |
||
197 | * @param \Illuminate\Http\Request $request |
||
198 | * |
||
199 | * @return mixed |
||
200 | */ |
||
201 | protected function authenticateViaCookie($request) |
||
214 | |||
215 | /** |
||
216 | * Get the token cookie via the incoming request. |
||
217 | * |
||
218 | * @param \Illuminate\Http\Request $request |
||
219 | * |
||
220 | * @return mixed |
||
221 | */ |
||
222 | protected function getTokenViaCookie($request) |
||
243 | |||
244 | /** |
||
245 | * Decode and decrypt the JWT token cookie. |
||
246 | * |
||
247 | * @param \Illuminate\Http\Request $request |
||
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | protected function decodeJwtTokenCookie($request) |
||
259 | |||
260 | /** |
||
261 | * Determine if the CSRF / header are valid and match. |
||
262 | * |
||
263 | * @param array $token |
||
264 | * @param \Illuminate\Http\Request $request |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | protected function validCsrf($token, $request) |
||
275 | |||
276 | /** |
||
277 | * Get the CSRF token from the request. |
||
278 | * |
||
279 | * @param \Illuminate\Http\Request $request |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | protected function getTokenFromRequest($request) |
||
293 | |||
294 | /** |
||
295 | * Determine if the cookie contents should be serialized. |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | public static function serialized() |
||
303 | } |
||
304 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.