Complex classes like databases 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 databases, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | trait databases { |
||
| 18 | /** |
||
| 19 | * Get array of databases |
||
| 20 | */ |
||
| 21 | static function admin_databases_get () { |
||
| 48 | /** |
||
| 49 | * Update database or database mirror settings |
||
| 50 | * |
||
| 51 | * @param int[] $route_ids |
||
| 52 | * |
||
| 53 | * @throws ExitException |
||
| 54 | */ |
||
| 55 | static function admin_databases_patch ($route_ids) { |
||
| 56 | if ( |
||
| 57 | !isset($route_ids[0], $_POST['host'], $_POST['type'], $_POST['prefix'], $_POST['name'], $_POST['user'], $_POST['password'], $_POST['charset']) || |
||
| 58 | !strlen($_POST['host']) || |
||
| 59 | !in_array($_POST['type'], static::admin_databases_get_engines()) |
||
| 60 | ) { |
||
| 61 | throw new ExitException(400); |
||
| 62 | } |
||
| 63 | $Config = Config::instance(); |
||
| 64 | $databases = &$Config->db; |
||
| 65 | $database_index = $route_ids[0]; |
||
| 66 | if (!isset($databases[$database_index])) { |
||
| 67 | throw new ExitException(404); |
||
| 68 | } |
||
| 69 | $database = &$databases[$database_index]; |
||
| 70 | // Maybe, we are changing database mirror |
||
| 71 | if (isset($route_ids[1])) { |
||
| 72 | if (!isset($database['mirrors'][$route_ids[1]])) { |
||
| 73 | throw new ExitException(404); |
||
| 74 | } |
||
| 75 | $database = &$database['mirrors'][$route_ids[1]]; |
||
| 76 | } elseif ($database_index == 0) { |
||
| 77 | throw new ExitException(400); |
||
| 78 | } |
||
| 79 | $database['host'] = $_POST['host']; |
||
| 80 | $database['type'] = $_POST['type']; |
||
| 81 | $database['prefix'] = $_POST['prefix']; |
||
| 82 | $database['name'] = $_POST['name']; |
||
| 83 | $database['user'] = $_POST['user']; |
||
| 84 | $database['password'] = $_POST['password']; |
||
| 85 | $database['charset'] = $_POST['charset']; |
||
| 86 | if (!$Config->save()) { |
||
| 87 | throw new ExitException(500); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | /** |
||
| 91 | * Create database or database mirror |
||
| 92 | * |
||
| 93 | * @param int[] $route_ids |
||
| 94 | * |
||
| 95 | * @throws ExitException |
||
| 96 | */ |
||
| 97 | static function admin_databases_post ($route_ids) { |
||
| 98 | if ( |
||
| 99 | !isset($_POST['mirror'], $_POST['host'], $_POST['type'], $_POST['prefix'], $_POST['name'], $_POST['user'], $_POST['password'], $_POST['charset']) || |
||
| 100 | !strlen($_POST['host']) || |
||
| 101 | !in_array($_POST['type'], static::admin_databases_get_engines()) |
||
| 102 | ) { |
||
| 103 | throw new ExitException(400); |
||
| 104 | } |
||
| 105 | $Config = Config::instance(); |
||
| 106 | $databases = &$Config->db; |
||
| 107 | // Maybe, we are adding database mirror |
||
| 108 | if (isset($route_ids[0])) { |
||
| 109 | if (!isset($databases[$route_ids[0]])) { |
||
| 110 | throw new ExitException(404); |
||
| 111 | } |
||
| 112 | $databases = &$databases[$route_ids[0]]['mirrors']; |
||
| 113 | } |
||
| 114 | $databases[] = [ |
||
| 115 | 'mirror' => $_POST['mirror'], |
||
| 116 | 'host' => $_POST['host'], |
||
| 117 | 'type' => $_POST['type'], |
||
| 118 | 'prefix' => $_POST['prefix'], |
||
| 119 | 'name' => $_POST['name'], |
||
| 120 | 'user' => $_POST['user'], |
||
| 121 | 'password' => $_POST['password'], |
||
| 122 | 'charset' => $_POST['charset'] |
||
| 123 | ]; |
||
| 124 | if (!$Config->save()) { |
||
| 125 | throw new ExitException(500); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | /** |
||
| 129 | * Delete database or database mirror |
||
| 130 | * |
||
| 131 | * @param int[] $route_ids |
||
| 132 | * |
||
| 133 | * @throws ExitException |
||
| 134 | */ |
||
| 135 | static function admin_databases_delete ($route_ids) { |
||
| 136 | if (!isset($route_ids[0])) { |
||
| 137 | throw new ExitException(400); |
||
| 138 | } |
||
| 139 | $Config = Config::instance(); |
||
| 140 | $databases = &$Config->db; |
||
| 141 | $database_index = $route_ids[0]; |
||
| 142 | if (!isset($databases[$database_index])) { |
||
| 143 | throw new ExitException(404); |
||
| 144 | } |
||
| 145 | // Maybe, we are deleting database mirror |
||
| 146 | if (isset($route_ids[1])) { |
||
| 147 | if (!isset($databases[$database_index]['mirrors'][$route_ids[1]])) { |
||
| 148 | throw new ExitException(404); |
||
| 149 | } |
||
| 150 | unset($databases[$database_index]['mirrors'][$route_ids[1]]); |
||
| 151 | } elseif ($database_index == 0) { |
||
| 152 | throw new ExitException(400); |
||
| 153 | } else { |
||
| 154 | static::admin_databases_delete_check_usages($database_index); |
||
| 155 | unset($databases[$database_index]); |
||
| 156 | } |
||
| 157 | if (!$Config->save()) { |
||
| 158 | throw new ExitException(500); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | protected static function admin_databases_delete_check_usages ($database_index) { |
||
| 162 | $Config = Config::instance(); |
||
| 163 | $used_by = []; |
||
| 164 | foreach ($Config->components['modules'] as $module => $module_data) { |
||
| 165 | if (isset($module_data['db']) && is_array($module_data['db'])) { |
||
| 166 | foreach ($module_data['db'] as $index) { |
||
| 167 | if ($index == $database_index) { |
||
| 168 | $used_by[] = $module; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | if ($used_by) { |
||
| 174 | throw new ExitException( |
||
| 175 | Language::instance()->db_used_by_modules.': '.implode(', ', $used_by), |
||
| 176 | 409 |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | /** |
||
| 181 | * Get array of available database engines |
||
| 182 | */ |
||
| 183 | static function admin_databases_engines () { |
||
| 184 | Page::instance()->json( |
||
| 185 | static::admin_databases_get_engines() |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | /** |
||
| 189 | * @return string[] |
||
| 190 | */ |
||
| 191 | protected static function admin_databases_get_engines () { |
||
| 192 | return _mb_substr(get_files_list(ENGINES.'/DB', '/^[^_].*?\.php$/i', 'f'), 0, -4); |
||
| 193 | } |
||
| 194 | /** |
||
| 195 | * Test database connection |
||
| 196 | * |
||
| 197 | * @throws ExitException |
||
| 198 | */ |
||
| 199 | static function admin_databases_test () { |
||
| 224 | /** |
||
| 225 | * Get database settings |
||
| 226 | */ |
||
| 227 | static function admin_databases_get_settings () { |
||
| 228 | $Config = Config::instance(); |
||
| 229 | Page::instance()->json( |
||
| 230 | [ |
||
| 238 | /** |
||
| 239 | * Apply database settings |
||
| 240 | * |
||
| 241 | * @throws ExitException |
||
| 242 | */ |
||
| 243 | static function admin_databases_apply_settings () { |
||
| 249 | /** |
||
| 250 | * @throws ExitException |
||
| 251 | */ |
||
| 252 | protected static function admin_databases_settings_common () { |
||
| 264 | /** |
||
| 265 | * Save database settings |
||
| 266 | * |
||
| 267 | * @throws ExitException |
||
| 268 | */ |
||
| 269 | static function admin_databases_save_settings () { |
||
| 275 | /** |
||
| 276 | * Cancel database settings |
||
| 277 | * |
||
| 278 | * @throws ExitException |
||
| 279 | */ |
||
| 280 | static function admin_databases_cancel_settings () { |
||
| 285 | } |
||
| 286 |