Complex classes like JwtGuard 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 JwtGuard, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 20 | class JwtGuard implements Guard | ||
| 21 | { | ||
| 22 | |||
| 23 | use GuardHelpers; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * @var string | ||
| 27 | */ | ||
| 28 | protected $token; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @var bool | ||
| 32 | */ | ||
| 33 | protected $isTokenRefreshable = false; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @var JwtService | ||
| 37 | */ | ||
| 38 | protected $jwtService; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @var Request | ||
| 42 | */ | ||
| 43 | protected $request; | ||
| 44 | |||
| 45 | /** | ||
| 46 | * Indicates if the logout method has been called. | ||
| 47 | * | ||
| 48 | * @var bool | ||
| 49 | */ | ||
| 50 | protected $loggedOut = false; | ||
| 51 | |||
| 52 | /** | ||
| 53 | * JwtGuard constructor | ||
| 54 | * | ||
| 55 | * @param UserProvider $provider | ||
| 56 | * @param JwtService $jwtService | ||
| 57 | * @param Request|null $request | ||
| 58 | */ | ||
| 59 | public function __construct( | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Get the currently authenticated user. | ||
| 71 | * | ||
| 72 | * @return \Illuminate\Contracts\Auth\Authenticatable|null | ||
| 73 | */ | ||
| 74 | public function user() | ||
| 98 | |||
| 99 | /** | ||
| 100 | * Retrieve the user by the given payload. | ||
| 101 | * | ||
| 102 | * @param string $token | ||
| 103 | * @return AuthenticatableContract|null | ||
| 104 | * @throws InaccessibleException | ||
| 105 | * @throws MalformedException | ||
| 106 | * @throws TokenExpiredException | ||
| 107 | * @throws InvalidTokenException | ||
| 108 | */ | ||
| 109 | protected function getUserByToken($token) | ||
| 120 | |||
| 121 | /** | ||
| 122 | * Validate a user's credentials. | ||
| 123 | * | ||
| 124 | * @param array $credentials | ||
| 125 | * @return bool | ||
| 126 | */ | ||
| 127 | public function validate(array $credentials = []) | ||
| 131 | |||
| 132 | /** | ||
| 133 | * Attempt to authenticate a user using the given credentials. | ||
| 134 | * | ||
| 135 | * @param array $credentials | ||
| 136 | * @param bool $login | ||
| 137 | * @return bool | ||
| 138 | */ | ||
| 139 | public function attempt(array $credentials = [], $login = true) | ||
| 158 | |||
| 159 | /** | ||
| 160 | * Determine if the user matches the credentials. | ||
| 161 | * | ||
| 162 | * @param mixed $user | ||
| 163 | * @param array $credentials | ||
| 164 | * @return bool | ||
| 165 | */ | ||
| 166 | protected function hasValidCredentials($user, $credentials) | ||
| 170 | |||
| 171 | /** | ||
| 172 | * Fire the attempt event with the arguments. | ||
| 173 | * | ||
| 174 | * @param array $credentials | ||
| 175 | * @param bool $login | ||
| 176 | * @return void | ||
| 177 | */ | ||
| 178 | protected function fireAttemptEvent(array $credentials, $login) | ||
| 186 | |||
| 187 | /** | ||
| 188 | * Register an authentication attempt event listener. | ||
| 189 | * | ||
| 190 | * @param mixed $callback | ||
| 191 | * @return void | ||
| 192 | */ | ||
| 193 | public function attempting($callback) | ||
| 199 | |||
| 200 | /** | ||
| 201 | * Log a user into the application. | ||
| 202 | * | ||
| 203 | * @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
| 204 | * @return void | ||
| 205 | */ | ||
| 206 | public function login(AuthenticatableContract $user) | ||
| 224 | |||
| 225 | /** | ||
| 226 | * generateTokenForUser method | ||
| 227 | * | ||
| 228 | * @param string $token | ||
| 229 | * @return string | ||
| 230 | */ | ||
| 231 | protected function refreshTokenForUser($token) | ||
| 241 | |||
| 242 | /** | ||
| 243 | * Fire the login event if the dispatcher is set. | ||
| 244 | * | ||
| 245 | * @param \Illuminate\Contracts\Auth\Authenticatable $user | ||
| 246 | * @param bool $remember | ||
| 247 | * @return void | ||
| 248 | */ | ||
| 249 | protected function fireLoginEvent($user, $remember = false) | ||
| 255 | |||
| 256 | /** | ||
| 257 | * Log the given user ID into the application. | ||
| 258 | * | ||
| 259 | * @param mixed $id | ||
| 260 | * @return \Illuminate\Contracts\Auth\Authenticatable | ||
| 261 | */ | ||
| 262 | public function loginUsingId($id) | ||
| 268 | |||
| 269 | /** | ||
| 270 | * Log the user out of the application. | ||
| 271 | * | ||
| 272 | * @return void | ||
| 273 | */ | ||
| 274 | public function logout() | ||
| 286 | |||
| 287 | /** | ||
| 288 | * log this user out from every token | ||
| 289 | * | ||
| 290 | * @return void | ||
| 291 | */ | ||
| 292 | public function logoutAll() | ||
| 307 | |||
| 308 | /** | ||
| 309 | * logoutCurrentUser method | ||
| 310 | */ | ||
| 311 | protected function logoutCurrentUser() | ||
| 324 | |||
| 325 | /** | ||
| 326 | * Refresh user token | ||
| 327 | * | ||
| 328 | * @return string|null | ||
| 329 | */ | ||
| 330 | public function refreshToken() | ||
| 340 | |||
| 341 | /** | ||
| 342 | * setToken method | ||
| 343 | * | ||
| 344 | * @param string $token | ||
| 345 | */ | ||
| 346 | public function setToken($token) | ||
| 350 | |||
| 351 | /** | ||
| 352 | * getToken method | ||
| 353 | * | ||
| 354 | * @return null|string | ||
| 355 | */ | ||
| 356 | public function getToken() | ||
| 360 | |||
| 361 | /** | ||
| 362 | * isTokenRefreshable method | ||
| 363 | */ | ||
| 364 | public function isTokenRefreshable() | ||
| 368 | |||
| 369 | /** | ||
| 370 | * getBearerToken method | ||
| 371 | * | ||
| 372 | * @return string|null | ||
| 373 | */ | ||
| 374 | protected function getBearerToken() | ||
| 384 | |||
| 385 | } | 
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: