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:
| 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 JwtService |
||
| 32 | */ |
||
| 33 | protected $jwtService; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Request |
||
| 37 | */ |
||
| 38 | protected $request; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Indicates if the logout method has been called. |
||
| 42 | * |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $loggedOut = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * JwtGuard constructor |
||
| 49 | * |
||
| 50 | * @param UserProvider $provider |
||
| 51 | * @param JwtService $jwtService |
||
| 52 | * @param Request|null $request |
||
| 53 | */ |
||
| 54 | public function __construct( |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the currently authenticated user. |
||
| 66 | * |
||
| 67 | * @return \Illuminate\Contracts\Auth\Authenticatable|null |
||
| 68 | */ |
||
| 69 | public function user() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Retrieve the user by the given payload. |
||
| 89 | * |
||
| 90 | * @param string $token |
||
| 91 | * @return AuthenticatableContract|null |
||
| 92 | * @throws InaccessibleException |
||
| 93 | * @throws MalformedException |
||
| 94 | * @throws TokenExpiredException |
||
| 95 | * @throws InvalidTokenException |
||
| 96 | */ |
||
| 97 | protected function getUserByToken($token) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Validate a user's credentials. |
||
| 106 | * |
||
| 107 | * @param array $credentials |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | public function validate(array $credentials = []) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Attempt to authenticate a user using the given credentials. |
||
| 117 | * |
||
| 118 | * @param array $credentials |
||
| 119 | * @param bool $login |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | public function attempt(array $credentials = [], $login = true) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Determine if the user matches the credentials. |
||
| 144 | * |
||
| 145 | * @param mixed $user |
||
| 146 | * @param array $credentials |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | protected function hasValidCredentials($user, $credentials) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Fire the attempt event with the arguments. |
||
| 156 | * |
||
| 157 | * @param array $credentials |
||
| 158 | * @param bool $login |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | protected function fireAttemptEvent(array $credentials, $login) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Register an authentication attempt event listener. |
||
| 172 | * |
||
| 173 | * @param mixed $callback |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | public function attempting($callback) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Log a user into the application. |
||
| 185 | * |
||
| 186 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | public function login(AuthenticatableContract $user) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * generateTokenForUser method |
||
| 204 | * |
||
| 205 | * @param string $token |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | protected function refreshTokenForUser($token) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Fire the login event if the dispatcher is set. |
||
| 217 | * |
||
| 218 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
| 219 | * @param bool $remember |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | protected function fireLoginEvent($user, $remember = false) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Log the given user ID into the application. |
||
| 231 | * |
||
| 232 | * @param mixed $id |
||
| 233 | * @return \Illuminate\Contracts\Auth\Authenticatable |
||
| 234 | */ |
||
| 235 | public function loginUsingId($id) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Log the user out of the application. |
||
| 244 | * |
||
| 245 | * @return void |
||
| 246 | */ |
||
| 247 | View Code Duplication | public function logout() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * log this user out from every token |
||
| 271 | * |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | View Code Duplication | public function logoutAll() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Refresh user token |
||
| 301 | * |
||
| 302 | * @return string|null |
||
| 303 | */ |
||
| 304 | public function refreshToken() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * setToken method |
||
| 317 | * |
||
| 318 | * @param string $token |
||
| 319 | */ |
||
| 320 | public function setToken($token) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * getToken method |
||
| 327 | * |
||
| 328 | * @return null|string |
||
| 329 | */ |
||
| 330 | public function getToken() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * getBearerToken method |
||
| 337 | * |
||
| 338 | * @return string|null |
||
| 339 | */ |
||
| 340 | protected function getBearerToken() |
||
| 348 | |||
| 349 | } |
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: