Complex classes like Database 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 Database, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | Class Database { |
||
7 | |||
8 | /** |
||
9 | * Constant to indicate whether the query is a write operation. |
||
10 | * |
||
11 | * @var integer |
||
12 | */ |
||
13 | const __WRITE_OPERATION__ = 0; |
||
14 | |||
15 | /** |
||
16 | * Constant to indicate whether the query is a write operation |
||
17 | * |
||
18 | * @var integer |
||
19 | */ |
||
20 | const __READ_OPERATION__ = 1; |
||
21 | |||
22 | /** |
||
23 | * An instance of the current PDO object |
||
24 | * @var PDO |
||
25 | */ |
||
26 | public $conn = null; |
||
27 | |||
28 | /** |
||
29 | * Sets the current `$_log` to be an empty array |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | public $log = array(); |
||
34 | |||
35 | /** |
||
36 | * The number of queries this class has executed, defaults to 0. |
||
37 | * |
||
38 | * @var integer |
||
39 | */ |
||
40 | protected $_query_count = 0; |
||
41 | |||
42 | /** |
||
43 | * The table prefix for this connection. Queries to be written using |
||
44 | * a `tbl_table_name` syntax, where `tbl_` will be replaced by this |
||
45 | * variable. By default it is `sym_` but it configured in the configuration |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $_prefix = 'sym_'; |
||
50 | |||
51 | /** |
||
52 | * Whether query caching is enabled or not. By default this set |
||
53 | * to true which will use SQL_CACHE to cache the results of queries |
||
54 | * |
||
55 | * @var boolean |
||
56 | */ |
||
57 | protected $_cache = true; |
||
58 | |||
59 | /** |
||
60 | * Whether to log this query in the internal `$log`. |
||
61 | * Defaults to true |
||
62 | * |
||
63 | * @var boolean |
||
64 | */ |
||
65 | protected $_logging = true; |
||
66 | |||
67 | /** |
||
68 | * @var PDOStatement |
||
69 | */ |
||
70 | protected $_result = null; |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $_lastResult = array(); |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $_lastQuery = null; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $_lastQueryHash = null; |
||
86 | |||
87 | /** |
||
88 | * Creates a new Database object given an associative array of configuration |
||
89 | * parameters in `$config`. If `$config` contains a key, `pdo` then this |
||
90 | * `Database` instance will use that PDO connection. Otherwise, `$config` |
||
91 | * should include `driver`, `host`, `port`, `user`, `password` and an optional |
||
92 | * array of PDO options in `options`. |
||
93 | * |
||
94 | * @param array $config |
||
95 | */ |
||
96 | public function __construct(array $config = array()) |
||
111 | |||
112 | /** |
||
113 | * Magic function that will flush the MySQL log and close the MySQL |
||
114 | * connection when the MySQL class is removed or destroyed. |
||
115 | * |
||
116 | * @link http://php.net/manual/en/language.oop5.decon.php |
||
117 | */ |
||
118 | public function __destruct() |
||
123 | |||
124 | /** |
||
125 | * Resets the `result`, `lastResult`, `lastQuery` and lastQueryHash properties to `null`. |
||
126 | * Called on each query and when the class is destroyed. |
||
127 | */ |
||
128 | public function flush() |
||
135 | |||
136 | /** |
||
137 | * Creates a PDO connection to the desired database given the parameters. |
||
138 | * This will also set the error mode to be exceptions (handled by this class) |
||
139 | * |
||
140 | * @link http://www.php.net/manual/en/pdo.drivers.php |
||
141 | * @param string $dsn |
||
142 | * @param string $username |
||
143 | * @param string $password |
||
144 | * @param array $options |
||
145 | * @return boolean |
||
146 | */ |
||
147 | public function connect($dsn = null, $username = null, $password = null, array $options = array()) |
||
161 | |||
162 | /** |
||
163 | * Returns the number of queries that has been executed |
||
164 | * |
||
165 | * @return integer |
||
166 | */ |
||
167 | public function queryCount() |
||
171 | |||
172 | /** |
||
173 | * Sets query caching to true, this will prepend all READ_OPERATION |
||
174 | * queries with SQL_CACHE. Symphony be default enables caching. It |
||
175 | * can be turned off by setting the query_cache parameter to 'off' in the |
||
176 | * Symphony config file. |
||
177 | * |
||
178 | * @link http://dev.mysql.com/doc/refman/5.1/en/query-cache.html |
||
179 | */ |
||
180 | public function enableCaching() |
||
184 | |||
185 | /** |
||
186 | * Sets query caching to false, this will prepend all READ_OPERATION |
||
187 | * queries will SQL_NO_CACHE. |
||
188 | */ |
||
189 | public function disableCaching() |
||
193 | |||
194 | /** |
||
195 | * Returns boolean if query caching is enabled or not |
||
196 | * |
||
197 | * @return boolean |
||
198 | */ |
||
199 | public function isCachingEnabled() |
||
203 | |||
204 | /** |
||
205 | * Enables the logging of queries |
||
206 | */ |
||
207 | public function enableLogging() |
||
211 | |||
212 | /** |
||
213 | * Sets logging to false |
||
214 | */ |
||
215 | public function disableLogging() |
||
219 | |||
220 | /** |
||
221 | * Returns boolean if logging is enabled or not |
||
222 | * |
||
223 | * @return boolean |
||
224 | */ |
||
225 | public function isLoggingEnabled() |
||
229 | |||
230 | /** |
||
231 | * Symphony uses a prefix for all it's database tables so it can live peacefully |
||
232 | * on the same database as other applications. By default this is sym_, but it |
||
233 | * can be changed when Symphony is installed. |
||
234 | * |
||
235 | * @param string $prefix |
||
236 | * The table prefix for Symphony, by default this is sym_ |
||
237 | */ |
||
238 | public function setPrefix($prefix) |
||
242 | |||
243 | /** |
||
244 | * Returns the prefix used by Symphony for this Database instance. |
||
245 | * |
||
246 | * @since Symphony 2.4 |
||
247 | * @return string |
||
248 | */ |
||
249 | public function getPrefix() |
||
253 | |||
254 | /** |
||
255 | * Given a string, replace the default table prefixes with the |
||
256 | * table prefix for this database instance. |
||
257 | * |
||
258 | * @param string $query |
||
259 | * @return string |
||
260 | */ |
||
261 | public function replaceTablePrefix($query) |
||
269 | |||
270 | /** |
||
271 | * Function looks over a query to determine if it's a READ or WRITE operation. |
||
272 | * WRITE operations are any query that starts with: SET, CREATE, INSERT, REPLACE |
||
273 | * ALTER, DELETE, UPDATE, OPTIMIZE, TRUNCATE or DROP. All other queries are considered |
||
274 | * READ operations |
||
275 | * |
||
276 | * @param string $query |
||
277 | * @return integer |
||
278 | */ |
||
279 | public function determineQueryType($query) |
||
285 | |||
286 | /** |
||
287 | * @param array $values |
||
288 | * @return string |
||
289 | */ |
||
290 | public static function addPlaceholders(array $values = array()) |
||
299 | |||
300 | /** |
||
301 | * Given a query that has been prepared and an array of values to subsitute |
||
302 | * into the query, the function will return the result. |
||
303 | * |
||
304 | * @param string $query |
||
305 | * @param array $values |
||
306 | * @return PDOStatement |
||
307 | */ |
||
308 | public function insert($query, array $values) |
||
314 | |||
315 | /** |
||
316 | * Returns the last insert ID from the previous query. This is |
||
317 | * the value from an auto_increment field. |
||
318 | * |
||
319 | * @return integer |
||
320 | * The last interested row's ID |
||
321 | */ |
||
322 | public function getInsertID() |
||
326 | |||
327 | /** |
||
328 | * Given a query that has been prepared and an array of values to subsitute |
||
329 | * into the query, the function will return the result. |
||
330 | * |
||
331 | * @param string $query |
||
332 | * @param array $values |
||
333 | * @return PDOStatement |
||
334 | */ |
||
335 | public function update($query, array $values) |
||
341 | |||
342 | /** |
||
343 | * Given a query that has been prepared and an array of values to subsitute |
||
344 | * into the query, the function will return the result. |
||
345 | * |
||
346 | * @param string $query |
||
347 | * @param array $values |
||
348 | * @return PDOStatement |
||
349 | */ |
||
350 | public function delete($query, array $values) |
||
356 | |||
357 | /** |
||
358 | * Given a query that has been prepared and an array of optional |
||
359 | * parameters, this function will return the results of a query |
||
360 | * as an array. |
||
361 | * |
||
362 | * @param string $query |
||
363 | * @param array $params |
||
364 | * - `fetch-type` = 'ASSOC'/'OBJECT' |
||
365 | * Return result as array or an object |
||
366 | * - `index` = 'column_name' |
||
367 | * The name of a column in the table to use it's value to index |
||
368 | * the result by. If this is omitted (and it is by default), an |
||
369 | * array of associative arrays is returned, with the key being the |
||
370 | * column names |
||
371 | * `offset` = `0` |
||
372 | * An integer representing the row to return |
||
373 | * @param array $values |
||
374 | * @return array |
||
375 | */ |
||
376 | public function fetch($query = null, array $params = array(), array $values = array()) |
||
401 | |||
402 | /** |
||
403 | * Takes an SQL string and creates a prepared statement. |
||
404 | * |
||
405 | * @link http://php.net/manual/en/pdo.prepare.php |
||
406 | * @param string $query |
||
407 | * @param array $driver_options |
||
408 | * This array holds one or more key=>value pairs to set attribute values |
||
409 | * for the DatabaseStatement object that this method returns. |
||
410 | * @return DatabaseStatement |
||
411 | */ |
||
412 | public function prepare($query, array $driver_options = array()) |
||
418 | |||
419 | /** |
||
420 | * Create a transaction. |
||
421 | * |
||
422 | * @return DatabaseTransaction |
||
423 | */ |
||
424 | public function transaction() |
||
428 | |||
429 | /** |
||
430 | * Given a query that has been prepared and an array of values to subsitute |
||
431 | * into the query, the function will return the result. Unlike `insert` and |
||
432 | * `update`, this function is a bit of a catch all and will be able to populate |
||
433 | * `$this->_lastResult` with an array of data. This function is usually used |
||
434 | * via `fetch()`. |
||
435 | * |
||
436 | * @see fetch() |
||
437 | * @param string $query |
||
438 | * @param array $params |
||
439 | * Supports `fetch-type` and `offset` parameters for the moment |
||
440 | * @param array $values |
||
441 | * If the `$query` has placeholders, this parameter will include the data |
||
442 | * to subsitute into the placeholders |
||
443 | * @return boolean |
||
444 | */ |
||
445 | public function query($query, array $params = array(), array $values = array()) |
||
484 | |||
485 | /** |
||
486 | * This function is actually responsible for subsituting the values into |
||
487 | * the query and logging the query for basic profiling/debugging. |
||
488 | * |
||
489 | * @param string $query |
||
490 | * @param array $values |
||
491 | * @param boolean $close |
||
492 | * If true, once the query is executed, the cursor will be closed, |
||
493 | * otherwise it'll be left open for further manipulation (as done by |
||
494 | * `query()`). Defaults to `true` |
||
495 | * @return PDOStatement |
||
496 | */ |
||
497 | private function q($query, $values, $close = true) |
||
542 | |||
543 | /** |
||
544 | * Given an Exception, or called when an error occurs, this function will |
||
545 | * fire the `QueryExecutionError` delegate and then raise a `DatabaseException` |
||
546 | * |
||
547 | * @uses QueryExecutionError |
||
548 | * @throws DatabaseException |
||
549 | * @param Exception $ex |
||
550 | * The exception thrown while doing something with the Database |
||
551 | * @return void |
||
552 | */ |
||
553 | public function error(Exception $ex = null) |
||
601 | |||
602 | /** |
||
603 | * Throw a new DatabaseException when given an original exception and a query. |
||
604 | * |
||
605 | * @param Exception $error |
||
606 | * @param string $query |
||
607 | * @param string $query_hash |
||
608 | */ |
||
609 | public function throwError(Exception $error, $query, $query_hash) |
||
616 | |||
617 | /** |
||
618 | * Function is called everytime a query is executed to log it for |
||
619 | * basic profiling/debugging purposes |
||
620 | * |
||
621 | * @uses PostQueryExecution |
||
622 | * @param string $query |
||
623 | * @param string $query_hash |
||
624 | * @param integer $stop |
||
625 | */ |
||
626 | public function logQuery($query, $query_hash, $stop) |
||
675 | |||
676 | /** |
||
677 | * Returns all the log entries by type. There are two valid types, |
||
678 | * error and debug. If no type is given, the entire log is returned, |
||
679 | * otherwise only log messages for that type are returned |
||
680 | * |
||
681 | * @param string $type |
||
682 | * @return array |
||
683 | * An array of associative array's. Log entries of the error type |
||
684 | * return the query the error occurred on and the error number and |
||
685 | * message from MySQL. Log entries of the debug type return the |
||
686 | * the query and the start/stop time to indicate how long it took |
||
687 | * to run |
||
688 | */ |
||
689 | public function debug($type = null) |
||
695 | |||
696 | /** |
||
697 | * Returns some basic statistics from the MySQL class about the |
||
698 | * number of queries, the time it took to query and any slow queries. |
||
699 | * A slow query is defined as one that took longer than 0.0999 seconds |
||
700 | * This function is used by the Profile devkit |
||
701 | * |
||
702 | * @return array |
||
703 | * An associative array with the number of queries, an array of slow |
||
704 | * queries and the total query time. |
||
705 | */ |
||
706 | public function getStatistics() |
||
722 | } |
||
723 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..