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 |
||
| 17 | trait users { |
||
| 18 | /** |
||
| 19 | * 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 |
||
| 20 | * be returned) |
||
| 21 | * |
||
| 22 | * Data will be pre-processed with `reg_date_formatted` and `reg_ip_formatted` keys added |
||
| 23 | * |
||
| 24 | * @param \cs\Request $Request |
||
| 25 | * |
||
| 26 | * @return array |
||
| 27 | * |
||
| 28 | * @throws ExitException |
||
| 29 | */ |
||
| 30 | static function admin_users___get ($Request) { |
||
| 31 | $User = User::instance(); |
||
| 32 | $columns = static::admin_users___search_options()['columns']; |
||
| 33 | $id = $Request->route_ids(0); |
||
| 34 | $ids = $Request->query('ids'); |
||
| 35 | $search = $Request->query('search'); |
||
| 36 | if ($id) { |
||
| 37 | $result = static::admin_users___get_post_process( |
||
| 38 | $User->get($columns, $id) |
||
| 39 | ); |
||
| 40 | } elseif ($ids) { |
||
| 41 | $ids = _int(explode(',', $ids)); |
||
| 42 | $result = []; |
||
| 43 | foreach ($ids as $id) { |
||
| 44 | $result[] = static::admin_users___get_post_process( |
||
| 45 | $User->get($columns, $id) |
||
| 46 | ); |
||
| 47 | } |
||
| 48 | } elseif ($search) { |
||
| 49 | $result = _int($User->search_users($search)); |
||
| 50 | } else { |
||
| 51 | throw new ExitException(400); |
||
| 52 | } |
||
| 53 | if (!$result) { |
||
| 54 | throw new ExitException(404); |
||
| 55 | } |
||
| 56 | return $result; |
||
| 57 | } |
||
| 58 | protected static function admin_users___get_post_process ($data) { |
||
| 64 | /** |
||
| 65 | * Update user's data |
||
| 66 | * |
||
| 67 | * @param \cs\Request $Request |
||
| 68 | * |
||
| 69 | * @throws ExitException |
||
| 70 | */ |
||
| 71 | static function admin_users___patch ($Request) { |
||
| 114 | /** |
||
| 115 | * Add new user |
||
| 116 | * |
||
| 117 | * @param \cs\Request $Request |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | * |
||
| 121 | * @throws ExitException |
||
| 122 | */ |
||
| 123 | static function admin_users___post ($Request) { |
||
| 124 | $User = User::instance(); |
||
| 125 | $email = $Request->data('email'); |
||
| 126 | if (!$email) { |
||
| 127 | throw new ExitException(400); |
||
| 128 | } |
||
| 129 | $result = $User->registration($email, false, false); |
||
| 130 | if (!$result) { |
||
| 131 | throw new ExitException(500); |
||
| 132 | } |
||
| 133 | if ($result === 'exists') { |
||
| 134 | $L = new Prefix('system_admin_users_'); |
||
| 135 | throw new ExitException($L->user_already_exists, 400); |
||
| 136 | } |
||
| 137 | Response::instance()->code = 201; |
||
| 138 | return [ |
||
| 139 | 'login' => $User->get('login', $result['id']), |
||
| 140 | 'password' => $result['password'] |
||
| 141 | ]; |
||
| 142 | } |
||
| 143 | /** |
||
| 144 | * Advanced search for users (users data will be returned similar to GET method) |
||
| 145 | * |
||
| 146 | * @param \cs\Request $Request |
||
| 147 | * |
||
| 148 | * @return array |
||
|
1 ignored issue
–
show
|
|||
| 149 | * |
||
| 150 | * @throws ExitException |
||
| 151 | */ |
||
| 152 | static function admin_users___search ($Request) { |
||
| 153 | $options = $Request->data('mode', 'column', 'text', 'page', 'limit'); |
||
| 154 | if (!$options) { |
||
| 155 | throw new ExitException(400); |
||
| 156 | } |
||
| 157 | $mode = $options['mode']; |
||
| 158 | $column = $options['column']; |
||
| 159 | $text = $options['text']; |
||
| 160 | $page = (int)$options['page']; |
||
| 161 | $limit = (int)$options['limit']; |
||
| 162 | $search_options = static::admin_users___search_options(); |
||
| 163 | if ( |
||
| 164 | !in_array($mode, $search_options['modes']) || |
||
| 165 | ( |
||
| 166 | $column !== '' && |
||
| 167 | !in_array($column, $search_options['columns']) |
||
| 168 | ) |
||
| 169 | ) { |
||
| 170 | throw new ExitException(400); |
||
| 171 | } |
||
| 172 | $cdb = User::instance()->db(); |
||
| 173 | $where = static::admin_users___search_prepare_where($mode, $text, $column ?: $search_options['columns'], $cdb); |
||
| 174 | $count = $cdb->qfs( |
||
| 175 | [ |
||
| 176 | "SELECT COUNT(`id`) |
||
| 177 | FROM `[prefix]users` |
||
| 178 | WHERE $where" |
||
| 179 | ] |
||
| 180 | ); |
||
| 181 | if (!$count) { |
||
| 182 | throw new ExitException(404); |
||
| 183 | } |
||
| 184 | $where = str_replace('%', '%%', $where); |
||
| 185 | $ids = $cdb->qfas( |
||
| 186 | [ |
||
| 187 | "SELECT `id` |
||
| 188 | FROM `[prefix]users` |
||
| 189 | WHERE $where |
||
| 190 | ORDER BY `id` |
||
| 191 | LIMIT %d, %d", |
||
| 192 | ($page - 1) * $limit, |
||
| 193 | $limit |
||
| 194 | ] |
||
| 195 | ); |
||
| 196 | return [ |
||
| 197 | 'count' => $count, |
||
| 198 | 'users' => static::admin_users___search_get($ids, $search_options['columns']) |
||
| 199 | ]; |
||
| 200 | } |
||
| 201 | /** |
||
| 202 | * @param string $mode |
||
| 203 | * @param string $text |
||
| 204 | * @param string|string[] $column |
||
| 205 | * @param \cs\DB\_Abstract $cdb |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | protected static function admin_users___search_prepare_where ($mode, $text, $column, $cdb) { |
||
| 248 | /** |
||
| 249 | * @param string $where |
||
| 250 | * @param string|string[] $column |
||
| 251 | * @param string $text |
||
| 252 | * |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | protected static function admin_users___search_prepare_where_compose ($where, $column, $text) { |
||
| 265 | /** |
||
| 266 | * @param int[] $users |
||
| 267 | * @param string[] $columns |
||
| 268 | * |
||
| 269 | * @return array[] |
||
| 270 | */ |
||
| 271 | protected static function admin_users___search_get ($users, $columns) { |
||
| 286 | /** |
||
| 287 | * Get available search options |
||
| 288 | * |
||
| 289 | * @return string[][] |
||
| 290 | */ |
||
| 291 | static function admin_users___search_options () { |
||
| 319 | } |
||
| 320 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.