Complex classes like MySQLDatabase 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 MySQLDatabase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class MySQLDatabase extends AbstractSQLDatabase |
||
| 18 | { |
||
| 19 | |||
| 20 | 4 | private static function bindToStatement(mysqli_stmt $statement, array $bindings) |
|
| 41 | |||
| 42 | private $connection; |
||
| 43 | private $host; |
||
| 44 | private $port; |
||
| 45 | private $username; |
||
| 46 | private $password; |
||
| 47 | private $databaseName; |
||
| 48 | private $options; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Initalizes this class, so a connection can be made to the database. If url is not null, the |
||
| 52 | * connection parameters will be decoded from the url. |
||
| 53 | * |
||
| 54 | * The provided url will contain the username, password, hostname, port, and the database name. |
||
| 55 | * Take the following url for example. |
||
| 56 | * |
||
| 57 | * root:[email protected]:3603/administration |
||
| 58 | * |
||
| 59 | * It will connect to mysql.example.com at port 3603. It will use the username root, and use |
||
| 60 | * the password secret. The database is named administration. |
||
| 61 | * Please note that the scheme and fragment will be ignored. |
||
| 62 | * |
||
| 63 | * @param string|null $url |
||
| 64 | */ |
||
| 65 | 16 | public function __construct($url = null) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @return mysqli|null |
||
| 109 | */ |
||
| 110 | 3 | public function getConnection() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | 3 | public function getHost() |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $value |
||
| 127 | * |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | 2 | public function setHost($value) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @return int |
||
| 137 | */ |
||
| 138 | 3 | public function getPort() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param int $value |
||
| 145 | * |
||
| 146 | * @return void |
||
| 147 | */ |
||
| 148 | 1 | public function setPort($value) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | 3 | public function getUsername() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $newUsername |
||
| 163 | */ |
||
| 164 | 1 | public function setUsername($newUsername) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | 3 | public function getPassword() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $newPassword |
||
| 179 | */ |
||
| 180 | 1 | public function setPassword($newPassword) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | 3 | public function getDatabaseName() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $databaseName |
||
| 195 | */ |
||
| 196 | 1 | public function setDatabaseName($databaseName) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | 1 | public function getAllOptions() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @return mixed |
||
| 211 | */ |
||
| 212 | 2 | public function getOption($key) |
|
| 216 | |||
| 217 | 1 | public function setOption($key, $newOption) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @return integer |
||
| 224 | */ |
||
| 225 | 1 | public function lastIdCreated() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @return array[] |
||
| 232 | */ |
||
| 233 | private static function processResult(mysqli_result $result) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param SqlStatementBuilder $statement |
||
| 245 | * @param array $options |
||
| 246 | * @param bool $returnResult |
||
| 247 | * @return array[]|null |
||
| 248 | * |
||
| 249 | * @throws DatabaseException If there was an error connecting to the database. |
||
| 250 | */ |
||
| 251 | 4 | protected function sendQueryToDatabase(SqlStatementBuilder $statement, array $options, $returnResult) |
|
| 297 | |||
| 298 | 6 | private function initalizeConnection() |
|
| 330 | } |
||
| 331 |