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 = 'localhost'; |
||
44 | private $port = 3306; |
||
45 | private $username = 'root'; |
||
46 | private $password = ''; |
||
47 | private $databaseName = ''; |
||
48 | private $options = []; |
||
49 | |||
50 | /** |
||
51 | * Initalizes this class, so a connection can be made to the database. The first argument is a |
||
52 | * url. If url is not null, the 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) |
|
90 | 10 | ||
91 | 16 | /** |
|
92 | 10 | * @return mysqli|null |
|
93 | 10 | */ |
|
94 | 16 | public function getConnection() |
|
100 | 16 | ||
101 | 10 | /** |
|
102 | 10 | * Returns the host address for the MySQL client. |
|
103 | 16 | * |
|
104 | 16 | * @return string |
|
105 | 16 | */ |
|
106 | public function getHost() |
||
110 | 3 | ||
111 | /** |
||
112 | 3 | * @param string $value |
|
113 | * @return void |
||
114 | 2 | */ |
|
115 | public function setHost($value) |
||
119 | |||
120 | 3 | /** |
|
121 | * Returns the port number for the MySQL client. |
||
122 | 3 | * |
|
123 | * @return int |
||
124 | */ |
||
125 | public function getPort() |
||
129 | |||
130 | 2 | /** |
|
131 | * @param int $value |
||
132 | 2 | * |
|
133 | 2 | * @return void |
|
134 | */ |
||
135 | public function setPort($value) |
||
139 | |||
140 | 3 | /** |
|
141 | * Returns the username for the MySQL client. |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getUsername() |
||
149 | |||
150 | 1 | /** |
|
151 | 1 | * Sets the username for the MySQL client. |
|
152 | * |
||
153 | * @param string $newUsername |
||
154 | */ |
||
155 | public function setUsername($newUsername) |
||
159 | |||
160 | /** |
||
161 | * Returns the password for the MySQL client. |
||
162 | * |
||
163 | * @return string |
||
164 | 1 | */ |
|
165 | public function getPassword() |
||
169 | |||
170 | /** |
||
171 | * Sets the password for the MySQL client. |
||
172 | 3 | * |
|
173 | * @param string $newPassword |
||
174 | 3 | */ |
|
175 | public function setPassword($newPassword) |
||
179 | |||
180 | 1 | /** |
|
181 | * Returns the database's name for the MySQL client. |
||
182 | 1 | * |
|
183 | 1 | * @return string |
|
184 | */ |
||
185 | public function getDatabaseName() |
||
189 | |||
190 | 3 | /** |
|
191 | * Sets the database's name for the MySQL client. |
||
192 | * |
||
193 | * @param string $databaseName |
||
194 | */ |
||
195 | public function setDatabaseName($databaseName) |
||
199 | 1 | ||
200 | /** |
||
201 | * @return array |
||
202 | */ |
||
203 | public function getAllOptions() |
||
207 | |||
208 | /** |
||
209 | * @return mixed |
||
210 | */ |
||
211 | public function getOption($key) |
||
215 | |||
216 | public function setOption($key, $newOption) |
||
220 | 1 | ||
221 | /** |
||
222 | * @return integer |
||
223 | */ |
||
224 | public function lastIdCreated() |
||
228 | |||
229 | /** |
||
230 | * @return array[] |
||
231 | */ |
||
232 | private static function processResult(mysqli_result $result) |
||
241 | |||
242 | /** |
||
243 | * @param SqlStatementBuilder $statement |
||
244 | * @param array $options |
||
245 | * @param bool $returnResult |
||
246 | * @return array[]|null |
||
247 | * |
||
248 | * @throws DatabaseException If there was an error connecting to the database. |
||
249 | */ |
||
250 | protected function sendQueryToDatabase(SqlStatementBuilder $statement, array $options, $returnResult) |
||
296 | |||
297 | private function initalizeConnection() |
||
329 | } |
||
330 |