Complex classes like _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 _Abstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | abstract class _Abstract { |
||
| 13 | /** |
||
| 14 | * Is connection established |
||
| 15 | * |
||
| 16 | * @var bool |
||
| 17 | */ |
||
| 18 | protected $connected = false; |
||
| 19 | /** |
||
| 20 | * DB type, may be used for constructing requests, accounting particular features of current DB (lowercase name) |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $db_type = ''; |
||
| 25 | /** |
||
| 26 | * Current DB |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $database; |
||
| 31 | /** |
||
| 32 | * Current prefix |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $prefix; |
||
| 37 | /** |
||
| 38 | * Total time of requests execution |
||
| 39 | * |
||
| 40 | * @var float |
||
| 41 | */ |
||
| 42 | protected $time = 0; |
||
| 43 | /** |
||
| 44 | * Total number of executed requests |
||
| 45 | * |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | protected $queries_count = 0; |
||
| 49 | /** |
||
| 50 | * Connection time |
||
| 51 | * |
||
| 52 | * @var float |
||
| 53 | */ |
||
| 54 | protected $connecting_time = 0; |
||
| 55 | /** |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $in_transaction = false; |
||
| 59 | /** |
||
| 60 | * Connecting to the DB |
||
| 61 | * |
||
| 62 | * @param string $database |
||
| 63 | * @param string $user |
||
| 64 | * @param string $password |
||
| 65 | * @param string $host |
||
| 66 | * @param string $prefix |
||
| 67 | */ |
||
| 68 | abstract public function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = ''); |
||
| 69 | /** |
||
| 70 | * Query |
||
| 71 | * |
||
| 72 | * SQL request into DB |
||
| 73 | * |
||
| 74 | * @abstract |
||
| 75 | * |
||
| 76 | * @param string|string[] $query SQL query string or array, may be a format string in accordance with the first parameter of sprintf() function or may |
||
| 77 | * contain markers for prepared statements (but not both at the same time) |
||
| 78 | * @param array $parameters There might be arbitrary number of parameters for formatting SQL statement or for using in prepared statements.<br> |
||
| 79 | * If an array provided as second argument - its items will be used, so that you can either specify parameters as an |
||
| 80 | * array, or in line. |
||
| 81 | * |
||
| 82 | * @return bool|object|resource |
||
| 83 | */ |
||
| 84 | 64 | public function q ($query, ...$parameters) { |
|
| 98 | /** |
||
| 99 | * @param string|string[] $query |
||
| 100 | * @param array $parameters |
||
| 101 | * |
||
| 102 | * @return array|false |
||
| 103 | */ |
||
| 104 | 64 | protected function normalize_parameters ($query, $parameters) { |
|
| 118 | /** |
||
| 119 | * @param string[] $queries |
||
| 120 | * @param string[] $parameters |
||
| 121 | * |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | 16 | protected function execute_multiple ($queries, $parameters) { |
|
| 142 | /** |
||
| 143 | * @param string $query |
||
| 144 | * @param string[] $parameters |
||
| 145 | * |
||
| 146 | * @return array |
||
| 147 | */ |
||
| 148 | 64 | protected function prepare_query_and_parameters ($query, $parameters) { |
|
| 157 | /** |
||
| 158 | * @param string $query |
||
| 159 | * @param string[] $parameters |
||
| 160 | * |
||
| 161 | * @return bool|object|resource |
||
| 162 | */ |
||
| 163 | 64 | protected function execute_single ($query, $parameters) { |
|
| 171 | /** |
||
| 172 | * SQL request to DB |
||
| 173 | * |
||
| 174 | * @abstract |
||
| 175 | * |
||
| 176 | * @param string $query |
||
| 177 | * @param string[] $parameters If not empty, than server-side prepared statements should be used |
||
| 178 | * |
||
| 179 | * @return bool|object|resource |
||
| 180 | */ |
||
| 181 | abstract protected function q_internal ($query, $parameters = []); |
||
| 182 | /** |
||
| 183 | * Multiple SQL request to DB |
||
| 184 | * |
||
| 185 | * @abstract |
||
| 186 | * |
||
| 187 | * @param string[] $query |
||
| 188 | * @param string[] $parameters If not empty, than server-side prepared statements should be used |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | 16 | protected function q_multi_internal ($query, $parameters = []) { |
|
| 199 | /** |
||
| 200 | * Fetch |
||
| 201 | * |
||
| 202 | * Fetch a result row as an associative array |
||
| 203 | * |
||
| 204 | * @abstract |
||
| 205 | * |
||
| 206 | * @param false|object|resource $query_result |
||
| 207 | * @param bool $single_column If <b>true</b> function will return not array with one element, but directly its value |
||
| 208 | * @param bool $array If <b>true</b> returns array of associative arrays of all fetched rows |
||
| 209 | * @param bool $indexed If <b>false</b> - associative array will be returned |
||
| 210 | * |
||
| 211 | * @return array[]|false|int|int[]|string|string[] |
||
| 212 | */ |
||
| 213 | abstract public function f ($query_result, $single_column = false, $array = false, $indexed = false); |
||
| 214 | /** |
||
| 215 | * Query, Fetch |
||
| 216 | * |
||
| 217 | * Short for `::f(::q())`, arguments are exactly the same as in `::q()` |
||
| 218 | * |
||
| 219 | * @param string[] $query |
||
| 220 | * |
||
| 221 | * @return array|false |
||
| 222 | */ |
||
| 223 | 54 | public function qf (...$query) { |
|
| 226 | /** |
||
| 227 | * Query, Fetch, Array |
||
| 228 | * |
||
| 229 | * Short for `::f(::q(), false, true)`, arguments are exactly the same as in `::q()` |
||
| 230 | * |
||
| 231 | * @param string[] $query |
||
| 232 | * |
||
| 233 | * @return array[]|false |
||
| 234 | */ |
||
| 235 | 34 | public function qfa (...$query) { |
|
| 238 | /** |
||
| 239 | * Query, Fetch, Single |
||
| 240 | * |
||
| 241 | * Short for `::f(::q(), true)`, arguments are exactly the same as in `::q()` |
||
| 242 | * |
||
| 243 | * @param string[] $query |
||
| 244 | * |
||
| 245 | * @return false|int|string |
||
| 246 | */ |
||
| 247 | 48 | public function qfs (...$query) { |
|
| 250 | /** |
||
| 251 | * Query, Fetch, Array, Single |
||
| 252 | * |
||
| 253 | * Short for `::f(::q(), true, true)`, arguments are exactly the same as in `::q()` |
||
| 254 | * |
||
| 255 | * @param string[] $query |
||
| 256 | * |
||
| 257 | * @return false|int[]|string[] |
||
| 258 | */ |
||
| 259 | 47 | public function qfas (...$query) { |
|
| 262 | /** |
||
| 263 | * Method for simplified inserting of several rows |
||
| 264 | * |
||
| 265 | * @param string $query |
||
| 266 | * @param array|array[] $parameters Array of array of parameters for inserting |
||
| 267 | * @param bool $join If true - inserting of several rows will be combined in one query. For this, be sure, that your query has keyword |
||
| 268 | * <i>VALUES</i> in uppercase. Part of query after this keyword will be multiplied with coma separator. |
||
| 269 | * |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | 42 | public function insert ($query, $parameters, $join = true) { |
|
| 298 | /** |
||
| 299 | * Id |
||
| 300 | * |
||
| 301 | * Get id of last inserted row |
||
| 302 | * |
||
| 303 | * @abstract |
||
| 304 | * |
||
| 305 | * @return int |
||
| 306 | */ |
||
| 307 | abstract public function id (); |
||
| 308 | /** |
||
| 309 | * Affected |
||
| 310 | * |
||
| 311 | * Get number of affected rows during last query |
||
| 312 | * |
||
| 313 | * @abstract |
||
| 314 | * |
||
| 315 | * @return int |
||
| 316 | */ |
||
| 317 | abstract public function affected (); |
||
| 318 | /** |
||
| 319 | * Execute transaction |
||
| 320 | * |
||
| 321 | * All queries done inside callback will be within single transaction, throwing any exception or returning boolean `false` from callback will cause |
||
| 322 | * rollback. Nested transaction calls will be wrapped into single big outer transaction, so you might call it safely if needed. |
||
| 323 | * |
||
| 324 | * @param callable $callback This instance will be used as single argument |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | * |
||
| 328 | * @throws Exception Re-throws exception thrown inside callback |
||
| 329 | */ |
||
| 330 | 54 | public function transaction ($callback) { |
|
| 359 | /** |
||
| 360 | * Free result memory |
||
| 361 | * |
||
| 362 | * @abstract |
||
| 363 | * |
||
| 364 | * @param false|object|resource $query_result |
||
| 365 | */ |
||
| 366 | abstract public function free ($query_result); |
||
| 367 | /** |
||
| 368 | * Get columns list of table |
||
| 369 | * |
||
| 370 | * @param string $table |
||
| 371 | * @param false|string $like |
||
| 372 | * |
||
| 373 | * @return string[] |
||
| 374 | */ |
||
| 375 | abstract public function columns ($table, $like = false); |
||
| 376 | /** |
||
| 377 | * Get tables list |
||
| 378 | * |
||
| 379 | * @param false|string $like |
||
| 380 | * |
||
| 381 | * @return string[] |
||
| 382 | */ |
||
| 383 | abstract public function tables ($like = false); |
||
| 384 | /** |
||
| 385 | * Safe |
||
| 386 | * |
||
| 387 | * Preparing string for using in SQL query |
||
| 388 | * SQL Injection Protection |
||
| 389 | * |
||
| 390 | * @param string|string[] $string |
||
| 391 | * @param bool $single_quotes_around |
||
| 392 | * |
||
| 393 | * @return string|string[] |
||
| 394 | */ |
||
| 395 | 58 | public function s ($string, $single_quotes_around = true) { |
|
| 404 | /** |
||
| 405 | * Preparing string for using in SQL query |
||
| 406 | * SQL Injection Protection |
||
| 407 | * |
||
| 408 | * @param string $string |
||
| 409 | * @param bool $single_quotes_around |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | abstract protected function s_internal ($string, $single_quotes_around); |
||
| 414 | /** |
||
| 415 | * Get information about server |
||
| 416 | * |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | abstract public function server (); |
||
| 420 | /** |
||
| 421 | * Connection state |
||
| 422 | * |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | 66 | public function connected () { |
|
| 428 | /** |
||
| 429 | * Database type (lowercase, for example <i>mysql</i>) |
||
| 430 | * |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | 2 | public function db_type () { |
|
| 436 | /** |
||
| 437 | * Database name |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | 4 | public function database () { |
|
| 444 | /** |
||
| 445 | * Number of made SQL queries |
||
| 446 | * |
||
| 447 | * @return int |
||
| 448 | */ |
||
| 449 | 4 | public function queries_count () { |
|
| 452 | /** |
||
| 453 | * Total working time (including connection, queries execution and other delays) |
||
| 454 | * |
||
| 455 | * @return float |
||
| 456 | */ |
||
| 457 | 4 | public function time () { |
|
| 460 | /** |
||
| 461 | * Connecting time |
||
| 462 | * |
||
| 463 | * @return float |
||
| 464 | */ |
||
| 465 | 4 | public function connecting_time () { |
|
| 468 | /** |
||
| 469 | * Cloning restriction |
||
| 470 | * |
||
| 471 | * @final |
||
| 472 | */ |
||
| 473 | final protected function __clone () { |
||
| 475 | /** |
||
| 476 | * Disconnecting from DB |
||
| 477 | */ |
||
| 478 | abstract public function __destruct (); |
||
| 479 | } |
||
| 480 |