Complex classes like Database_Abstract 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_Abstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | abstract class Database_Abstract implements Database |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Current connection to the database |
||
| 29 | * @var resource |
||
| 30 | */ |
||
| 31 | protected $_connection = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Number of queries run (may include queries from $_SESSION if is a redirect) |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | protected $_query_count = 0; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Yet another way to skip a database error |
||
| 41 | * @var boolean |
||
| 42 | */ |
||
| 43 | protected $_skip_error = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * This is used to remember the "previous" state of the skip_error parameter |
||
| 47 | * @var null|boolean |
||
| 48 | */ |
||
| 49 | protected $_old_skip_error = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Private constructor. |
||
| 53 | */ |
||
| 54 | protected function __construct() |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Callback for preg_replace_callback on the query. |
||
| 61 | * It allows to replace on the fly a few pre-defined strings, for |
||
| 62 | * convenience ('query_see_board', 'query_wanna_see_board'), with |
||
| 63 | * their current values from $user_info. |
||
| 64 | * In addition, it performs checks and sanitization on the values |
||
| 65 | * sent to the database. |
||
| 66 | * |
||
| 67 | * @param mixed[] $matches |
||
| 68 | */ |
||
| 69 | 24 | public function replacement__callback($matches) |
|
| 70 | { |
||
| 71 | 24 | global $db_callback, $user_info, $db_prefix; |
|
| 72 | |||
| 73 | 24 | list ($values, $connection) = $db_callback; |
|
| 74 | |||
| 75 | // Connection gone??? This should *never* happen at this point, yet it does :'( |
||
| 76 | 24 | if (!$this->_validConnection($connection)) |
|
|
|
|||
| 77 | 24 | Errors::instance()->display_db_error(); |
|
| 78 | |||
| 79 | 24 | if ($matches[1] === 'db_prefix') |
|
| 80 | 24 | return $db_prefix; |
|
| 81 | |||
| 82 | 24 | if ($matches[1] === 'query_see_board') |
|
| 83 | 24 | return $user_info['query_see_board']; |
|
| 84 | |||
| 85 | 24 | if ($matches[1] === 'query_wanna_see_board') |
|
| 86 | 24 | return $user_info['query_wanna_see_board']; |
|
| 87 | |||
| 88 | 24 | if (!isset($matches[2])) |
|
| 89 | 24 | $this->error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 90 | |||
| 91 | 24 | if (!isset($values[$matches[2]])) |
|
| 92 | 24 | $this->error_backtrace('The database value you\'re trying to insert does not exist: ' . htmlspecialchars($matches[2], ENT_COMPAT, 'UTF-8'), '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 93 | |||
| 94 | 24 | $replacement = $values[$matches[2]]; |
|
| 95 | |||
| 96 | 24 | switch ($matches[1]) |
|
| 97 | { |
||
| 98 | 24 | case 'int': |
|
| 99 | 15 | if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) |
|
| 100 | 15 | $this->error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 101 | 15 | return (string) (int) $replacement; |
|
| 102 | break; |
||
| 103 | |||
| 104 | 24 | case 'string': |
|
| 105 | 24 | case 'text': |
|
| 106 | 22 | return sprintf('\'%1$s\'', $this->escape_string($replacement)); |
|
| 107 | break; |
||
| 108 | |||
| 109 | 15 | case 'array_int': |
|
| 110 | 11 | if (is_array($replacement)) |
|
| 111 | 11 | { |
|
| 112 | 11 | if (empty($replacement)) |
|
| 113 | 11 | $this->error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 114 | |||
| 115 | 11 | foreach ($replacement as $key => $value) |
|
| 116 | { |
||
| 117 | 11 | if (!is_numeric($value) || (string) $value !== (string) (int) $value) |
|
| 118 | 11 | $this->error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 119 | |||
| 120 | 11 | $replacement[$key] = (string) (int) $value; |
|
| 121 | 11 | } |
|
| 122 | |||
| 123 | 11 | return implode(', ', $replacement); |
|
| 124 | } |
||
| 125 | else |
||
| 126 | $this->error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
||
| 127 | |||
| 128 | break; |
||
| 129 | |||
| 130 | 14 | case 'array_string': |
|
| 131 | 10 | if (is_array($replacement)) |
|
| 132 | 10 | { |
|
| 133 | 10 | if (empty($replacement)) |
|
| 134 | 10 | $this->error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 135 | |||
| 136 | 10 | foreach ($replacement as $key => $value) |
|
| 137 | 10 | $replacement[$key] = sprintf('\'%1$s\'', $this->escape_string($value)); |
|
| 138 | |||
| 139 | 10 | return implode(', ', $replacement); |
|
| 140 | } |
||
| 141 | else |
||
| 142 | $this->error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
||
| 143 | break; |
||
| 144 | |||
| 145 | 13 | case 'date': |
|
| 146 | if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) |
||
| 147 | return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]); |
||
| 148 | else |
||
| 149 | $this->error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
||
| 150 | break; |
||
| 151 | |||
| 152 | 13 | case 'float': |
|
| 153 | 1 | if (!is_numeric($replacement)) |
|
| 154 | 1 | $this->error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
|
| 155 | 1 | return (string) (float) $replacement; |
|
| 156 | break; |
||
| 157 | |||
| 158 | 12 | case 'identifier': |
|
| 159 | return '`' . strtr($replacement, array('`' => '', '.' => '')) . '`'; |
||
| 160 | break; |
||
| 161 | |||
| 162 | 12 | case 'raw': |
|
| 163 | 12 | return $replacement; |
|
| 164 | break; |
||
| 165 | |||
| 166 | default: |
||
| 167 | $this->error_backtrace('Undefined type used in the database query. (' . $matches[1] . ':' . $matches[2] . ')', '', false, __FILE__, __LINE__); |
||
| 168 | break; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * This function works like $this->query(), escapes and quotes a string, |
||
| 174 | * but it doesn't execute the query. |
||
| 175 | * |
||
| 176 | * @param string $db_string |
||
| 177 | * @param mixed[] $db_values |
||
| 178 | * @param resource|null $connection = null |
||
| 179 | */ |
||
| 180 | 17 | public function quote($db_string, $db_values, $connection = null) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritDoc} |
||
| 202 | */ |
||
| 203 | 1 | public function fetchQuery($db_string, $db_values = array(), $seeds = null) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * {@inheritDoc} |
||
| 217 | */ |
||
| 218 | 2 | public function fetchQueryCallback($db_string, $db_values = array(), $callback = null, $seeds = null) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * This function combines the keys and values of the data passed to db::insert. |
||
| 235 | * |
||
| 236 | * @param integer[] $keys |
||
| 237 | * @param mixed[] $values |
||
| 238 | * @return mixed[] |
||
| 239 | */ |
||
| 240 | 17 | protected function _array_combine($keys, $values) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * This function tries to work out additional error information from a back trace. |
||
| 262 | * |
||
| 263 | * @param string $error_message |
||
| 264 | * @param string $log_message |
||
| 265 | * @param string|boolean $error_type |
||
| 266 | * @param string|null $file |
||
| 267 | * @param integer|null $line |
||
| 268 | */ |
||
| 269 | public function error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Escape the LIKE wildcards so that they match the character and not the wildcard. |
||
| 313 | * |
||
| 314 | * @param string $string |
||
| 315 | * @param bool $translate_human_wildcards = false, if true, turns human readable wildcards into SQL wildcards. |
||
| 316 | */ |
||
| 317 | public function escape_wildcard_string($string, $translate_human_wildcards = false) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * This function optimizes a table. |
||
| 335 | * |
||
| 336 | * - reclaims storage occupied by dead tuples. In normal PostgreSQL operation, tuples |
||
| 337 | * that are deleted or obsoleted by an update are not physically removed from their table; |
||
| 338 | * they remain present until a VACUUM is done. Therefore it's necessary to do VACUUM periodically, |
||
| 339 | * especially on frequently-updated tables. |
||
| 340 | * |
||
| 341 | * @param string $table - the table to be optimized |
||
| 342 | * |
||
| 343 | * @deprecated since 1.1 - the function was moved to DbTable class |
||
| 344 | * |
||
| 345 | * @return int how much it was gained |
||
| 346 | */ |
||
| 347 | public function db_optimize_table($table) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Retrieve the connection object |
||
| 356 | * |
||
| 357 | * @return resource what? The connection |
||
| 358 | */ |
||
| 359 | public function connection() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Return the number of queries executed |
||
| 367 | * |
||
| 368 | * @return int |
||
| 369 | */ |
||
| 370 | public function num_queries() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Defines if the class should or not return the error in case of failures. |
||
| 377 | * |
||
| 378 | * @param null|boolean $set if true the query method will not return any error |
||
| 379 | * if null will restore the last known value of skip_error |
||
| 380 | */ |
||
| 381 | public function skip_error($set = true) |
||
| 390 | } |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.