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 Token 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 Token, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | final class Token |
||
13 | { |
||
14 | const HEADER_TYP = 'typ'; |
||
15 | const HEADER_ALG = 'alg'; |
||
16 | |||
17 | const CLAIM_ISS = 'iss'; |
||
18 | const CLAIM_SUB = 'sub'; |
||
19 | const CLAIM_AUD = 'aud'; |
||
20 | const CLAIM_EXP = 'exp'; |
||
21 | const CLAIM_NBF = 'nbf'; |
||
22 | const CLAIM_IAT = 'iat'; |
||
23 | const CLAIM_JTI = 'jti'; |
||
24 | |||
25 | private $headers; |
||
26 | private $claims; |
||
27 | private $signature; |
||
28 | |||
29 | /** |
||
30 | * @param string $token |
||
31 | * |
||
32 | * @return self |
||
33 | * |
||
34 | * @throws MalformedToken |
||
35 | */ |
||
36 | 25 | public static function fromString($token) |
|
61 | |||
62 | /** |
||
63 | * @param array $claims |
||
64 | * @param array $headers |
||
65 | * @param string|null $signature |
||
66 | */ |
||
67 | 55 | public function __construct(array $claims, array $headers = [], $signature = null) |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 11 | public function __toString() |
|
81 | |||
82 | /** |
||
83 | * @param Signer $signer |
||
84 | * @param mixed $key |
||
85 | * |
||
86 | * @return self |
||
87 | */ |
||
88 | 29 | public function sign(Signer $signer, $key) |
|
95 | |||
96 | /** |
||
97 | * @param Signer $signer |
||
98 | * @param mixed $key |
||
99 | * |
||
100 | * @return self |
||
101 | * |
||
102 | * @throws UnverifiedToken |
||
103 | */ |
||
104 | 29 | public function verify(Signer $signer, $key) |
|
116 | |||
117 | /** |
||
118 | * @param Validator $validator |
||
119 | * |
||
120 | * @return self |
||
121 | * |
||
122 | * @throws ValidationFailed |
||
123 | */ |
||
124 | 26 | public function validate(Validator $validator) |
|
130 | |||
131 | /** |
||
132 | * @return array |
||
133 | */ |
||
134 | 32 | public function headers() |
|
138 | |||
139 | /** |
||
140 | * @return array |
||
141 | */ |
||
142 | 32 | public function claims() |
|
146 | |||
147 | /** |
||
148 | * @return string |
||
149 | */ |
||
150 | 30 | public function algorithm() |
|
154 | |||
155 | /** |
||
156 | * @return string|null |
||
157 | */ |
||
158 | 1 | public function issuer() |
|
162 | |||
163 | /** |
||
164 | * @return string|null |
||
165 | */ |
||
166 | 1 | public function subject() |
|
170 | |||
171 | /** |
||
172 | * @return string|null |
||
173 | */ |
||
174 | 1 | public function audience() |
|
178 | |||
179 | /** |
||
180 | * @return string|null |
||
181 | */ |
||
182 | 1 | public function id() |
|
186 | |||
187 | /** |
||
188 | * @return \DateTime|null |
||
189 | */ |
||
190 | 6 | public function expiresAt() |
|
194 | |||
195 | /** |
||
196 | * @return \DateTime|null |
||
197 | */ |
||
198 | 2 | public function issuedAt() |
|
202 | |||
203 | /** |
||
204 | * @return \DateTime|null |
||
205 | */ |
||
206 | 4 | public function notBefore() |
|
210 | |||
211 | /** |
||
212 | * @param \DateTime|null $currentTime |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | 5 | View Code Duplication | public function isExpired(\DateTime $currentTime = null) |
226 | |||
227 | /** |
||
228 | * @param \DateTime|null $currentTime |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | 3 | View Code Duplication | public function isAcceptable(\DateTime $currentTime = null) |
242 | |||
243 | /** |
||
244 | * @param string $key |
||
245 | * @param mixed|null $default |
||
246 | * |
||
247 | * @return mixed |
||
248 | */ |
||
249 | 32 | public function get($key, $default = null) |
|
257 | |||
258 | /** |
||
259 | * @param string $key |
||
260 | * @param mixed|null $default |
||
261 | * |
||
262 | * @return mixed |
||
263 | */ |
||
264 | 31 | public function getHeader($key, $default = null) |
|
272 | |||
273 | /** |
||
274 | * @param string $data |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | 30 | private static function encode($data) |
|
282 | |||
283 | /** |
||
284 | * @param string $data |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | 34 | private static function decode($data) |
|
292 | |||
293 | /** |
||
294 | * @param string $data |
||
295 | * |
||
296 | * @return array |
||
297 | * |
||
298 | * @throws \InvalidArgumentException |
||
299 | */ |
||
300 | 25 | private static function jsonDecode($data) |
|
310 | |||
311 | /** |
||
312 | * @return string |
||
313 | */ |
||
314 | 30 | private function createPayload() |
|
321 | |||
322 | /** |
||
323 | * @param string $claim |
||
324 | * |
||
325 | * @return \DateTime|null |
||
326 | */ |
||
327 | 12 | private function getDateClaim($claim) |
|
335 | } |
||
336 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.