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 | * __wakeup |
|
| 217 | * |
||
| 218 | 10 | * @return void |
|
| 219 | */ |
||
| 220 | 10 | public function __wakeup() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Load the config from the constructor. |
||
| 227 | * |
||
| 228 | * @param string $hostname |
||
| 229 | * @param string $username |
||
| 230 | 10 | * @param string $password |
|
| 231 | * @param string $database |
||
| 232 | * @param int|string $port <p>default is (int)3306</p> |
||
| 233 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 234 | 10 | * @param boolean|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 235 | 10 | * @param boolean|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 236 | 9 | * @param string $logger_class_name |
|
| 237 | 9 | * @param string $logger_level |
|
| 238 | 8 | * @param array $extra_config <p> |
|
| 239 | 10 | * 'session_to_db' => false|true<br> |
|
| 240 | * 'socket' => 'string (path)'<br> |
||
| 241 | 3 | * 'ssl' => 'bool'<br> |
|
| 242 | 1 | * 'clientkey' => 'string (path)'<br> |
|
| 243 | * 'clientcert' => 'string (path)'<br> |
||
| 244 | * 'cacert' => 'string (path)'<br> |
||
| 245 | 2 | * </p> |
|
| 246 | 1 | * |
|
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | 1 | private function _loadConfig($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 325 | * |
||
| 326 | 2 | * @param array $arrayPair |
|
| 327 | * @param string $glue <p>This is the separator.</p> |
||
| 328 | 2 | * |
|
| 329 | * @return string |
||
| 330 | * |
||
| 331 | * @internal |
||
| 332 | */ |
||
| 333 | public function _parseArrayPair($arrayPair, $glue = ',') |
||
| 479 | 33 | ||
| 480 | 28 | /** |
|
| 481 | 28 | * _parseQueryParams |
|
| 482 | 24 | * |
|
| 483 | * @param string $sql |
||
| 484 | * @param array $params |
||
| 485 | 33 | * |
|
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | 27 | private function _parseQueryParams($sql, array $params) |
|
| 509 | |||
| 510 | 8 | /** |
|
| 511 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 512 | * |
||
| 513 | * @return int |
||
| 514 | */ |
||
| 515 | public function affected_rows() |
||
| 519 | |||
| 520 | 8 | /** |
|
| 521 | * Begins a transaction, by turning off auto commit. |
||
| 522 | 8 | * |
|
| 523 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 524 | */ |
||
| 525 | public function beginTransaction() |
||
| 543 | |||
| 544 | 3 | /** |
|
| 545 | 3 | * Clear the errors in "_debug->_errors". |
|
| 546 | 3 | * |
|
| 547 | 3 | * @return bool |
|
| 548 | 3 | */ |
|
| 549 | 3 | public function clearErrors() |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Closes a previously opened database connection. |
||
| 556 | * |
||
| 557 | * @return bool |
||
| 558 | */ |
||
| 559 | public function close() |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Open a new connection to the MySQL server. |
||
| 577 | * |
||
| 578 | * @return bool |
||
| 579 | * |
||
| 580 | * @throws DBConnectException |
||
| 581 | */ |
||
| 582 | public function connect() |
||
| 657 | |||
| 658 | 33 | /** |
|
| 659 | * Execute a "delete"-query. |
||
| 660 | 33 | * |
|
| 661 | 33 | * @param string $table |
|
| 662 | * @param string|array $where |
||
| 663 | 33 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 664 | 33 | * |
|
| 665 | 33 | * @return false|int <p>false on error</p> |
|
| 666 | 33 | * |
|
| 667 | * * @throws QueryException |
||
| 668 | 33 | */ |
|
| 669 | 33 | View Code Duplication | public function delete($table, $where, $databaseName = null) |
| 696 | |||
| 697 | /** |
||
| 698 | 1 | * Ends a transaction and commits if no errors, then ends autocommit. |
|
| 699 | 1 | * |
|
| 700 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 701 | 1 | */ |
|
| 702 | 1 | public function endTransaction() |
|
| 716 | 3 | ||
| 717 | 3 | /** |
|
| 718 | 33 | * Get all errors from "$this->_errors". |
|
| 719 | * |
||
| 720 | * @return array|false <p>false === on errors</p> |
||
| 721 | */ |
||
| 722 | 33 | public function errors() |
|
| 728 | 33 | ||
| 729 | /** |
||
| 730 | 1 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
|
| 731 | 1 | * |
|
| 732 | * @param mixed $var boolean: convert into "integer"<br /> |
||
| 733 | 33 | * int: int (don't change it)<br /> |
|
| 734 | * float: float (don't change it)<br /> |
||
| 735 | 33 | * null: null (don't change it)<br /> |
|
| 736 | * array: run escape() for every key => value<br /> |
||
| 737 | 33 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
|
| 738 | * @param bool $stripe_non_utf8 |
||
| 739 | * @param bool $html_entity_decode |
||
| 740 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
||
| 741 | 3 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
|
| 742 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 743 | * |
||
| 744 | * @return mixed |
||
| 745 | */ |
||
| 746 | 3 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) |
|
| 860 | 3 | ||
| 861 | 3 | /** |
|
| 862 | 3 | * Execute select/insert/update/delete sql-queries. |
|
| 863 | * |
||
| 864 | * @param string $query sql-query |
||
| 865 | 3 | * @param bool $useCache use cache? |
|
| 866 | * @param int $cacheTTL cache-ttl in seconds |
||
| 867 | * |
||
| 868 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 869 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 870 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 871 | * "true" by e.g. "DROP"-queries<br /> |
||
| 872 | * "false" on error |
||
| 873 | * |
||
| 874 | * @throws QueryException |
||
| 875 | */ |
||
| 876 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600) |
||
| 923 | 1 | ||
| 924 | 1 | /** |
|
| 925 | 1 | * Get all table-names via "SHOW TABLES". |
|
| 926 | * |
||
| 927 | 1 | * @return array |
|
| 928 | 2 | */ |
|
| 929 | public function getAllTables() |
||
| 936 | |||
| 937 | /** |
||
| 938 | * @return Debug |
||
| 939 | 1 | */ |
|
| 940 | public function getDebugger() |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Get errors from "$this->_errors". |
||
| 947 | * |
||
| 948 | * @return array |
||
| 949 | */ |
||
| 950 | public function getErrors() |
||
| 954 | |||
| 955 | /** |
||
| 956 | * getInstance() |
||
| 957 | * |
||
| 958 | * @param string $hostname |
||
| 959 | * @param string $username |
||
| 960 | * @param string $password |
||
| 961 | * @param string $database |
||
| 962 | * @param int|string $port <p>default is (int)3306</p> |
||
| 963 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 964 | * @param bool|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 965 | * @param bool|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 966 | * @param string $logger_class_name |
||
| 967 | * @param string $logger_level |
||
| 968 | * @param array $extra_config <p> |
||
| 969 | * 'session_to_db' => false|true<br> |
||
| 970 | * 'socket' => 'string (path)'<br> |
||
| 971 | * 'ssl' => 'bool'<br> |
||
| 972 | * 'clientkey' => 'string (path)'<br> |
||
| 973 | * 'clientcert' => 'string (path)'<br> |
||
| 974 | * 'cacert' => 'string (path)'<br> |
||
| 975 | * </p> |
||
| 976 | * |
||
| 977 | * @return \voku\db\DB |
||
| 978 | */ |
||
| 979 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = '', $echo_on_error = '', $logger_class_name = '', $logger_level = '', $extra_config = array()) |
||
| 1035 | |||
| 1036 | 1 | /** |
|
| 1037 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1038 | 1 | * |
|
| 1039 | 1 | * @return \mysqli |
|
| 1040 | */ |
||
| 1041 | 1 | public function getLink() |
|
| 1045 | |||
| 1046 | /** |
||
| 1047 | * Get the current charset. |
||
| 1048 | * |
||
| 1049 | * @return string |
||
| 1050 | */ |
||
| 1051 | public function get_charset() |
||
| 1055 | |||
| 1056 | 1 | /** |
|
| 1057 | * Check if we are in a transaction. |
||
| 1058 | 1 | * |
|
| 1059 | * @return bool |
||
| 1060 | */ |
||
| 1061 | public function inTransaction() |
||
| 1065 | 1 | ||
| 1066 | /** |
||
| 1067 | * Execute a "insert"-query. |
||
| 1068 | 1 | * |
|
| 1069 | 1 | * @param string $table |
|
| 1070 | 1 | * @param array $data |
|
| 1071 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1072 | 1 | * |
|
| 1073 | * @return false|int <p>false on error</p> |
||
| 1074 | 1 | * |
|
| 1075 | 1 | * @throws QueryException |
|
| 1076 | 1 | */ |
|
| 1077 | public function insert($table, array $data = array(), $databaseName = null) |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * Returns the auto generated id used in the last query. |
||
| 1107 | * |
||
| 1108 | 1 | * @return int|string |
|
| 1109 | 1 | */ |
|
| 1110 | public function insert_id() |
||
| 1114 | |||
| 1115 | /** |
||
| 1116 | * Check if db-connection is ready. |
||
| 1117 | * |
||
| 1118 | * @return boolean |
||
| 1119 | */ |
||
| 1120 | public function isReady() |
||
| 1124 | 1 | ||
| 1125 | 1 | /** |
|
| 1126 | * Get the last sql-error. |
||
| 1127 | * |
||
| 1128 | * @return string|false <p>false === there was no error</p> |
||
| 1129 | */ |
||
| 1130 | public function lastError() |
||
| 1136 | 4 | ||
| 1137 | 1 | /** |
|
| 1138 | * Execute a sql-multi-query. |
||
| 1139 | 1 | * |
|
| 1140 | * @param string $sql |
||
| 1141 | * |
||
| 1142 | 4 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
|
| 1143 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
||
| 1144 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1145 | * "boolean" by only by e.g. "DROP"-queries<br /> |
||
| 1146 | * |
||
| 1147 | * @throws QueryException |
||
| 1148 | 4 | */ |
|
| 1149 | 4 | public function multi_query($sql) |
|
| 1213 | |||
| 1214 | 2 | /** |
|
| 1215 | * Pings a server connection, or tries to reconnect |
||
| 1216 | 2 | * if the connection has gone down. |
|
| 1217 | 2 | * |
|
| 1218 | 2 | * @return boolean |
|
| 1219 | 2 | */ |
|
| 1220 | 2 | public function ping() |
|
| 1234 | |||
| 1235 | /** |
||
| 1236 | 21 | * Selects a different database than the one specified on construction. |
|
| 1237 | * |
||
| 1238 | * @param string $database <p>Database name to switch to.</p> |
||
| 1239 | 21 | * |
|
| 1240 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1241 | 21 | */ |
|
| 1242 | 2 | public function select_db($database) |
|
| 1250 | 3 | ||
| 1251 | /** |
||
| 1252 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1253 | 18 | * |
|
| 1254 | * @param string $query |
||
| 1255 | 18 | * |
|
| 1256 | * @return Prepare |
||
| 1257 | */ |
||
| 1258 | public function prepare($query) |
||
| 1262 | |||
| 1263 | /** |
||
| 1264 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1265 | * |
||
| 1266 | * @param $query |
||
| 1267 | * |
||
| 1268 | * @return mixed |
||
| 1269 | * @deprecated |
||
| 1270 | * @throws \Exception |
||
| 1271 | */ |
||
| 1272 | public static function qry($query) |
||
| 1297 | |||
| 1298 | 23 | /** |
|
| 1299 | 1 | * Execute a sql-query. |
|
| 1300 | 1 | * |
|
| 1301 | * @param string $sql <p>The sql query-string.</p> |
||
| 1302 | 23 | * |
|
| 1303 | 1 | * @param array|boolean $params <p> |
|
| 1304 | 1 | * "array" of sql-query-parameters<br/> |
|
| 1305 | * "false" if you don't need any parameter (default)<br/> |
||
| 1306 | 23 | * </p> |
|
| 1307 | 1 | * |
|
| 1308 | 1 | * @return bool|int|Result <p> |
|
| 1309 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1310 | 23 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
|
| 1311 | 1 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 1312 | 1 | * "true" by e.g. "DROP"-queries<br /> |
|
| 1313 | * "false" on error |
||
| 1314 | 23 | * </p> |
|
| 1315 | 1 | * |
|
| 1316 | 1 | * @throws QueryException |
|
| 1317 | */ |
||
| 1318 | 23 | public function query($sql = '', $params = false) |
|
| 1391 | 23 | ||
| 1392 | /** |
||
| 1393 | 23 | * Error-handling for the sql-query. |
|
| 1394 | * |
||
| 1395 | 23 | * @param string $errorMessage |
|
| 1396 | * @param int $errorNumber |
||
| 1397 | 23 | * @param string $sql |
|
| 1398 | 1 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
|
| 1399 | 23 | * @param bool $sqlMultiQuery |
|
| 1400 | 23 | * |
|
| 1401 | 23 | * @throws QueryException |
|
| 1402 | * @throws DBGoneAwayException |
||
| 1403 | 23 | * |
|
| 1404 | 1 | * @return bool |
|
| 1405 | 1 | */ |
|
| 1406 | private function queryErrorHandling($errorMessage, $errorNumber, $sql, $sqlParams = false, $sqlMultiQuery = false) |
||
| 1446 | |||
| 1447 | 1 | /** |
|
| 1448 | * Quote && Escape e.g. a table name string. |
||
| 1449 | 1 | * |
|
| 1450 | * @param string $str |
||
| 1451 | * |
||
| 1452 | * @return string |
||
| 1453 | */ |
||
| 1454 | public function quote_string($str) |
||
| 1467 | |||
| 1468 | 1 | /** |
|
| 1469 | 1 | * Reconnect to the MySQL-Server. |
|
| 1470 | * |
||
| 1471 | 1 | * @param bool $checkViaPing |
|
| 1472 | * |
||
| 1473 | * @return bool |
||
| 1474 | 1 | */ |
|
| 1475 | 1 | public function reconnect($checkViaPing = false) |
|
| 1490 | 1 | ||
| 1491 | /** |
||
| 1492 | 1 | * Execute a "replace"-query. |
|
| 1493 | 1 | * |
|
| 1494 | 1 | * @param string $table |
|
| 1495 | * @param array $data |
||
| 1496 | 1 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 1497 | * |
||
| 1498 | * @return false|int <p>false on error</p> |
||
| 1499 | * |
||
| 1500 | 1 | * @throws QueryException |
|
| 1501 | */ |
||
| 1502 | 1 | public function replace($table, array $data = array(), $databaseName = null) |
|
| 1543 | |||
| 1544 | 6 | /** |
|
| 1545 | * Rollback in a transaction and end the transaction. |
||
| 1546 | * |
||
| 1547 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1548 | 6 | */ |
|
| 1549 | public function rollback() |
||
| 1561 | |||
| 1562 | /** |
||
| 1563 | * Commits the current transaction and end the transaction. |
||
| 1564 | 2 | * |
|
| 1565 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1566 | */ |
||
| 1567 | 2 | public function commit() |
|
| 1580 | 1 | ||
| 1581 | /** |
||
| 1582 | * Execute a callback inside a transaction. |
||
| 1583 | 2 | * |
|
| 1584 | * @param callback $callback The callback to run inside the transaction |
||
| 1585 | * |
||
| 1586 | * @return bool Boolean true on success, false otherwise |
||
| 1587 | 2 | */ |
|
| 1588 | public function transact($callback) |
||
| 1599 | |||
| 1600 | /** |
||
| 1601 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 1602 | * |
||
| 1603 | 20 | * <p> |
|
| 1604 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 1605 | * 1. parse into "int" |
||
| 1606 | 20 | * </p><br /> |
|
| 1607 | * |
||
| 1608 | 20 | * <p> |
|
| 1609 | 1 | * <strong>float:</strong><br /> |
|
| 1610 | * 1. return "float" |
||
| 1611 | 1 | * </p><br /> |
|
| 1612 | * |
||
| 1613 | * <p> |
||
| 1614 | 20 | * <strong>string:</strong><br /> |
|
| 1615 | 5 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
|
| 1616 | 20 | * 2. trim whitespace<br /> |
|
| 1617 | 16 | * 3. trim '<br /> |
|
| 1618 | 16 | * 4. escape the string (and remove non utf-8 chars)<br /> |
|
| 1619 | 1 | * 5. trim ' again (because we maybe removed some chars)<br /> |
|
| 1620 | * 6. add ' around the new string<br /> |
||
| 1621 | * </p><br /> |
||
| 1622 | 20 | * |
|
| 1623 | * <p> |
||
| 1624 | * <strong>array:</strong><br /> |
||
| 1625 | * 1. return null |
||
| 1626 | 20 | * </p><br /> |
|
| 1627 | * |
||
| 1628 | 20 | * <p> |
|
| 1629 | * <strong>object:</strong><br /> |
||
| 1630 | * 1. return false |
||
| 1631 | * </p><br /> |
||
| 1632 | * |
||
| 1633 | * <p> |
||
| 1634 | * <strong>null:</strong><br /> |
||
| 1635 | * 1. return null |
||
| 1636 | 1 | * </p> |
|
| 1637 | * |
||
| 1638 | 1 | * @param mixed $var |
|
| 1639 | * |
||
| 1640 | 1 | * @return mixed |
|
| 1641 | */ |
||
| 1642 | public function secure($var) |
||
| 1668 | 2 | ||
| 1669 | /** |
||
| 1670 | 2 | * Execute a "select"-query. |
|
| 1671 | 2 | * |
|
| 1672 | 2 | * @param string $table |
|
| 1673 | 2 | * @param string|array $where |
|
| 1674 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1675 | * |
||
| 1676 | * @return false|Result <p>false on error</p> |
||
| 1677 | * |
||
| 1678 | * @throws QueryException |
||
| 1679 | */ |
||
| 1680 | View Code Duplication | public function select($table, $where = '1=1', $databaseName = null) |
|
| 1707 | |||
| 1708 | /** |
||
| 1709 | * Set the current charset. |
||
| 1710 | * |
||
| 1711 | * @param string $charset |
||
| 1712 | * |
||
| 1713 | * @return bool |
||
| 1714 | */ |
||
| 1715 | public function set_charset($charset) |
||
| 1737 | |||
| 1738 | /** |
||
| 1739 | * Set the option to convert null to "''" (empty string). |
||
| 1740 | * |
||
| 1741 | * Used in secure() => select(), insert(), update(), delete() |
||
| 1742 | * |
||
| 1743 | * @param $bool |
||
| 1744 | */ |
||
| 1745 | public function set_convert_null_to_empty_string($bool) |
||
| 1749 | |||
| 1750 | /** |
||
| 1751 | * Enables or disables internal report functions |
||
| 1752 | * |
||
| 1753 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 1754 | * |
||
| 1755 | * @param int $flags <p> |
||
| 1756 | * <table> |
||
| 1757 | * Supported flags |
||
| 1758 | * <tr valign="top"> |
||
| 1759 | * <td>Name</td> |
||
| 1760 | * <td>Description</td> |
||
| 1761 | * </tr> |
||
| 1762 | * <tr valign="top"> |
||
| 1763 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 1764 | * <td>Turns reporting off</td> |
||
| 1765 | * </tr> |
||
| 1766 | * <tr valign="top"> |
||
| 1767 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 1768 | * <td>Report errors from mysqli function calls</td> |
||
| 1769 | * </tr> |
||
| 1770 | * <tr valign="top"> |
||
| 1771 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 1772 | * <td> |
||
| 1773 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 1774 | * instead of warnings |
||
| 1775 | * </td> |
||
| 1776 | * </tr> |
||
| 1777 | * <tr valign="top"> |
||
| 1778 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 1779 | * <td>Report if no index or bad index was used in a query</td> |
||
| 1780 | * </tr> |
||
| 1781 | * <tr valign="top"> |
||
| 1782 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 1783 | * <td>Set all options (report all)</td> |
||
| 1784 | * </tr> |
||
| 1785 | * </table> |
||
| 1786 | * </p> |
||
| 1787 | * |
||
| 1788 | * @return bool |
||
| 1789 | */ |
||
| 1790 | public function set_mysqli_report($flags) |
||
| 1794 | |||
| 1795 | /** |
||
| 1796 | * Show config errors by throw exceptions. |
||
| 1797 | * |
||
| 1798 | * @return bool |
||
| 1799 | * |
||
| 1800 | * @throws \InvalidArgumentException |
||
| 1801 | */ |
||
| 1802 | public function showConfigError() |
||
| 1830 | |||
| 1831 | /** |
||
| 1832 | * alias: "beginTransaction()" |
||
| 1833 | */ |
||
| 1834 | public function startTransaction() |
||
| 1838 | |||
| 1839 | /** |
||
| 1840 | * Execute a "update"-query. |
||
| 1841 | * |
||
| 1842 | * @param string $table |
||
| 1843 | * @param array $data |
||
| 1844 | * @param array|string $where |
||
| 1845 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1846 | * |
||
| 1847 | * @return false|int <p>false on error</p> |
||
| 1848 | * |
||
| 1849 | * @throws QueryException |
||
| 1850 | */ |
||
| 1851 | public function update($table, array $data = array(), $where = '1=1', $databaseName = null) |
||
| 1886 | |||
| 1887 | } |
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.