Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DB 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 DB, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | final class DB |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | public $query_count = 0; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var \mysqli|null |
||
| 25 | */ |
||
| 26 | private $mysqli_link; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var bool |
||
| 30 | */ |
||
| 31 | private $connected = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private $mysqlDefaultTimeFunctions; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $hostname = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $username = ''; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $password = ''; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $database = ''; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var int |
||
| 60 | */ |
||
| 61 | private $port = 3306; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $charset = 'utf8'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | private $socket = ''; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var bool |
||
| 75 | */ |
||
| 76 | private $session_to_db = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | private $in_transaction = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | private $convert_null_to_empty_string = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | private $ssl = false; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The path name to the key file |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | private $clientkey; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The path name to the certificate file |
||
| 102 | * |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | private $clientcert; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The path name to the certificate authority file |
||
| 109 | * |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | private $cacert; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var Debug |
||
| 116 | */ |
||
| 117 | private $debug; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var \Doctrine\DBAL\Connection|null |
||
| 121 | */ |
||
| 122 | private $doctrine_connection; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @var int|null |
||
| 126 | */ |
||
| 127 | private $affected_rows; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * __construct() |
||
| 131 | * |
||
| 132 | * @param string $hostname |
||
| 133 | * @param string $username |
||
| 134 | * @param string $password |
||
| 135 | * @param string $database |
||
| 136 | * @param int $port |
||
| 137 | * @param string $charset |
||
| 138 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return |
||
| 139 | * 'false'. Use false to disable it.</p> |
||
| 140 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 141 | * Use false to disable it.</p> |
||
| 142 | * @param string $logger_class_name |
||
| 143 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 144 | * @param array $extra_config <p> |
||
| 145 | * 'session_to_db' => bool<br> |
||
| 146 | * 'socket' => 'string (path)'<br> |
||
| 147 | * 'ssl' => bool<br> |
||
| 148 | * 'clientkey' => 'string (path)'<br> |
||
| 149 | * 'clientcert' => 'string (path)'<br> |
||
| 150 | * 'cacert' => 'string (path)'<br> |
||
| 151 | * </p> |
||
| 152 | */ |
||
| 153 | 23 | private function __construct(string $hostname, string $username, string $password, string $database, $port, string $charset, bool $exit_on_error, bool $echo_on_error, string $logger_class_name, string $logger_level, array $extra_config = []) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Prevent the instance from being cloned. |
||
| 205 | * |
||
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | private function __clone() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * __destruct |
||
| 214 | */ |
||
| 215 | public function __destruct() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param string|null $sql |
||
| 225 | * @param array $bindings |
||
| 226 | * |
||
| 227 | * @return bool|DB|int|Result <p> |
||
| 228 | * "DB" by "$sql" === null<br /> |
||
| 229 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 230 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 231 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 232 | * "true" by e.g. "DROP"-queries<br /> |
||
| 233 | * "false" on error |
||
| 234 | * </p> |
||
| 235 | */ |
||
| 236 | 4 | public function __invoke(string $sql = null, array $bindings = []) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * __wakeup |
||
| 243 | * |
||
| 244 | * @return void |
||
| 245 | */ |
||
| 246 | 4 | public function __wakeup() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Load the config from the constructor. |
||
| 253 | * |
||
| 254 | * @param string $hostname |
||
| 255 | * @param string $username |
||
| 256 | * @param string $password |
||
| 257 | * @param string $database |
||
| 258 | * @param int $port <p>default is (int)3306</p> |
||
| 259 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 260 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return |
||
| 261 | * 'false'. Use false to disable it.</p> |
||
| 262 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 263 | * Use false to disable it.</p> |
||
| 264 | * @param string $logger_class_name |
||
| 265 | * @param string $logger_level |
||
| 266 | * @param array $extra_config <p> |
||
| 267 | * 'session_to_db' => false|true<br> |
||
| 268 | * 'socket' => 'string (path)'<br> |
||
| 269 | * 'ssl' => 'bool'<br> |
||
| 270 | * 'clientkey' => 'string (path)'<br> |
||
| 271 | * 'clientcert' => 'string (path)'<br> |
||
| 272 | * 'cacert' => 'string (path)'<br> |
||
| 273 | * </p> |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | 23 | private function _loadConfig( |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 329 | * |
||
| 330 | * @param array $arrayPair |
||
| 331 | * @param string $glue <p>This is the separator.</p> |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | * |
||
| 335 | * @internal |
||
| 336 | */ |
||
| 337 | 72 | public function _parseArrayPair(array $arrayPair, string $glue = ','): string |
|
| 338 | { |
||
| 339 | // init |
||
| 340 | 72 | $sql = ''; |
|
| 341 | |||
| 342 | 72 | if (\count($arrayPair) === 0) { |
|
| 343 | return ''; |
||
| 344 | } |
||
| 345 | |||
| 346 | 72 | $arrayPairCounter = 0; |
|
| 347 | 72 | foreach ($arrayPair as $_key => $_value) { |
|
| 348 | 72 | $_connector = '='; |
|
| 349 | 72 | $_glueHelper = ''; |
|
| 350 | 72 | $_key_upper = \strtoupper($_key); |
|
| 351 | |||
| 352 | 72 | if (\strpos($_key_upper, ' NOT') !== false) { |
|
| 353 | 6 | $_connector = 'NOT'; |
|
| 354 | } |
||
| 355 | |||
| 356 | 72 | if (\strpos($_key_upper, ' IS') !== false) { |
|
| 357 | 3 | $_connector = 'IS'; |
|
| 358 | } |
||
| 359 | |||
| 360 | 72 | if (\strpos($_key_upper, ' IS NOT') !== false) { |
|
| 361 | 3 | $_connector = 'IS NOT'; |
|
| 362 | } |
||
| 363 | |||
| 364 | 72 | if (\strpos($_key_upper, ' IN') !== false) { |
|
| 365 | 3 | $_connector = 'IN'; |
|
| 366 | } |
||
| 367 | |||
| 368 | 72 | if (\strpos($_key_upper, ' NOT IN') !== false) { |
|
| 369 | 3 | $_connector = 'NOT IN'; |
|
| 370 | } |
||
| 371 | |||
| 372 | 72 | if (\strpos($_key_upper, ' BETWEEN') !== false) { |
|
| 373 | 3 | $_connector = 'BETWEEN'; |
|
| 374 | } |
||
| 375 | |||
| 376 | 72 | if (\strpos($_key_upper, ' NOT BETWEEN') !== false) { |
|
| 377 | 3 | $_connector = 'NOT BETWEEN'; |
|
| 378 | } |
||
| 379 | |||
| 380 | 72 | if (\strpos($_key_upper, ' LIKE') !== false) { |
|
| 381 | 6 | $_connector = 'LIKE'; |
|
| 382 | } |
||
| 383 | |||
| 384 | 72 | if (\strpos($_key_upper, ' NOT LIKE') !== false) { |
|
| 385 | 6 | $_connector = 'NOT LIKE'; |
|
| 386 | } |
||
| 387 | |||
| 388 | 72 | View Code Duplication | if (\strpos($_key_upper, ' >') !== false && \strpos($_key_upper, ' =') === false) { |
| 389 | 8 | $_connector = '>'; |
|
| 390 | } |
||
| 391 | |||
| 392 | 72 | View Code Duplication | if (\strpos($_key_upper, ' <') !== false && \strpos($_key_upper, ' =') === false) { |
| 393 | 3 | $_connector = '<'; |
|
| 394 | } |
||
| 395 | |||
| 396 | 72 | if (\strpos($_key_upper, ' >=') !== false) { |
|
| 397 | 8 | $_connector = '>='; |
|
| 398 | } |
||
| 399 | |||
| 400 | 72 | if (\strpos($_key_upper, ' <=') !== false) { |
|
| 401 | 3 | $_connector = '<='; |
|
| 402 | } |
||
| 403 | |||
| 404 | 72 | if (\strpos($_key_upper, ' <>') !== false) { |
|
| 405 | 3 | $_connector = '<>'; |
|
| 406 | } |
||
| 407 | |||
| 408 | 72 | if (\strpos($_key_upper, ' OR') !== false) { |
|
| 409 | 6 | $_glueHelper = 'OR'; |
|
| 410 | } |
||
| 411 | |||
| 412 | 72 | if (\strpos($_key_upper, ' AND') !== false) { |
|
| 413 | 3 | $_glueHelper = 'AND'; |
|
| 414 | } |
||
| 415 | |||
| 416 | 72 | if (\is_array($_value) === true) { |
|
| 417 | 7 | $firstKey = null; |
|
| 418 | 7 | $firstValue = null; |
|
| 419 | 7 | foreach ($_value as $oldKey => $oldValue) { |
|
| 420 | 7 | $_value[$oldKey] = $this->secure($oldValue); |
|
| 421 | |||
| 422 | 7 | if ($firstKey === null) { |
|
| 423 | 7 | $firstKey = $oldKey; |
|
| 424 | } |
||
| 425 | |||
| 426 | 7 | if ($firstValue === null) { |
|
| 427 | 7 | $firstValue = $_value[$oldKey]; |
|
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | 7 | if ($_connector === 'NOT IN' || $_connector === 'IN') { |
|
| 432 | 3 | $_value = '(' . \implode(',', $_value) . ')'; |
|
| 433 | 7 | } elseif ($_connector === 'NOT BETWEEN' || $_connector === 'BETWEEN') { |
|
| 434 | 3 | $_value = '(' . \implode(' AND ', $_value) . ')'; |
|
| 435 | 7 | } elseif ($firstKey && $firstValue) { |
|
| 436 | 1 | View Code Duplication | if (\strpos($firstKey, ' +') !== false) { |
| 437 | 1 | $firstKey = \str_replace(' +', '', $firstKey); |
|
| 438 | 1 | $_value = $firstKey . ' + ' . $firstValue; |
|
| 439 | } |
||
| 440 | |||
| 441 | 1 | View Code Duplication | if (\strpos($firstKey, ' -') !== false) { |
| 442 | 1 | $firstKey = \str_replace(' -', '', $firstKey); |
|
| 443 | 7 | $_value = $firstKey . ' - ' . $firstValue; |
|
| 444 | } |
||
| 445 | } |
||
| 446 | } else { |
||
| 447 | 72 | $_value = $this->secure($_value); |
|
| 448 | } |
||
| 449 | |||
| 450 | 72 | $quoteString = $this->quote_string( |
|
| 451 | 72 | \trim( |
|
| 452 | 72 | \str_ireplace( |
|
| 453 | [ |
||
| 454 | 72 | $_connector, |
|
| 455 | 72 | $_glueHelper, |
|
| 456 | ], |
||
| 457 | 72 | '', |
|
| 458 | 72 | $_key |
|
| 459 | ) |
||
| 460 | ) |
||
| 461 | ); |
||
| 462 | |||
| 463 | 72 | $_value = (array) $_value; |
|
| 464 | |||
| 465 | 72 | if (!$_glueHelper) { |
|
| 466 | 72 | $_glueHelper = $glue; |
|
| 467 | } |
||
| 468 | |||
| 469 | 72 | $tmpCounter = 0; |
|
| 470 | 72 | foreach ($_value as $valueInner) { |
|
| 471 | 72 | $_glueHelperInner = $_glueHelper; |
|
| 472 | |||
| 473 | 72 | if ($arrayPairCounter === 0) { |
|
| 474 | 72 | if ($tmpCounter === 0 && $_glueHelper === 'OR') { |
|
| 475 | 3 | $_glueHelperInner = '1 = 1 AND ('; // first "OR"-query glue |
|
| 476 | 72 | } elseif ($tmpCounter === 0) { |
|
| 477 | 72 | $_glueHelperInner = ''; // first query glue e.g. for "INSERT"-query -> skip the first "," |
|
| 478 | } |
||
| 479 | 68 | } elseif ($tmpCounter === 0 && $_glueHelper === 'OR') { |
|
| 480 | 3 | $_glueHelperInner = 'AND ('; // inner-loop "OR"-query glue |
|
| 481 | } |
||
| 482 | |||
| 483 | 72 | if (\is_string($valueInner) && $valueInner === '') { |
|
| 484 | $valueInner = "''"; |
||
| 485 | } |
||
| 486 | |||
| 487 | 72 | $sql .= ' ' . $_glueHelperInner . ' ' . $quoteString . ' ' . $_connector . ' ' . $valueInner . " \n"; |
|
| 488 | 72 | $tmpCounter++; |
|
| 489 | } |
||
| 490 | |||
| 491 | 72 | if ($_glueHelper === 'OR') { |
|
| 492 | 6 | $sql .= ' ) '; |
|
| 493 | } |
||
| 494 | |||
| 495 | 72 | $arrayPairCounter++; |
|
| 496 | } |
||
| 497 | |||
| 498 | 72 | return $sql; |
|
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * _parseQueryParams |
||
| 503 | * |
||
| 504 | * @param string $sql |
||
| 505 | * @param array $params |
||
| 506 | * |
||
| 507 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 508 | */ |
||
| 509 | 7 | private function _parseQueryParams(string $sql, array $params = []): array |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Returns the SQL by replacing :placeholders with SQL-escaped values. |
||
| 546 | * |
||
| 547 | * @param mixed $sql <p>The SQL string.</p> |
||
| 548 | * @param array $params <p>An array of key-value bindings.</p> |
||
| 549 | * |
||
| 550 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 551 | */ |
||
| 552 | 10 | private function _parseQueryParamsByName(string $sql, array $params = []): array |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 601 | * |
||
| 602 | * @return int |
||
| 603 | */ |
||
| 604 | 25 | public function affected_rows(): int |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Begins a transaction, by turning off auto commit. |
||
| 619 | * |
||
| 620 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 621 | */ |
||
| 622 | 18 | public function beginTransaction(): bool |
|
| 652 | |||
| 653 | /** |
||
| 654 | * Clear the errors in "_debug->_errors". |
||
| 655 | * |
||
| 656 | * @return bool |
||
| 657 | */ |
||
| 658 | 18 | public function clearErrors(): bool |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Closes a previously opened database connection. |
||
| 665 | * |
||
| 666 | * @return bool |
||
| 667 | * Will return "true", if the connection was closed, |
||
| 668 | * otherwise (e.g. if the connection was already closed) "false". |
||
| 669 | */ |
||
| 670 | 6 | public function close(): bool |
|
| 707 | |||
| 708 | /** |
||
| 709 | * Commits the current transaction and end the transaction. |
||
| 710 | * |
||
| 711 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 712 | */ |
||
| 713 | 9 | View Code Duplication | public function commit(): bool |
| 739 | |||
| 740 | /** |
||
| 741 | * Open a new connection to the MySQL server. |
||
| 742 | * |
||
| 743 | * @throws DBConnectException |
||
| 744 | * |
||
| 745 | * @return bool |
||
| 746 | */ |
||
| 747 | 20 | public function connect(): bool |
|
| 865 | |||
| 866 | /** |
||
| 867 | * Execute a "delete"-query. |
||
| 868 | * |
||
| 869 | * @param string $table |
||
| 870 | * @param array|string $where |
||
| 871 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 872 | * |
||
| 873 | * @throws QueryException |
||
| 874 | * |
||
| 875 | * @return false|int <p>false on error</p> |
||
| 876 | */ |
||
| 877 | 4 | View Code Duplication | public function delete(string $table, $where, string $databaseName = null) |
| 904 | |||
| 905 | /** |
||
| 906 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 907 | * |
||
| 908 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 909 | */ |
||
| 910 | public function endTransaction(): bool |
||
| 941 | |||
| 942 | /** |
||
| 943 | * Get all errors from "$this->errors". |
||
| 944 | * |
||
| 945 | * @return array|false <p>false === on errors</p> |
||
| 946 | */ |
||
| 947 | public function errors() |
||
| 953 | |||
| 954 | /** |
||
| 955 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 956 | * |
||
| 957 | * @param mixed $var bool: convert into "integer"<br /> |
||
| 958 | * int: int (don't change it)<br /> |
||
| 959 | * float: float (don't change it)<br /> |
||
| 960 | * null: null (don't change it)<br /> |
||
| 961 | * array: run escape() for every key => value<br /> |
||
| 962 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 963 | * @param bool $stripe_non_utf8 |
||
| 964 | * @param bool $html_entity_decode |
||
| 965 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 966 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 967 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 968 | * |
||
| 969 | * @return mixed |
||
| 970 | */ |
||
| 971 | public function escape($var = '', bool $stripe_non_utf8 = true, bool $html_entity_decode = false, $convert_array = false) |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Execute select/insert/update/delete sql-queries. |
||
| 1078 | * |
||
| 1079 | * @param string $query <p>sql-query</p> |
||
| 1080 | * @param bool $useCache optional <p>use cache?</p> |
||
| 1081 | * @param int $cacheTTL optional <p>cache-ttl in seconds</p> |
||
| 1082 | * @param DB|null $db optional <p>the database connection</p> |
||
| 1083 | * |
||
| 1084 | * @throws QueryException |
||
| 1085 | * |
||
| 1086 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 1087 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 1088 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1089 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1090 | * "false" on error |
||
| 1091 | */ |
||
| 1092 | public static function execSQL(string $query, bool $useCache = false, int $cacheTTL = 3600, self $db = null) |
||
| 1138 | |||
| 1139 | /** |
||
| 1140 | * Get all table-names via "SHOW TABLES". |
||
| 1141 | * |
||
| 1142 | * @return array |
||
| 1143 | */ |
||
| 1144 | public function getAllTables(): array |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * @return array |
||
| 1154 | */ |
||
| 1155 | public function getConfig() |
||
| 1176 | |||
| 1177 | /** |
||
| 1178 | * @return Debug |
||
| 1179 | */ |
||
| 1180 | public function getDebugger(): Debug |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * @return \Doctrine\DBAL\Connection|null|null |
||
| 1187 | */ |
||
| 1188 | public function getDoctrineConnection() |
||
| 1192 | |||
| 1193 | /** |
||
| 1194 | * @return \Doctrine\DBAL\Driver\Connection|false |
||
| 1195 | */ |
||
| 1196 | View Code Duplication | private function getDoctrinePDOConnection() |
|
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Get errors from "$this->errors". |
||
| 1210 | * |
||
| 1211 | * @return array |
||
| 1212 | */ |
||
| 1213 | public function getErrors(): array |
||
| 1217 | |||
| 1218 | /** |
||
| 1219 | * @param string $hostname <p>Hostname of the mysql server</p> |
||
| 1220 | * @param string $username <p>Username for the mysql connection</p> |
||
| 1221 | * @param string $password <p>Password for the mysql connection</p> |
||
| 1222 | * @param string $database <p>Database for the mysql connection</p> |
||
| 1223 | * @param int $port <p>default is (int)3306</p> |
||
| 1224 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1225 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 1226 | * Use false to disable it.</p> |
||
| 1227 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1228 | * Use false to disable it.</p> |
||
| 1229 | * @param string $logger_class_name |
||
| 1230 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1231 | * @param array $extra_config <p> |
||
| 1232 | * 're_connect' => bool<br> |
||
| 1233 | * 'session_to_db' => bool<br> |
||
| 1234 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 1235 | * 'socket' => 'string (path)'<br> |
||
| 1236 | * 'ssl' => bool<br> |
||
| 1237 | * 'clientkey' => 'string (path)'<br> |
||
| 1238 | * 'clientcert' => 'string (path)'<br> |
||
| 1239 | * 'cacert' => 'string (path)'<br> |
||
| 1240 | * </p> |
||
| 1241 | * |
||
| 1242 | * @return self |
||
| 1243 | */ |
||
| 1244 | public static function getInstance( |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * @param \Doctrine\DBAL\Connection $doctrine |
||
| 1327 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1328 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will |
||
| 1329 | * return 'false'. Use false to disable it.</p> |
||
| 1330 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1331 | * Use false to disable it.</p> |
||
| 1332 | * @param string $logger_class_name |
||
| 1333 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1334 | * @param array $extra_config <p> |
||
| 1335 | * 're_connect' => bool<br> |
||
| 1336 | * 'session_to_db' => bool<br> |
||
| 1337 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 1338 | * 'socket' => 'string (path)'<br> |
||
| 1339 | * 'ssl' => bool<br> |
||
| 1340 | * 'clientkey' => 'string (path)'<br> |
||
| 1341 | * 'clientcert' => 'string (path)'<br> |
||
| 1342 | * 'cacert' => 'string (path)'<br> |
||
| 1343 | * </p> |
||
| 1344 | * |
||
| 1345 | * @return self |
||
| 1346 | */ |
||
| 1347 | public static function getInstanceDoctrineHelper( |
||
| 1372 | |||
| 1373 | /** |
||
| 1374 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1375 | * |
||
| 1376 | * @return \mysqli|null |
||
| 1377 | */ |
||
| 1378 | public function getLink() |
||
| 1382 | |||
| 1383 | /** |
||
| 1384 | * Get the current charset. |
||
| 1385 | * |
||
| 1386 | * @return string |
||
| 1387 | */ |
||
| 1388 | public function get_charset(): string |
||
| 1392 | |||
| 1393 | /** |
||
| 1394 | * Check if we are in a transaction. |
||
| 1395 | * |
||
| 1396 | * @return bool |
||
| 1397 | */ |
||
| 1398 | public function inTransaction(): bool |
||
| 1402 | |||
| 1403 | /** |
||
| 1404 | * Execute a "insert"-query. |
||
| 1405 | * |
||
| 1406 | * @param string $table |
||
| 1407 | * @param array $data |
||
| 1408 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1409 | * |
||
| 1410 | * @throws QueryException |
||
| 1411 | * |
||
| 1412 | * @return false|int <p>false on error</p> |
||
| 1413 | */ |
||
| 1414 | public function insert(string $table, array $data = [], string $databaseName = null) |
||
| 1441 | |||
| 1442 | /** |
||
| 1443 | * Returns the auto generated id used in the last query. |
||
| 1444 | * |
||
| 1445 | * @return int|string |
||
| 1446 | */ |
||
| 1447 | public function insert_id() |
||
| 1457 | |||
| 1458 | /** |
||
| 1459 | * @return bool |
||
| 1460 | */ |
||
| 1461 | public function isDoctrineMySQLiConnection(): bool |
||
| 1472 | |||
| 1473 | /** |
||
| 1474 | * @return bool |
||
| 1475 | */ |
||
| 1476 | View Code Duplication | public function isDoctrinePDOConnection(): bool |
|
| 1487 | |||
| 1488 | /** |
||
| 1489 | * Check if db-connection is ready. |
||
| 1490 | * |
||
| 1491 | * @return bool |
||
| 1492 | */ |
||
| 1493 | public function isReady(): bool |
||
| 1497 | |||
| 1498 | /** |
||
| 1499 | * Get the last sql-error. |
||
| 1500 | * |
||
| 1501 | * @return false|string <p>false === there was no error</p> |
||
| 1502 | */ |
||
| 1503 | public function lastError() |
||
| 1509 | |||
| 1510 | /** |
||
| 1511 | * Execute a sql-multi-query. |
||
| 1512 | * |
||
| 1513 | * @param string $sql |
||
| 1514 | * |
||
| 1515 | * @throws QueryException |
||
| 1516 | * |
||
| 1517 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1518 | * "bool" by only "<b>INSERT</b>"-queries<br /> |
||
| 1519 | * "bool" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1520 | * "bool" by only by e.g. "DROP"-queries<br /> |
||
| 1521 | */ |
||
| 1522 | public function multi_query(string $sql) |
||
| 1646 | |||
| 1647 | /** |
||
| 1648 | * Count number of rows found matching a specific query. |
||
| 1649 | * |
||
| 1650 | * @param string $query |
||
| 1651 | * |
||
| 1652 | * @return int |
||
| 1653 | */ |
||
| 1654 | public function num_rows(string $query): int |
||
| 1668 | |||
| 1669 | /** |
||
| 1670 | * Pings a server connection, or tries to reconnect |
||
| 1671 | * if the connection has gone down. |
||
| 1672 | * |
||
| 1673 | * @return bool |
||
| 1674 | */ |
||
| 1675 | public function ping(): bool |
||
| 1695 | |||
| 1696 | /** |
||
| 1697 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1698 | * |
||
| 1699 | * @param string $query |
||
| 1700 | * |
||
| 1701 | * @return Prepare |
||
| 1702 | */ |
||
| 1703 | public function prepare(string $query): Prepare |
||
| 1707 | |||
| 1708 | /** |
||
| 1709 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1710 | * |
||
| 1711 | * @param string $query |
||
| 1712 | * |
||
| 1713 | * @throws \Exception |
||
| 1714 | * |
||
| 1715 | * @return mixed |
||
| 1716 | * |
||
| 1717 | * @deprecated |
||
| 1718 | */ |
||
| 1719 | public static function qry(string $query) |
||
| 1744 | |||
| 1745 | /** |
||
| 1746 | * Execute a sql-query. |
||
| 1747 | * |
||
| 1748 | * example: |
||
| 1749 | * <code> |
||
| 1750 | * $sql = "INSERT INTO TABLE_NAME_HERE |
||
| 1751 | * SET |
||
| 1752 | * foo = :foo, |
||
| 1753 | * bar = :bar |
||
| 1754 | * "; |
||
| 1755 | * $insert_id = $db->query( |
||
| 1756 | * $sql, |
||
| 1757 | * [ |
||
| 1758 | * 'foo' => 1.1, |
||
| 1759 | * 'bar' => 1, |
||
| 1760 | * ] |
||
| 1761 | * ); |
||
| 1762 | * </code> |
||
| 1763 | * |
||
| 1764 | * @param string $sql <p>The sql query-string.</p> |
||
| 1765 | * @param array|bool $params <p> |
||
| 1766 | * "array" of sql-query-parameters<br/> |
||
| 1767 | * "false" if you don't need any parameter (default)<br/> |
||
| 1768 | * </p> |
||
| 1769 | * |
||
| 1770 | * @throws QueryException |
||
| 1771 | * |
||
| 1772 | * @return bool|int|Result <p> |
||
| 1773 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1774 | * "int|string" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1775 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1776 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1777 | * "false" on error |
||
| 1778 | * </p> |
||
| 1779 | */ |
||
| 1780 | public function query(string $sql = '', $params = false) |
||
| 1914 | |||
| 1915 | /** |
||
| 1916 | * Error-handling for the sql-query. |
||
| 1917 | * |
||
| 1918 | * @param string $errorMessage |
||
| 1919 | * @param int $errorNumber |
||
| 1920 | * @param string $sql |
||
| 1921 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1922 | * @param bool $sqlMultiQuery |
||
| 1923 | * |
||
| 1924 | * @throws QueryException |
||
| 1925 | * @throws DBGoneAwayException |
||
| 1926 | * |
||
| 1927 | * @return false|mixed |
||
| 1928 | */ |
||
| 1929 | private function queryErrorHandling(string $errorMessage, int $errorNumber, string $sql, $sqlParams = false, bool $sqlMultiQuery = false) |
||
| 1973 | |||
| 1974 | /** |
||
| 1975 | * Quote && Escape e.g. a table name string. |
||
| 1976 | * |
||
| 1977 | * @param mixed $str |
||
| 1978 | * |
||
| 1979 | * @return string |
||
| 1980 | */ |
||
| 1981 | public function quote_string($str): string |
||
| 1994 | |||
| 1995 | /** |
||
| 1996 | * Reconnect to the MySQL-Server. |
||
| 1997 | * |
||
| 1998 | * @param bool $checkViaPing |
||
| 1999 | * |
||
| 2000 | * @return bool |
||
| 2001 | */ |
||
| 2002 | public function reconnect(bool $checkViaPing = false): bool |
||
| 2016 | |||
| 2017 | /** |
||
| 2018 | * Execute a "replace"-query. |
||
| 2019 | * |
||
| 2020 | * @param string $table |
||
| 2021 | * @param array $data |
||
| 2022 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2023 | * |
||
| 2024 | * @throws QueryException |
||
| 2025 | * |
||
| 2026 | * @return false|int <p>false on error</p> |
||
| 2027 | */ |
||
| 2028 | public function replace(string $table, array $data = [], string $databaseName = null) |
||
| 2069 | |||
| 2070 | /** |
||
| 2071 | * Rollback in a transaction and end the transaction. |
||
| 2072 | * |
||
| 2073 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2074 | */ |
||
| 2075 | View Code Duplication | public function rollback(): bool |
|
| 2101 | |||
| 2102 | /** |
||
| 2103 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 2104 | * |
||
| 2105 | * <p> |
||
| 2106 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 2107 | * 1. parse into "int" |
||
| 2108 | * </p><br /> |
||
| 2109 | * |
||
| 2110 | * <p> |
||
| 2111 | * <strong>float:</strong><br /> |
||
| 2112 | * 1. return "float" |
||
| 2113 | * </p><br /> |
||
| 2114 | * |
||
| 2115 | * <p> |
||
| 2116 | * <strong>string:</strong><br /> |
||
| 2117 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 2118 | * 2. trim '<br /> |
||
| 2119 | * 3. escape the string (and remove non utf-8 chars)<br /> |
||
| 2120 | * 4. trim ' again (because we maybe removed some chars)<br /> |
||
| 2121 | * 5. add ' around the new string<br /> |
||
| 2122 | * </p><br /> |
||
| 2123 | * |
||
| 2124 | * <p> |
||
| 2125 | * <strong>array:</strong><br /> |
||
| 2126 | * 1. return null |
||
| 2127 | * </p><br /> |
||
| 2128 | * |
||
| 2129 | * <p> |
||
| 2130 | * <strong>object:</strong><br /> |
||
| 2131 | * 1. return false |
||
| 2132 | * </p><br /> |
||
| 2133 | * |
||
| 2134 | * <p> |
||
| 2135 | * <strong>null:</strong><br /> |
||
| 2136 | * 1. return null |
||
| 2137 | * </p> |
||
| 2138 | * |
||
| 2139 | * @param mixed $var |
||
| 2140 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 2141 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 2142 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 2143 | * |
||
| 2144 | * @return mixed |
||
| 2145 | */ |
||
| 2146 | public function secure($var, $convert_array = true) |
||
| 2209 | |||
| 2210 | /** |
||
| 2211 | * Execute a "select"-query. |
||
| 2212 | * |
||
| 2213 | * @param string $table |
||
| 2214 | * @param array|string $where |
||
| 2215 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2216 | * |
||
| 2217 | * @throws QueryException |
||
| 2218 | * |
||
| 2219 | * @return false|Result <p>false on error</p> |
||
| 2220 | */ |
||
| 2221 | View Code Duplication | public function select(string $table, $where = '1=1', string $databaseName = null) |
|
| 2248 | |||
| 2249 | /** |
||
| 2250 | * Selects a different database than the one specified on construction. |
||
| 2251 | * |
||
| 2252 | * @param string $database <p>Database name to switch to.</p> |
||
| 2253 | * |
||
| 2254 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2255 | */ |
||
| 2256 | public function select_db(string $database): bool |
||
| 2272 | |||
| 2273 | /** |
||
| 2274 | * @param array $extra_config <p> |
||
| 2275 | * 'session_to_db' => false|true<br> |
||
| 2276 | * 'socket' => 'string (path)'<br> |
||
| 2277 | * 'ssl' => 'bool'<br> |
||
| 2278 | * 'clientkey' => 'string (path)'<br> |
||
| 2279 | * 'clientcert' => 'string (path)'<br> |
||
| 2280 | * 'cacert' => 'string (path)'<br> |
||
| 2281 | * </p> |
||
| 2282 | */ |
||
| 2283 | public function setConfigExtra(array $extra_config) |
||
| 2317 | |||
| 2318 | /** |
||
| 2319 | * Set the current charset. |
||
| 2320 | * |
||
| 2321 | * @param string $charset |
||
| 2322 | * |
||
| 2323 | * @return bool |
||
| 2324 | */ |
||
| 2325 | public function set_charset(string $charset): bool |
||
| 2361 | |||
| 2362 | /** |
||
| 2363 | * Set the option to convert null to "''" (empty string). |
||
| 2364 | * |
||
| 2365 | * Used in secure() => select(), insert(), update(), delete() |
||
| 2366 | * |
||
| 2367 | * @deprecated It's not recommended to convert NULL into an empty string! |
||
| 2368 | * |
||
| 2369 | * @param bool $bool |
||
| 2370 | * |
||
| 2371 | * @return self |
||
| 2372 | */ |
||
| 2373 | public function set_convert_null_to_empty_string(bool $bool): self |
||
| 2379 | |||
| 2380 | /** |
||
| 2381 | * Enables or disables internal report functions |
||
| 2382 | * |
||
| 2383 | * @see http://php.net/manual/en/function.mysqli-report.php |
||
| 2384 | * |
||
| 2385 | * @param int $flags <p> |
||
| 2386 | * <table> |
||
| 2387 | * Supported flags |
||
| 2388 | * <tr valign="top"> |
||
| 2389 | * <td>Name</td> |
||
| 2390 | * <td>Description</td> |
||
| 2391 | * </tr> |
||
| 2392 | * <tr valign="top"> |
||
| 2393 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 2394 | * <td>Turns reporting off</td> |
||
| 2395 | * </tr> |
||
| 2396 | * <tr valign="top"> |
||
| 2397 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 2398 | * <td>Report errors from mysqli function calls</td> |
||
| 2399 | * </tr> |
||
| 2400 | * <tr valign="top"> |
||
| 2401 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 2402 | * <td> |
||
| 2403 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 2404 | * instead of warnings |
||
| 2405 | * </td> |
||
| 2406 | * </tr> |
||
| 2407 | * <tr valign="top"> |
||
| 2408 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 2409 | * <td>Report if no index or bad index was used in a query</td> |
||
| 2410 | * </tr> |
||
| 2411 | * <tr valign="top"> |
||
| 2412 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 2413 | * <td>Set all options (report all)</td> |
||
| 2414 | * </tr> |
||
| 2415 | * </table> |
||
| 2416 | * </p> |
||
| 2417 | * |
||
| 2418 | * @return bool |
||
| 2419 | */ |
||
| 2420 | public function set_mysqli_report(int $flags): bool |
||
| 2432 | |||
| 2433 | /** |
||
| 2434 | * Show config errors by throw exceptions. |
||
| 2435 | * |
||
| 2436 | * @throws \InvalidArgumentException |
||
| 2437 | * |
||
| 2438 | * @return bool |
||
| 2439 | */ |
||
| 2440 | public function showConfigError(): bool |
||
| 2475 | |||
| 2476 | /** |
||
| 2477 | * alias: "beginTransaction()" |
||
| 2478 | */ |
||
| 2479 | public function startTransaction(): bool |
||
| 2483 | |||
| 2484 | /** |
||
| 2485 | * Determine if database table exists |
||
| 2486 | * |
||
| 2487 | * @param string $table |
||
| 2488 | * |
||
| 2489 | * @return bool |
||
| 2490 | */ |
||
| 2491 | public function table_exists(string $table): bool |
||
| 2501 | |||
| 2502 | /** |
||
| 2503 | * Execute a callback inside a transaction. |
||
| 2504 | * |
||
| 2505 | * @param callback $callback <p>The callback to run inside the transaction, if it's throws an "Exception" or if it's |
||
| 2506 | * returns "false", all SQL-statements in the callback will be rollbacked.</p> |
||
| 2507 | * |
||
| 2508 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2509 | */ |
||
| 2510 | public function transact($callback): bool |
||
| 2533 | |||
| 2534 | /** |
||
| 2535 | * Execute a "update"-query. |
||
| 2536 | * |
||
| 2537 | * @param string $table |
||
| 2538 | * @param array $data |
||
| 2539 | * @param array|string $where |
||
| 2540 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2541 | * |
||
| 2542 | * @throws QueryException |
||
| 2543 | * |
||
| 2544 | * @return false|int <p>false on error</p> |
||
| 2545 | */ |
||
| 2546 | public function update(string $table, array $data = [], $where = '1=1', string $databaseName = null) |
||
| 2587 | } |
||
| 2588 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: