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 | abstract class AbstractAuthToken extends AbstractModel implements |
||
23 | AuthTokenInterface, |
||
24 | TimestampableInterface |
||
25 | { |
||
26 | use TimestampableTrait; |
||
27 | |||
28 | /** |
||
29 | * The token key. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $ident; |
||
34 | |||
35 | /** |
||
36 | * The token value. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $token; |
||
41 | |||
42 | /** |
||
43 | * The related user ID. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $userId; |
||
48 | |||
49 | /** |
||
50 | * The token's expiration date. |
||
51 | * |
||
52 | * @var DateTimeInterface|null |
||
53 | */ |
||
54 | private $expiry; |
||
55 | |||
56 | /** |
||
57 | * @return string |
||
58 | */ |
||
59 | public function key() |
||
63 | |||
64 | /** |
||
65 | * @param string $ident The token ident. |
||
66 | * @return self |
||
67 | */ |
||
68 | public function setIdent($ident) |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | public function getIdent() |
||
81 | |||
82 | /** |
||
83 | * @param string $token The token. |
||
84 | * @return self |
||
85 | */ |
||
86 | public function setToken($token) |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | public function getToken() |
||
99 | |||
100 | /** |
||
101 | * @param string $id The user ID. |
||
102 | * @throws InvalidArgumentException If the user ID is not a string. |
||
103 | * @return self |
||
104 | */ |
||
105 | public function setUserId($id) |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getUserId() |
||
124 | |||
125 | /** |
||
126 | * @param DateTimeInterface|string|null $expiry The date/time at object's creation. |
||
127 | * @throws InvalidArgumentException If the date/time is invalid. |
||
128 | * @return self |
||
129 | */ |
||
130 | View Code Duplication | public function setExpiry($expiry) |
|
150 | |||
151 | /** |
||
152 | * @return DateTimeInterface|null |
||
153 | */ |
||
154 | public function getExpiry() |
||
158 | |||
159 | /** |
||
160 | * Generate auth token data for the given user ID. |
||
161 | * |
||
162 | * Note: the `random_bytes()` function is new to PHP-7. Available in PHP 5 with `compat-random`. |
||
163 | * |
||
164 | * @param string $userId The user ID to generate the auth token from. |
||
165 | * @return self |
||
166 | */ |
||
167 | public function generate($userId) |
||
182 | |||
183 | /** |
||
184 | * Determine if authentication by token is supported. |
||
185 | * |
||
186 | * @return boolean |
||
187 | */ |
||
188 | public function isEnabled() |
||
192 | |||
193 | /** |
||
194 | * Determine if authentication by token should be only over HTTPS. |
||
195 | * |
||
196 | * @return boolean |
||
197 | */ |
||
198 | public function isSecure() |
||
202 | |||
203 | /** |
||
204 | * @param mixed $ident The auth-token identifier. |
||
205 | * @param string $token The token to validate against. |
||
206 | * @return mixed The user id. An empty string if no token match. |
||
207 | */ |
||
208 | public function getUserIdFromToken($ident, $token) |
||
253 | |||
254 | /** |
||
255 | * Delete all auth tokens from storage for the current user. |
||
256 | * |
||
257 | * @return void |
||
258 | */ |
||
259 | public function deleteUserAuthTokens() |
||
278 | |||
279 | /** |
||
280 | * Something is seriously wrong: a auth ident was in the database but with a tampered token. |
||
281 | * |
||
282 | * @return void |
||
283 | */ |
||
284 | protected function panic() |
||
292 | |||
293 | /** |
||
294 | * @see \Charcoal\Source\StorableTrait::preSave() |
||
295 | * |
||
296 | * @return boolean |
||
297 | */ |
||
298 | protected function preSave() |
||
309 | |||
310 | /** |
||
311 | * @see \Charcoal\Source\StorableTrait::preUpdate() |
||
312 | * |
||
313 | * @param array $properties The properties (ident) set for update. |
||
314 | * @return boolean |
||
315 | */ |
||
316 | protected function preUpdate(array $properties = null) |
||
324 | |||
325 | /** |
||
326 | * @return void |
||
327 | */ |
||
328 | protected function touchToken() |
||
335 | |||
336 | /** |
||
337 | * Create a new metadata object. |
||
338 | * |
||
339 | * @param array $data Optional metadata to merge on the object. |
||
340 | * @return AuthTokenMetadata |
||
341 | */ |
||
342 | protected function createMetadata(array $data = null) |
||
347 | |||
348 | /** |
||
349 | * Retrieve the class name of the metadata object. |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | protected function metadataClass() |
||
357 | } |
||
358 |
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.