Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | View Code Duplication | public function logout() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * log this user out from every token |
||
| 298 | * |
||
| 299 | * @return void |
||
| 300 | */ |
||
| 301 | View Code Duplication | public function logoutAll() |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Refresh user token |
||
| 328 | * |
||
| 329 | * @return string|null |
||
| 330 | */ |
||
| 331 | public function refreshToken() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * setToken method |
||
| 344 | * |
||
| 345 | * @param string $token |
||
| 346 | */ |
||
| 347 | public function setToken($token) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * getToken method |
||
| 354 | * |
||
| 355 | * @return null|string |
||
| 356 | */ |
||
| 357 | public function getToken() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * isTokenRefreshable method |
||
| 364 | */ |
||
| 365 | public function isTokenRefreshable() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * getBearerToken method |
||
| 372 | * |
||
| 373 | * @return string|null |
||
| 374 | */ |
||
| 375 | protected function getBearerToken() |
||
| 385 | |||
| 386 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: