Complex classes like MySqlAdapter 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 MySqlAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class MySqlAdapter implements AdapterInterface |
||
15 | { |
||
16 | /** |
||
17 | * The instance of the \PDO class |
||
18 | * |
||
19 | * @var \PDO |
||
20 | */ |
||
21 | private $connection; |
||
22 | |||
23 | /** |
||
24 | * The database host |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | private $host; |
||
29 | |||
30 | /** |
||
31 | * The database user |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $user; |
||
36 | |||
37 | /** |
||
38 | * The database user's password |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $password; |
||
43 | |||
44 | /** |
||
45 | * The database schema name |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $schema; |
||
50 | |||
51 | /** |
||
52 | * The default connection configuration used with the create() method |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | private static $defaultConfig = ['host' => 'localhost', 'schema' => '', 'password' => '', 'user' => 'root']; |
||
57 | |||
58 | /** |
||
59 | * This is a utility method that returns an instance of the class; allowing you to pass an associative array |
||
60 | * containing the configuration while providing defaults. |
||
61 | * The available configuration keys are: |
||
62 | * host the database hostname (required, default: localhost) |
||
63 | * user the database user (default: root) |
||
64 | * password the database user password |
||
65 | * schema the database schema to connect to |
||
66 | * autoconnect whether or not to automatically call the connect() method |
||
67 | * |
||
68 | * @param array $config |
||
69 | * |
||
70 | * @return MySqlAdapter |
||
71 | */ |
||
72 | public static function create(array $config = []): MySqlAdapter |
||
81 | |||
82 | /** |
||
83 | * Returns the value of the default configuration option identified by the key name |
||
84 | * |
||
85 | * @param string $key |
||
86 | * |
||
87 | * @return string |
||
88 | */ |
||
89 | public static function getDefault(string $key): string |
||
93 | |||
94 | /** |
||
95 | * Sets the value for default configuration key |
||
96 | * |
||
97 | * @param string $key |
||
98 | * @param string $value the value for the setting (for boolean keys, pass NULL or a non-empty string) |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public static function setDefault(string $key, string $value = null) |
||
110 | |||
111 | /** |
||
112 | * MySqlAdapter constructor. |
||
113 | * |
||
114 | * @param string $host |
||
115 | * @param string $schema |
||
116 | * @param string $user |
||
117 | * @param string $password |
||
118 | * @param bool $autoConnect |
||
119 | */ |
||
120 | public function __construct(string $host, string $schema, string $user, string $password, bool $autoConnect = false) |
||
130 | |||
131 | /** |
||
132 | * Opens a connection to the database if no connection was previously opened, or if reconnect is TRUE |
||
133 | * |
||
134 | * @param bool $throwExceptionOnFail throws an exception if connection failed |
||
135 | * @param bool $reconnect this forces a reconnect even when a connection was previously established |
||
136 | * |
||
137 | * @return AdapterInterface |
||
138 | * @throws ConfigurationException |
||
139 | */ |
||
140 | public function connect(bool $throwExceptionOnFail = true, bool $reconnect = false): AdapterInterface |
||
172 | |||
173 | /** |
||
174 | * Returns the database hostname [to be] used for the connection |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | public function getHost(): string |
||
182 | |||
183 | /** |
||
184 | * Returns the connection password for the specified database username |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | public function getPassword(): string |
||
192 | |||
193 | /** |
||
194 | * Returns the database schema to connect to with the connection |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getSchema(): string |
||
202 | |||
203 | /** |
||
204 | * Returns the database username [to be] used for the connection |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getUser(): string |
||
212 | |||
213 | /** |
||
214 | * Returns the actual \PDO object created after a successful connection or NULL |
||
215 | * |
||
216 | * @return \PDO |
||
217 | */ |
||
218 | public function getConnection() |
||
223 | |||
224 | /** |
||
225 | * Sets the database hostname to be used in the connection |
||
226 | * |
||
227 | * @param string $host |
||
228 | * |
||
229 | * @return AdapterInterface |
||
230 | */ |
||
231 | public function setHost(string $host): AdapterInterface |
||
239 | |||
240 | /** |
||
241 | * Sets the database password to be used in the connection |
||
242 | * |
||
243 | * @param string $password |
||
244 | * |
||
245 | * @return AdapterInterface |
||
246 | */ |
||
247 | public function setPassword(string $password = ''): AdapterInterface |
||
252 | |||
253 | /** |
||
254 | * Sets the database schema name to be used by the connection |
||
255 | * |
||
256 | * @param string $schema |
||
257 | * |
||
258 | * @return AdapterInterface |
||
259 | */ |
||
260 | public function setSchema(string $schema): AdapterInterface |
||
268 | |||
269 | /** |
||
270 | * Sets the database username to be used in the connection |
||
271 | * |
||
272 | * @param string $user |
||
273 | * |
||
274 | * @return AdapterInterface |
||
275 | */ |
||
276 | public function setUser(string $user): AdapterInterface |
||
281 | |||
282 | /** |
||
283 | * Starts a new database transaction on the active connection, if any |
||
284 | * |
||
285 | * @return void |
||
286 | * @throws ConnectionException |
||
287 | */ |
||
288 | public function transactionStart() |
||
296 | |||
297 | /** |
||
298 | * Commits an active database transaction |
||
299 | * |
||
300 | * @return void |
||
301 | * @throws ConnectionException |
||
302 | */ |
||
303 | public function transactionCommit() |
||
310 | |||
311 | /** |
||
312 | * Rolls back an active database transaction |
||
313 | * |
||
314 | * @return void |
||
315 | * @throws ConnectionException |
||
316 | */ |
||
317 | public function transactionRollback() |
||
324 | |||
325 | /** |
||
326 | * Returns the last AUTO INCREMENT id used for the current connection instance |
||
327 | * |
||
328 | * @return int |
||
329 | */ |
||
330 | public function getInsertId(): int |
||
334 | |||
335 | /** |
||
336 | * Executes an SQL query against the active database connection, sending the supplied arguments with |
||
337 | * the query. it will either return FALSE on failure, but for successes, depending on the value of $isSelect, |
||
338 | * it will wither return TRUE or the retrieved database records. |
||
339 | * |
||
340 | * @param string $sql the sql query to execute |
||
341 | * @param bool $isSelect whether or not to get the matching records. Useful for SELECT queries |
||
342 | * @param array ...$arguments the arguments to use in the PDOStatement::execute() method call |
||
343 | * |
||
344 | * @return array|bool |
||
345 | * @throws ConnectionException |
||
346 | */ |
||
347 | public function exec(string $sql, bool $isSelect = false, ...$arguments) |
||
365 | |||
366 | /** |
||
367 | * Executes an SQL SELECT query against the database, then copies the results of the query into an array |
||
368 | * |
||
369 | * @param string $sql |
||
370 | * @param array ...$arguments |
||
371 | * |
||
372 | * @return array |
||
373 | */ |
||
374 | public function selectAll(string $sql, ...$arguments): array |
||
379 | |||
380 | /** |
||
381 | * Similar to the selectAll() except that instead of returning the data as a multi-dimensional array, |
||
382 | * it returns the first index of the array |
||
383 | * |
||
384 | * @param string $sql |
||
385 | * @param array ...$arguments |
||
386 | * |
||
387 | * @return array |
||
388 | */ |
||
389 | public function selectOne(string $sql, ...$arguments): array |
||
394 | |||
395 | /** |
||
396 | * @return string |
||
397 | */ |
||
398 | public function __toString() |
||
402 | } |
||
403 |