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 |
||
12 | final class Database implements DBInterface{ |
||
13 | protected $db; |
||
|
|||
14 | protected $sql; |
||
15 | private $key; |
||
16 | |||
17 | protected $logLocation; |
||
18 | public $logErrors = true; |
||
19 | public $logQueries = false; |
||
20 | public $displayErrors = false; |
||
21 | |||
22 | protected $database; |
||
23 | protected $cacheEnabled = false; |
||
24 | protected $cacheObj; |
||
25 | protected $cacheValue; |
||
26 | protected $modified = false; |
||
27 | |||
28 | private $query; |
||
29 | private $values = []; |
||
30 | private $prepare = []; |
||
31 | |||
32 | /** |
||
33 | * Connect to database using PDO connection |
||
34 | * @param string $hostname This should be the host of the database e.g. 'localhost' |
||
35 | * @param string $username This should be the username for the chosen database |
||
36 | * @param string $password This should be the password for the chosen database |
||
37 | * @param string $database This should be the database that you wish to connect to |
||
38 | * @param string|false $backuphost If you have a replication server set up put the hostname or IP address incase the primary server goes down |
||
39 | * @param object|false $cache If you want to cache the queries with Memcache(d)/Redis/APC/Xcache This should be the object else set to false |
||
40 | * @param int $port This should be the port number of the MySQL database connection |
||
41 | */ |
||
42 | public function __construct($hostname, $username, $password, $database, $backuphost = false, $cache = false, $port = 3306){ |
||
57 | |||
58 | /** |
||
59 | * Closes the PDO database connection when Database object unset |
||
60 | */ |
||
61 | public function __destruct(){ |
||
64 | |||
65 | /** |
||
66 | * Connect to the database using PDO connection |
||
67 | * @param string $username This should be the username for the chosen database |
||
68 | * @param string $password This should be the password for the chosen database |
||
69 | * @param string $database This should be the database that you wish to connect to |
||
70 | * @param string $hostname The hostname for the database |
||
71 | * @param int $port The port number to connect to the MySQL server |
||
72 | */ |
||
73 | protected function connectToServer($username, $password, $database, $hostname, $port = 3306){ |
||
79 | |||
80 | /** |
||
81 | * Enables the caching and set the caching object to the one provided |
||
82 | * @param object $caching This should be class of the type of caching you are using |
||
83 | */ |
||
84 | public function setCaching($caching){ |
||
91 | |||
92 | /** |
||
93 | * This query function is used for more advanced SQL queries for which non of the other methods fit |
||
94 | * @param string $sql This should be the SQL query which you wish to run |
||
95 | * @return array Returns array of results for the query that has just been run |
||
96 | */ |
||
97 | public function query($sql, $variables = array(), $cache = true){ |
||
110 | |||
111 | /** |
||
112 | * Returns a single record for a select query for the chosen table |
||
113 | * @param string $table This should be the table you wish to select the values from |
||
114 | * @param array $where Should be the field names and values you wish to use as the where query e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
||
115 | * @param string|array $fields This should be the records you wis to select from the table. It should be either set as '*' which is the default or set as an array in the following format array('field', 'field2', 'field3', etc). |
||
116 | * @param array $order This is the order you wish the results to be ordered in should be formatted as follows array('fieldname' => 'ASC') or array("'fieldname', 'fieldname2'" => 'DESC') |
||
117 | * @param boolean $cache If the query should be cached or loaded from cache set to true else set to false |
||
118 | * @return array Returns a single table record as the standard array when running SQL queries |
||
119 | */ |
||
120 | public function select($table, $where = array(), $fields = '*', $order = array(), $cache = true){ |
||
123 | |||
124 | /** |
||
125 | * Returns a multidimensional array of the results from the selected table given the given parameters |
||
126 | * @param string $table This should be the table you wish to select the values from |
||
127 | * @param array $where Should be the field names and values you wish to use as the where query e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
||
128 | * @param string|array $fields This should be the records you wis to select from the table. It should be either set as '*' which is the default or set as an array in the following format array('field', 'field2', 'field3', etc). |
||
129 | * @param array $order This is the order you wish the results to be ordered in should be formatted as follows array('fieldname' => 'ASC') or array("'fieldname', 'fieldname2'" => 'DESC') |
||
130 | * @param integer|array $limit The number of results you want to return 0 is default and returns all results, else should be formated either as a standard integer or as an array as the start and end values e.g. array(0 => 150) |
||
131 | * @param boolean $cache If the query should be cached or loaded from cache set to true else set to false |
||
132 | * @return array Returns a multidimensional array with the chosen fields from the table |
||
133 | */ |
||
134 | public function selectAll($table, $where = array(), $fields = '*', $order = array(), $limit = 0, $cache = true){ |
||
144 | |||
145 | /** |
||
146 | * Returns a single column value for a given query |
||
147 | * @param string $table This should be the table you wish to select the values from |
||
148 | * @param array $where Should be the field names and values you wish to use as the where query e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
||
149 | * @param array $fields This should be the records you wis to select from the table. It should be either set as '*' which is the default or set as an array in the following format array('field', 'field2', 'field3', etc). |
||
150 | * @param int $colNum This should be the column number you wish to get (starts at 0) |
||
151 | * @param array $order This is the order you wish the results to be ordered in should be formatted as follows array('fieldname' => 'ASC') or array("'fieldname', 'fieldname2'" => 'DESC') so it can be done in both directions |
||
152 | * @param boolean $cache If the query should be cached or loaded from cache set to true else set to false |
||
153 | * @return mixed If a result is found will return the value of the colum given else will return false |
||
154 | */ |
||
155 | public function fetchColumn($table, $where = array(), $fields = '*', $colNum = 0, $order = array(), $cache = true){ |
||
164 | |||
165 | /** |
||
166 | * Inserts into database using the prepared PDO statements |
||
167 | * @param string $table This should be the table you wish to insert the values into |
||
168 | * @param array $records This should be the field names and values in the format of array('fieldname' => 'value', 'fieldname2' => 'value2', etc.) |
||
169 | * @return boolean If data is inserted returns true else returns false |
||
170 | */ |
||
171 | public function insert($table, $records){ |
||
178 | |||
179 | /** |
||
180 | * Updates values in a database using the provide variables |
||
181 | * @param string $table This should be the table you wish to update the values for |
||
182 | * @param array $records This should be the field names and new values in the format of array('fieldname' => 'newvalue', 'fieldname2' => 'newvalue2', etc.) |
||
183 | * @param array $where Should be the field names and values you wish to update in the form of an array e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
||
184 | * @param int $limit The number of results you want to return 0 is default and will update all results that match the query, else should be formated as a standard integer |
||
185 | * @return boolean Returns true if update is successful else returns false |
||
186 | */ |
||
187 | public function update($table, $records, $where = array(), $limit = 0){ |
||
192 | |||
193 | /** |
||
194 | * Deletes records from the given table based on the variables given |
||
195 | * @param string $table This should be the table you wish to delete the records from |
||
196 | * @param array $where This should be an array of for the where statement |
||
197 | * @param int $limit The number of results you want to return 0 is default and will delete all results that match the query, else should be formated as a standard integer |
||
198 | */ |
||
199 | public function delete($table, $where, $limit = 0){ |
||
204 | |||
205 | /** |
||
206 | * Count the number of return results |
||
207 | * @param string $table The table you wish to count the result of |
||
208 | * @param array $where Should be the field names and values you wish to use as the where query e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
||
209 | * @param boolean $cache If the query should be cached or loaded from cache set to true else set to false |
||
210 | * @return int Returns the number of results |
||
211 | */ |
||
212 | public function count($table, $where = array(), $cache = true){ |
||
223 | |||
224 | /** |
||
225 | * Truncates a given table from the selected database so there are no values in the table |
||
226 | * @param string $table This should be the table you wish to truncate |
||
227 | * @return boolean If the table is emptied returns true else returns false |
||
228 | */ |
||
229 | public function truncate($table){ |
||
239 | |||
240 | /** |
||
241 | * Returns the number of rows for the last query sent |
||
242 | * @return int Returns the number of rows for the last query |
||
243 | */ |
||
244 | public function numRows(){ |
||
250 | |||
251 | /** |
||
252 | * Returns the number of rows for the last query sent (Looks a the numRows() function just added incase of habbit) |
||
253 | * @return int Returns the number of rows for the last query |
||
254 | */ |
||
255 | public function rowCount(){ |
||
258 | |||
259 | /** |
||
260 | * Returns the ID of the last record last inserted |
||
261 | * @param string $name This should be the name of the sequence object you wish to retrieve |
||
262 | * @return int|string Returns the last inserted ID of the last insert item if $name is null else returns string with sequenced object |
||
263 | */ |
||
264 | public function lastInsertId($name = null) { |
||
267 | |||
268 | /** |
||
269 | * Returns the index of the given table or tables within the database |
||
270 | * @param string|array $table Table can wither be a standard string with a single table name or an array with multiple table names |
||
271 | * @return array Returns the table index for the selected table as an array |
||
272 | */ |
||
273 | public function fulltextIndex($table){ |
||
296 | |||
297 | /** |
||
298 | * Checks to see if a connection has been made to the server |
||
299 | * @return boolean |
||
300 | */ |
||
301 | public function isConnected(){ |
||
304 | |||
305 | /** |
||
306 | * Returns the server version information |
||
307 | */ |
||
308 | public function serverVersion(){ |
||
311 | |||
312 | /** |
||
313 | * Sets the location of the log files |
||
314 | * @param string $location This should be where you wish the logs to be stored |
||
315 | * @return $this |
||
316 | */ |
||
317 | public function setLogLocation($location = false){ |
||
327 | |||
328 | /** |
||
329 | * Displays the error massage which occurs |
||
330 | * @param \Exception $error This should be an instance of Exception |
||
331 | */ |
||
332 | private function error($error){ |
||
343 | |||
344 | /** |
||
345 | * Writes all queries to a log file |
||
346 | */ |
||
347 | public function writeQueryToLog(){ |
||
353 | |||
354 | /** |
||
355 | * Closes the PDO database connection by setting the connection to NULL |
||
356 | */ |
||
357 | public function closeDatabase(){ |
||
360 | |||
361 | /** |
||
362 | * Build the SQL query but doesn't execute it |
||
363 | * @param string $table This should be the table you wish to select the values from |
||
364 | * @param array $where Should be the field names and values you wish to use as the where query e.g. array('fieldname' => 'value', 'fieldname2' => 'value2', etc). |
||
365 | * @param string|array $fields This should be the records you wis to select from the table. It should be either set as '*' which is the default or set as an array in the following format array('field', 'field2', 'field3', etc). |
||
366 | * @param array $order This is the order you wish the results to be ordered in should be formatted as follows array('fieldname' => 'ASC') or array("'fieldname', 'fieldname2'" => 'DESC') so it can be done in both directions |
||
367 | * @param integer|array $limit The number of results you want to return 0 is default and returns all results, else should be formated either as a standard integer or as an array as the start and end values e.g. array(0 => 150) |
||
368 | */ |
||
369 | protected function buildSelectQuery($table, $where = array(), $fields = '*', $order = array(), $limit = 0){ |
||
382 | |||
383 | /** |
||
384 | * Execute the current query if no cache value is available |
||
385 | * @param boolean $cache If the cache should be checked for the checked for the values of the query set to true else set to false |
||
386 | * @return mixed If a cached value exists will be returned else if cache is not checked and query is executed will not return anything |
||
387 | */ |
||
388 | protected function executeQuery($cache = true){ |
||
403 | |||
404 | /** |
||
405 | * This outputs the SQL where query based on a given array |
||
406 | * @param array $where This should be an array that you wish to create the where query for in the for array('field1' => 'test') or array('field1' => array('>=', 0)) |
||
407 | * @return string|false If the where query is an array will return the where string and set the values else returns false if no array sent |
||
408 | */ |
||
409 | private function where($where){ |
||
433 | |||
434 | /** |
||
435 | * Sets the order sting for the SQL query based on an array or string |
||
436 | * @param array|string $order This should be either set to array('fieldname' => 'ASC/DESC') or RAND() |
||
437 | * @return string|false If the SQL query has an valid order by will return a string else returns false |
||
438 | */ |
||
439 | private function orderBy($order){ |
||
450 | |||
451 | /** |
||
452 | * Build the field list for the query |
||
453 | * @param array $records This should be an array listing all of the fields |
||
454 | * @param boolean $insert If this is an insert statement should be set to true to create the correct amount of queries for the prepared statement |
||
455 | * @return string The fields list will be returned as a string to insert into the SQL query |
||
456 | */ |
||
457 | private function fields($records, $insert = false){ |
||
472 | |||
473 | /** |
||
474 | * Returns the limit SQL for the current query as a string |
||
475 | * @param integer|array $limit This should either be set as an integer or should be set as an array with a start and end value |
||
476 | * @return string|false Will return the LIMIT string for the current query if it is valid else returns false |
||
477 | */ |
||
478 | private function limit($limit = 0){ |
||
489 | |||
490 | |||
491 | /** |
||
492 | * Set the cache with a key and value |
||
493 | * @param string $key The unique key to store the value against |
||
494 | * @param mixed $value The value of the MYSQL query |
||
495 | */ |
||
496 | public function setCache($key, $value){ |
||
501 | |||
502 | /** |
||
503 | * Get the results for a given key |
||
504 | * @param string $key The unique key to check for stored variables |
||
505 | * @return mixed Returned the cached results from |
||
506 | */ |
||
507 | public function getCache($key){ |
||
514 | |||
515 | /** |
||
516 | * Clears the cache |
||
517 | */ |
||
518 | public function flushDB(){ |
||
521 | } |
||
522 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.