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 = false; |
||
| 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 | 10 | * The path name to the certificate authority file |
|
| 110 | * |
||
| 111 | 10 | * @var string |
|
| 112 | */ |
||
| 113 | 10 | private $_cacert; |
|
| 114 | |||
| 115 | 10 | /** |
|
| 116 | 10 | * @var Debug |
|
| 117 | 10 | */ |
|
| 118 | 10 | private $_debug; |
|
| 119 | 10 | ||
| 120 | 10 | /** |
|
| 121 | 10 | * __construct() |
|
| 122 | 10 | * |
|
| 123 | 10 | * @param string $hostname |
|
| 124 | 10 | * @param string $username |
|
| 125 | 10 | * @param string $password |
|
| 126 | * @param string $database |
||
| 127 | 10 | * @param int $port |
|
| 128 | * @param string $charset |
||
| 129 | 7 | * @param boolean|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 130 | * @param boolean|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 131 | 4 | * @param string $logger_class_name |
|
| 132 | * @param string $logger_level <p>'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'</p> |
||
| 133 | 4 | * @param array $extra_config <p> |
|
| 134 | * 'session_to_db' => false|true<br> |
||
| 135 | 4 | * 'socket' => 'string (path)'<br> |
|
| 136 | * 'ssl' => 'bool'<br> |
||
| 137 | 4 | * 'clientkey' => 'string (path)'<br> |
|
| 138 | * 'clientcert' => 'string (path)'<br> |
||
| 139 | 4 | * 'cacert' => 'string (path)'<br> |
|
| 140 | * </p> |
||
| 141 | 4 | */ |
|
| 142 | protected function __construct($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config = array()) |
||
| 193 | |||
| 194 | 7 | /** |
|
| 195 | * Prevent the instance from being cloned. |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | 10 | */ |
|
| 199 | private function __clone() |
||
| 202 | 10 | ||
| 203 | /** |
||
| 204 | 10 | * __destruct |
|
| 205 | 10 | * |
|
| 206 | */ |
||
| 207 | 10 | public function __destruct() |
|
| 214 | |||
| 215 | 10 | /** |
|
| 216 | 10 | * @param null|string $sql |
|
| 217 | * @param array $bindings |
||
| 218 | 10 | * |
|
| 219 | * @return bool|int|Result|DB <p> |
||
| 220 | 10 | * "DB" by "$sql" === null<br /> |
|
| 221 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 222 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 223 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 224 | * "true" by e.g. "DROP"-queries<br /> |
||
| 225 | * "false" on error |
||
| 226 | * </p> |
||
| 227 | */ |
||
| 228 | public function __invoke($sql = null, array $bindings = array()) |
||
| 232 | |||
| 233 | /** |
||
| 234 | 10 | * __wakeup |
|
| 235 | 10 | * |
|
| 236 | 9 | * @return void |
|
| 237 | 9 | */ |
|
| 238 | 8 | public function __wakeup() |
|
| 242 | 1 | ||
| 243 | /** |
||
| 244 | * Load the config from the constructor. |
||
| 245 | 2 | * |
|
| 246 | 1 | * @param string $hostname |
|
| 247 | * @param string $username |
||
| 248 | * @param string $password |
||
| 249 | 1 | * @param string $database |
|
| 250 | 1 | * @param int|string $port <p>default is (int)3306</p> |
|
| 251 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 252 | * @param boolean|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 253 | * @param boolean|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 254 | * @param string $logger_class_name |
||
| 255 | * @param string $logger_level |
||
| 256 | 7 | * @param array $extra_config <p> |
|
| 257 | * 'session_to_db' => false|true<br> |
||
| 258 | * 'socket' => 'string (path)'<br> |
||
| 259 | * 'ssl' => 'bool'<br> |
||
| 260 | * 'clientkey' => 'string (path)'<br> |
||
| 261 | * 'clientcert' => 'string (path)'<br> |
||
| 262 | * 'cacert' => 'string (path)'<br> |
||
| 263 | * </p> |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | 9 | */ |
|
| 267 | private function _loadConfig($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 343 | * |
||
| 344 | * @param array $arrayPair |
||
| 345 | * @param string $glue <p>This is the separator.</p> |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | * |
||
| 349 | * @internal |
||
| 350 | */ |
||
| 351 | public function _parseArrayPair($arrayPair, $glue = ',') |
||
| 497 | |||
| 498 | 23 | /** |
|
| 499 | 22 | * _parseQueryParams |
|
| 500 | 22 | * |
|
| 501 | * @param string $sql |
||
| 502 | 22 | * @param array $params |
|
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | 8 | private function _parseQueryParams($sql, array $params) |
|
| 531 | |||
| 532 | /** |
||
| 533 | 3 | * Gets the number of affected rows in a previous MySQL operation. |
|
| 534 | * |
||
| 535 | * @return int |
||
| 536 | 3 | */ |
|
| 537 | public function affected_rows() |
||
| 541 | 3 | ||
| 542 | 3 | /** |
|
| 543 | * Begins a transaction, by turning off auto commit. |
||
| 544 | 3 | * |
|
| 545 | 3 | * @return bool <p>This will return true or false indicating success of transaction</p> |
|
| 546 | 3 | */ |
|
| 547 | 3 | View Code Duplication | public function beginTransaction() |
| 564 | |||
| 565 | /** |
||
| 566 | * Clear the errors in "_debug->_errors". |
||
| 567 | * |
||
| 568 | * @return bool |
||
| 569 | */ |
||
| 570 | public function clearErrors() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Closes a previously opened database connection. |
||
| 577 | */ |
||
| 578 | public function close() |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Commits the current transaction and end the transaction. |
||
| 589 | * |
||
| 590 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 591 | */ |
||
| 592 | View Code Duplication | public function commit() |
|
| 606 | |||
| 607 | 26 | /** |
|
| 608 | 1 | * Open a new connection to the MySQL server. |
|
| 609 | * |
||
| 610 | * @return bool |
||
| 611 | 26 | * |
|
| 612 | 22 | * @throws DBConnectException |
|
| 613 | 22 | */ |
|
| 614 | public function connect() |
||
| 689 | |||
| 690 | /** |
||
| 691 | 1 | * Execute a "delete"-query. |
|
| 692 | 1 | * |
|
| 693 | * @param string $table |
||
| 694 | 1 | * @param string|array $where |
|
| 695 | 1 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 696 | * |
||
| 697 | * @return false|int <p>false on error</p> |
||
| 698 | 1 | * |
|
| 699 | 1 | * * @throws QueryException |
|
| 700 | */ |
||
| 701 | 1 | View Code Duplication | public function delete($table, $where, $databaseName = null) |
| 728 | 33 | ||
| 729 | /** |
||
| 730 | 1 | * Ends a transaction and commits if no errors, then ends autocommit. |
|
| 731 | 1 | * |
|
| 732 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 733 | 33 | */ |
|
| 734 | View Code Duplication | public function endTransaction() |
|
| 754 | |||
| 755 | /** |
||
| 756 | * Get all errors from "$this->_errors". |
||
| 757 | * |
||
| 758 | * @return array|false <p>false === on errors</p> |
||
| 759 | */ |
||
| 760 | public function errors() |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Returns the SQL by replacing :placeholders with SQL-escaped values. |
||
| 769 | * |
||
| 770 | * @param mixed $sql <p>The SQL string.</p> |
||
| 771 | 22 | * @param array $bindings <p>An array of key-value bindings.</p> |
|
| 772 | * |
||
| 773 | 22 | * @return string |
|
| 774 | */ |
||
| 775 | public function _parseQueryParamsByName($sql, array $bindings = array()) |
||
| 797 | |||
| 798 | 9 | /** |
|
| 799 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
||
| 800 | 9 | * |
|
| 801 | 1 | * @param mixed $var boolean: convert into "integer"<br /> |
|
| 802 | * int: int (don't change it)<br /> |
||
| 803 | * float: float (don't change it)<br /> |
||
| 804 | 1 | * null: null (don't change it)<br /> |
|
| 805 | * array: run escape() for every key => value<br /> |
||
| 806 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 807 | * @param bool $stripe_non_utf8 |
||
| 808 | * @param bool $html_entity_decode |
||
| 809 | 1 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
|
| 810 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 811 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 812 | 1 | * |
|
| 813 | 1 | * @return mixed |
|
| 814 | */ |
||
| 815 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) |
||
| 929 | |||
| 930 | /** |
||
| 931 | 3 | * Execute select/insert/update/delete sql-queries. |
|
| 932 | * |
||
| 933 | * @param string $query <p>sql-query</p> |
||
| 934 | * @param bool $useCache <p>use cache?</p> |
||
| 935 | * @param int $cacheTTL <p>cache-ttl in seconds</p> |
||
| 936 | * @param DB $db optional <p>the database connection</p> |
||
| 937 | * |
||
| 938 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 939 | 1 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
|
| 940 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 941 | 1 | * "true" by e.g. "DROP"-queries<br /> |
|
| 942 | * "false" on error |
||
| 943 | * |
||
| 944 | * @throws QueryException |
||
| 945 | */ |
||
| 946 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600, DB $db = null) |
||
| 995 | |||
| 996 | 7 | /** |
|
| 997 | * Get all table-names via "SHOW TABLES". |
||
| 998 | 7 | * |
|
| 999 | 7 | * @return array |
|
| 1000 | 5 | */ |
|
| 1001 | 5 | public function getAllTables() |
|
| 1008 | 7 | ||
| 1009 | /** |
||
| 1010 | * @return Debug |
||
| 1011 | 7 | */ |
|
| 1012 | public function getDebugger() |
||
| 1016 | 7 | ||
| 1017 | /** |
||
| 1018 | * Get errors from "$this->_errors". |
||
| 1019 | * |
||
| 1020 | * @return array |
||
| 1021 | */ |
||
| 1022 | public function getErrors() |
||
| 1026 | 1 | ||
| 1027 | /** |
||
| 1028 | 1 | * getInstance() |
|
| 1029 | 1 | * |
|
| 1030 | * @param string $hostname |
||
| 1031 | * @param string $username |
||
| 1032 | * @param string $password |
||
| 1033 | * @param string $database |
||
| 1034 | * @param int|string $port <p>default is (int)3306</p> |
||
| 1035 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 1036 | 1 | * @param bool|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 1037 | * @param bool|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 1038 | 1 | * @param string $logger_class_name |
|
| 1039 | 1 | * @param string $logger_level |
|
| 1040 | * @param array $extra_config <p> |
||
| 1041 | 1 | * 'session_to_db' => false|true<br> |
|
| 1042 | * 'socket' => 'string (path)'<br> |
||
| 1043 | * 'ssl' => 'bool'<br> |
||
| 1044 | * 'clientkey' => 'string (path)'<br> |
||
| 1045 | * 'clientcert' => 'string (path)'<br> |
||
| 1046 | * 'cacert' => 'string (path)'<br> |
||
| 1047 | * </p> |
||
| 1048 | * |
||
| 1049 | * @return \voku\db\DB |
||
| 1050 | */ |
||
| 1051 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = '', $echo_on_error = '', $logger_class_name = '', $logger_level = '', $extra_config = array()) |
||
| 1107 | |||
| 1108 | 1 | /** |
|
| 1109 | 1 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
|
| 1110 | * |
||
| 1111 | * @return \mysqli |
||
| 1112 | 1 | */ |
|
| 1113 | 1 | public function getLink() |
|
| 1117 | |||
| 1118 | /** |
||
| 1119 | * Get the current charset. |
||
| 1120 | * |
||
| 1121 | * @return string |
||
| 1122 | 1 | */ |
|
| 1123 | public function get_charset() |
||
| 1127 | |||
| 1128 | /** |
||
| 1129 | * Check if we are in a transaction. |
||
| 1130 | * |
||
| 1131 | * @return bool |
||
| 1132 | 4 | */ |
|
| 1133 | public function inTransaction() |
||
| 1137 | 1 | ||
| 1138 | /** |
||
| 1139 | 1 | * Execute a "insert"-query. |
|
| 1140 | * |
||
| 1141 | * @param string $table |
||
| 1142 | 4 | * @param array $data |
|
| 1143 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1144 | * |
||
| 1145 | * @return false|int <p>false on error</p> |
||
| 1146 | * |
||
| 1147 | * @throws QueryException |
||
| 1148 | 4 | */ |
|
| 1149 | 4 | public function insert($table, array $data = array(), $databaseName = null) |
|
| 1176 | |||
| 1177 | /** |
||
| 1178 | * Returns the auto generated id used in the last query. |
||
| 1179 | 2 | * |
|
| 1180 | * @return int|string |
||
| 1181 | */ |
||
| 1182 | 2 | public function insert_id() |
|
| 1186 | 1 | ||
| 1187 | 1 | /** |
|
| 1188 | * Check if db-connection is ready. |
||
| 1189 | * |
||
| 1190 | 2 | * @return boolean |
|
| 1191 | 2 | */ |
|
| 1192 | public function isReady() |
||
| 1196 | |||
| 1197 | /** |
||
| 1198 | * Get the last sql-error. |
||
| 1199 | * |
||
| 1200 | * @return string|false <p>false === there was no error</p> |
||
| 1201 | 2 | */ |
|
| 1202 | public function lastError() |
||
| 1208 | |||
| 1209 | /** |
||
| 1210 | * Execute a sql-multi-query. |
||
| 1211 | 2 | * |
|
| 1212 | * @param string $sql |
||
| 1213 | * |
||
| 1214 | 2 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
|
| 1215 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
||
| 1216 | 2 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 1217 | 2 | * "boolean" by only by e.g. "DROP"-queries<br /> |
|
| 1218 | 2 | * |
|
| 1219 | 2 | * @throws QueryException |
|
| 1220 | 2 | */ |
|
| 1221 | public function multi_query($sql) |
||
| 1288 | 23 | ||
| 1289 | /** |
||
| 1290 | 23 | * Pings a server connection, or tries to reconnect |
|
| 1291 | 2 | * if the connection has gone down. |
|
| 1292 | 2 | * |
|
| 1293 | * @return boolean |
||
| 1294 | 23 | */ |
|
| 1295 | 1 | public function ping() |
|
| 1309 | |||
| 1310 | 23 | /** |
|
| 1311 | 1 | * Get a new "Prepare"-Object for your sql-query. |
|
| 1312 | 1 | * |
|
| 1313 | * @param string $query |
||
| 1314 | 23 | * |
|
| 1315 | 1 | * @return Prepare |
|
| 1316 | 1 | */ |
|
| 1317 | public function prepare($query) |
||
| 1321 | |||
| 1322 | 23 | /** |
|
| 1323 | 2 | * Execute a sql-query and return the result-array for select-statements. |
|
| 1324 | 2 | * |
|
| 1325 | * @param string $query |
||
| 1326 | 23 | * |
|
| 1327 | 4 | * @return mixed |
|
| 1328 | 4 | * @deprecated |
|
| 1329 | * @throws \Exception |
||
| 1330 | 23 | */ |
|
| 1331 | 1 | public static function qry($query) |
|
| 1356 | 2 | ||
| 1357 | 2 | /** |
|
| 1358 | * Execute a sql-query. |
||
| 1359 | 2 | * |
|
| 1360 | 1 | * @param string $sql <p>The sql query-string.</p> |
|
| 1361 | 2 | * |
|
| 1362 | 1 | * @param array|boolean $params <p> |
|
| 1363 | 1 | * "array" of sql-query-parameters<br/> |
|
| 1364 | * "false" if you don't need any parameter (default)<br/> |
||
| 1365 | 2 | * </p> |
|
| 1366 | 23 | * |
|
| 1367 | * @return bool|int|Result <p> |
||
| 1368 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1369 | 23 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
|
| 1370 | 23 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 1371 | 23 | * "true" by e.g. "DROP"-queries<br /> |
|
| 1372 | * "false" on error |
||
| 1373 | 23 | * </p> |
|
| 1374 | 23 | * |
|
| 1375 | 23 | * @throws QueryException |
|
| 1376 | 23 | */ |
|
| 1377 | public function query($sql = '', $params = false) |
||
| 1451 | |||
| 1452 | /** |
||
| 1453 | * Error-handling for the sql-query. |
||
| 1454 | * |
||
| 1455 | * @param string $errorMessage |
||
| 1456 | * @param int $errorNumber |
||
| 1457 | * @param string $sql |
||
| 1458 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1459 | * @param bool $sqlMultiQuery |
||
| 1460 | * |
||
| 1461 | * @throws QueryException |
||
| 1462 | * @throws DBGoneAwayException |
||
| 1463 | 1 | * |
|
| 1464 | * @return bool |
||
| 1465 | */ |
||
| 1466 | 1 | private function queryErrorHandling($errorMessage, $errorNumber, $sql, $sqlParams = false, $sqlMultiQuery = false) |
|
| 1506 | |||
| 1507 | /** |
||
| 1508 | * Quote && Escape e.g. a table name string. |
||
| 1509 | * |
||
| 1510 | * @param string $str |
||
| 1511 | * |
||
| 1512 | * @return string |
||
| 1513 | */ |
||
| 1514 | public function quote_string($str) |
||
| 1527 | |||
| 1528 | 6 | /** |
|
| 1529 | 2 | * Reconnect to the MySQL-Server. |
|
| 1530 | * |
||
| 1531 | 2 | * @param bool $checkViaPing |
|
| 1532 | * |
||
| 1533 | * @return bool |
||
| 1534 | 6 | */ |
|
| 1535 | public function reconnect($checkViaPing = false) |
||
| 1550 | 6 | ||
| 1551 | /** |
||
| 1552 | * Execute a "replace"-query. |
||
| 1553 | * |
||
| 1554 | * @param string $table |
||
| 1555 | * @param array $data |
||
| 1556 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1557 | * |
||
| 1558 | * @return false|int <p>false on error</p> |
||
| 1559 | * |
||
| 1560 | * @throws QueryException |
||
| 1561 | */ |
||
| 1562 | public function replace($table, array $data = array(), $databaseName = null) |
||
| 1603 | 20 | ||
| 1604 | /** |
||
| 1605 | * Rollback in a transaction and end the transaction. |
||
| 1606 | 20 | * |
|
| 1607 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1608 | 20 | */ |
|
| 1609 | 1 | View Code Duplication | public function rollback() |
| 1623 | |||
| 1624 | /** |
||
| 1625 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 1626 | 20 | * |
|
| 1627 | * <p> |
||
| 1628 | 20 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
|
| 1629 | * 1. parse into "int" |
||
| 1630 | * </p><br /> |
||
| 1631 | * |
||
| 1632 | * <p> |
||
| 1633 | * <strong>float:</strong><br /> |
||
| 1634 | * 1. return "float" |
||
| 1635 | * </p><br /> |
||
| 1636 | 1 | * |
|
| 1637 | * <p> |
||
| 1638 | 1 | * <strong>string:</strong><br /> |
|
| 1639 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 1640 | 1 | * 2. trim whitespace<br /> |
|
| 1641 | * 3. trim '<br /> |
||
| 1642 | * 4. escape the string (and remove non utf-8 chars)<br /> |
||
| 1643 | * 5. trim ' again (because we maybe removed some chars)<br /> |
||
| 1644 | * 6. add ' around the new string<br /> |
||
| 1645 | * </p><br /> |
||
| 1646 | 9 | * |
|
| 1647 | * <p> |
||
| 1648 | 9 | * <strong>array:</strong><br /> |
|
| 1649 | * 1. return null |
||
| 1650 | * </p><br /> |
||
| 1651 | * |
||
| 1652 | * <p> |
||
| 1653 | * <strong>object:</strong><br /> |
||
| 1654 | * 1. return false |
||
| 1655 | * </p><br /> |
||
| 1656 | * |
||
| 1657 | * <p> |
||
| 1658 | * <strong>null:</strong><br /> |
||
| 1659 | * 1. return null |
||
| 1660 | * </p> |
||
| 1661 | * |
||
| 1662 | * @param mixed $var |
||
| 1663 | * |
||
| 1664 | * @return mixed |
||
| 1665 | */ |
||
| 1666 | 2 | public function secure($var) |
|
| 1692 | 2 | ||
| 1693 | /** |
||
| 1694 | * Execute a "select"-query. |
||
| 1695 | * |
||
| 1696 | * @param string $table |
||
| 1697 | * @param string|array $where |
||
| 1698 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1699 | * |
||
| 1700 | * @return false|Result <p>false on error</p> |
||
| 1701 | * |
||
| 1702 | * @throws QueryException |
||
| 1703 | */ |
||
| 1704 | View Code Duplication | public function select($table, $where = '1=1', $databaseName = null) |
|
| 1731 | |||
| 1732 | /** |
||
| 1733 | * Selects a different database than the one specified on construction. |
||
| 1734 | * |
||
| 1735 | * @param string $database <p>Database name to switch to.</p> |
||
| 1736 | * |
||
| 1737 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1738 | */ |
||
| 1739 | public function select_db($database) |
||
| 1747 | |||
| 1748 | /** |
||
| 1749 | * Set the current charset. |
||
| 1750 | * |
||
| 1751 | * @param string $charset |
||
| 1752 | * |
||
| 1753 | * @return bool |
||
| 1754 | */ |
||
| 1755 | public function set_charset($charset) |
||
| 1777 | |||
| 1778 | /** |
||
| 1779 | * Set the option to convert null to "''" (empty string). |
||
| 1780 | * |
||
| 1781 | * Used in secure() => select(), insert(), update(), delete() |
||
| 1782 | * |
||
| 1783 | * @param $bool |
||
| 1784 | */ |
||
| 1785 | public function set_convert_null_to_empty_string($bool) |
||
| 1789 | |||
| 1790 | /** |
||
| 1791 | * Enables or disables internal report functions |
||
| 1792 | * |
||
| 1793 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 1794 | * |
||
| 1795 | * @param int $flags <p> |
||
| 1796 | * <table> |
||
| 1797 | * Supported flags |
||
| 1798 | * <tr valign="top"> |
||
| 1799 | * <td>Name</td> |
||
| 1800 | * <td>Description</td> |
||
| 1801 | * </tr> |
||
| 1802 | * <tr valign="top"> |
||
| 1803 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 1804 | * <td>Turns reporting off</td> |
||
| 1805 | * </tr> |
||
| 1806 | * <tr valign="top"> |
||
| 1807 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 1808 | * <td>Report errors from mysqli function calls</td> |
||
| 1809 | * </tr> |
||
| 1810 | * <tr valign="top"> |
||
| 1811 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 1812 | * <td> |
||
| 1813 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 1814 | * instead of warnings |
||
| 1815 | * </td> |
||
| 1816 | * </tr> |
||
| 1817 | * <tr valign="top"> |
||
| 1818 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 1819 | * <td>Report if no index or bad index was used in a query</td> |
||
| 1820 | * </tr> |
||
| 1821 | * <tr valign="top"> |
||
| 1822 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 1823 | * <td>Set all options (report all)</td> |
||
| 1824 | * </tr> |
||
| 1825 | * </table> |
||
| 1826 | * </p> |
||
| 1827 | * |
||
| 1828 | * @return bool |
||
| 1829 | */ |
||
| 1830 | public function set_mysqli_report($flags) |
||
| 1834 | |||
| 1835 | /** |
||
| 1836 | * Show config errors by throw exceptions. |
||
| 1837 | * |
||
| 1838 | * @return bool |
||
| 1839 | * |
||
| 1840 | * @throws \InvalidArgumentException |
||
| 1841 | */ |
||
| 1842 | public function showConfigError() |
||
| 1870 | |||
| 1871 | /** |
||
| 1872 | * alias: "beginTransaction()" |
||
| 1873 | */ |
||
| 1874 | public function startTransaction() |
||
| 1878 | |||
| 1879 | /** |
||
| 1880 | * Execute a callback inside a transaction. |
||
| 1881 | * |
||
| 1882 | * @param callback $callback The callback to run inside the transaction |
||
| 1883 | * |
||
| 1884 | * @return bool Boolean true on success, false otherwise |
||
| 1885 | */ |
||
| 1886 | public function transact($callback) |
||
| 1899 | |||
| 1900 | /** |
||
| 1901 | * Execute a "update"-query. |
||
| 1902 | * |
||
| 1903 | * @param string $table |
||
| 1904 | * @param array $data |
||
| 1905 | * @param array|string $where |
||
| 1906 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1907 | * |
||
| 1908 | * @return false|int <p>false on error</p> |
||
| 1909 | * |
||
| 1910 | * @throws QueryException |
||
| 1911 | */ |
||
| 1912 | public function update($table, array $data = array(), $where = '1=1', $databaseName = null) |
||
| 1947 | |||
| 1948 | } |
||
| 1949 |
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.