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 user ID should be unique and mandatory. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $userId; |
||
36 | |||
37 | /** |
||
38 | * @var DateTimeInterface|null |
||
39 | */ |
||
40 | private $expiry; |
||
41 | |||
42 | /** |
||
43 | * Token creation date (set automatically on save) |
||
44 | * @var DateTimeInterface|null |
||
45 | */ |
||
46 | private $created; |
||
47 | |||
48 | /** |
||
49 | * Token last modified date (set automatically on save and update) |
||
50 | * @var DateTimeInterface|null |
||
51 | */ |
||
52 | private $lastModified; |
||
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | public function key() |
||
61 | |||
62 | /** |
||
63 | * @param string $ident The token ident. |
||
64 | * @return AuthToken Chainable |
||
65 | */ |
||
66 | public function setIdent($ident) |
||
71 | |||
72 | /** |
||
73 | * @return string |
||
74 | */ |
||
75 | public function ident() |
||
79 | |||
80 | /** |
||
81 | * @param string $token The token. |
||
82 | * @return AuthToken Chainable |
||
83 | */ |
||
84 | public function setToken($token) |
||
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | public function token() |
||
97 | |||
98 | |||
99 | /** |
||
100 | * @param string $id The user ID. |
||
101 | * @throws InvalidArgumentException If the user ID is not a string. |
||
102 | * @return AuthToken Chainable |
||
103 | */ |
||
104 | public function setUserId($id) |
||
114 | |||
115 | /** |
||
116 | * @return string |
||
117 | */ |
||
118 | public function userId() |
||
122 | |||
123 | /** |
||
124 | * @param DateTimeInterface|string|null $expiry The date/time at object's creation. |
||
125 | * @throws InvalidArgumentException If the date/time is invalid. |
||
126 | * @return AuthToken Chainable |
||
127 | */ |
||
128 | View Code Duplication | public function setExpiry($expiry) |
|
145 | |||
146 | /** |
||
147 | * @return DateTimeInterface|null |
||
148 | */ |
||
149 | public function expiry() |
||
153 | |||
154 | /** |
||
155 | * @param DateTimeInterface|string|null $created The date/time at object's creation. |
||
156 | * @throws InvalidArgumentException If the date/time is invalid. |
||
157 | * @return AuthToken Chainable |
||
158 | */ |
||
159 | View Code Duplication | public function setCreated($created) |
|
176 | |||
177 | /** |
||
178 | * @return DateTimeInterface|null |
||
179 | */ |
||
180 | public function created() |
||
184 | |||
185 | /** |
||
186 | * @param DateTimeInterface|string|null $lastModified The last modified date/time. |
||
187 | * @throws InvalidArgumentException If the date/time is invalid. |
||
188 | * @return AuthToken Chainable |
||
189 | */ |
||
190 | View Code Duplication | public function setLastModified($lastModified) |
|
207 | |||
208 | /** |
||
209 | * @return DateTimeInterface|null |
||
210 | */ |
||
211 | public function lastModified() |
||
215 | |||
216 | /** |
||
217 | * Note: the `random_bytes()` function is new to PHP-7. Available in PHP 5 with `compat-random`. |
||
218 | * |
||
219 | * @param string $userId The user ID to generate the auth token from. |
||
220 | * @return AuthToken Chainable |
||
221 | */ |
||
222 | public function generate($userId) |
||
232 | |||
233 | /** |
||
234 | * @return AuthToken Chainable |
||
235 | */ |
||
236 | public function sendCookie() |
||
248 | |||
249 | /** |
||
250 | * @return array|null `['ident'=>'', 'token'=>''] |
||
251 | */ |
||
252 | public function getTokenDataFromCookie() |
||
272 | |||
273 | /** |
||
274 | * @param mixed $ident The auth-token identifier. |
||
275 | * @param string $token The token to validate against. |
||
276 | * @return mixed The user id. An empty string if no token match. |
||
277 | */ |
||
278 | public function getUserIdFromToken($ident, $token) |
||
311 | |||
312 | /** |
||
313 | * StorableTrait > preSave(): Called automatically before saving the object to source. |
||
314 | * @return boolean |
||
315 | */ |
||
316 | protected function preSave() |
||
328 | |||
329 | /** |
||
330 | * StorableTrait > preUpdate(): Called automatically before updating the object to source. |
||
331 | * @param array $properties The properties (ident) set for update. |
||
332 | * @return boolean |
||
333 | */ |
||
334 | protected function preUpdate(array $properties = null) |
||
342 | |||
343 | /** |
||
344 | * Something is seriously wrong: a cookie ident was in the database but with a tampered token. |
||
345 | * |
||
346 | * @return void |
||
347 | */ |
||
348 | protected function panic() |
||
366 | |||
367 | /** |
||
368 | * Create a new metadata object. |
||
369 | * |
||
370 | * @param array $data Optional metadata to merge on the object. |
||
371 | * @return AuthTokenMetadata |
||
372 | */ |
||
373 | protected function createMetadata(array $data = null) |
||
378 | |||
379 | /** |
||
380 | * Retrieve the class name of the metadata object. |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | protected function metadataClass() |
||
388 | } |
||
389 |