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 int |
||
| 41 | */ |
||
| 42 | protected $time; |
||
| 43 | /** |
||
| 44 | * Array for storing of data of the last executed request |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $query = [ |
||
| 49 | 'time' => '', |
||
| 50 | 'text' => '' |
||
| 51 | ]; |
||
| 52 | /** |
||
| 53 | * Array for storing data of all executed requests |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $queries = [ |
||
| 58 | 'num' => '', |
||
| 59 | 'time' => [], |
||
| 60 | 'text' => [] |
||
| 61 | ]; |
||
| 62 | /** |
||
| 63 | * Connection time |
||
| 64 | * |
||
| 65 | * @var float |
||
| 66 | */ |
||
| 67 | protected $connecting_time; |
||
| 68 | /** |
||
| 69 | * Asynchronous request |
||
| 70 | * |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | protected $async = false; |
||
| 74 | /** |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | protected $in_transaction = false; |
||
| 78 | /** |
||
| 79 | * Connecting to the DB |
||
| 80 | * |
||
| 81 | * @param string $database |
||
| 82 | * @param string $user |
||
| 83 | * @param string $password |
||
| 84 | * @param string $host |
||
| 85 | * @param string $charset |
||
| 86 | * @param string $prefix |
||
| 87 | */ |
||
| 88 | abstract function __construct ($database, $user = '', $password = '', $host = 'localhost', $charset = 'utf8', $prefix = ''); |
||
| 89 | /** |
||
| 90 | * Query |
||
| 91 | * |
||
| 92 | * SQL request into DB |
||
| 93 | * |
||
| 94 | * @abstract |
||
| 95 | * |
||
| 96 | * @param string|string[] $query SQL query string or array, may be a format string in accordance with the first parameter of sprintf() function |
||
| 97 | * @param string|string[] $params May be array of arguments for formatting of <b>$query</b><br> |
||
| 98 | * or string - in this case it will be first argument for formatting of <b>$query</b> |
||
| 99 | * @param string[] $param if <b>$params</b> is string - this parameter will be second argument for formatting of <b>$query</b>. |
||
| 100 | * If you need more arguments - add them after this one, function will accept them. |
||
| 101 | * |
||
| 102 | * @return false|object|resource |
||
| 103 | */ |
||
| 104 | function q ($query, $params = [], ...$param) { |
||
| 118 | /** |
||
| 119 | * @param string|string[] $query |
||
| 120 | * @param array $arguments |
||
| 121 | * |
||
| 122 | * @return array|false |
||
|
1 ignored issue
–
show
|
|||
| 123 | */ |
||
| 124 | protected function prepare_and_normalize_arguments ($query, $arguments) { |
||
| 148 | /** |
||
| 149 | * @param string[] $queries |
||
| 150 | * @param string[] $params |
||
| 151 | * |
||
| 152 | * @return false|object|resource |
||
| 153 | */ |
||
| 154 | protected function execute_multiple ($queries, $params) { |
||
| 165 | /** |
||
| 166 | * @param string $query |
||
| 167 | * @param string[] $params |
||
| 168 | * |
||
| 169 | * @return false|object|resource |
||
| 170 | */ |
||
| 171 | protected function execute_single ($query, $params) { |
||
| 186 | /** |
||
| 187 | * Asynchronous, Query |
||
| 188 | * |
||
| 189 | * Asynchronous SQL request into DB (if is not supported - ordinary request will me executed). |
||
| 190 | * Result of execution can't be obtained, so, use it, for example, for deleting some non-critical data |
||
| 191 | * |
||
| 192 | * @abstract |
||
| 193 | * |
||
| 194 | * @param string|string[] $query SQL query string, may be a format string in accordance with the first parameter of sprintf() function |
||
| 195 | * @param string|string[] $params May be array of arguments for formatting of <b>$query</b><br> |
||
| 196 | * or string - in this case it will be first argument for formatting of <b>$query</b> |
||
| 197 | * @param string[] $param if <b>$params</b> is string - this parameter will be second argument for formatting of <b>$query</b>. |
||
| 198 | * If you need more arguments - add them after this one, function will accept them. |
||
| 199 | * |
||
| 200 | * @return false|object|resource |
||
| 201 | */ |
||
| 202 | function aq ($query, $params = [], ...$param) { |
||
| 208 | /** |
||
| 209 | * SQL request to DB |
||
| 210 | * |
||
| 211 | * @abstract |
||
| 212 | * |
||
| 213 | * @param string|string[] $query |
||
| 214 | * |
||
| 215 | * @return false|object|resource |
||
| 216 | */ |
||
| 217 | abstract protected function q_internal ($query); |
||
| 218 | /** |
||
| 219 | * Multiple SQL request to DB |
||
| 220 | * |
||
| 221 | * @abstract |
||
| 222 | * |
||
| 223 | * @param string[] $query |
||
| 224 | * |
||
| 225 | * @return false|object|resource |
||
| 226 | */ |
||
| 227 | abstract protected function q_multi_internal ($query); |
||
| 228 | /** |
||
| 229 | * Number |
||
| 230 | * |
||
| 231 | * Getting number of selected rows |
||
| 232 | * |
||
| 233 | * @deprecated |
||
| 234 | * @todo remove after 4.x release |
||
| 235 | * |
||
| 236 | * @abstract |
||
| 237 | * |
||
| 238 | * @param object|resource $query_result |
||
| 239 | * |
||
| 240 | * @return false|int |
||
| 241 | */ |
||
| 242 | abstract function n ($query_result); |
||
| 243 | /** |
||
| 244 | * Fetch |
||
| 245 | * |
||
| 246 | * Fetch a result row as an associative array |
||
| 247 | * |
||
| 248 | * @abstract |
||
| 249 | * |
||
| 250 | * @param false|object|resource $query_result |
||
| 251 | * @param bool $single_column If <b>true</b> function will return not array with one element, but directly its value |
||
| 252 | * @param bool $array If <b>true</b> returns array of associative arrays of all fetched rows |
||
| 253 | * @param bool $indexed If <b>false</b> - associative array will be returned |
||
| 254 | * |
||
| 255 | * @return array[]|false|int|int[]|string|string[] |
||
| 256 | */ |
||
| 257 | abstract function f ($query_result, $single_column = false, $array = false, $indexed = false); |
||
| 258 | /** |
||
| 259 | * Fetch, Array |
||
| 260 | * |
||
| 261 | * Similar to ::f() method, with parameter <b>$array</b> = true |
||
| 262 | * |
||
| 263 | * @deprecated |
||
| 264 | * @todo remove after 4.x release |
||
| 265 | * |
||
| 266 | * @param false|object|resource $query_result |
||
| 267 | * @param bool $single_column If <b>true</b> function will return not array with one element, but directly its value |
||
| 268 | * @param bool $indexed If <b>false</b> - associative array will be returned |
||
| 269 | * |
||
| 270 | * @return array[]|false |
||
| 271 | */ |
||
| 272 | function fa ($query_result, $single_column = false, $indexed = false) { |
||
| 275 | /** |
||
| 276 | * Fetch, Single |
||
| 277 | * |
||
| 278 | * Similar to ::f() method, with parameter <b>$single_column</b> = true |
||
| 279 | * |
||
| 280 | * @deprecated |
||
| 281 | * @todo remove after 4.x release |
||
| 282 | * |
||
| 283 | * @param false|object|resource $query_result |
||
| 284 | * @param bool $array If <b>true</b> returns array of associative arrays of all fetched rows |
||
| 285 | * |
||
| 286 | * @return false|int|int[]|string|string[] |
||
| 287 | */ |
||
| 288 | function fs ($query_result, $array = false) { |
||
| 291 | /** |
||
| 292 | * Fetch, Array, Single |
||
| 293 | * |
||
| 294 | * Combination of ::fa() and ::fs() methods |
||
| 295 | * |
||
| 296 | * @deprecated |
||
| 297 | * @todo remove after 4.x release |
||
| 298 | * |
||
| 299 | * @param false|object|resource $query_result |
||
| 300 | * |
||
| 301 | * @return false|int[]|string[] |
||
| 302 | */ |
||
| 303 | function fas ($query_result) { |
||
| 306 | /** |
||
| 307 | * Query, Fetch |
||
| 308 | * |
||
| 309 | * Combination of ::q() and ::f() methods |
||
| 310 | * |
||
| 311 | * @param string[] $query SQL query string, or you can put all parameters, that ::q() function can accept in form of array |
||
| 312 | * |
||
| 313 | * @return array|false |
||
| 314 | */ |
||
| 315 | function qf (...$query) { |
||
| 334 | /** |
||
| 335 | * Query, Fetch, Array |
||
| 336 | * |
||
| 337 | * Short for ::f(::q(), false, true, false) |
||
| 338 | * |
||
| 339 | * @param string[] $query SQL query string, or you can put all parameters, that ::q() function can accept in form of array |
||
| 340 | * |
||
| 341 | * @return array[]|false |
||
| 342 | */ |
||
| 343 | function qfa (...$query) { |
||
| 358 | /** |
||
| 359 | * Query, Fetch, Single |
||
| 360 | * |
||
| 361 | * Short for ::f(::q(), true, false, false) |
||
| 362 | * |
||
| 363 | * @param string[] $query SQL query string, or you can put all parameters, that ::q() function can accept in form of array |
||
| 364 | * |
||
| 365 | * @return false|int|string |
||
| 366 | */ |
||
| 367 | function qfs (...$query) { |
||
| 378 | /** |
||
| 379 | * Query, Fetch, Array, Single |
||
| 380 | * |
||
| 381 | * Short for ::f(::q(), true, true, false) |
||
| 382 | * |
||
| 383 | * @param string[] $query SQL query string, or you can put all parameters, that ::q() function can accept in form of array |
||
| 384 | * |
||
| 385 | * @return false|int[]|string[] |
||
| 386 | */ |
||
| 387 | function qfas (...$query) { |
||
| 394 | /** |
||
| 395 | * Method for simplified inserting of several rows |
||
| 396 | * |
||
| 397 | * @param string $query |
||
| 398 | * @param array|array[] $params Array of array of parameters for inserting |
||
| 399 | * @param bool $join If true - inserting of several rows will be combined in one query. For this, be sure, that your query has keyword |
||
| 400 | * <i>VALUES</i> in uppercase. Part of query after this keyword will be multiplied with coma separator. |
||
| 401 | * |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | function insert ($query, $params, $join = true) { |
||
| 433 | /** |
||
| 434 | * Id |
||
| 435 | * |
||
| 436 | * Get id of last inserted row |
||
| 437 | * |
||
| 438 | * @abstract |
||
| 439 | * |
||
| 440 | * @return int |
||
| 441 | */ |
||
| 442 | abstract function id (); |
||
| 443 | /** |
||
| 444 | * Affected |
||
| 445 | * |
||
| 446 | * Get number of affected rows during last query |
||
| 447 | * |
||
| 448 | * @abstract |
||
| 449 | * |
||
| 450 | * @return int |
||
| 451 | */ |
||
| 452 | abstract function affected (); |
||
| 453 | /** |
||
| 454 | * Execute transaction |
||
| 455 | * |
||
| 456 | * All queries done inside callback will be within single transaction, throwing any exception or returning boolean `false` from callback will cause |
||
| 457 | * rollback. Nested transaction calls will be wrapped into single big outer transaction, so you might call it safely if needed. |
||
| 458 | * |
||
| 459 | * @param callable $callback This instance will be used as single argument |
||
| 460 | * |
||
| 461 | * @return bool |
||
| 462 | * |
||
| 463 | * @throws Exception Re-throws exception thrown inside callback |
||
| 464 | */ |
||
| 465 | function transaction ($callback) { |
||
| 493 | /** |
||
| 494 | * Free result memory |
||
| 495 | * |
||
| 496 | * @abstract |
||
| 497 | * |
||
| 498 | * @param object|resource $query_result |
||
| 499 | */ |
||
| 500 | abstract function free ($query_result); |
||
| 501 | /** |
||
| 502 | * Get columns list of table |
||
| 503 | * |
||
| 504 | * @param string $table |
||
| 505 | * @param false|string $like |
||
| 506 | * |
||
| 507 | * @return string[] |
||
| 508 | */ |
||
| 509 | function columns ($table, $like = false) { |
||
| 524 | /** |
||
| 525 | * Get tables list |
||
| 526 | * |
||
| 527 | * @param false|string $like |
||
| 528 | * |
||
| 529 | * @return string[] |
||
| 530 | */ |
||
| 531 | function tables ($like = false) { |
||
| 539 | /** |
||
| 540 | * Safe |
||
| 541 | * |
||
| 542 | * Preparing string for using in SQL query |
||
| 543 | * SQL Injection Protection |
||
| 544 | * |
||
| 545 | * @param string|string[] $string |
||
| 546 | * @param bool $single_quotes_around |
||
| 547 | * |
||
| 548 | * @return string|string[] |
||
| 549 | */ |
||
| 550 | function s ($string, $single_quotes_around = true) { |
||
| 559 | /** |
||
| 560 | * Preparing string for using in SQL query |
||
| 561 | * SQL Injection Protection |
||
| 562 | * |
||
| 563 | * @param string $string |
||
| 564 | * @param bool $single_quotes_around |
||
| 565 | * |
||
| 566 | * @return string |
||
| 567 | */ |
||
| 568 | abstract protected function s_internal ($string, $single_quotes_around); |
||
| 569 | /** |
||
| 570 | * Get information about server |
||
| 571 | * |
||
| 572 | * @return string |
||
| 573 | */ |
||
| 574 | abstract function server (); |
||
| 575 | /** |
||
| 576 | * Connection state |
||
| 577 | * |
||
| 578 | * @return bool |
||
| 579 | */ |
||
| 580 | function connected () { |
||
| 583 | /** |
||
| 584 | * Database type (lowercase, for example <i>mysql</i>) |
||
| 585 | * |
||
| 586 | * @return string |
||
| 587 | */ |
||
| 588 | function db_type () { |
||
| 591 | /** |
||
| 592 | * Database name |
||
| 593 | * |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | function database () { |
||
| 599 | /** |
||
| 600 | * Queries array, has 3 properties:<ul> |
||
| 601 | * <li>num - total number of performed queries |
||
| 602 | * <li>time - array with time of each query execution |
||
| 603 | * <li>text - array with text text of each query |
||
| 604 | * |
||
| 605 | * @return array |
||
| 606 | */ |
||
| 607 | function queries () { |
||
| 610 | /** |
||
| 611 | * Last query information, has 2 properties:<ul> |
||
| 612 | * <li>time - execution time |
||
| 613 | * <li>text - query text |
||
| 614 | * |
||
| 615 | * @return array |
||
| 616 | */ |
||
| 617 | function query () { |
||
| 620 | /** |
||
| 621 | * Total working time (including connection, queries execution and other delays) |
||
| 622 | * |
||
| 623 | * @return int |
||
| 624 | */ |
||
| 625 | function time () { |
||
| 628 | /** |
||
| 629 | * Connecting time |
||
| 630 | * |
||
| 631 | * @return float |
||
| 632 | */ |
||
| 633 | function connecting_time () { |
||
| 636 | /** |
||
| 637 | * Cloning restriction |
||
| 638 | * |
||
| 639 | * @final |
||
| 640 | */ |
||
| 641 | final function __clone () { |
||
| 643 | /** |
||
| 644 | * Disconnecting from DB |
||
| 645 | */ |
||
| 646 | abstract function __destruct (); |
||
| 647 | } |
||
| 648 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.