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 |
||
| 18 | trait users { |
||
| 19 | /** |
||
| 20 | * Get user's data or data of several specified groups if specified in ids query parameter or allows to search for users by login or email (users id will |
||
| 21 | * be returned) |
||
| 22 | * |
||
| 23 | * Data will be pre-processed with `reg_date_formatted` and `reg_ip_formatted` keys added |
||
| 24 | * |
||
| 25 | * @param \cs\Request $Request |
||
| 26 | * |
||
| 27 | * @throws ExitException |
||
| 28 | */ |
||
| 29 | static function admin_users___get ($Request) { |
||
| 30 | $User = User::instance(); |
||
| 31 | $Page = Page::instance(); |
||
| 32 | $columns = static::admin_users___search_options_get()['columns']; |
||
| 33 | if (isset($Request->route_ids[0])) { |
||
| 34 | $result = static::admin_users___get_post_process( |
||
| 35 | $User->get($columns, $Request->route_ids[0]) |
||
| 36 | ); |
||
| 37 | } elseif (isset($_GET['ids'])) { |
||
| 38 | $ids = _int(explode(',', $_GET['ids'])); |
||
| 39 | $result = []; |
||
| 40 | foreach ($ids as $id) { |
||
| 41 | $result[] = static::admin_users___get_post_process( |
||
| 42 | $User->get($columns, $id) |
||
| 43 | ); |
||
| 44 | } |
||
| 45 | } elseif (isset($_GET['search'])) { |
||
| 46 | $result = _int($User->search_users($_GET['search'])); |
||
| 47 | } else { |
||
| 48 | throw new ExitException(400); |
||
| 49 | } |
||
| 50 | if (!$result) { |
||
| 51 | throw new ExitException(404); |
||
| 52 | } |
||
| 53 | $Page->json($result); |
||
| 54 | } |
||
| 55 | protected static function admin_users___get_post_process ($data) { |
||
| 61 | /** |
||
| 62 | * Update user's data |
||
| 63 | * |
||
| 64 | * @param \cs\Request $Request |
||
| 65 | * |
||
| 66 | * @throws ExitException |
||
| 67 | */ |
||
| 68 | static function admin_users___patch ($Request) { |
||
| 110 | /** |
||
| 111 | * Add new user |
||
| 112 | * |
||
| 113 | * @throws ExitException |
||
| 114 | */ |
||
| 115 | static function admin_users___post () { |
||
| 140 | /** |
||
| 141 | * Advanced search for users (users data will be returned similar to GET method) |
||
| 142 | * |
||
| 143 | * @throws ExitException |
||
| 144 | */ |
||
| 145 | static function admin_users___search () { |
||
| 195 | /** |
||
| 196 | * @param string $mode |
||
| 197 | * @param string $text |
||
| 198 | * @param string|string[] $column |
||
| 199 | * @param \cs\DB\_Abstract $cdb |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | protected static function admin_users___search_prepare_where ($mode, $text, $column, $cdb) { |
||
| 242 | /** |
||
| 243 | * @param string $where |
||
| 244 | * @param string|string[] $column |
||
| 245 | * @param string $text |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | protected static function admin_users___search_prepare_where_compose ($where, $column, $text) { |
||
| 259 | /** |
||
| 260 | * @param int[] $users |
||
| 261 | * @param string[] $columns |
||
| 262 | * |
||
| 263 | * @return array[] |
||
|
|
|||
| 264 | */ |
||
| 265 | protected static function admin_users___search_get ($users, $columns) { |
||
| 280 | /** |
||
| 281 | * Get available search options |
||
| 282 | */ |
||
| 283 | static function admin_users___search_options () { |
||
| 288 | /* |
||
| 289 | * @return string[][] |
||
| 290 | */ |
||
| 291 | protected static function admin_users___search_options_get () { |
||
| 319 | } |
||
| 320 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.