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 AuthToken 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 AuthToken, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class AuthToken extends AbstractModel |
||
|
|||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $ident; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $token; |
||
29 | |||
30 | /** |
||
31 | * The username should be unique and mandatory. |
||
32 | * @var string |
||
33 | */ |
||
34 | private $username; |
||
35 | |||
36 | /** |
||
37 | * @var DateTimeInterface|null |
||
38 | */ |
||
39 | private $expiry; |
||
40 | |||
41 | /** |
||
42 | * Token creation date (set automatically on save) |
||
43 | * @var DateTimeInterface|null |
||
44 | */ |
||
45 | private $created; |
||
46 | |||
47 | /** |
||
48 | * Token last modified date (set automatically on save and update) |
||
49 | * @var DateTimeInterface|null |
||
50 | */ |
||
51 | private $lastModified; |
||
52 | |||
53 | /** |
||
54 | * @return string |
||
55 | */ |
||
56 | public function key() |
||
60 | |||
61 | /** |
||
62 | * @param string $ident The token ident. |
||
63 | * @return AuthToken Chainable |
||
64 | */ |
||
65 | public function setIdent($ident) |
||
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | */ |
||
74 | public function ident() |
||
78 | |||
79 | /** |
||
80 | * @param string $token The token. |
||
81 | * @return AuthToken Chainable |
||
82 | */ |
||
83 | public function setToken($token) |
||
88 | |||
89 | /** |
||
90 | * @return string |
||
91 | */ |
||
92 | public function token() |
||
96 | |||
97 | |||
98 | /** |
||
99 | * Force a lowercase username |
||
100 | * |
||
101 | * @param string $username The username (also the login name). |
||
102 | * @throws InvalidArgumentException If the username is not a string. |
||
103 | * @return AuthToken Chainable |
||
104 | */ |
||
105 | View Code Duplication | public function setUsername($username) |
|
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | public function username() |
||
123 | |||
124 | /** |
||
125 | * @param DateTimeInterface|string|null $expiry The date/time at object's creation. |
||
126 | * @throws InvalidArgumentException If the date/time is invalid. |
||
127 | * @return AuthToken Chainable |
||
128 | */ |
||
129 | View Code Duplication | public function setExpiry($expiry) |
|
146 | |||
147 | /** |
||
148 | * @return DateTimeInterface|null |
||
149 | */ |
||
150 | public function expiry() |
||
154 | |||
155 | /** |
||
156 | * @param DateTimeInterface|string|null $created The date/time at object's creation. |
||
157 | * @throws InvalidArgumentException If the date/time is invalid. |
||
158 | * @return AuthToken Chainable |
||
159 | */ |
||
160 | View Code Duplication | public function setCreated($created) |
|
177 | |||
178 | /** |
||
179 | * @return DateTimeInterface|null |
||
180 | */ |
||
181 | public function created() |
||
185 | |||
186 | /** |
||
187 | * @param DateTimeInterface|string|null $lastModified The last modified date/time. |
||
188 | * @throws InvalidArgumentException If the date/time is invalid. |
||
189 | * @return AuthToken Chainable |
||
190 | */ |
||
191 | View Code Duplication | public function setLastModified($lastModified) |
|
208 | |||
209 | /** |
||
210 | * @return DateTimeInterface|null |
||
211 | */ |
||
212 | public function lastModified() |
||
216 | |||
217 | /** |
||
218 | * Note: the `random_bytes()` function is new to PHP-7. Available in PHP 5 with `compat-random`. |
||
219 | * |
||
220 | * @param string $username The username to generate the auth token from. |
||
221 | * @return AuthToken Chainable |
||
222 | */ |
||
223 | public function generate($username) |
||
233 | |||
234 | /** |
||
235 | * @return AuthToken Chainable |
||
236 | */ |
||
237 | public function sendCookie() |
||
249 | |||
250 | /** |
||
251 | * @return array|null `['ident'=>'', 'token'=>''] |
||
252 | */ |
||
253 | public function getTokenDataFromCookie() |
||
273 | |||
274 | /** |
||
275 | * @param mixed $ident The auth-token identifier. |
||
276 | * @param string $token The token key to validate against. |
||
277 | * @return mixed The user id. |
||
278 | */ |
||
279 | public function getUserId($ident, $token) |
||
283 | |||
284 | /** |
||
285 | * @param mixed $ident The auth-token identifier (username). |
||
286 | * @param string $token The token to validate against. |
||
287 | * @return mixed The user id. An empty string if no token match. |
||
288 | */ |
||
289 | public function getUsernameFromToken($ident, $token) |
||
322 | |||
323 | /** |
||
324 | * StorableTrait > preSave(): Called automatically before saving the object to source. |
||
325 | * @return boolean |
||
326 | */ |
||
327 | protected function preSave() |
||
339 | |||
340 | /** |
||
341 | * StorableTrait > preUpdate(): Called automatically before updating the object to source. |
||
342 | * @param array $properties The properties (ident) set for update. |
||
343 | * @return boolean |
||
344 | */ |
||
345 | protected function preUpdate(array $properties = null) |
||
353 | |||
354 | /** |
||
355 | * Something is seriously wrong: a cookie ident was in the database but with a tampered token. |
||
356 | * |
||
357 | * @return void |
||
358 | */ |
||
359 | protected function panic() |
||
377 | |||
378 | /** |
||
379 | * Create a new metadata object. |
||
380 | * |
||
381 | * @param array $data Optional metadata to merge on the object. |
||
382 | * @return AuthTokenMetadata |
||
383 | */ |
||
384 | protected function createMetadata(array $data = null) |
||
389 | |||
390 | /** |
||
391 | * Retrieve the class name of the metadata object. |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | protected function metadataClass() |
||
399 | } |
||
400 |