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 |
||
17 | class JwtGuard implements Guard |
||
18 | { |
||
19 | |||
20 | use GuardHelpers; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $token; |
||
26 | |||
27 | /** |
||
28 | * @var TokenManager |
||
29 | */ |
||
30 | protected $tokenManager; |
||
31 | |||
32 | /** |
||
33 | * @var Request |
||
34 | */ |
||
35 | protected $request; |
||
36 | |||
37 | /** |
||
38 | * Indicates if the logout method has been called. |
||
39 | * |
||
40 | * @var bool |
||
41 | */ |
||
42 | protected $loggedOut = false; |
||
43 | |||
44 | /** |
||
45 | * JwtGuard constructor |
||
46 | * |
||
47 | * @param UserProvider $provider |
||
48 | * @param TokenManager $tokenManager |
||
49 | * @param Request|null $request |
||
50 | */ |
||
51 | public function __construct( |
||
60 | |||
61 | /** |
||
62 | * Get the currently authenticated user. |
||
63 | * |
||
64 | * @return \Illuminate\Contracts\Auth\Authenticatable|null |
||
65 | */ |
||
66 | public function user() |
||
91 | |||
92 | /** |
||
93 | * Retrieve the payload of the given token. |
||
94 | * |
||
95 | * @param string $token |
||
96 | * @return array |
||
97 | */ |
||
98 | protected function getPayloadOfToken($token) |
||
102 | |||
103 | /** |
||
104 | * Retrieve the user by the given payload. |
||
105 | * |
||
106 | * @param Payload $payload |
||
107 | * @return AuthenticatableContract|null |
||
108 | */ |
||
109 | protected function getUserByPayload(Payload $payload) |
||
113 | |||
114 | /** |
||
115 | * Determine whether the user has a token attached. |
||
116 | * |
||
117 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
118 | * @param Payload $payload |
||
119 | * @return bool |
||
120 | */ |
||
121 | protected function userHasToken($user, Payload $payload) |
||
125 | |||
126 | /** |
||
127 | * Remove given token from the given user |
||
128 | * |
||
129 | * @param $user |
||
130 | * @param Payload $payload |
||
131 | */ |
||
132 | protected function removeUserToken($user, Payload $payload) |
||
136 | |||
137 | /** |
||
138 | * Validate a user's credentials. |
||
139 | * |
||
140 | * @param array $credentials |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function validate(array $credentials = []) |
||
147 | |||
148 | /** |
||
149 | * Attempt to authenticate a user using the given credentials. |
||
150 | * |
||
151 | * @param array $credentials |
||
152 | * @param bool $login |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function attempt(array $credentials = [], $login = true) |
||
174 | |||
175 | /** |
||
176 | * Determine if the user matches the credentials. |
||
177 | * |
||
178 | * @param mixed $user |
||
179 | * @param array $credentials |
||
180 | * @return bool |
||
181 | */ |
||
182 | protected function hasValidCredentials($user, $credentials) |
||
186 | |||
187 | /** |
||
188 | * Fire the attempt event with the arguments. |
||
189 | * |
||
190 | * @param array $credentials |
||
191 | * @param bool $login |
||
192 | * @return void |
||
193 | */ |
||
194 | protected function fireAttemptEvent(array $credentials, $login) |
||
202 | |||
203 | /** |
||
204 | * Register an authentication attempt event listener. |
||
205 | * |
||
206 | * @param mixed $callback |
||
207 | * @return void |
||
208 | */ |
||
209 | public function attempting($callback) |
||
215 | |||
216 | /** |
||
217 | * Log a user into the application. |
||
218 | * |
||
219 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
220 | * @return void |
||
221 | */ |
||
222 | public function login(AuthenticatableContract $user) |
||
235 | |||
236 | /** |
||
237 | * generateTokenForUser method |
||
238 | * |
||
239 | * @param int $userId |
||
240 | * @return string |
||
241 | */ |
||
242 | protected function generateTokenForUserId($userId) |
||
250 | |||
251 | /** |
||
252 | * generateTokenForUser method |
||
253 | * |
||
254 | * @param string $token |
||
255 | * @return string |
||
256 | */ |
||
257 | protected function refreshTokenForUser($token) |
||
267 | |||
268 | /** |
||
269 | * Fire the login event if the dispatcher is set. |
||
270 | * |
||
271 | * @param \Illuminate\Contracts\Auth\Authenticatable $user |
||
272 | * @param bool $remember |
||
273 | * @return void |
||
274 | */ |
||
275 | protected function fireLoginEvent($user, $remember = false) |
||
281 | |||
282 | /** |
||
283 | * Log the given user ID into the application. |
||
284 | * |
||
285 | * @param mixed $id |
||
286 | * @return \Illuminate\Contracts\Auth\Authenticatable |
||
287 | */ |
||
288 | public function loginUsingId($id) |
||
294 | |||
295 | /** |
||
296 | * Log the user out of the application. |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | View Code Duplication | public function logout() |
|
326 | |||
327 | /** |
||
328 | * log this user out from every token |
||
329 | * |
||
330 | * @return void |
||
331 | */ |
||
332 | View Code Duplication | public function logoutAll() |
|
358 | |||
359 | /** |
||
360 | * Refresh user token |
||
361 | * |
||
362 | * @return string|null |
||
363 | */ |
||
364 | public function refreshToken() |
||
385 | |||
386 | /** |
||
387 | * setToken method |
||
388 | * |
||
389 | * @param string $token |
||
390 | */ |
||
391 | public function setToken($token) |
||
395 | |||
396 | /** |
||
397 | * getToken method |
||
398 | * |
||
399 | * @return null|string |
||
400 | */ |
||
401 | public function getToken() |
||
405 | |||
406 | /** |
||
407 | * getBearerToken method |
||
408 | * |
||
409 | * @return string|null |
||
410 | */ |
||
411 | protected function getBearerToken() |
||
419 | |||
420 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: