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 |
||
22 | class JWTHandler |
||
23 | { |
||
24 | /** |
||
25 | * @var Builder |
||
26 | */ |
||
27 | private $builder; |
||
28 | |||
29 | /** |
||
30 | * @var \Lcobucci\JWT\Signer |
||
31 | */ |
||
32 | private $signer; |
||
33 | |||
34 | /** |
||
35 | * @var Parser |
||
36 | */ |
||
37 | private $parser; |
||
38 | |||
39 | /** |
||
40 | * @var ValidationData |
||
41 | */ |
||
42 | private $validationData; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $privateKey; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $publicKey; |
||
53 | |||
54 | /** |
||
55 | * @param JWTConfig $config The auth / JWT configuration. |
||
56 | */ |
||
57 | public function __construct(JWTConfig $config) |
||
66 | |||
67 | /** |
||
68 | * Builds and signs a token with a "uid" claim. |
||
69 | * |
||
70 | * @param string $userId The user to generate the token for. |
||
71 | * @return Token |
||
72 | */ |
||
73 | public function generateTokenForUserId($userId) |
||
80 | |||
81 | /** |
||
82 | * Retrieves, parses and validates the token from request's HTTP_AUTHORIZATION header. |
||
83 | * |
||
84 | * @param Request $request A PSR-7 Request. |
||
85 | * @throws Exception If there is no authorization headers in request or the token is invalid. |
||
86 | * @return Token |
||
87 | */ |
||
88 | public function getTokenFromRequest(Request $request) |
||
105 | |||
106 | /** |
||
107 | * Validates and verifies a token. |
||
108 | * |
||
109 | * @param Token $token The token to validate and verify. |
||
110 | * @return boolean |
||
111 | */ |
||
112 | public function isTokenValid(Token $token) |
||
124 | |||
125 | /** |
||
126 | * Retrieves the uid claim (user id) from a token. |
||
127 | * |
||
128 | * @param Token $token The Token to load the user from. |
||
129 | * @throws Exception If the token does not have a user (uid claim) or the user can not be loaded. |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getUserIdFromToken(Token $token) |
||
142 | |||
143 | /** |
||
144 | * @param JWTConfig $config The JWT / auth configuration. |
||
145 | * @return Builder |
||
146 | */ |
||
147 | private function createBuilder(JWTConfig $config) |
||
159 | |||
160 | /** |
||
161 | * @return Parser |
||
162 | */ |
||
163 | private function createParser() |
||
167 | |||
168 | /** |
||
169 | * @return \Lcobucci\JWT\Signer |
||
170 | */ |
||
171 | private function createSigner() |
||
175 | |||
176 | /** |
||
177 | * @param JWTConfig $config The JWT / auth configuration. |
||
178 | * @return ValidationData |
||
179 | */ |
||
180 | private function createValidationData(JWTConfig $config) |
||
188 | |||
189 | /** |
||
190 | * @param JWTConfig $config The JWT / auth configuration. |
||
191 | * @throws Exception If the key is not set in config or not a string. |
||
192 | * @return string |
||
193 | */ |
||
194 | View Code Duplication | private function loadPrivateKey(JWTConfig $config) |
|
209 | |||
210 | /** |
||
211 | * @param JWTConfig $config The JWT / auth configuration. |
||
212 | * @throws Exception If the key is not set in config or not a string. |
||
213 | * @return string |
||
214 | */ |
||
215 | View Code Duplication | private function loadPublicKey(JWTConfig $config) |
|
231 | } |
||
232 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.