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 | public static function admin_databases_get () { |
||
| 47 | /** |
||
| 48 | * Update database or database mirror settings |
||
| 49 | * |
||
| 50 | * @param \cs\Request $Request |
||
| 51 | * |
||
| 52 | * @throws ExitException |
||
| 53 | */ |
||
| 54 | public static function admin_databases_patch ($Request) { |
||
| 81 | /** |
||
| 82 | * Create database or database mirror |
||
| 83 | * |
||
| 84 | * @param \cs\Request $Request |
||
| 85 | * |
||
| 86 | * @throws ExitException |
||
| 87 | */ |
||
| 88 | public static function admin_databases_post ($Request) { |
||
| 108 | /** |
||
| 109 | * Delete database or database mirror |
||
| 110 | * |
||
| 111 | * @param \cs\Request $Request |
||
| 112 | * |
||
| 113 | * @throws ExitException |
||
| 114 | */ |
||
| 115 | public static function admin_databases_delete ($Request) { |
||
| 161 | /** |
||
| 162 | * Get array of available database drivers |
||
| 163 | */ |
||
| 164 | public static function admin_databases_drivers () { |
||
| 165 | return static::admin_databases_get_drivers(); |
||
| 166 | } |
||
| 167 | /** |
||
| 168 | * @return string[] |
||
| 169 | */ |
||
| 170 | protected static function admin_databases_get_drivers () { |
||
| 171 | return _mb_substr(get_files_list(DIR.'/core/drivers/DB', '/^[^_].*?\.php$/i', 'f'), 0, -4); |
||
| 172 | } |
||
| 173 | /** |
||
| 174 | * Test database connection |
||
| 175 | * |
||
| 176 | * @param \cs\Request $Request |
||
| 177 | * |
||
| 178 | * @throws ExitException |
||
| 179 | */ |
||
| 180 | public static function admin_databases_test ($Request) { |
||
| 195 | /** |
||
| 196 | * Get database settings |
||
| 197 | */ |
||
| 198 | public static function admin_databases_get_settings () { |
||
| 206 | /** |
||
| 207 | * Apply database settings |
||
| 208 | * |
||
| 209 | * @param \cs\Request $Request |
||
| 210 | * |
||
| 211 | * @throws ExitException |
||
| 212 | */ |
||
| 213 | public static function admin_databases_apply_settings ($Request) { |
||
| 219 | /** |
||
| 220 | * @param \cs\Request $Request |
||
| 221 | * |
||
| 222 | * @throws ExitException |
||
| 223 | */ |
||
| 224 | protected static function admin_databases_settings_common ($Request) { |
||
| 233 | /** |
||
| 234 | * Save database settings |
||
| 235 | * |
||
| 236 | * @param \cs\Request $Request |
||
| 237 | * |
||
| 238 | * @throws ExitException |
||
| 239 | */ |
||
| 240 | public static function admin_databases_save_settings ($Request) { |
||
| 246 | /** |
||
| 247 | * Cancel database settings |
||
| 248 | * |
||
| 249 | * @throws ExitException |
||
| 250 | */ |
||
| 251 | public static function admin_databases_cancel_settings () { |
||
| 254 | } |
||
| 255 |