Complex classes like CDatabaseBasic 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 CDatabaseBasic, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 9 | class CDatabaseBasic | ||
| 10 | { | ||
| 11 | |||
| 12 | use TSQLQueryBuilderBasic; | ||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | /** | ||
| 17 | * Properties | ||
| 18 | */ | ||
| 19 | private $options; // Options used when creating the PDO object | ||
| 20 | private $db = null; // The PDO object | ||
| 21 | private $stmt = null; // The latest statement used to execute a query | ||
| 22 | private static $numQueries = 0; // Count all queries made | ||
| 23 | private static $queries = []; // Save all queries for debugging purpose | ||
| 24 | private static $params = []; // Save all parameters for debugging purpose | ||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | /** | ||
| 29 | * Constructor creating a PDO object connecting to a choosen database. | ||
| 30 | * | ||
| 31 | * @param array $options containing details for connecting to the database. | ||
| 32 | */ | ||
| 33 | 9 | public function __construct($options = []) | |
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | /** | ||
| 41 | * Set options and connection details. | ||
| 42 | * | ||
| 43 | * @param array $options containing details for connecting to the database. | ||
| 44 | * | ||
| 45 | * @return void | ||
| 46 | */ | ||
| 47 | 9 | public function setOptions($options = []) | |
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | /** | ||
| 75 | * Connect to the database. | ||
| 76 | * | ||
| 77 | * @param boolean $debug default false, set to true to throw exception with full connection details | ||
| 78 | * when connection fails. | ||
| 79 | * | ||
| 80 | * @return void | ||
| 81 | */ | ||
| 82 | 9 | public function connect($debug = false) | |
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | /** | ||
| 121 | * Set and unset verbose mode to display queries made. | ||
| 122 | * | ||
| 123 | * @param boolean $on set true to display queries made through echo, false to disable. | ||
| 124 | * | ||
| 125 | * @return void | ||
| 126 | */ | ||
| 127 | public function setVerbose($on = true) | ||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | /** | ||
| 135 | * Set fetch mode. (OBSOLETE?) | ||
| 136 | * | ||
| 137 | * @param int $fetchmode as \PDO::FETCH_OBJ, \PDO::FETCH_CLASS, \PDO::FETCH_INTO, etc. | ||
| 138 | * | ||
| 139 | * @return void | ||
| 140 | */ | ||
| 141 | public function setFetchMode($fetchmode = null) | ||
| 149 | |||
| 150 | |||
| 151 | |||
| 152 | /** | ||
| 153 | * Set fetchmode to insert Fetch one resultset from previous select statement as an object. | ||
| 154 | * | ||
| 155 | * @param string $class to insert values into. | ||
| 156 | * | ||
| 157 | * @return boolean Returns TRUE on success or FALSE on failure. | ||
| 158 | */ | ||
| 159 | public function setFetchModeClass($class) | ||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | /** | ||
| 167 | * Load query-history from session if available. | ||
| 168 | * | ||
| 169 | * @return int number of database queries made. | ||
| 170 | */ | ||
| 171 | 9 | public function loadHistory() | |
| 181 | |||
| 182 | |||
| 183 | |||
| 184 | /** | ||
| 185 | * Save query-history in session, useful as a flashmemory when redirecting to another page. | ||
| 186 | * | ||
| 187 | * @param string $extra enables to save some extra debug information. | ||
| 188 | * | ||
| 189 | * @return void | ||
| 190 | */ | ||
| 191 | public function saveHistory($extra = null) | ||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | /** | ||
| 210 | * Get how many queries have been processed. | ||
| 211 | * | ||
| 212 | * @return int number of database queries made. | ||
| 213 | */ | ||
| 214 | public function getNumQueries() | ||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | /** | ||
| 222 | * Get all the queries that have been processed. | ||
| 223 | * | ||
| 224 | * @return array with queries. | ||
| 225 | */ | ||
| 226 | public function getQueries() | ||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | /** | ||
| 234 | * Get a html representation of all queries made, for debugging and analysing purpose. | ||
| 235 | * | ||
| 236 | * @return string with html. | ||
| 237 | */ | ||
| 238 | public function dump() | ||
| 249 | |||
| 250 | |||
| 251 | |||
| 252 | /** | ||
| 253 | * Extend params array to support arrays in it, extract array items and add to $params and insert ? for each entry. | ||
| 254 | * | ||
| 255 | * @param string $query as the query to prepare. | ||
| 256 | * @param array $params the parameters that may contain arrays. | ||
| 257 | * | ||
| 258 | * @return array with query and params. | ||
| 259 | */ | ||
| 260 | 6 | protected function expandParamArray($query, $params) | |
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | /** | ||
| 290 | * Execute a select-query with arguments and return all resultset. | ||
| 291 | * | ||
| 292 | * @param string $query the SQL query with ?. | ||
| 293 | * @param array $params array which contains the argument to replace ?. | ||
| 294 | * | ||
| 295 | * @return array with resultset. | ||
| 296 | */ | ||
| 297 | 1 | public function executeFetchAll( | |
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | /** | ||
| 309 | * Execute a select-query with arguments and return one resultset. | ||
| 310 | * | ||
| 311 | * @param string $query the SQL query with ?. | ||
| 312 | * @param array $params array which contains the argument to replace ?. | ||
| 313 | * | ||
| 314 | * @return array with resultset. | ||
| 315 | */ | ||
| 316 | public function executeFetchOne( | ||
| 324 | |||
| 325 | |||
| 326 | |||
| 327 | /** | ||
| 328 | * Fetch all resultset from previous select statement. | ||
| 329 | * | ||
| 330 | * @return array with resultset. | ||
| 331 | */ | ||
| 332 | 1 | public function fetchAll() | |
| 336 | |||
| 337 | |||
| 338 | |||
| 339 | /** | ||
| 340 | * Fetch one resultset from previous select statement. | ||
| 341 | * | ||
| 342 | * @return array with resultset. | ||
| 343 | */ | ||
| 344 | public function fetchOne() | ||
| 348 | |||
| 349 | |||
| 350 | |||
| 351 | /** | ||
| 352 | * Fetch one resultset from previous select statement as an object. | ||
| 353 | * | ||
| 354 | * @param object $class which type of object to instantiate. | ||
| 355 | * | ||
| 356 | * @return array with resultset. | ||
| 357 | */ | ||
| 358 | public function fetchObject($class) | ||
| 362 | |||
| 363 | |||
| 364 | |||
| 365 | /** | ||
| 366 | * Fetch one resultset from previous select statement as an object. | ||
| 367 | * | ||
| 368 | * @param object $object to insert values into. | ||
| 369 | * | ||
| 370 | * @return array with resultset. | ||
| 371 | */ | ||
| 372 | public function fetchInto($object) | ||
| 377 | |||
| 378 | |||
| 379 | |||
| 380 | /** | ||
| 381 | * Execute a SQL-query and ignore the resultset. | ||
| 382 | * | ||
| 383 | * @param string $query the SQL query with ?. | ||
| 384 | * @param array $params array which contains the argument to replace ?. | ||
| 385 | * | ||
| 386 | * @throws Exception when failing to prepare question. | ||
| 387 | * | ||
| 388 | * @return boolean returns TRUE on success or FALSE on failure. | ||
| 389 | */ | ||
| 390 | 6 | public function execute( | |
| 442 | |||
| 443 | |||
| 444 | |||
| 445 | /** | ||
| 446 | * Return last insert id. | ||
| 447 | */ | ||
| 448 | 1 | public function lastInsertId() | |
| 452 | |||
| 453 | |||
| 454 | |||
| 455 | /** | ||
| 456 | * Return rows affected of last INSERT, UPDATE, DELETE | ||
| 457 | */ | ||
| 458 | public function rowCount() | ||
| 464 | } | ||
| 465 |