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 |
||
| 18 | final class DB |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | public $query_count = 0; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var \mysqli|null |
||
| 28 | */ |
||
| 29 | private $mysqli_link; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | private $connected = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $mysqlDefaultTimeFunctions; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $hostname = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $username = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $password = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $database = ''; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | private $port = 3306; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $charset = 'utf8'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $socket = ''; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | private $session_to_db = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | private $in_transaction = false; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | private $convert_null_to_empty_string = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | private $ssl = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The path name to the key file |
||
| 98 | * |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $clientkey; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The path name to the certificate file |
||
| 105 | * |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | private $clientcert; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The path name to the certificate authority file |
||
| 112 | * |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | private $cacert; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var Debug |
||
| 119 | */ |
||
| 120 | private $debug; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var null|\Doctrine\DBAL\Connection |
||
| 124 | */ |
||
| 125 | private $doctrine_connection; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var null|int |
||
| 129 | */ |
||
| 130 | private $affected_rows; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * __construct() |
||
| 134 | * |
||
| 135 | * @param string $hostname |
||
| 136 | * @param string $username |
||
| 137 | * @param string $password |
||
| 138 | * @param string $database |
||
| 139 | * @param int $port |
||
| 140 | * @param string $charset |
||
| 141 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 142 | * Use false to disable it.</p> |
||
| 143 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 144 | * Use false to disable it.</p> |
||
| 145 | * @param string $logger_class_name |
||
| 146 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 147 | * @param array $extra_config <p> |
||
| 148 | * 'session_to_db' => bool<br> |
||
| 149 | * 'socket' => 'string (path)'<br> |
||
| 150 | * 'ssl' => bool<br> |
||
| 151 | * 'clientkey' => 'string (path)'<br> |
||
| 152 | * 'clientcert' => 'string (path)'<br> |
||
| 153 | * 'cacert' => 'string (path)'<br> |
||
| 154 | * </p> |
||
| 155 | */ |
||
| 156 | 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 = []) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Prevent the instance from being cloned. |
||
| 208 | * |
||
| 209 | * @return void |
||
| 210 | */ |
||
| 211 | private function __clone() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * __destruct |
||
| 217 | */ |
||
| 218 | public function __destruct() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param null|string $sql |
||
| 228 | * @param array $bindings |
||
| 229 | * |
||
| 230 | * @return bool|int|Result|DB <p> |
||
| 231 | * "DB" by "$sql" === null<br /> |
||
| 232 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 233 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 234 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 235 | * "true" by e.g. "DROP"-queries<br /> |
||
| 236 | * "false" on error |
||
| 237 | * </p> |
||
| 238 | */ |
||
| 239 | 4 | public function __invoke(string $sql = null, array $bindings = []) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * __wakeup |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | 4 | public function __wakeup() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Load the config from the constructor. |
||
| 256 | * |
||
| 257 | * @param string $hostname |
||
| 258 | * @param string $username |
||
| 259 | * @param string $password |
||
| 260 | * @param string $database |
||
| 261 | * @param int $port <p>default is (int)3306</p> |
||
| 262 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 263 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 264 | * Use false to disable it.</p> |
||
| 265 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 266 | * Use false to disable it.</p> |
||
| 267 | * @param string $logger_class_name |
||
| 268 | * @param string $logger_level |
||
| 269 | * @param array $extra_config <p> |
||
| 270 | * 'session_to_db' => false|true<br> |
||
| 271 | * 'socket' => 'string (path)'<br> |
||
| 272 | * 'ssl' => 'bool'<br> |
||
| 273 | * 'clientkey' => 'string (path)'<br> |
||
| 274 | * 'clientcert' => 'string (path)'<br> |
||
| 275 | * 'cacert' => 'string (path)'<br> |
||
| 276 | * </p> |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | */ |
||
| 280 | 23 | private function _loadConfig( |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 333 | * |
||
| 334 | * @param array $arrayPair |
||
| 335 | * @param string $glue <p>This is the separator.</p> |
||
| 336 | * |
||
| 337 | * @return string |
||
| 338 | * |
||
| 339 | * @internal |
||
| 340 | */ |
||
| 341 | 72 | public function _parseArrayPair(array $arrayPair, string $glue = ','): string |
|
| 342 | { |
||
| 343 | // init |
||
| 344 | 72 | $sql = ''; |
|
| 345 | |||
| 346 | 72 | if (\count($arrayPair) === 0) { |
|
| 347 | return ''; |
||
| 348 | } |
||
| 349 | |||
| 350 | 72 | $arrayPairCounter = 0; |
|
| 351 | 72 | foreach ($arrayPair as $_key => $_value) { |
|
| 352 | 72 | $_connector = '='; |
|
| 353 | 72 | $_glueHelper = ''; |
|
| 354 | 72 | $_key_upper = \strtoupper($_key); |
|
| 355 | |||
| 356 | 72 | if (\strpos($_key_upper, ' NOT') !== false) { |
|
| 357 | 6 | $_connector = 'NOT'; |
|
| 358 | } |
||
| 359 | |||
| 360 | 72 | if (\strpos($_key_upper, ' IS') !== false) { |
|
| 361 | 3 | $_connector = 'IS'; |
|
| 362 | } |
||
| 363 | |||
| 364 | 72 | if (\strpos($_key_upper, ' IS NOT') !== false) { |
|
| 365 | 3 | $_connector = 'IS NOT'; |
|
| 366 | } |
||
| 367 | |||
| 368 | 72 | if (\strpos($_key_upper, ' IN') !== false) { |
|
| 369 | 3 | $_connector = 'IN'; |
|
| 370 | } |
||
| 371 | |||
| 372 | 72 | if (\strpos($_key_upper, ' NOT IN') !== false) { |
|
| 373 | 3 | $_connector = 'NOT IN'; |
|
| 374 | } |
||
| 375 | |||
| 376 | 72 | if (\strpos($_key_upper, ' BETWEEN') !== false) { |
|
| 377 | 3 | $_connector = 'BETWEEN'; |
|
| 378 | } |
||
| 379 | |||
| 380 | 72 | if (\strpos($_key_upper, ' NOT BETWEEN') !== false) { |
|
| 381 | 3 | $_connector = 'NOT BETWEEN'; |
|
| 382 | } |
||
| 383 | |||
| 384 | 72 | if (\strpos($_key_upper, ' LIKE') !== false) { |
|
| 385 | 6 | $_connector = 'LIKE'; |
|
| 386 | } |
||
| 387 | |||
| 388 | 72 | if (\strpos($_key_upper, ' NOT LIKE') !== false) { |
|
| 389 | 6 | $_connector = 'NOT LIKE'; |
|
| 390 | } |
||
| 391 | |||
| 392 | 72 | View Code Duplication | if (\strpos($_key_upper, ' >') !== false && \strpos($_key_upper, ' =') === false) { |
| 393 | 8 | $_connector = '>'; |
|
| 394 | } |
||
| 395 | |||
| 396 | 72 | View Code Duplication | if (\strpos($_key_upper, ' <') !== false && \strpos($_key_upper, ' =') === false) { |
| 397 | 3 | $_connector = '<'; |
|
| 398 | } |
||
| 399 | |||
| 400 | 72 | if (\strpos($_key_upper, ' >=') !== false) { |
|
| 401 | 8 | $_connector = '>='; |
|
| 402 | } |
||
| 403 | |||
| 404 | 72 | if (\strpos($_key_upper, ' <=') !== false) { |
|
| 405 | 3 | $_connector = '<='; |
|
| 406 | } |
||
| 407 | |||
| 408 | 72 | if (\strpos($_key_upper, ' <>') !== false) { |
|
| 409 | 3 | $_connector = '<>'; |
|
| 410 | } |
||
| 411 | |||
| 412 | 72 | if (\strpos($_key_upper, ' OR') !== false) { |
|
| 413 | 3 | $_glueHelper = 'OR'; |
|
| 414 | } |
||
| 415 | |||
| 416 | 72 | if (\strpos($_key_upper, ' AND') !== false) { |
|
| 417 | $_glueHelper = 'AND'; |
||
| 418 | } |
||
| 419 | |||
| 420 | 72 | if (\is_array($_value) === true) { |
|
| 421 | 4 | $firstKey = null; |
|
| 422 | 4 | $firstValue = null; |
|
| 423 | 4 | foreach ($_value as $oldKey => $oldValue) { |
|
| 424 | 4 | $_value[$oldKey] = $this->secure($oldValue); |
|
| 425 | |||
| 426 | 4 | if ($firstKey === null) { |
|
| 427 | 4 | $firstKey = $oldKey; |
|
| 428 | } |
||
| 429 | |||
| 430 | 4 | if ($firstValue === null) { |
|
| 431 | 4 | $firstValue = $_value[$oldKey]; |
|
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | 4 | if ($_connector === 'NOT IN' || $_connector === 'IN') { |
|
| 436 | 3 | $_value = '(' . \implode(',', $_value) . ')'; |
|
| 437 | 4 | } elseif ($_connector === 'NOT BETWEEN' || $_connector === 'BETWEEN') { |
|
| 438 | 3 | $_value = '(' . \implode(' AND ', $_value) . ')'; |
|
| 439 | 4 | } elseif ($firstKey && $firstValue) { |
|
| 440 | |||
| 441 | 1 | View Code Duplication | if (\strpos($firstKey, ' +') !== false) { |
| 442 | 1 | $firstKey = \str_replace(' +', '', $firstKey); |
|
| 443 | 1 | $_value = $firstKey . ' + ' . $firstValue; |
|
| 444 | } |
||
| 445 | |||
| 446 | 1 | View Code Duplication | if (\strpos($firstKey, ' -') !== false) { |
| 447 | 1 | $firstKey = \str_replace(' -', '', $firstKey); |
|
| 448 | 4 | $_value = $firstKey . ' - ' . $firstValue; |
|
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | } else { |
||
| 453 | 72 | $_value = $this->secure($_value); |
|
| 454 | } |
||
| 455 | |||
| 456 | 72 | $quoteString = $this->quote_string( |
|
| 457 | 72 | \trim( |
|
| 458 | 72 | \str_ireplace( |
|
| 459 | [ |
||
| 460 | 72 | $_connector, |
|
| 461 | 72 | $_glueHelper, |
|
| 462 | ], |
||
| 463 | 72 | '', |
|
| 464 | 72 | $_key |
|
| 465 | ) |
||
| 466 | ) |
||
| 467 | ); |
||
| 468 | |||
| 469 | 72 | $_value = (array)$_value; |
|
| 470 | |||
| 471 | 72 | if (!$_glueHelper) { |
|
| 472 | 72 | $_glueHelper = $glue; |
|
| 473 | } |
||
| 474 | |||
| 475 | 72 | $tmpCounter = 0; |
|
| 476 | 72 | foreach ($_value as $valueInner) { |
|
| 477 | |||
| 478 | 72 | $_glueHelperInner = $_glueHelper; |
|
| 479 | |||
| 480 | 72 | if ($arrayPairCounter === 0) { |
|
| 481 | |||
| 482 | 72 | if ($tmpCounter === 0 && $_glueHelper === 'OR') { |
|
| 483 | $_glueHelperInner = '1 = 1 AND ('; // first "OR"-query glue |
||
| 484 | 72 | } elseif ($tmpCounter === 0) { |
|
| 485 | 72 | $_glueHelperInner = ''; // first query glue e.g. for "INSERT"-query -> skip the first "," |
|
| 486 | } |
||
| 487 | |||
| 488 | 68 | } elseif ($tmpCounter === 0 && $_glueHelper === 'OR') { |
|
| 489 | 3 | $_glueHelperInner = 'AND ('; // inner-loop "OR"-query glue |
|
| 490 | } |
||
| 491 | |||
| 492 | 72 | if (\is_string($valueInner) && $valueInner === '') { |
|
| 493 | $valueInner = "''"; |
||
| 494 | } |
||
| 495 | |||
| 496 | 72 | $sql .= ' ' . $_glueHelperInner . ' ' . $quoteString . ' ' . $_connector . ' ' . $valueInner . " \n"; |
|
| 497 | 72 | $tmpCounter++; |
|
| 498 | } |
||
| 499 | |||
| 500 | 72 | if ($_glueHelper === 'OR') { |
|
| 501 | 3 | $sql .= ' ) '; |
|
| 502 | } |
||
| 503 | |||
| 504 | 72 | $arrayPairCounter++; |
|
| 505 | } |
||
| 506 | |||
| 507 | 72 | return $sql; |
|
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * _parseQueryParams |
||
| 512 | * |
||
| 513 | * @param string $sql |
||
| 514 | * @param array $params |
||
| 515 | * |
||
| 516 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 517 | */ |
||
| 518 | 17 | private function _parseQueryParams(string $sql, array $params = []): array |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Returns the SQL by replacing :placeholders with SQL-escaped values. |
||
| 555 | * |
||
| 556 | * @param mixed $sql <p>The SQL string.</p> |
||
| 557 | * @param array $params <p>An array of key-value bindings.</p> |
||
| 558 | * |
||
| 559 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 560 | */ |
||
| 561 | 20 | private function _parseQueryParamsByName(string $sql, array $params = []): array |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 610 | * |
||
| 611 | * @return int |
||
| 612 | */ |
||
| 613 | 28 | public function affected_rows(): int |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Begins a transaction, by turning off auto commit. |
||
| 628 | * |
||
| 629 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 630 | */ |
||
| 631 | 18 | public function beginTransaction(): bool |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Clear the errors in "_debug->_errors". |
||
| 664 | * |
||
| 665 | * @return bool |
||
| 666 | */ |
||
| 667 | 18 | public function clearErrors(): bool |
|
| 671 | |||
| 672 | /** |
||
| 673 | * Closes a previously opened database connection. |
||
| 674 | * |
||
| 675 | * @return bool |
||
| 676 | * Will return "true", if the connection was closed, |
||
| 677 | * otherwise (e.g. if the connection was already closed) "false". |
||
| 678 | */ |
||
| 679 | 6 | public function close(): bool |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Commits the current transaction and end the transaction. |
||
| 720 | * |
||
| 721 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 722 | */ |
||
| 723 | 9 | View Code Duplication | public function commit(): bool |
| 749 | |||
| 750 | /** |
||
| 751 | * Open a new connection to the MySQL server. |
||
| 752 | * |
||
| 753 | * @return bool |
||
| 754 | * |
||
| 755 | * @throws DBConnectException |
||
| 756 | */ |
||
| 757 | 20 | public function connect(): bool |
|
| 873 | |||
| 874 | /** |
||
| 875 | * Execute a "delete"-query. |
||
| 876 | * |
||
| 877 | * @param string $table |
||
| 878 | * @param string|array $where |
||
| 879 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 880 | * |
||
| 881 | * @return false|int <p>false on error</p> |
||
| 882 | * |
||
| 883 | * @throws QueryException |
||
| 884 | */ |
||
| 885 | 4 | View Code Duplication | public function delete(string $table, $where, string $databaseName = null) |
| 912 | |||
| 913 | /** |
||
| 914 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 915 | * |
||
| 916 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 917 | */ |
||
| 918 | 12 | public function endTransaction(): bool |
|
| 949 | |||
| 950 | /** |
||
| 951 | * Get all errors from "$this->errors". |
||
| 952 | * |
||
| 953 | * @return array|false <p>false === on errors</p> |
||
| 954 | */ |
||
| 955 | 12 | public function errors() |
|
| 961 | |||
| 962 | /** |
||
| 963 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 964 | * |
||
| 965 | * @param mixed $var bool: convert into "integer"<br /> |
||
| 966 | * int: int (don't change it)<br /> |
||
| 967 | * float: float (don't change it)<br /> |
||
| 968 | * null: null (don't change it)<br /> |
||
| 969 | * array: run escape() for every key => value<br /> |
||
| 970 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 971 | * @param bool $stripe_non_utf8 |
||
| 972 | * @param bool $html_entity_decode |
||
| 973 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 974 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 975 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 976 | * |
||
| 977 | * @return mixed |
||
| 978 | */ |
||
| 979 | 119 | public function escape($var = '', bool $stripe_non_utf8 = true, bool $html_entity_decode = false, $convert_array = false) |
|
| 1084 | |||
| 1085 | /** |
||
| 1086 | * Execute select/insert/update/delete sql-queries. |
||
| 1087 | * |
||
| 1088 | * @param string $query <p>sql-query</p> |
||
| 1089 | * @param bool $useCache optional <p>use cache?</p> |
||
| 1090 | * @param int $cacheTTL optional <p>cache-ttl in seconds</p> |
||
| 1091 | * @param DB|null $db optional <p>the database connection</p> |
||
| 1092 | * |
||
| 1093 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 1094 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 1095 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1096 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1097 | * "false" on error |
||
| 1098 | * |
||
| 1099 | * @throws QueryException |
||
| 1100 | */ |
||
| 1101 | 9 | public static function execSQL(string $query, bool $useCache = false, int $cacheTTL = 3600, self $db = null) |
|
| 1150 | |||
| 1151 | /** |
||
| 1152 | * Get all table-names via "SHOW TABLES". |
||
| 1153 | * |
||
| 1154 | * @return array |
||
| 1155 | */ |
||
| 1156 | 3 | public function getAllTables(): array |
|
| 1163 | |||
| 1164 | /** |
||
| 1165 | * @return array |
||
| 1166 | */ |
||
| 1167 | 8 | public function getConfig() |
|
| 1188 | |||
| 1189 | /** |
||
| 1190 | * @return Debug |
||
| 1191 | */ |
||
| 1192 | 9 | public function getDebugger(): Debug |
|
| 1196 | |||
| 1197 | /** |
||
| 1198 | * @return null|\Doctrine\DBAL\Connection|null |
||
| 1199 | */ |
||
| 1200 | 2 | public function getDoctrineConnection() |
|
| 1204 | |||
| 1205 | /** |
||
| 1206 | * @return false|\Doctrine\DBAL\Driver\Connection |
||
| 1207 | */ |
||
| 1208 | View Code Duplication | private function getDoctrinePDOConnection() |
|
| 1219 | |||
| 1220 | /** |
||
| 1221 | * Get errors from "$this->errors". |
||
| 1222 | * |
||
| 1223 | * @return array |
||
| 1224 | */ |
||
| 1225 | 3 | public function getErrors(): array |
|
| 1229 | |||
| 1230 | /** |
||
| 1231 | * @param string $hostname <p>Hostname of the mysql server</p> |
||
| 1232 | * @param string $username <p>Username for the mysql connection</p> |
||
| 1233 | * @param string $password <p>Password for the mysql connection</p> |
||
| 1234 | * @param string $database <p>Database for the mysql connection</p> |
||
| 1235 | * @param int $port <p>default is (int)3306</p> |
||
| 1236 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1237 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 1238 | * Use false to disable it.</p> |
||
| 1239 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1240 | * Use false to disable it.</p> |
||
| 1241 | * @param string $logger_class_name |
||
| 1242 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1243 | * @param array $extra_config <p> |
||
| 1244 | * 're_connect' => bool<br> |
||
| 1245 | * 'session_to_db' => bool<br> |
||
| 1246 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 1247 | * 'socket' => 'string (path)'<br> |
||
| 1248 | * 'ssl' => bool<br> |
||
| 1249 | * 'clientkey' => 'string (path)'<br> |
||
| 1250 | * 'clientcert' => 'string (path)'<br> |
||
| 1251 | * 'cacert' => 'string (path)'<br> |
||
| 1252 | * </p> |
||
| 1253 | * |
||
| 1254 | * @return self |
||
| 1255 | */ |
||
| 1256 | 231 | public static function getInstance( |
|
| 1337 | |||
| 1338 | /** |
||
| 1339 | * @param \Doctrine\DBAL\Connection $doctrine |
||
| 1340 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1341 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will |
||
| 1342 | * return 'false'. Use false to disable it.</p> |
||
| 1343 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1344 | * Use false to disable it.</p> |
||
| 1345 | * @param string $logger_class_name |
||
| 1346 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1347 | * @param array $extra_config <p> |
||
| 1348 | * 're_connect' => bool<br> |
||
| 1349 | * 'session_to_db' => bool<br> |
||
| 1350 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 1351 | * 'socket' => 'string (path)'<br> |
||
| 1352 | * 'ssl' => bool<br> |
||
| 1353 | * 'clientkey' => 'string (path)'<br> |
||
| 1354 | * 'clientcert' => 'string (path)'<br> |
||
| 1355 | * 'cacert' => 'string (path)'<br> |
||
| 1356 | * </p> |
||
| 1357 | * |
||
| 1358 | * @return self |
||
| 1359 | */ |
||
| 1360 | 55 | public static function getInstanceDoctrineHelper( |
|
| 1386 | |||
| 1387 | /** |
||
| 1388 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1389 | * |
||
| 1390 | * @return null|\mysqli |
||
| 1391 | */ |
||
| 1392 | 53 | public function getLink() |
|
| 1396 | |||
| 1397 | /** |
||
| 1398 | * Get the current charset. |
||
| 1399 | * |
||
| 1400 | * @return string |
||
| 1401 | */ |
||
| 1402 | 3 | public function get_charset(): string |
|
| 1406 | |||
| 1407 | /** |
||
| 1408 | * Check if we are in a transaction. |
||
| 1409 | * |
||
| 1410 | * @return bool |
||
| 1411 | */ |
||
| 1412 | public function inTransaction(): bool |
||
| 1416 | |||
| 1417 | /** |
||
| 1418 | * Execute a "insert"-query. |
||
| 1419 | * |
||
| 1420 | * @param string $table |
||
| 1421 | * @param array $data |
||
| 1422 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1423 | * |
||
| 1424 | * @return false|int <p>false on error</p> |
||
| 1425 | * |
||
| 1426 | * @throws QueryException |
||
| 1427 | */ |
||
| 1428 | 74 | public function insert(string $table, array $data = [], string $databaseName = null) |
|
| 1455 | |||
| 1456 | /** |
||
| 1457 | * Returns the auto generated id used in the last query. |
||
| 1458 | * |
||
| 1459 | * @return int|string |
||
| 1460 | */ |
||
| 1461 | 104 | public function insert_id() |
|
| 1471 | |||
| 1472 | /** |
||
| 1473 | * @return bool |
||
| 1474 | */ |
||
| 1475 | public function isDoctrineMySQLiConnection(): bool |
||
| 1486 | |||
| 1487 | /** |
||
| 1488 | * @return bool |
||
| 1489 | */ |
||
| 1490 | 9 | View Code Duplication | public function isDoctrinePDOConnection(): bool |
| 1501 | |||
| 1502 | /** |
||
| 1503 | * Check if db-connection is ready. |
||
| 1504 | * |
||
| 1505 | * @return bool |
||
| 1506 | */ |
||
| 1507 | 184 | public function isReady(): bool |
|
| 1511 | |||
| 1512 | /** |
||
| 1513 | * Get the last sql-error. |
||
| 1514 | * |
||
| 1515 | * @return string|false <p>false === there was no error</p> |
||
| 1516 | */ |
||
| 1517 | 3 | public function lastError() |
|
| 1523 | |||
| 1524 | /** |
||
| 1525 | * Execute a sql-multi-query. |
||
| 1526 | * |
||
| 1527 | * @param string $sql |
||
| 1528 | * |
||
| 1529 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1530 | * "bool" by only "<b>INSERT</b>"-queries<br /> |
||
| 1531 | * "bool" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1532 | * "bool" by only by e.g. "DROP"-queries<br /> |
||
| 1533 | * |
||
| 1534 | * @throws QueryException |
||
| 1535 | */ |
||
| 1536 | 3 | public function multi_query(string $sql) |
|
| 1677 | |||
| 1678 | /** |
||
| 1679 | * Count number of rows found matching a specific query. |
||
| 1680 | * |
||
| 1681 | * @param string $query |
||
| 1682 | * |
||
| 1683 | * @return int |
||
| 1684 | */ |
||
| 1685 | 3 | public function num_rows(string $query): int |
|
| 1699 | |||
| 1700 | /** |
||
| 1701 | * Pings a server connection, or tries to reconnect |
||
| 1702 | * if the connection has gone down. |
||
| 1703 | * |
||
| 1704 | * @return bool |
||
| 1705 | */ |
||
| 1706 | 9 | public function ping(): bool |
|
| 1726 | |||
| 1727 | /** |
||
| 1728 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1729 | * |
||
| 1730 | * @param string $query |
||
| 1731 | * |
||
| 1732 | * @return Prepare |
||
| 1733 | */ |
||
| 1734 | 2 | public function prepare(string $query): Prepare |
|
| 1738 | |||
| 1739 | /** |
||
| 1740 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1741 | * |
||
| 1742 | * @param string $query |
||
| 1743 | * |
||
| 1744 | * @return mixed |
||
| 1745 | * @deprecated |
||
| 1746 | * @throws \Exception |
||
| 1747 | */ |
||
| 1748 | 3 | public static function qry(string $query) |
|
| 1773 | |||
| 1774 | /** |
||
| 1775 | * Execute a sql-query. |
||
| 1776 | * |
||
| 1777 | * example: |
||
| 1778 | * <code> |
||
| 1779 | * $sql = "INSERT INTO TABLE_NAME_HERE |
||
| 1780 | * SET |
||
| 1781 | * foo = :foo, |
||
| 1782 | * bar = :bar |
||
| 1783 | * "; |
||
| 1784 | * $insert_id = $db->query( |
||
| 1785 | * $sql, |
||
| 1786 | * [ |
||
| 1787 | * 'foo' => 1.1, |
||
| 1788 | * 'bar' => 1, |
||
| 1789 | * ] |
||
| 1790 | * ); |
||
| 1791 | * </code> |
||
| 1792 | * |
||
| 1793 | * @param string $sql <p>The sql query-string.</p> |
||
| 1794 | * |
||
| 1795 | * @param array|bool $params <p> |
||
| 1796 | * "array" of sql-query-parameters<br/> |
||
| 1797 | * "false" if you don't need any parameter (default)<br/> |
||
| 1798 | * </p> |
||
| 1799 | * |
||
| 1800 | * @return bool|int|Result <p> |
||
| 1801 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1802 | * "int|string" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1803 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1804 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1805 | * "false" on error |
||
| 1806 | * </p> |
||
| 1807 | * |
||
| 1808 | * @throws QueryException |
||
| 1809 | */ |
||
| 1810 | 164 | public function query(string $sql = '', $params = false) |
|
| 1955 | |||
| 1956 | /** |
||
| 1957 | * Error-handling for the sql-query. |
||
| 1958 | * |
||
| 1959 | * @param string $errorMessage |
||
| 1960 | * @param int $errorNumber |
||
| 1961 | * @param string $sql |
||
| 1962 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1963 | * @param bool $sqlMultiQuery |
||
| 1964 | * |
||
| 1965 | * @return mixed|false |
||
| 1966 | * |
||
| 1967 | * @throws QueryException |
||
| 1968 | * @throws DBGoneAwayException |
||
| 1969 | */ |
||
| 1970 | 39 | private function queryErrorHandling(string $errorMessage, int $errorNumber, string $sql, $sqlParams = false, bool $sqlMultiQuery = false) |
|
| 2013 | |||
| 2014 | /** |
||
| 2015 | * Quote && Escape e.g. a table name string. |
||
| 2016 | * |
||
| 2017 | * @param mixed $str |
||
| 2018 | * |
||
| 2019 | * @return string |
||
| 2020 | */ |
||
| 2021 | 86 | public function quote_string($str): string |
|
| 2034 | |||
| 2035 | /** |
||
| 2036 | * Reconnect to the MySQL-Server. |
||
| 2037 | * |
||
| 2038 | * @param bool $checkViaPing |
||
| 2039 | * |
||
| 2040 | * @return bool |
||
| 2041 | */ |
||
| 2042 | 7 | public function reconnect(bool $checkViaPing = false): bool |
|
| 2056 | |||
| 2057 | /** |
||
| 2058 | * Execute a "replace"-query. |
||
| 2059 | * |
||
| 2060 | * @param string $table |
||
| 2061 | * @param array $data |
||
| 2062 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2063 | * |
||
| 2064 | * @return false|int <p>false on error</p> |
||
| 2065 | * |
||
| 2066 | * @throws QueryException |
||
| 2067 | */ |
||
| 2068 | 3 | public function replace(string $table, array $data = [], string $databaseName = null) |
|
| 2109 | |||
| 2110 | /** |
||
| 2111 | * Rollback in a transaction and end the transaction. |
||
| 2112 | * |
||
| 2113 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2114 | */ |
||
| 2115 | 12 | View Code Duplication | public function rollback(): bool |
| 2141 | |||
| 2142 | /** |
||
| 2143 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 2144 | * |
||
| 2145 | * <p> |
||
| 2146 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 2147 | * 1. parse into "int" |
||
| 2148 | * </p><br /> |
||
| 2149 | * |
||
| 2150 | * <p> |
||
| 2151 | * <strong>float:</strong><br /> |
||
| 2152 | * 1. return "float" |
||
| 2153 | * </p><br /> |
||
| 2154 | * |
||
| 2155 | * <p> |
||
| 2156 | * <strong>string:</strong><br /> |
||
| 2157 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 2158 | * 2. trim '<br /> |
||
| 2159 | * 3. escape the string (and remove non utf-8 chars)<br /> |
||
| 2160 | * 4. trim ' again (because we maybe removed some chars)<br /> |
||
| 2161 | * 5. add ' around the new string<br /> |
||
| 2162 | * </p><br /> |
||
| 2163 | * |
||
| 2164 | * <p> |
||
| 2165 | * <strong>array:</strong><br /> |
||
| 2166 | * 1. return null |
||
| 2167 | * </p><br /> |
||
| 2168 | * |
||
| 2169 | * <p> |
||
| 2170 | * <strong>object:</strong><br /> |
||
| 2171 | * 1. return false |
||
| 2172 | * </p><br /> |
||
| 2173 | * |
||
| 2174 | * <p> |
||
| 2175 | * <strong>null:</strong><br /> |
||
| 2176 | * 1. return null |
||
| 2177 | * </p> |
||
| 2178 | * |
||
| 2179 | * @param mixed $var |
||
| 2180 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 2181 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 2182 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 2183 | * |
||
| 2184 | * @return mixed |
||
| 2185 | */ |
||
| 2186 | 97 | public function secure($var, $convert_array = true) |
|
| 2254 | |||
| 2255 | /** |
||
| 2256 | * Execute a "select"-query. |
||
| 2257 | * |
||
| 2258 | * @param string $table |
||
| 2259 | * @param string|array $where |
||
| 2260 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2261 | * |
||
| 2262 | * @return false|Result <p>false on error</p> |
||
| 2263 | * |
||
| 2264 | * @throws QueryException |
||
| 2265 | */ |
||
| 2266 | 62 | View Code Duplication | public function select(string $table, $where = '1=1', string $databaseName = null) |
| 2293 | |||
| 2294 | /** |
||
| 2295 | * Selects a different database than the one specified on construction. |
||
| 2296 | * |
||
| 2297 | * @param string $database <p>Database name to switch to.</p> |
||
| 2298 | * |
||
| 2299 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2300 | */ |
||
| 2301 | public function select_db(string $database): bool |
||
| 2317 | |||
| 2318 | /** |
||
| 2319 | * @param array $extra_config <p> |
||
| 2320 | * 'session_to_db' => false|true<br> |
||
| 2321 | * 'socket' => 'string (path)'<br> |
||
| 2322 | * 'ssl' => 'bool'<br> |
||
| 2323 | * 'clientkey' => 'string (path)'<br> |
||
| 2324 | * 'clientcert' => 'string (path)'<br> |
||
| 2325 | * 'cacert' => 'string (path)'<br> |
||
| 2326 | * </p> |
||
| 2327 | */ |
||
| 2328 | 23 | public function setConfigExtra(array $extra_config) |
|
| 2362 | |||
| 2363 | /** |
||
| 2364 | * Set the current charset. |
||
| 2365 | * |
||
| 2366 | * @param string $charset |
||
| 2367 | * |
||
| 2368 | * @return bool |
||
| 2369 | */ |
||
| 2370 | 14 | public function set_charset(string $charset): bool |
|
| 2412 | |||
| 2413 | /** |
||
| 2414 | * Set the option to convert null to "''" (empty string). |
||
| 2415 | * |
||
| 2416 | * Used in secure() => select(), insert(), update(), delete() |
||
| 2417 | * |
||
| 2418 | * @deprecated It's not recommended to convert NULL into an empty string! |
||
| 2419 | * |
||
| 2420 | * @param bool $bool |
||
| 2421 | * |
||
| 2422 | * @return self |
||
| 2423 | */ |
||
| 2424 | 3 | public function set_convert_null_to_empty_string(bool $bool): self |
|
| 2430 | |||
| 2431 | /** |
||
| 2432 | * Enables or disables internal report functions |
||
| 2433 | * |
||
| 2434 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 2435 | * |
||
| 2436 | * @param int $flags <p> |
||
| 2437 | * <table> |
||
| 2438 | * Supported flags |
||
| 2439 | * <tr valign="top"> |
||
| 2440 | * <td>Name</td> |
||
| 2441 | * <td>Description</td> |
||
| 2442 | * </tr> |
||
| 2443 | * <tr valign="top"> |
||
| 2444 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 2445 | * <td>Turns reporting off</td> |
||
| 2446 | * </tr> |
||
| 2447 | * <tr valign="top"> |
||
| 2448 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 2449 | * <td>Report errors from mysqli function calls</td> |
||
| 2450 | * </tr> |
||
| 2451 | * <tr valign="top"> |
||
| 2452 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 2453 | * <td> |
||
| 2454 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 2455 | * instead of warnings |
||
| 2456 | * </td> |
||
| 2457 | * </tr> |
||
| 2458 | * <tr valign="top"> |
||
| 2459 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 2460 | * <td>Report if no index or bad index was used in a query</td> |
||
| 2461 | * </tr> |
||
| 2462 | * <tr valign="top"> |
||
| 2463 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 2464 | * <td>Set all options (report all)</td> |
||
| 2465 | * </tr> |
||
| 2466 | * </table> |
||
| 2467 | * </p> |
||
| 2468 | * |
||
| 2469 | * @return bool |
||
| 2470 | */ |
||
| 2471 | public function set_mysqli_report(int $flags): bool |
||
| 2483 | |||
| 2484 | /** |
||
| 2485 | * Show config errors by throw exceptions. |
||
| 2486 | * |
||
| 2487 | * @return bool |
||
| 2488 | * |
||
| 2489 | * @throws \InvalidArgumentException |
||
| 2490 | */ |
||
| 2491 | 23 | public function showConfigError(): bool |
|
| 2527 | |||
| 2528 | /** |
||
| 2529 | * alias: "beginTransaction()" |
||
| 2530 | */ |
||
| 2531 | 3 | public function startTransaction(): bool |
|
| 2535 | |||
| 2536 | /** |
||
| 2537 | * Determine if database table exists |
||
| 2538 | * |
||
| 2539 | * @param string $table |
||
| 2540 | * |
||
| 2541 | * @return bool |
||
| 2542 | */ |
||
| 2543 | 3 | public function table_exists(string $table): bool |
|
| 2553 | |||
| 2554 | /** |
||
| 2555 | * Execute a callback inside a transaction. |
||
| 2556 | * |
||
| 2557 | * @param callback $callback <p>The callback to run inside the transaction, if it's throws an "Exception" or if it's |
||
| 2558 | * returns "false", all SQL-statements in the callback will be rollbacked.</p> |
||
| 2559 | * |
||
| 2560 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2561 | */ |
||
| 2562 | 3 | public function transact($callback): bool |
|
| 2588 | |||
| 2589 | /** |
||
| 2590 | * Execute a "update"-query. |
||
| 2591 | * |
||
| 2592 | * @param string $table |
||
| 2593 | * @param array $data |
||
| 2594 | * @param array|string $where |
||
| 2595 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2596 | * |
||
| 2597 | * @return false|int <p>false on error</p> |
||
| 2598 | * |
||
| 2599 | * @throws QueryException |
||
| 2600 | */ |
||
| 2601 | 21 | public function update(string $table, array $data = [], $where = '1=1', string $databaseName = null) |
|
| 2642 | |||
| 2643 | } |
||
| 2644 |
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: