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 |
|
| 488 | |||
| 489 | /** |
||
| 490 | * _parseQueryParams |
||
| 491 | * |
||
| 492 | * @param string $sql |
||
| 493 | * @param array $params |
||
| 494 | * |
||
| 495 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 496 | */ |
||
| 497 | 17 | private function _parseQueryParams(string $sql, array $params = []): array |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Returns the SQL by replacing :placeholders with SQL-escaped values. |
||
| 534 | * |
||
| 535 | * @param mixed $sql <p>The SQL string.</p> |
||
| 536 | * @param array $params <p>An array of key-value bindings.</p> |
||
| 537 | * |
||
| 538 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 539 | */ |
||
| 540 | 20 | private function _parseQueryParamsByName(string $sql, array $params = []): array |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 589 | * |
||
| 590 | * @return int |
||
| 591 | */ |
||
| 592 | 28 | public function affected_rows(): int |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Begins a transaction, by turning off auto commit. |
||
| 607 | * |
||
| 608 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 609 | */ |
||
| 610 | 18 | public function beginTransaction(): bool |
|
| 611 | { |
||
| 612 | 18 | if ($this->in_transaction === true) { |
|
| 613 | 6 | $this->debug->displayError('Error: mysql server already in transaction!', false); |
|
| 614 | |||
| 615 | 6 | return false; |
|
| 616 | } |
||
| 617 | |||
| 618 | 18 | $this->clearErrors(); // needed for "$this->endTransaction()" |
|
| 619 | 18 | $this->in_transaction = true; |
|
| 620 | |||
| 621 | 18 | if ($this->mysqli_link) { |
|
| 622 | 18 | $return = \mysqli_autocommit($this->mysqli_link, false); |
|
| 623 | } elseif ($this->isDoctrinePDOConnection() === true) { |
||
| 624 | $this->doctrine_connection->setAutoCommit(false); |
||
| 625 | $this->doctrine_connection->beginTransaction(); |
||
| 626 | |||
| 627 | if ($this->doctrine_connection->isTransactionActive() === true) { |
||
| 628 | $return = true; |
||
| 629 | } else { |
||
| 630 | $return = false; |
||
| 631 | } |
||
| 632 | } |
||
| 633 | |||
| 634 | 18 | if ($return === false) { |
|
|
|
|||
| 635 | $this->in_transaction = false; |
||
| 636 | } |
||
| 637 | |||
| 638 | 18 | return $return; |
|
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Clear the errors in "_debug->_errors". |
||
| 643 | * |
||
| 644 | * @return bool |
||
| 645 | */ |
||
| 646 | 18 | public function clearErrors(): bool |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Closes a previously opened database connection. |
||
| 653 | * |
||
| 654 | * @return bool |
||
| 655 | * Will return "true", if the connection was closed, |
||
| 656 | * otherwise (e.g. if the connection was already closed) "false". |
||
| 657 | */ |
||
| 658 | 6 | public function close(): bool |
|
| 696 | |||
| 697 | /** |
||
| 698 | * Commits the current transaction and end the transaction. |
||
| 699 | * |
||
| 700 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 701 | */ |
||
| 702 | 9 | View Code Duplication | public function commit(): bool |
| 728 | |||
| 729 | /** |
||
| 730 | * Open a new connection to the MySQL server. |
||
| 731 | * |
||
| 732 | * @return bool |
||
| 733 | * |
||
| 734 | * @throws DBConnectException |
||
| 735 | */ |
||
| 736 | 20 | public function connect(): bool |
|
| 852 | |||
| 853 | /** |
||
| 854 | * Execute a "delete"-query. |
||
| 855 | * |
||
| 856 | * @param string $table |
||
| 857 | * @param string|array $where |
||
| 858 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 859 | * |
||
| 860 | * @return false|int <p>false on error</p> |
||
| 861 | * |
||
| 862 | * @throws QueryException |
||
| 863 | */ |
||
| 864 | 4 | View Code Duplication | public function delete(string $table, $where, string $databaseName = null) |
| 891 | |||
| 892 | /** |
||
| 893 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 894 | * |
||
| 895 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 896 | */ |
||
| 897 | 12 | public function endTransaction(): bool |
|
| 928 | |||
| 929 | /** |
||
| 930 | * Get all errors from "$this->errors". |
||
| 931 | * |
||
| 932 | * @return array|false <p>false === on errors</p> |
||
| 933 | */ |
||
| 934 | 12 | public function errors() |
|
| 940 | |||
| 941 | /** |
||
| 942 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 943 | * |
||
| 944 | * @param mixed $var bool: convert into "integer"<br /> |
||
| 945 | * int: int (don't change it)<br /> |
||
| 946 | * float: float (don't change it)<br /> |
||
| 947 | * null: null (don't change it)<br /> |
||
| 948 | * array: run escape() for every key => value<br /> |
||
| 949 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 950 | * @param bool $stripe_non_utf8 |
||
| 951 | * @param bool $html_entity_decode |
||
| 952 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 953 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 954 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 955 | * |
||
| 956 | * @return mixed |
||
| 957 | */ |
||
| 958 | 119 | public function escape($var = '', bool $stripe_non_utf8 = true, bool $html_entity_decode = false, $convert_array = false) |
|
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Execute select/insert/update/delete sql-queries. |
||
| 1066 | * |
||
| 1067 | * @param string $query <p>sql-query</p> |
||
| 1068 | * @param bool $useCache optional <p>use cache?</p> |
||
| 1069 | * @param int $cacheTTL optional <p>cache-ttl in seconds</p> |
||
| 1070 | * @param DB|null $db optional <p>the database connection</p> |
||
| 1071 | * |
||
| 1072 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 1073 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 1074 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1075 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1076 | * "false" on error |
||
| 1077 | * |
||
| 1078 | * @throws QueryException |
||
| 1079 | */ |
||
| 1080 | 9 | public static function execSQL(string $query, bool $useCache = false, int $cacheTTL = 3600, self $db = null) |
|
| 1129 | |||
| 1130 | /** |
||
| 1131 | * Get all table-names via "SHOW TABLES". |
||
| 1132 | * |
||
| 1133 | * @return array |
||
| 1134 | */ |
||
| 1135 | 3 | public function getAllTables(): array |
|
| 1142 | |||
| 1143 | /** |
||
| 1144 | * @return array |
||
| 1145 | */ |
||
| 1146 | 8 | public function getConfig() |
|
| 1167 | |||
| 1168 | /** |
||
| 1169 | * @return Debug |
||
| 1170 | */ |
||
| 1171 | 9 | public function getDebugger(): Debug |
|
| 1175 | |||
| 1176 | /** |
||
| 1177 | * @return null|\Doctrine\DBAL\Connection|null |
||
| 1178 | */ |
||
| 1179 | 2 | public function getDoctrineConnection() |
|
| 1183 | |||
| 1184 | /** |
||
| 1185 | * @return false|\Doctrine\DBAL\Driver\Connection |
||
| 1186 | */ |
||
| 1187 | View Code Duplication | private function getDoctrinePDOConnection() |
|
| 1198 | |||
| 1199 | /** |
||
| 1200 | * Get errors from "$this->errors". |
||
| 1201 | * |
||
| 1202 | * @return array |
||
| 1203 | */ |
||
| 1204 | 3 | public function getErrors(): array |
|
| 1208 | |||
| 1209 | /** |
||
| 1210 | * @param string $hostname <p>Hostname of the mysql server</p> |
||
| 1211 | * @param string $username <p>Username for the mysql connection</p> |
||
| 1212 | * @param string $password <p>Password for the mysql connection</p> |
||
| 1213 | * @param string $database <p>Database for the mysql connection</p> |
||
| 1214 | * @param int $port <p>default is (int)3306</p> |
||
| 1215 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1216 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 1217 | * Use false to disable it.</p> |
||
| 1218 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1219 | * Use false to disable it.</p> |
||
| 1220 | * @param string $logger_class_name |
||
| 1221 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1222 | * @param array $extra_config <p> |
||
| 1223 | * 're_connect' => bool<br> |
||
| 1224 | * 'session_to_db' => bool<br> |
||
| 1225 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 1226 | * 'socket' => 'string (path)'<br> |
||
| 1227 | * 'ssl' => bool<br> |
||
| 1228 | * 'clientkey' => 'string (path)'<br> |
||
| 1229 | * 'clientcert' => 'string (path)'<br> |
||
| 1230 | * 'cacert' => 'string (path)'<br> |
||
| 1231 | * </p> |
||
| 1232 | * |
||
| 1233 | * @return self |
||
| 1234 | */ |
||
| 1235 | 231 | public static function getInstance( |
|
| 1316 | |||
| 1317 | /** |
||
| 1318 | * @param \Doctrine\DBAL\Connection $doctrine |
||
| 1319 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1320 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will |
||
| 1321 | * return 'false'. Use false to disable it.</p> |
||
| 1322 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1323 | * Use false to disable it.</p> |
||
| 1324 | * @param string $logger_class_name |
||
| 1325 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1326 | * @param array $extra_config <p> |
||
| 1327 | * 're_connect' => bool<br> |
||
| 1328 | * 'session_to_db' => bool<br> |
||
| 1329 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 1330 | * 'socket' => 'string (path)'<br> |
||
| 1331 | * 'ssl' => bool<br> |
||
| 1332 | * 'clientkey' => 'string (path)'<br> |
||
| 1333 | * 'clientcert' => 'string (path)'<br> |
||
| 1334 | * 'cacert' => 'string (path)'<br> |
||
| 1335 | * </p> |
||
| 1336 | * |
||
| 1337 | * @return self |
||
| 1338 | */ |
||
| 1339 | 55 | public static function getInstanceDoctrineHelper( |
|
| 1365 | |||
| 1366 | /** |
||
| 1367 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1368 | * |
||
| 1369 | * @return null|\mysqli |
||
| 1370 | */ |
||
| 1371 | 53 | public function getLink() |
|
| 1375 | |||
| 1376 | /** |
||
| 1377 | * Get the current charset. |
||
| 1378 | * |
||
| 1379 | * @return string |
||
| 1380 | */ |
||
| 1381 | 3 | public function get_charset(): string |
|
| 1385 | |||
| 1386 | /** |
||
| 1387 | * Check if we are in a transaction. |
||
| 1388 | * |
||
| 1389 | * @return bool |
||
| 1390 | */ |
||
| 1391 | public function inTransaction(): bool |
||
| 1395 | |||
| 1396 | /** |
||
| 1397 | * Execute a "insert"-query. |
||
| 1398 | * |
||
| 1399 | * @param string $table |
||
| 1400 | * @param array $data |
||
| 1401 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1402 | * |
||
| 1403 | * @return false|int <p>false on error</p> |
||
| 1404 | * |
||
| 1405 | * @throws QueryException |
||
| 1406 | */ |
||
| 1407 | 74 | public function insert(string $table, array $data = [], string $databaseName = null) |
|
| 1434 | |||
| 1435 | /** |
||
| 1436 | * Returns the auto generated id used in the last query. |
||
| 1437 | * |
||
| 1438 | * @return int|string |
||
| 1439 | */ |
||
| 1440 | 104 | public function insert_id() |
|
| 1450 | |||
| 1451 | /** |
||
| 1452 | * @return bool |
||
| 1453 | */ |
||
| 1454 | public function isDoctrineMySQLiConnection(): bool |
||
| 1465 | |||
| 1466 | /** |
||
| 1467 | * @return bool |
||
| 1468 | */ |
||
| 1469 | 9 | View Code Duplication | public function isDoctrinePDOConnection(): bool |
| 1480 | |||
| 1481 | /** |
||
| 1482 | * Check if db-connection is ready. |
||
| 1483 | * |
||
| 1484 | * @return bool |
||
| 1485 | */ |
||
| 1486 | 184 | public function isReady(): bool |
|
| 1490 | |||
| 1491 | /** |
||
| 1492 | * Get the last sql-error. |
||
| 1493 | * |
||
| 1494 | * @return string|false <p>false === there was no error</p> |
||
| 1495 | */ |
||
| 1496 | 3 | public function lastError() |
|
| 1502 | |||
| 1503 | /** |
||
| 1504 | * Execute a sql-multi-query. |
||
| 1505 | * |
||
| 1506 | * @param string $sql |
||
| 1507 | * |
||
| 1508 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1509 | * "bool" by only "<b>INSERT</b>"-queries<br /> |
||
| 1510 | * "bool" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1511 | * "bool" by only by e.g. "DROP"-queries<br /> |
||
| 1512 | * |
||
| 1513 | * @throws QueryException |
||
| 1514 | */ |
||
| 1515 | 3 | public function multi_query(string $sql) |
|
| 1656 | |||
| 1657 | /** |
||
| 1658 | * Count number of rows found matching a specific query. |
||
| 1659 | * |
||
| 1660 | * @param string $query |
||
| 1661 | * |
||
| 1662 | * @return int |
||
| 1663 | */ |
||
| 1664 | 3 | public function num_rows(string $query): int |
|
| 1678 | |||
| 1679 | /** |
||
| 1680 | * Pings a server connection, or tries to reconnect |
||
| 1681 | * if the connection has gone down. |
||
| 1682 | * |
||
| 1683 | * @return bool |
||
| 1684 | */ |
||
| 1685 | 9 | public function ping(): bool |
|
| 1705 | |||
| 1706 | /** |
||
| 1707 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1708 | * |
||
| 1709 | * @param string $query |
||
| 1710 | * |
||
| 1711 | * @return Prepare |
||
| 1712 | */ |
||
| 1713 | 2 | public function prepare(string $query): Prepare |
|
| 1717 | |||
| 1718 | /** |
||
| 1719 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1720 | * |
||
| 1721 | * @param string $query |
||
| 1722 | * |
||
| 1723 | * @return mixed |
||
| 1724 | * @deprecated |
||
| 1725 | * @throws \Exception |
||
| 1726 | */ |
||
| 1727 | 3 | public static function qry(string $query) |
|
| 1752 | |||
| 1753 | /** |
||
| 1754 | * Execute a sql-query. |
||
| 1755 | * |
||
| 1756 | * example: |
||
| 1757 | * <code> |
||
| 1758 | * $sql = "INSERT INTO TABLE_NAME_HERE |
||
| 1759 | * SET |
||
| 1760 | * foo = :foo, |
||
| 1761 | * bar = :bar |
||
| 1762 | * "; |
||
| 1763 | * $insert_id = $db->query( |
||
| 1764 | * $sql, |
||
| 1765 | * [ |
||
| 1766 | * 'foo' => 1.1, |
||
| 1767 | * 'bar' => 1, |
||
| 1768 | * ] |
||
| 1769 | * ); |
||
| 1770 | * </code> |
||
| 1771 | * |
||
| 1772 | * @param string $sql <p>The sql query-string.</p> |
||
| 1773 | * |
||
| 1774 | * @param array|bool $params <p> |
||
| 1775 | * "array" of sql-query-parameters<br/> |
||
| 1776 | * "false" if you don't need any parameter (default)<br/> |
||
| 1777 | * </p> |
||
| 1778 | * |
||
| 1779 | * @return bool|int|Result <p> |
||
| 1780 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1781 | * "int|string" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1782 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1783 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1784 | * "false" on error |
||
| 1785 | * </p> |
||
| 1786 | * |
||
| 1787 | * @throws QueryException |
||
| 1788 | */ |
||
| 1789 | 164 | public function query(string $sql = '', $params = false) |
|
| 1934 | |||
| 1935 | /** |
||
| 1936 | * Error-handling for the sql-query. |
||
| 1937 | * |
||
| 1938 | * @param string $errorMessage |
||
| 1939 | * @param int $errorNumber |
||
| 1940 | * @param string $sql |
||
| 1941 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1942 | * @param bool $sqlMultiQuery |
||
| 1943 | * |
||
| 1944 | * @return mixed|false |
||
| 1945 | * |
||
| 1946 | * @throws QueryException |
||
| 1947 | * @throws DBGoneAwayException |
||
| 1948 | */ |
||
| 1949 | 39 | private function queryErrorHandling(string $errorMessage, int $errorNumber, string $sql, $sqlParams = false, bool $sqlMultiQuery = false) |
|
| 1992 | |||
| 1993 | /** |
||
| 1994 | * Quote && Escape e.g. a table name string. |
||
| 1995 | * |
||
| 1996 | * @param mixed $str |
||
| 1997 | * |
||
| 1998 | * @return string |
||
| 1999 | */ |
||
| 2000 | 86 | public function quote_string($str): string |
|
| 2013 | |||
| 2014 | /** |
||
| 2015 | * Reconnect to the MySQL-Server. |
||
| 2016 | * |
||
| 2017 | * @param bool $checkViaPing |
||
| 2018 | * |
||
| 2019 | * @return bool |
||
| 2020 | */ |
||
| 2021 | 7 | public function reconnect(bool $checkViaPing = false): bool |
|
| 2035 | |||
| 2036 | /** |
||
| 2037 | * Execute a "replace"-query. |
||
| 2038 | * |
||
| 2039 | * @param string $table |
||
| 2040 | * @param array $data |
||
| 2041 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2042 | * |
||
| 2043 | * @return false|int <p>false on error</p> |
||
| 2044 | * |
||
| 2045 | * @throws QueryException |
||
| 2046 | */ |
||
| 2047 | 3 | public function replace(string $table, array $data = [], string $databaseName = null) |
|
| 2088 | |||
| 2089 | /** |
||
| 2090 | * Rollback in a transaction and end the transaction. |
||
| 2091 | * |
||
| 2092 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2093 | */ |
||
| 2094 | 12 | View Code Duplication | public function rollback(): bool |
| 2120 | |||
| 2121 | /** |
||
| 2122 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 2123 | * |
||
| 2124 | * <p> |
||
| 2125 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 2126 | * 1. parse into "int" |
||
| 2127 | * </p><br /> |
||
| 2128 | * |
||
| 2129 | * <p> |
||
| 2130 | * <strong>float:</strong><br /> |
||
| 2131 | * 1. return "float" |
||
| 2132 | * </p><br /> |
||
| 2133 | * |
||
| 2134 | * <p> |
||
| 2135 | * <strong>string:</strong><br /> |
||
| 2136 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 2137 | * 2. trim '<br /> |
||
| 2138 | * 3. escape the string (and remove non utf-8 chars)<br /> |
||
| 2139 | * 4. trim ' again (because we maybe removed some chars)<br /> |
||
| 2140 | * 5. add ' around the new string<br /> |
||
| 2141 | * </p><br /> |
||
| 2142 | * |
||
| 2143 | * <p> |
||
| 2144 | * <strong>array:</strong><br /> |
||
| 2145 | * 1. return null |
||
| 2146 | * </p><br /> |
||
| 2147 | * |
||
| 2148 | * <p> |
||
| 2149 | * <strong>object:</strong><br /> |
||
| 2150 | * 1. return false |
||
| 2151 | * </p><br /> |
||
| 2152 | * |
||
| 2153 | * <p> |
||
| 2154 | * <strong>null:</strong><br /> |
||
| 2155 | * 1. return null |
||
| 2156 | * </p> |
||
| 2157 | * |
||
| 2158 | * @param mixed $var |
||
| 2159 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 2160 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 2161 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 2162 | * |
||
| 2163 | * @return mixed |
||
| 2164 | */ |
||
| 2165 | 97 | public function secure($var, $convert_array = true) |
|
| 2233 | |||
| 2234 | /** |
||
| 2235 | * Execute a "select"-query. |
||
| 2236 | * |
||
| 2237 | * @param string $table |
||
| 2238 | * @param string|array $where |
||
| 2239 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2240 | * |
||
| 2241 | * @return false|Result <p>false on error</p> |
||
| 2242 | * |
||
| 2243 | * @throws QueryException |
||
| 2244 | */ |
||
| 2245 | 62 | View Code Duplication | public function select(string $table, $where = '1=1', string $databaseName = null) |
| 2272 | |||
| 2273 | /** |
||
| 2274 | * Selects a different database than the one specified on construction. |
||
| 2275 | * |
||
| 2276 | * @param string $database <p>Database name to switch to.</p> |
||
| 2277 | * |
||
| 2278 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2279 | */ |
||
| 2280 | public function select_db(string $database): bool |
||
| 2296 | |||
| 2297 | /** |
||
| 2298 | * @param array $extra_config <p> |
||
| 2299 | * 'session_to_db' => false|true<br> |
||
| 2300 | * 'socket' => 'string (path)'<br> |
||
| 2301 | * 'ssl' => 'bool'<br> |
||
| 2302 | * 'clientkey' => 'string (path)'<br> |
||
| 2303 | * 'clientcert' => 'string (path)'<br> |
||
| 2304 | * 'cacert' => 'string (path)'<br> |
||
| 2305 | * </p> |
||
| 2306 | */ |
||
| 2307 | 23 | public function setConfigExtra(array $extra_config) |
|
| 2341 | |||
| 2342 | /** |
||
| 2343 | * Set the current charset. |
||
| 2344 | * |
||
| 2345 | * @param string $charset |
||
| 2346 | * |
||
| 2347 | * @return bool |
||
| 2348 | */ |
||
| 2349 | 14 | public function set_charset(string $charset): bool |
|
| 2391 | |||
| 2392 | /** |
||
| 2393 | * Set the option to convert null to "''" (empty string). |
||
| 2394 | * |
||
| 2395 | * Used in secure() => select(), insert(), update(), delete() |
||
| 2396 | * |
||
| 2397 | * @deprecated It's not recommended to convert NULL into an empty string! |
||
| 2398 | * |
||
| 2399 | * @param bool $bool |
||
| 2400 | * |
||
| 2401 | * @return self |
||
| 2402 | */ |
||
| 2403 | 3 | public function set_convert_null_to_empty_string(bool $bool): self |
|
| 2409 | |||
| 2410 | /** |
||
| 2411 | * Enables or disables internal report functions |
||
| 2412 | * |
||
| 2413 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 2414 | * |
||
| 2415 | * @param int $flags <p> |
||
| 2416 | * <table> |
||
| 2417 | * Supported flags |
||
| 2418 | * <tr valign="top"> |
||
| 2419 | * <td>Name</td> |
||
| 2420 | * <td>Description</td> |
||
| 2421 | * </tr> |
||
| 2422 | * <tr valign="top"> |
||
| 2423 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 2424 | * <td>Turns reporting off</td> |
||
| 2425 | * </tr> |
||
| 2426 | * <tr valign="top"> |
||
| 2427 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 2428 | * <td>Report errors from mysqli function calls</td> |
||
| 2429 | * </tr> |
||
| 2430 | * <tr valign="top"> |
||
| 2431 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 2432 | * <td> |
||
| 2433 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 2434 | * instead of warnings |
||
| 2435 | * </td> |
||
| 2436 | * </tr> |
||
| 2437 | * <tr valign="top"> |
||
| 2438 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 2439 | * <td>Report if no index or bad index was used in a query</td> |
||
| 2440 | * </tr> |
||
| 2441 | * <tr valign="top"> |
||
| 2442 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 2443 | * <td>Set all options (report all)</td> |
||
| 2444 | * </tr> |
||
| 2445 | * </table> |
||
| 2446 | * </p> |
||
| 2447 | * |
||
| 2448 | * @return bool |
||
| 2449 | */ |
||
| 2450 | public function set_mysqli_report(int $flags): bool |
||
| 2462 | |||
| 2463 | /** |
||
| 2464 | * Show config errors by throw exceptions. |
||
| 2465 | * |
||
| 2466 | * @return bool |
||
| 2467 | * |
||
| 2468 | * @throws \InvalidArgumentException |
||
| 2469 | */ |
||
| 2470 | 23 | public function showConfigError(): bool |
|
| 2506 | |||
| 2507 | /** |
||
| 2508 | * alias: "beginTransaction()" |
||
| 2509 | */ |
||
| 2510 | 3 | public function startTransaction(): bool |
|
| 2514 | |||
| 2515 | /** |
||
| 2516 | * Determine if database table exists |
||
| 2517 | * |
||
| 2518 | * @param string $table |
||
| 2519 | * |
||
| 2520 | * @return bool |
||
| 2521 | */ |
||
| 2522 | 3 | public function table_exists(string $table): bool |
|
| 2532 | |||
| 2533 | /** |
||
| 2534 | * Execute a callback inside a transaction. |
||
| 2535 | * |
||
| 2536 | * @param callback $callback <p>The callback to run inside the transaction, if it's throws an "Exception" or if it's |
||
| 2537 | * returns "false", all SQL-statements in the callback will be rollbacked.</p> |
||
| 2538 | * |
||
| 2539 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2540 | */ |
||
| 2541 | 3 | public function transact($callback): bool |
|
| 2567 | |||
| 2568 | /** |
||
| 2569 | * Execute a "update"-query. |
||
| 2570 | * |
||
| 2571 | * @param string $table |
||
| 2572 | * @param array $data |
||
| 2573 | * @param array|string $where |
||
| 2574 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2575 | * |
||
| 2576 | * @return false|int <p>false on error</p> |
||
| 2577 | * |
||
| 2578 | * @throws QueryException |
||
| 2579 | */ |
||
| 2580 | 21 | public function update(string $table, array $data = [], $where = '1=1', string $databaseName = null) |
|
| 2615 | |||
| 2616 | } |
||
| 2617 |
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: