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 |
|
| 512 | |||
| 513 | /** |
||
| 514 | * _parseQueryParams |
||
| 515 | * |
||
| 516 | * @param string $sql |
||
| 517 | * @param array $params |
||
| 518 | * |
||
| 519 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 520 | */ |
||
| 521 | 17 | private function _parseQueryParams(string $sql, array $params = []): array |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Returns the SQL by replacing :placeholders with SQL-escaped values. |
||
| 558 | * |
||
| 559 | * @param mixed $sql <p>The SQL string.</p> |
||
| 560 | * @param array $params <p>An array of key-value bindings.</p> |
||
| 561 | * |
||
| 562 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 563 | */ |
||
| 564 | 20 | private function _parseQueryParamsByName(string $sql, array $params = []): array |
|
| 610 | |||
| 611 | /** |
||
| 612 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 613 | * |
||
| 614 | * @return int |
||
| 615 | */ |
||
| 616 | 28 | public function affected_rows(): int |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Begins a transaction, by turning off auto commit. |
||
| 631 | * |
||
| 632 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 633 | */ |
||
| 634 | 18 | public function beginTransaction(): bool |
|
| 664 | |||
| 665 | /** |
||
| 666 | * Clear the errors in "_debug->_errors". |
||
| 667 | * |
||
| 668 | * @return bool |
||
| 669 | */ |
||
| 670 | 18 | public function clearErrors(): bool |
|
| 674 | |||
| 675 | /** |
||
| 676 | * Closes a previously opened database connection. |
||
| 677 | * |
||
| 678 | * @return bool |
||
| 679 | * Will return "true", if the connection was closed, |
||
| 680 | * otherwise (e.g. if the connection was already closed) "false". |
||
| 681 | */ |
||
| 682 | 6 | public function close(): bool |
|
| 720 | |||
| 721 | /** |
||
| 722 | * Commits the current transaction and end the transaction. |
||
| 723 | * |
||
| 724 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 725 | */ |
||
| 726 | 9 | View Code Duplication | public function commit(): bool |
| 752 | |||
| 753 | /** |
||
| 754 | * Open a new connection to the MySQL server. |
||
| 755 | * |
||
| 756 | * @return bool |
||
| 757 | * |
||
| 758 | * @throws DBConnectException |
||
| 759 | */ |
||
| 760 | 20 | public function connect(): bool |
|
| 876 | |||
| 877 | /** |
||
| 878 | * Execute a "delete"-query. |
||
| 879 | * |
||
| 880 | * @param string $table |
||
| 881 | * @param string|array $where |
||
| 882 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 883 | * |
||
| 884 | * @return false|int <p>false on error</p> |
||
| 885 | * |
||
| 886 | * @throws QueryException |
||
| 887 | */ |
||
| 888 | 4 | View Code Duplication | public function delete(string $table, $where, string $databaseName = null) |
| 915 | |||
| 916 | /** |
||
| 917 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 918 | * |
||
| 919 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 920 | */ |
||
| 921 | 12 | public function endTransaction(): bool |
|
| 952 | |||
| 953 | /** |
||
| 954 | * Get all errors from "$this->errors". |
||
| 955 | * |
||
| 956 | * @return array|false <p>false === on errors</p> |
||
| 957 | */ |
||
| 958 | 12 | public function errors() |
|
| 964 | |||
| 965 | /** |
||
| 966 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 967 | * |
||
| 968 | * @param mixed $var bool: convert into "integer"<br /> |
||
| 969 | * int: int (don't change it)<br /> |
||
| 970 | * float: float (don't change it)<br /> |
||
| 971 | * null: null (don't change it)<br /> |
||
| 972 | * array: run escape() for every key => value<br /> |
||
| 973 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 974 | * @param bool $stripe_non_utf8 |
||
| 975 | * @param bool $html_entity_decode |
||
| 976 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 977 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 978 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 979 | * |
||
| 980 | * @return mixed |
||
| 981 | */ |
||
| 982 | 119 | public function escape($var = '', bool $stripe_non_utf8 = true, bool $html_entity_decode = false, $convert_array = false) |
|
| 1087 | |||
| 1088 | /** |
||
| 1089 | * Execute select/insert/update/delete sql-queries. |
||
| 1090 | * |
||
| 1091 | * @param string $query <p>sql-query</p> |
||
| 1092 | * @param bool $useCache optional <p>use cache?</p> |
||
| 1093 | * @param int $cacheTTL optional <p>cache-ttl in seconds</p> |
||
| 1094 | * @param DB|null $db optional <p>the database connection</p> |
||
| 1095 | * |
||
| 1096 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 1097 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 1098 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1099 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1100 | * "false" on error |
||
| 1101 | * |
||
| 1102 | * @throws QueryException |
||
| 1103 | */ |
||
| 1104 | 9 | public static function execSQL(string $query, bool $useCache = false, int $cacheTTL = 3600, self $db = null) |
|
| 1153 | |||
| 1154 | /** |
||
| 1155 | * Get all table-names via "SHOW TABLES". |
||
| 1156 | * |
||
| 1157 | * @return array |
||
| 1158 | */ |
||
| 1159 | 3 | public function getAllTables(): array |
|
| 1166 | |||
| 1167 | /** |
||
| 1168 | * @return array |
||
| 1169 | */ |
||
| 1170 | 8 | public function getConfig() |
|
| 1191 | |||
| 1192 | /** |
||
| 1193 | * @return Debug |
||
| 1194 | */ |
||
| 1195 | 9 | public function getDebugger(): Debug |
|
| 1199 | |||
| 1200 | /** |
||
| 1201 | * @return null|\Doctrine\DBAL\Connection|null |
||
| 1202 | */ |
||
| 1203 | 2 | public function getDoctrineConnection() |
|
| 1207 | |||
| 1208 | /** |
||
| 1209 | * @return false|\Doctrine\DBAL\Driver\Connection |
||
| 1210 | */ |
||
| 1211 | View Code Duplication | private function getDoctrinePDOConnection() |
|
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Get errors from "$this->errors". |
||
| 1225 | * |
||
| 1226 | * @return array |
||
| 1227 | */ |
||
| 1228 | 3 | public function getErrors(): array |
|
| 1232 | |||
| 1233 | /** |
||
| 1234 | * @param string $hostname <p>Hostname of the mysql server</p> |
||
| 1235 | * @param string $username <p>Username for the mysql connection</p> |
||
| 1236 | * @param string $password <p>Password for the mysql connection</p> |
||
| 1237 | * @param string $database <p>Database for the mysql connection</p> |
||
| 1238 | * @param int $port <p>default is (int)3306</p> |
||
| 1239 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1240 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 1241 | * Use false to disable it.</p> |
||
| 1242 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1243 | * Use false to disable it.</p> |
||
| 1244 | * @param string $logger_class_name |
||
| 1245 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1246 | * @param array $extra_config <p> |
||
| 1247 | * 're_connect' => bool<br> |
||
| 1248 | * 'session_to_db' => bool<br> |
||
| 1249 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 1250 | * 'socket' => 'string (path)'<br> |
||
| 1251 | * 'ssl' => bool<br> |
||
| 1252 | * 'clientkey' => 'string (path)'<br> |
||
| 1253 | * 'clientcert' => 'string (path)'<br> |
||
| 1254 | * 'cacert' => 'string (path)'<br> |
||
| 1255 | * </p> |
||
| 1256 | * |
||
| 1257 | * @return self |
||
| 1258 | */ |
||
| 1259 | 231 | public static function getInstance( |
|
| 1340 | |||
| 1341 | /** |
||
| 1342 | * @param \Doctrine\DBAL\Connection $doctrine |
||
| 1343 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1344 | * @param bool $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will |
||
| 1345 | * return 'false'. Use false to disable it.</p> |
||
| 1346 | * @param bool $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1347 | * Use false to disable it.</p> |
||
| 1348 | * @param string $logger_class_name |
||
| 1349 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1350 | * @param array $extra_config <p> |
||
| 1351 | * 're_connect' => bool<br> |
||
| 1352 | * 'session_to_db' => bool<br> |
||
| 1353 | * 'doctrine' => \Doctrine\DBAL\Connection<br> |
||
| 1354 | * 'socket' => 'string (path)'<br> |
||
| 1355 | * 'ssl' => bool<br> |
||
| 1356 | * 'clientkey' => 'string (path)'<br> |
||
| 1357 | * 'clientcert' => 'string (path)'<br> |
||
| 1358 | * 'cacert' => 'string (path)'<br> |
||
| 1359 | * </p> |
||
| 1360 | * |
||
| 1361 | * @return self |
||
| 1362 | */ |
||
| 1363 | 55 | public static function getInstanceDoctrineHelper( |
|
| 1389 | |||
| 1390 | /** |
||
| 1391 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1392 | * |
||
| 1393 | * @return null|\mysqli |
||
| 1394 | */ |
||
| 1395 | 53 | public function getLink() |
|
| 1399 | |||
| 1400 | /** |
||
| 1401 | * Get the current charset. |
||
| 1402 | * |
||
| 1403 | * @return string |
||
| 1404 | */ |
||
| 1405 | 3 | public function get_charset(): string |
|
| 1409 | |||
| 1410 | /** |
||
| 1411 | * Check if we are in a transaction. |
||
| 1412 | * |
||
| 1413 | * @return bool |
||
| 1414 | */ |
||
| 1415 | public function inTransaction(): bool |
||
| 1419 | |||
| 1420 | /** |
||
| 1421 | * Execute a "insert"-query. |
||
| 1422 | * |
||
| 1423 | * @param string $table |
||
| 1424 | * @param array $data |
||
| 1425 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1426 | * |
||
| 1427 | * @return false|int <p>false on error</p> |
||
| 1428 | * |
||
| 1429 | * @throws QueryException |
||
| 1430 | */ |
||
| 1431 | 74 | public function insert(string $table, array $data = [], string $databaseName = null) |
|
| 1458 | |||
| 1459 | /** |
||
| 1460 | * Returns the auto generated id used in the last query. |
||
| 1461 | * |
||
| 1462 | * @return int|string |
||
| 1463 | */ |
||
| 1464 | 104 | public function insert_id() |
|
| 1474 | |||
| 1475 | /** |
||
| 1476 | * @return bool |
||
| 1477 | */ |
||
| 1478 | public function isDoctrineMySQLiConnection(): bool |
||
| 1489 | |||
| 1490 | /** |
||
| 1491 | * @return bool |
||
| 1492 | */ |
||
| 1493 | 9 | View Code Duplication | public function isDoctrinePDOConnection(): bool |
| 1504 | |||
| 1505 | /** |
||
| 1506 | * Check if db-connection is ready. |
||
| 1507 | * |
||
| 1508 | * @return bool |
||
| 1509 | */ |
||
| 1510 | 184 | public function isReady(): bool |
|
| 1514 | |||
| 1515 | /** |
||
| 1516 | * Get the last sql-error. |
||
| 1517 | * |
||
| 1518 | * @return string|false <p>false === there was no error</p> |
||
| 1519 | */ |
||
| 1520 | 3 | public function lastError() |
|
| 1526 | |||
| 1527 | /** |
||
| 1528 | * Execute a sql-multi-query. |
||
| 1529 | * |
||
| 1530 | * @param string $sql |
||
| 1531 | * |
||
| 1532 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1533 | * "bool" by only "<b>INSERT</b>"-queries<br /> |
||
| 1534 | * "bool" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1535 | * "bool" by only by e.g. "DROP"-queries<br /> |
||
| 1536 | * |
||
| 1537 | * @throws QueryException |
||
| 1538 | */ |
||
| 1539 | 3 | public function multi_query(string $sql) |
|
| 1680 | |||
| 1681 | /** |
||
| 1682 | * Count number of rows found matching a specific query. |
||
| 1683 | * |
||
| 1684 | * @param string $query |
||
| 1685 | * |
||
| 1686 | * @return int |
||
| 1687 | */ |
||
| 1688 | 3 | public function num_rows(string $query): int |
|
| 1702 | |||
| 1703 | /** |
||
| 1704 | * Pings a server connection, or tries to reconnect |
||
| 1705 | * if the connection has gone down. |
||
| 1706 | * |
||
| 1707 | * @return bool |
||
| 1708 | */ |
||
| 1709 | 9 | public function ping(): bool |
|
| 1729 | |||
| 1730 | /** |
||
| 1731 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1732 | * |
||
| 1733 | * @param string $query |
||
| 1734 | * |
||
| 1735 | * @return Prepare |
||
| 1736 | */ |
||
| 1737 | 2 | public function prepare(string $query): Prepare |
|
| 1741 | |||
| 1742 | /** |
||
| 1743 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1744 | * |
||
| 1745 | * @param string $query |
||
| 1746 | * |
||
| 1747 | * @return mixed |
||
| 1748 | * @deprecated |
||
| 1749 | * @throws \Exception |
||
| 1750 | */ |
||
| 1751 | 3 | public static function qry(string $query) |
|
| 1776 | |||
| 1777 | /** |
||
| 1778 | * Execute a sql-query. |
||
| 1779 | * |
||
| 1780 | * example: |
||
| 1781 | * <code> |
||
| 1782 | * $sql = "INSERT INTO TABLE_NAME_HERE |
||
| 1783 | * SET |
||
| 1784 | * foo = :foo, |
||
| 1785 | * bar = :bar |
||
| 1786 | * "; |
||
| 1787 | * $insert_id = $db->query( |
||
| 1788 | * $sql, |
||
| 1789 | * [ |
||
| 1790 | * 'foo' => 1.1, |
||
| 1791 | * 'bar' => 1, |
||
| 1792 | * ] |
||
| 1793 | * ); |
||
| 1794 | * </code> |
||
| 1795 | * |
||
| 1796 | * @param string $sql <p>The sql query-string.</p> |
||
| 1797 | * |
||
| 1798 | * @param array|bool $params <p> |
||
| 1799 | * "array" of sql-query-parameters<br/> |
||
| 1800 | * "false" if you don't need any parameter (default)<br/> |
||
| 1801 | * </p> |
||
| 1802 | * |
||
| 1803 | * @return bool|int|Result <p> |
||
| 1804 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1805 | * "int|string" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1806 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1807 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1808 | * "false" on error |
||
| 1809 | * </p> |
||
| 1810 | * |
||
| 1811 | * @throws QueryException |
||
| 1812 | */ |
||
| 1813 | 164 | public function query(string $sql = '', $params = false) |
|
| 1958 | |||
| 1959 | /** |
||
| 1960 | * Error-handling for the sql-query. |
||
| 1961 | * |
||
| 1962 | * @param string $errorMessage |
||
| 1963 | * @param int $errorNumber |
||
| 1964 | * @param string $sql |
||
| 1965 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1966 | * @param bool $sqlMultiQuery |
||
| 1967 | * |
||
| 1968 | * @return mixed|false |
||
| 1969 | * |
||
| 1970 | * @throws QueryException |
||
| 1971 | * @throws DBGoneAwayException |
||
| 1972 | */ |
||
| 1973 | 39 | private function queryErrorHandling(string $errorMessage, int $errorNumber, string $sql, $sqlParams = false, bool $sqlMultiQuery = false) |
|
| 2016 | |||
| 2017 | /** |
||
| 2018 | * Quote && Escape e.g. a table name string. |
||
| 2019 | * |
||
| 2020 | * @param mixed $str |
||
| 2021 | * |
||
| 2022 | * @return string |
||
| 2023 | */ |
||
| 2024 | 86 | public function quote_string($str): string |
|
| 2037 | |||
| 2038 | /** |
||
| 2039 | * Reconnect to the MySQL-Server. |
||
| 2040 | * |
||
| 2041 | * @param bool $checkViaPing |
||
| 2042 | * |
||
| 2043 | * @return bool |
||
| 2044 | */ |
||
| 2045 | 7 | public function reconnect(bool $checkViaPing = false): bool |
|
| 2059 | |||
| 2060 | /** |
||
| 2061 | * Execute a "replace"-query. |
||
| 2062 | * |
||
| 2063 | * @param string $table |
||
| 2064 | * @param array $data |
||
| 2065 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2066 | * |
||
| 2067 | * @return false|int <p>false on error</p> |
||
| 2068 | * |
||
| 2069 | * @throws QueryException |
||
| 2070 | */ |
||
| 2071 | 3 | public function replace(string $table, array $data = [], string $databaseName = null) |
|
| 2112 | |||
| 2113 | /** |
||
| 2114 | * Rollback in a transaction and end the transaction. |
||
| 2115 | * |
||
| 2116 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2117 | */ |
||
| 2118 | 12 | View Code Duplication | public function rollback(): bool |
| 2144 | |||
| 2145 | /** |
||
| 2146 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 2147 | * |
||
| 2148 | * <p> |
||
| 2149 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 2150 | * 1. parse into "int" |
||
| 2151 | * </p><br /> |
||
| 2152 | * |
||
| 2153 | * <p> |
||
| 2154 | * <strong>float:</strong><br /> |
||
| 2155 | * 1. return "float" |
||
| 2156 | * </p><br /> |
||
| 2157 | * |
||
| 2158 | * <p> |
||
| 2159 | * <strong>string:</strong><br /> |
||
| 2160 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 2161 | * 2. trim '<br /> |
||
| 2162 | * 3. escape the string (and remove non utf-8 chars)<br /> |
||
| 2163 | * 4. trim ' again (because we maybe removed some chars)<br /> |
||
| 2164 | * 5. add ' around the new string<br /> |
||
| 2165 | * </p><br /> |
||
| 2166 | * |
||
| 2167 | * <p> |
||
| 2168 | * <strong>array:</strong><br /> |
||
| 2169 | * 1. return null |
||
| 2170 | * </p><br /> |
||
| 2171 | * |
||
| 2172 | * <p> |
||
| 2173 | * <strong>object:</strong><br /> |
||
| 2174 | * 1. return false |
||
| 2175 | * </p><br /> |
||
| 2176 | * |
||
| 2177 | * <p> |
||
| 2178 | * <strong>null:</strong><br /> |
||
| 2179 | * 1. return null |
||
| 2180 | * </p> |
||
| 2181 | * |
||
| 2182 | * @param mixed $var |
||
| 2183 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 2184 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 2185 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 2186 | * |
||
| 2187 | * @return mixed |
||
| 2188 | */ |
||
| 2189 | 97 | public function secure($var, $convert_array = true) |
|
| 2257 | |||
| 2258 | /** |
||
| 2259 | * Execute a "select"-query. |
||
| 2260 | * |
||
| 2261 | * @param string $table |
||
| 2262 | * @param string|array $where |
||
| 2263 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2264 | * |
||
| 2265 | * @return false|Result <p>false on error</p> |
||
| 2266 | * |
||
| 2267 | * @throws QueryException |
||
| 2268 | */ |
||
| 2269 | 62 | View Code Duplication | public function select(string $table, $where = '1=1', string $databaseName = null) |
| 2296 | |||
| 2297 | /** |
||
| 2298 | * Selects a different database than the one specified on construction. |
||
| 2299 | * |
||
| 2300 | * @param string $database <p>Database name to switch to.</p> |
||
| 2301 | * |
||
| 2302 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2303 | */ |
||
| 2304 | public function select_db(string $database): bool |
||
| 2320 | |||
| 2321 | /** |
||
| 2322 | * @param array $extra_config <p> |
||
| 2323 | * 'session_to_db' => false|true<br> |
||
| 2324 | * 'socket' => 'string (path)'<br> |
||
| 2325 | * 'ssl' => 'bool'<br> |
||
| 2326 | * 'clientkey' => 'string (path)'<br> |
||
| 2327 | * 'clientcert' => 'string (path)'<br> |
||
| 2328 | * 'cacert' => 'string (path)'<br> |
||
| 2329 | * </p> |
||
| 2330 | */ |
||
| 2331 | 23 | public function setConfigExtra(array $extra_config) |
|
| 2365 | |||
| 2366 | /** |
||
| 2367 | * Set the current charset. |
||
| 2368 | * |
||
| 2369 | * @param string $charset |
||
| 2370 | * |
||
| 2371 | * @return bool |
||
| 2372 | */ |
||
| 2373 | 14 | public function set_charset(string $charset): bool |
|
| 2415 | |||
| 2416 | /** |
||
| 2417 | * Set the option to convert null to "''" (empty string). |
||
| 2418 | * |
||
| 2419 | * Used in secure() => select(), insert(), update(), delete() |
||
| 2420 | * |
||
| 2421 | * @deprecated It's not recommended to convert NULL into an empty string! |
||
| 2422 | * |
||
| 2423 | * @param bool $bool |
||
| 2424 | * |
||
| 2425 | * @return self |
||
| 2426 | */ |
||
| 2427 | 3 | public function set_convert_null_to_empty_string(bool $bool): self |
|
| 2433 | |||
| 2434 | /** |
||
| 2435 | * Enables or disables internal report functions |
||
| 2436 | * |
||
| 2437 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 2438 | * |
||
| 2439 | * @param int $flags <p> |
||
| 2440 | * <table> |
||
| 2441 | * Supported flags |
||
| 2442 | * <tr valign="top"> |
||
| 2443 | * <td>Name</td> |
||
| 2444 | * <td>Description</td> |
||
| 2445 | * </tr> |
||
| 2446 | * <tr valign="top"> |
||
| 2447 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 2448 | * <td>Turns reporting off</td> |
||
| 2449 | * </tr> |
||
| 2450 | * <tr valign="top"> |
||
| 2451 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 2452 | * <td>Report errors from mysqli function calls</td> |
||
| 2453 | * </tr> |
||
| 2454 | * <tr valign="top"> |
||
| 2455 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 2456 | * <td> |
||
| 2457 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 2458 | * instead of warnings |
||
| 2459 | * </td> |
||
| 2460 | * </tr> |
||
| 2461 | * <tr valign="top"> |
||
| 2462 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 2463 | * <td>Report if no index or bad index was used in a query</td> |
||
| 2464 | * </tr> |
||
| 2465 | * <tr valign="top"> |
||
| 2466 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 2467 | * <td>Set all options (report all)</td> |
||
| 2468 | * </tr> |
||
| 2469 | * </table> |
||
| 2470 | * </p> |
||
| 2471 | * |
||
| 2472 | * @return bool |
||
| 2473 | */ |
||
| 2474 | public function set_mysqli_report(int $flags): bool |
||
| 2486 | |||
| 2487 | /** |
||
| 2488 | * Show config errors by throw exceptions. |
||
| 2489 | * |
||
| 2490 | * @return bool |
||
| 2491 | * |
||
| 2492 | * @throws \InvalidArgumentException |
||
| 2493 | */ |
||
| 2494 | 23 | public function showConfigError(): bool |
|
| 2530 | |||
| 2531 | /** |
||
| 2532 | * alias: "beginTransaction()" |
||
| 2533 | */ |
||
| 2534 | 3 | public function startTransaction(): bool |
|
| 2538 | |||
| 2539 | /** |
||
| 2540 | * Determine if database table exists |
||
| 2541 | * |
||
| 2542 | * @param string $table |
||
| 2543 | * |
||
| 2544 | * @return bool |
||
| 2545 | */ |
||
| 2546 | 3 | public function table_exists(string $table): bool |
|
| 2556 | |||
| 2557 | /** |
||
| 2558 | * Execute a callback inside a transaction. |
||
| 2559 | * |
||
| 2560 | * @param callback $callback <p>The callback to run inside the transaction, if it's throws an "Exception" or if it's |
||
| 2561 | * returns "false", all SQL-statements in the callback will be rollbacked.</p> |
||
| 2562 | * |
||
| 2563 | * @return bool <p>bool true on success, false otherwise.</p> |
||
| 2564 | */ |
||
| 2565 | 3 | public function transact($callback): bool |
|
| 2591 | |||
| 2592 | /** |
||
| 2593 | * Execute a "update"-query. |
||
| 2594 | * |
||
| 2595 | * @param string $table |
||
| 2596 | * @param array $data |
||
| 2597 | * @param array|string $where |
||
| 2598 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 2599 | * |
||
| 2600 | * @return false|int <p>false on error</p> |
||
| 2601 | * |
||
| 2602 | * @throws QueryException |
||
| 2603 | */ |
||
| 2604 | 21 | public function update(string $table, array $data = [], $where = '1=1', string $databaseName = null) |
|
| 2645 | |||
| 2646 | } |
||
| 2647 |
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: