Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DB often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DB, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | final class DB |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | public $query_count = 0; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \mysqli |
||
| 26 | */ |
||
| 27 | private $link; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | private $connected = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $mysqlDefaultTimeFunctions; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $hostname = ''; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $username = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $password = ''; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $database = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var int |
||
| 61 | */ |
||
| 62 | private $port = 3306; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $charset = 'utf8'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $socket = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | private $session_to_db = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $_in_transaction = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var bool |
||
| 86 | */ |
||
| 87 | private $_convert_null_to_empty_string = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var bool |
||
| 91 | */ |
||
| 92 | private $_ssl = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The path name to the key file |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | private $_clientkey; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The path name to the certificate file |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | private $_clientcert; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The path name to the certificate authority file |
||
| 110 | * |
||
| 111 | * @var string |
||
| 112 | */ |
||
| 113 | private $_cacert; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var Debug |
||
| 117 | */ |
||
| 118 | private $_debug; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * __construct() |
||
| 122 | * |
||
| 123 | * @param string $hostname |
||
| 124 | * @param string $username |
||
| 125 | * @param string $password |
||
| 126 | * @param string $database |
||
| 127 | * @param int $port |
||
| 128 | * @param string $charset |
||
| 129 | * @param bool|string $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 130 | * Use a empty string "" or false to disable it.</p> |
||
| 131 | * @param bool|string $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 132 | * Use a empty string "" or false to disable it.</p> |
||
| 133 | * @param string $logger_class_name |
||
| 134 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 135 | * @param array $extra_config <p> |
||
| 136 | * 'session_to_db' => false|true<br> |
||
| 137 | * 'socket' => 'string (path)'<br> |
||
| 138 | * 'ssl' => 'bool'<br> |
||
| 139 | * 'clientkey' => 'string (path)'<br> |
||
| 140 | * 'clientcert' => 'string (path)'<br> |
||
| 141 | * 'cacert' => 'string (path)'<br> |
||
| 142 | * </p> |
||
| 143 | */ |
||
| 144 | 11 | protected function __construct($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config = array()) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Prevent the instance from being cloned. |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | private function __clone() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * __destruct |
||
| 207 | * |
||
| 208 | */ |
||
| 209 | public function __destruct() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param null|string $sql |
||
| 219 | * @param array $bindings |
||
| 220 | * |
||
| 221 | * @return bool|int|Result|DB <p> |
||
| 222 | * "DB" by "$sql" === null<br /> |
||
| 223 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 224 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 225 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 226 | * "true" by e.g. "DROP"-queries<br /> |
||
| 227 | * "false" on error |
||
| 228 | * </p> |
||
| 229 | */ |
||
| 230 | 2 | public function __invoke($sql = null, array $bindings = array()) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * __wakeup |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | 2 | public function __wakeup() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Load the config from the constructor. |
||
| 247 | * |
||
| 248 | * @param string $hostname |
||
| 249 | * @param string $username |
||
| 250 | * @param string $password |
||
| 251 | * @param string $database |
||
| 252 | * @param int|string $port <p>default is (int)3306</p> |
||
| 253 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 254 | * @param bool|string $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 255 | * Use a empty string "" or false to disable it.</p> |
||
| 256 | * @param bool|string $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 257 | * Use a empty string "" or false to disable it.</p> |
||
| 258 | * @param string $logger_class_name |
||
| 259 | * @param string $logger_level |
||
| 260 | * @param array $extra_config <p> |
||
| 261 | * 'session_to_db' => false|true<br> |
||
| 262 | * 'socket' => 'string (path)'<br> |
||
| 263 | * 'ssl' => 'bool'<br> |
||
| 264 | * 'clientkey' => 'string (path)'<br> |
||
| 265 | * 'clientcert' => 'string (path)'<br> |
||
| 266 | * 'cacert' => 'string (path)'<br> |
||
| 267 | * </p> |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | 11 | private function _loadConfig($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 347 | * |
||
| 348 | * @param array $arrayPair |
||
| 349 | * @param string $glue <p>This is the separator.</p> |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | * |
||
| 353 | * @internal |
||
| 354 | */ |
||
| 355 | 30 | public function _parseArrayPair(array $arrayPair, $glue = ',') |
|
| 500 | |||
| 501 | /** |
||
| 502 | * _parseQueryParams |
||
| 503 | * |
||
| 504 | * @param string $sql |
||
| 505 | * @param array $params |
||
| 506 | * |
||
| 507 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 508 | */ |
||
| 509 | private function _parseQueryParams($sql, array $params = array()) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 543 | * |
||
| 544 | * @return int |
||
| 545 | */ |
||
| 546 | public function affected_rows() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Begins a transaction, by turning off auto commit. |
||
| 553 | * |
||
| 554 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 555 | */ |
||
| 556 | View Code Duplication | public function beginTransaction() |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Clear the errors in "_debug->_errors". |
||
| 576 | * |
||
| 577 | * @return bool |
||
| 578 | */ |
||
| 579 | public function clearErrors() |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Closes a previously opened database connection. |
||
| 586 | */ |
||
| 587 | public function close() |
||
| 595 | 2 | ||
| 596 | /** |
||
| 597 | * Commits the current transaction and end the transaction. |
||
| 598 | * |
||
| 599 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 600 | */ |
||
| 601 | View Code Duplication | public function commit() |
|
| 615 | |||
| 616 | /** |
||
| 617 | * Open a new connection to the MySQL server. |
||
| 618 | * |
||
| 619 | * @return bool |
||
| 620 | * |
||
| 621 | * @throws DBConnectException |
||
| 622 | */ |
||
| 623 | public function connect() |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Execute a "delete"-query. |
||
| 701 | * |
||
| 702 | * @param string $table |
||
| 703 | * @param string|array $where |
||
| 704 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 705 | * |
||
| 706 | * @return false|int <p>false on error</p> |
||
| 707 | * |
||
| 708 | * * @throws QueryException |
||
| 709 | */ |
||
| 710 | View Code Duplication | public function delete($table, $where, $databaseName = null) |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 740 | * |
||
| 741 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 742 | */ |
||
| 743 | View Code Duplication | public function endTransaction() |
|
| 763 | |||
| 764 | /** |
||
| 765 | * Get all errors from "$this->_errors". |
||
| 766 | * |
||
| 767 | * @return array|false <p>false === on errors</p> |
||
| 768 | */ |
||
| 769 | public function errors() |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Returns the SQL by replacing :placeholders with SQL-escaped values. |
||
| 778 | * |
||
| 779 | * @param mixed $sql <p>The SQL string.</p> |
||
| 780 | * @param array $params <p>An array of key-value bindings.</p> |
||
| 781 | * |
||
| 782 | * @return array <p>with the keys -> 'sql', 'params'</p> |
||
| 783 | */ |
||
| 784 | public function _parseQueryParamsByName($sql, array $params = array()) |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 835 | * |
||
| 836 | * @param mixed $var boolean: convert into "integer"<br /> |
||
| 837 | * int: int (don't change it)<br /> |
||
| 838 | * float: float (don't change it)<br /> |
||
| 839 | * null: null (don't change it)<br /> |
||
| 840 | * array: run escape() for every key => value<br /> |
||
| 841 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 842 | * @param bool $stripe_non_utf8 |
||
| 843 | * @param bool $html_entity_decode |
||
| 844 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 845 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 846 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 847 | * |
||
| 848 | * @return mixed |
||
| 849 | */ |
||
| 850 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Execute select/insert/update/delete sql-queries. |
||
| 973 | * |
||
| 974 | * @param string $query <p>sql-query</p> |
||
| 975 | * @param bool $useCache <p>use cache?</p> |
||
| 976 | * @param int $cacheTTL <p>cache-ttl in seconds</p> |
||
| 977 | * @param DB $db optional <p>the database connection</p> |
||
| 978 | * |
||
| 979 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 980 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 981 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 982 | 3 | * "true" by e.g. "DROP"-queries<br /> |
|
| 983 | * "false" on error |
||
| 984 | * |
||
| 985 | 3 | * @throws QueryException |
|
| 986 | 3 | */ |
|
| 987 | 3 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600, DB $db = null) |
|
| 1036 | |||
| 1037 | 1 | /** |
|
| 1038 | * Get all table-names via "SHOW TABLES". |
||
| 1039 | 1 | * |
|
| 1040 | 1 | * @return array |
|
| 1041 | */ |
||
| 1042 | 1 | public function getAllTables() |
|
| 1049 | |||
| 1050 | 9 | /** |
|
| 1051 | * @return Debug |
||
| 1052 | */ |
||
| 1053 | public function getDebugger() |
||
| 1057 | |||
| 1058 | 1 | /** |
|
| 1059 | * Get errors from "$this->_errors". |
||
| 1060 | 1 | * |
|
| 1061 | * @return array |
||
| 1062 | */ |
||
| 1063 | public function getErrors() |
||
| 1067 | |||
| 1068 | /** |
||
| 1069 | * getInstance() |
||
| 1070 | * |
||
| 1071 | * @param string $hostname |
||
| 1072 | * @param string $username |
||
| 1073 | * @param string $password |
||
| 1074 | * @param string $database |
||
| 1075 | * @param int|string $port <p>default is (int)3306</p> |
||
| 1076 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1077 | * @param bool|string $exit_on_error <p>Throw a 'Exception' when a query failed, otherwise it will return 'false'. |
||
| 1078 | * Use a empty string "" or false to disable it.</p> |
||
| 1079 | * @param bool|string $echo_on_error <p>Echo the error if "checkForDev()" returns true. |
||
| 1080 | * Use a empty string "" or false to disable it.</p> |
||
| 1081 | * @param string $logger_class_name |
||
| 1082 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 1083 | * @param array $extra_config <p> |
||
| 1084 | * 'session_to_db' => false|true<br> |
||
| 1085 | * 'socket' => 'string (path)'<br> |
||
| 1086 | * 'ssl' => 'bool'<br> |
||
| 1087 | * 'clientkey' => 'string (path)'<br> |
||
| 1088 | * 'clientcert' => 'string (path)'<br> |
||
| 1089 | 97 | * 'cacert' => 'string (path)'<br> |
|
| 1090 | * </p> |
||
| 1091 | * |
||
| 1092 | * @return \voku\db\DB |
||
| 1093 | */ |
||
| 1094 | 97 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = true, $echo_on_error = true, $logger_class_name = '', $logger_level = '', $extra_config = array()) |
|
| 1150 | |||
| 1151 | 56 | /** |
|
| 1152 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1153 | 56 | * |
|
| 1154 | * @return \mysqli |
||
| 1155 | */ |
||
| 1156 | public function getLink() |
||
| 1160 | |||
| 1161 | 1 | /** |
|
| 1162 | * Get the current charset. |
||
| 1163 | 1 | * |
|
| 1164 | * @return string |
||
| 1165 | */ |
||
| 1166 | public function get_charset() |
||
| 1170 | |||
| 1171 | /** |
||
| 1172 | * Check if we are in a transaction. |
||
| 1173 | * |
||
| 1174 | * @return bool |
||
| 1175 | */ |
||
| 1176 | public function inTransaction() |
||
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Execute a "insert"-query. |
||
| 1183 | * |
||
| 1184 | * @param string $table |
||
| 1185 | * @param array $data |
||
| 1186 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1187 | 28 | * |
|
| 1188 | * @return false|int <p>false on error</p> |
||
| 1189 | * |
||
| 1190 | 28 | * @throws QueryException |
|
| 1191 | */ |
||
| 1192 | 28 | public function insert($table, array $data = array(), $databaseName = null) |
|
| 1219 | |||
| 1220 | 58 | /** |
|
| 1221 | * Returns the auto generated id used in the last query. |
||
| 1222 | 58 | * |
|
| 1223 | * @return int|string |
||
| 1224 | */ |
||
| 1225 | public function insert_id() |
||
| 1229 | |||
| 1230 | 106 | /** |
|
| 1231 | * Check if db-connection is ready. |
||
| 1232 | 106 | * |
|
| 1233 | * @return boolean |
||
| 1234 | */ |
||
| 1235 | public function isReady() |
||
| 1239 | |||
| 1240 | 1 | /** |
|
| 1241 | * Get the last sql-error. |
||
| 1242 | 1 | * |
|
| 1243 | * @return string|false <p>false === there was no error</p> |
||
| 1244 | 1 | */ |
|
| 1245 | public function lastError() |
||
| 1251 | |||
| 1252 | /** |
||
| 1253 | * Execute a sql-multi-query. |
||
| 1254 | * |
||
| 1255 | * @param string $sql |
||
| 1256 | * |
||
| 1257 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1258 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
||
| 1259 | 1 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 1260 | * "boolean" by only by e.g. "DROP"-queries<br /> |
||
| 1261 | 1 | * |
|
| 1262 | * @throws QueryException |
||
| 1263 | */ |
||
| 1264 | public function multi_query($sql) |
||
| 1331 | |||
| 1332 | /** |
||
| 1333 | 3 | * Pings a server connection, or tries to reconnect |
|
| 1334 | * if the connection has gone down. |
||
| 1335 | * |
||
| 1336 | 3 | * @return boolean |
|
| 1337 | 3 | */ |
|
| 1338 | 3 | public function ping() |
|
| 1352 | |||
| 1353 | /** |
||
| 1354 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1355 | 2 | * |
|
| 1356 | * @param string $query |
||
| 1357 | 2 | * |
|
| 1358 | * @return Prepare |
||
| 1359 | */ |
||
| 1360 | public function prepare($query) |
||
| 1364 | |||
| 1365 | /** |
||
| 1366 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1367 | * |
||
| 1368 | * @param string $query |
||
| 1369 | * |
||
| 1370 | * @return mixed |
||
| 1371 | * @deprecated |
||
| 1372 | * @throws \Exception |
||
| 1373 | */ |
||
| 1374 | public static function qry($query) |
||
| 1399 | |||
| 1400 | /** |
||
| 1401 | * Execute a sql-query. |
||
| 1402 | * |
||
| 1403 | * @param string $sql <p>The sql query-string.</p> |
||
| 1404 | * |
||
| 1405 | * @param array|boolean $params <p> |
||
| 1406 | * "array" of sql-query-parameters<br/> |
||
| 1407 | * "false" if you don't need any parameter (default)<br/> |
||
| 1408 | * </p> |
||
| 1409 | * |
||
| 1410 | * @return bool|int|Result <p> |
||
| 1411 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1412 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1413 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1414 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1415 | 96 | * "false" on error |
|
| 1416 | * </p> |
||
| 1417 | 96 | * |
|
| 1418 | * @throws QueryException |
||
| 1419 | */ |
||
| 1420 | public function query($sql = '', $params = false) |
||
| 1499 | |||
| 1500 | /** |
||
| 1501 | * Error-handling for the sql-query. |
||
| 1502 | * |
||
| 1503 | * @param string $errorMessage |
||
| 1504 | * @param int $errorNumber |
||
| 1505 | * @param string $sql |
||
| 1506 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1507 | * @param bool $sqlMultiQuery |
||
| 1508 | * |
||
| 1509 | 13 | * @throws QueryException |
|
| 1510 | * @throws DBGoneAwayException |
||
| 1511 | 13 | * |
|
| 1512 | * @return bool |
||
| 1513 | */ |
||
| 1514 | private function queryErrorHandling($errorMessage, $errorNumber, $sql, $sqlParams = false, $sqlMultiQuery = false) |
||
| 1559 | |||
| 1560 | /** |
||
| 1561 | * Quote && Escape e.g. a table name string. |
||
| 1562 | 36 | * |
|
| 1563 | * @param string $str |
||
| 1564 | 36 | * |
|
| 1565 | 36 | * @return string |
|
| 1566 | 36 | */ |
|
| 1567 | 36 | public function quote_string($str) |
|
| 1580 | |||
| 1581 | /** |
||
| 1582 | * Reconnect to the MySQL-Server. |
||
| 1583 | 3 | * |
|
| 1584 | * @param bool $checkViaPing |
||
| 1585 | 3 | * |
|
| 1586 | * @return bool |
||
| 1587 | 3 | */ |
|
| 1588 | 2 | public function reconnect($checkViaPing = false) |
|
| 1603 | |||
| 1604 | /** |
||
| 1605 | * Execute a "replace"-query. |
||
| 1606 | * |
||
| 1607 | * @param string $table |
||
| 1608 | * @param array $data |
||
| 1609 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1610 | 1 | * |
|
| 1611 | * @return false|int <p>false on error</p> |
||
| 1612 | * |
||
| 1613 | 1 | * @throws QueryException |
|
| 1614 | */ |
||
| 1615 | 1 | public function replace($table, array $data = array(), $databaseName = null) |
|
| 1656 | |||
| 1657 | 4 | /** |
|
| 1658 | * Rollback in a transaction and end the transaction. |
||
| 1659 | 4 | * |
|
| 1660 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1661 | */ |
||
| 1662 | View Code Duplication | public function rollback() |
|
| 1676 | |||
| 1677 | /** |
||
| 1678 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 1679 | * |
||
| 1680 | * <p> |
||
| 1681 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 1682 | * 1. parse into "int" |
||
| 1683 | * </p><br /> |
||
| 1684 | * |
||
| 1685 | * <p> |
||
| 1686 | * <strong>float:</strong><br /> |
||
| 1687 | * 1. return "float" |
||
| 1688 | * </p><br /> |
||
| 1689 | * |
||
| 1690 | * <p> |
||
| 1691 | * <strong>string:</strong><br /> |
||
| 1692 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 1693 | * 2. trim whitespace<br /> |
||
| 1694 | * 3. trim '<br /> |
||
| 1695 | * 4. escape the string (and remove non utf-8 chars)<br /> |
||
| 1696 | * 5. trim ' again (because we maybe removed some chars)<br /> |
||
| 1697 | * 6. add ' around the new string<br /> |
||
| 1698 | * </p><br /> |
||
| 1699 | * |
||
| 1700 | * <p> |
||
| 1701 | * <strong>array:</strong><br /> |
||
| 1702 | * 1. return null |
||
| 1703 | * </p><br /> |
||
| 1704 | * |
||
| 1705 | * <p> |
||
| 1706 | * <strong>object:</strong><br /> |
||
| 1707 | * 1. return false |
||
| 1708 | * </p><br /> |
||
| 1709 | * |
||
| 1710 | * <p> |
||
| 1711 | * <strong>null:</strong><br /> |
||
| 1712 | * 1. return null |
||
| 1713 | * </p> |
||
| 1714 | 44 | * |
|
| 1715 | * @param mixed $var |
||
| 1716 | * |
||
| 1717 | * @return mixed |
||
| 1718 | 44 | */ |
|
| 1719 | public function secure($var) |
||
| 1751 | |||
| 1752 | /** |
||
| 1753 | * Execute a "select"-query. |
||
| 1754 | * |
||
| 1755 | * @param string $table |
||
| 1756 | * @param string|array $where |
||
| 1757 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1758 | * |
||
| 1759 | * @return false|Result <p>false on error</p> |
||
| 1760 | * |
||
| 1761 | * @throws QueryException |
||
| 1762 | */ |
||
| 1763 | View Code Duplication | public function select($table, $where = '1=1', $databaseName = null) |
|
| 1790 | |||
| 1791 | /** |
||
| 1792 | * Selects a different database than the one specified on construction. |
||
| 1793 | * |
||
| 1794 | * @param string $database <p>Database name to switch to.</p> |
||
| 1795 | * |
||
| 1796 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1797 | */ |
||
| 1798 | public function select_db($database) |
||
| 1806 | |||
| 1807 | /** |
||
| 1808 | * Set the current charset. |
||
| 1809 | * |
||
| 1810 | * @param string $charset |
||
| 1811 | * |
||
| 1812 | * @return bool |
||
| 1813 | */ |
||
| 1814 | public function set_charset($charset) |
||
| 1836 | |||
| 1837 | /** |
||
| 1838 | * Set the option to convert null to "''" (empty string). |
||
| 1839 | * |
||
| 1840 | * Used in secure() => select(), insert(), update(), delete() |
||
| 1841 | * |
||
| 1842 | * @deprecated It's not recommended to convert NULL into an empty string! |
||
| 1843 | * |
||
| 1844 | * @param $bool |
||
| 1845 | * |
||
| 1846 | * @return $this |
||
| 1847 | */ |
||
| 1848 | public function set_convert_null_to_empty_string($bool) |
||
| 1854 | |||
| 1855 | /** |
||
| 1856 | * Enables or disables internal report functions |
||
| 1857 | * |
||
| 1858 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 1859 | * |
||
| 1860 | * @param int $flags <p> |
||
| 1861 | * <table> |
||
| 1862 | * Supported flags |
||
| 1863 | * <tr valign="top"> |
||
| 1864 | * <td>Name</td> |
||
| 1865 | * <td>Description</td> |
||
| 1866 | * </tr> |
||
| 1867 | * <tr valign="top"> |
||
| 1868 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 1869 | * <td>Turns reporting off</td> |
||
| 1870 | * </tr> |
||
| 1871 | * <tr valign="top"> |
||
| 1872 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 1873 | * <td>Report errors from mysqli function calls</td> |
||
| 1874 | * </tr> |
||
| 1875 | * <tr valign="top"> |
||
| 1876 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 1877 | * <td> |
||
| 1878 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 1879 | * instead of warnings |
||
| 1880 | * </td> |
||
| 1881 | * </tr> |
||
| 1882 | * <tr valign="top"> |
||
| 1883 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 1884 | * <td>Report if no index or bad index was used in a query</td> |
||
| 1885 | * </tr> |
||
| 1886 | * <tr valign="top"> |
||
| 1887 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 1888 | * <td>Set all options (report all)</td> |
||
| 1889 | * </tr> |
||
| 1890 | * </table> |
||
| 1891 | * </p> |
||
| 1892 | * |
||
| 1893 | * @return bool |
||
| 1894 | */ |
||
| 1895 | public function set_mysqli_report($flags) |
||
| 1899 | |||
| 1900 | /** |
||
| 1901 | * Show config errors by throw exceptions. |
||
| 1902 | * |
||
| 1903 | * @return bool |
||
| 1904 | * |
||
| 1905 | * @throws \InvalidArgumentException |
||
| 1906 | */ |
||
| 1907 | public function showConfigError() |
||
| 1935 | |||
| 1936 | /** |
||
| 1937 | * alias: "beginTransaction()" |
||
| 1938 | */ |
||
| 1939 | public function startTransaction() |
||
| 1943 | 1 | ||
| 1944 | /** |
||
| 1945 | * Execute a callback inside a transaction. |
||
| 1946 | * |
||
| 1947 | * @param callback $callback <p>The callback to run inside the transaction, if it's throws an "Exception" or if it's |
||
| 1948 | * returns "false", all SQL-statements in the callback will be rollbacked.</p> |
||
| 1949 | * |
||
| 1950 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1951 | */ |
||
| 1952 | public function transact($callback) |
||
| 1978 | |||
| 1979 | /** |
||
| 1980 | * Execute a "update"-query. |
||
| 1981 | * |
||
| 1982 | * @param string $table |
||
| 1983 | * @param array $data |
||
| 1984 | * @param array|string $where |
||
| 1985 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1986 | * |
||
| 1987 | * @return false|int <p>false on error</p> |
||
| 1988 | * |
||
| 1989 | * @throws QueryException |
||
| 1990 | */ |
||
| 1991 | public function update($table, array $data = array(), $where = '1=1', $databaseName = null) |
||
| 2026 | |||
| 2027 | /** |
||
| 2028 | * Determine if database table exists |
||
| 2029 | * |
||
| 2030 | * @param string $table |
||
| 2031 | * |
||
| 2032 | * @return bool |
||
| 2033 | */ |
||
| 2034 | public function table_exists($table) |
||
| 2049 | |||
| 2050 | /** |
||
| 2051 | * Count number of rows found matching a specific query. |
||
| 2052 | * |
||
| 2053 | * @param string |
||
| 2054 | * |
||
| 2055 | * @return int |
||
| 2056 | */ |
||
| 2057 | public function num_rows($query) |
||
| 2071 | } |
||
| 2072 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.