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 Users 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 Users, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Users extends DB |
||
17 | { |
||
18 | /** |
||
19 | * @var \FFCMS\Mappers\Users user mapper |
||
20 | */ |
||
21 | public $mapper; |
||
22 | |||
23 | /** |
||
24 | * @var \FFCMS\Mappers\UsersData mapper for user's data |
||
25 | */ |
||
26 | protected $dataMapper; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * initialize with array of params, 'db' and 'logger' can be injected |
||
31 | * |
||
32 | * @param null|\Log $logger |
||
33 | * @param null|\DB\SQL $db |
||
34 | */ |
||
35 | public function __construct(array $params = [], \Log $logger = null, \DB\SQL $db = null) |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Get the associated data mapper |
||
45 | * |
||
46 | * @return \FFCMS\Mappers\Users |
||
47 | */ |
||
48 | public function &getMapper() |
||
52 | |||
53 | |||
54 | /** |
||
55 | * Get the associated data mapper |
||
56 | * |
||
57 | * @return \FFCMS\Mappers\UsersData |
||
58 | */ |
||
59 | public function &getDataMapper() |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Get the user mapper by UUID |
||
67 | * |
||
68 | * @param string $uuid User UUID |
||
69 | * @return \FFCMS\Mappers\Users |
||
70 | */ |
||
71 | public function &getUserByUUID(string $uuid) |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Get the user mapper by email address |
||
81 | * |
||
82 | * @param string $email email address |
||
83 | * @return \FFCMS\Mappers\Users |
||
84 | */ |
||
85 | public function &getUserByEmail(string $email) |
||
91 | |||
92 | |||
93 | /** |
||
94 | * Fetch the users data, optionally only by specified keys |
||
95 | * |
||
96 | * @param string $uuid |
||
97 | * @param array $keys |
||
98 | * @return array $data |
||
99 | */ |
||
100 | public function getUserDetails(string $uuid, array $keys = []): array |
||
132 | |||
133 | |||
134 | /** |
||
135 | * Fetch the users profile, optionally only by specified keys |
||
136 | * |
||
137 | * @param string $uuid |
||
138 | * @param array $keys |
||
139 | * @return array $data |
||
140 | */ |
||
141 | public function getProfile(string $uuid, array $keys = []): array |
||
148 | |||
149 | |||
150 | /** |
||
151 | * Save user's data |
||
152 | * |
||
153 | * @param string $uuid |
||
154 | * @param array $keys |
||
155 | */ |
||
156 | public function saveData(string $uuid, array $keys = []): array |
||
185 | |||
186 | |||
187 | /** |
||
188 | * Perform a successful post-login action if the user is in the group 'user' |
||
189 | * and is with the status 'closed', 'suspended', 'cancelled' |
||
190 | * |
||
191 | * @param string optional $uuid logout the current mapper user or specified one |
||
192 | * @return boolean true/false if login permitted |
||
193 | */ |
||
194 | public function login($uuid = null): bool |
||
222 | |||
223 | |||
224 | /** |
||
225 | * Perform a logout action on the given user uuid |
||
226 | * |
||
227 | * @param string optional $uuid logout the current mapper user or specified one |
||
228 | */ |
||
229 | public function logout($uuid = null): bool |
||
241 | |||
242 | |||
243 | /** |
||
244 | * Create a template object for a new user |
||
245 | * |
||
246 | * @param \FFCMS\Mappers\Users|null $m User Mapper |
||
247 | * @link http://fatfreeframework.com/sql-mapper |
||
248 | */ |
||
249 | public function &newUserTemplate($m = null): \FFCMS\Mappers\Users |
||
278 | |||
279 | |||
280 | /** |
||
281 | * Register a new user from a newly populated usersMapper object |
||
282 | * |
||
283 | * @param Mappers\Users|null $m User Mapper |
||
284 | * @link http://fatfreeframework.com/sql-mapper |
||
285 | */ |
||
286 | public function register($m = null) |
||
310 | |||
311 | |||
312 | /** |
||
313 | * save (insert/update) a row to the users_data table |
||
314 | * $data['value'] is automatically encoded or serialized if array/object |
||
315 | * |
||
316 | * @param array $data existing data to update |
||
317 | * @return Users $data newly saved data |
||
318 | */ |
||
319 | public function saveKey(array $data = []) |
||
361 | |||
362 | } |
||
363 |