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) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 530 | * |
||
| 531 | * @return int |
||
| 532 | */ |
||
| 533 | 3 | public function affected_rows() |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Begins a transaction, by turning off auto commit. |
||
| 540 | 3 | * |
|
| 541 | 3 | * @return bool <p>This will return true or false indicating success of transaction</p> |
|
| 542 | 3 | */ |
|
| 543 | View Code Duplication | public function beginTransaction() |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Clear the errors in "_debug->_errors". |
||
| 563 | * |
||
| 564 | * @return bool |
||
| 565 | */ |
||
| 566 | public function clearErrors() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Closes a previously opened database connection. |
||
| 573 | */ |
||
| 574 | public function close() |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Commits the current transaction and end the transaction. |
||
| 585 | * |
||
| 586 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 587 | */ |
||
| 588 | View Code Duplication | public function commit() |
|
| 602 | 26 | ||
| 603 | 26 | /** |
|
| 604 | 1 | * Open a new connection to the MySQL server. |
|
| 605 | * |
||
| 606 | * @return bool |
||
| 607 | 26 | * |
|
| 608 | 1 | * @throws DBConnectException |
|
| 609 | */ |
||
| 610 | public function connect() |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Execute a "delete"-query. |
||
| 687 | 3 | * |
|
| 688 | 3 | * @param string $table |
|
| 689 | * @param string|array $where |
||
| 690 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 691 | 1 | * |
|
| 692 | 1 | * @return false|int <p>false on error</p> |
|
| 693 | * |
||
| 694 | 1 | * * @throws QueryException |
|
| 695 | 1 | */ |
|
| 696 | View Code Duplication | public function delete($table, $where, $databaseName = null) |
|
| 723 | |||
| 724 | 33 | /** |
|
| 725 | 9 | * Ends a transaction and commits if no errors, then ends autocommit. |
|
| 726 | 9 | * |
|
| 727 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
||
| 728 | 33 | */ |
|
| 729 | View Code Duplication | public function endTransaction() |
|
| 749 | |||
| 750 | /** |
||
| 751 | * Get all errors from "$this->_errors". |
||
| 752 | 2 | * |
|
| 753 | * @return array|false <p>false === on errors</p> |
||
| 754 | */ |
||
| 755 | public function errors() |
||
| 761 | 35 | ||
| 762 | /** |
||
| 763 | 35 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
|
| 764 | * |
||
| 765 | * @param mixed $var boolean: convert into "integer"<br /> |
||
| 766 | * int: int (don't change it)<br /> |
||
| 767 | * float: float (don't change it)<br /> |
||
| 768 | * null: null (don't change it)<br /> |
||
| 769 | * array: run escape() for every key => value<br /> |
||
| 770 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 771 | 22 | * @param bool $stripe_non_utf8 |
|
| 772 | * @param bool $html_entity_decode |
||
| 773 | 22 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
|
| 774 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 775 | * <strong>null</strong> => Convert the array into null, every time. |
||
| 776 | * |
||
| 777 | * @return mixed |
||
| 778 | */ |
||
| 779 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) |
||
| 893 | 1 | ||
| 894 | 1 | /** |
|
| 895 | * Execute select/insert/update/delete sql-queries. |
||
| 896 | * |
||
| 897 | 1 | * @param string $query <p>sql-query</p> |
|
| 898 | 1 | * @param bool $useCache <p>use cache?</p> |
|
| 899 | 1 | * @param int $cacheTTL <p>cache-ttl in seconds</p> |
|
| 900 | 1 | * @param DB $db optional <p>the database connection</p> |
|
| 901 | 1 | * |
|
| 902 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 903 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 904 | 1 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 905 | 3 | * "true" by e.g. "DROP"-queries<br /> |
|
| 906 | * "false" on error |
||
| 907 | * |
||
| 908 | 3 | * @throws QueryException |
|
| 909 | */ |
||
| 910 | 3 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600, DB $db = null) |
|
| 959 | |||
| 960 | /** |
||
| 961 | * Get all table-names via "SHOW TABLES". |
||
| 962 | * |
||
| 963 | * @return array |
||
| 964 | */ |
||
| 965 | public function getAllTables() |
||
| 972 | |||
| 973 | /** |
||
| 974 | * @return Debug |
||
| 975 | */ |
||
| 976 | public function getDebugger() |
||
| 980 | |||
| 981 | /** |
||
| 982 | * Get errors from "$this->_errors". |
||
| 983 | * |
||
| 984 | * @return array |
||
| 985 | */ |
||
| 986 | public function getErrors() |
||
| 990 | |||
| 991 | /** |
||
| 992 | * getInstance() |
||
| 993 | * |
||
| 994 | * @param string $hostname |
||
| 995 | * @param string $username |
||
| 996 | 7 | * @param string $password |
|
| 997 | * @param string $database |
||
| 998 | 7 | * @param int|string $port <p>default is (int)3306</p> |
|
| 999 | 7 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
|
| 1000 | 5 | * @param bool|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 1001 | 5 | * @param bool|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 1002 | 7 | * @param string $logger_class_name |
|
| 1003 | 5 | * @param string $logger_level |
|
| 1004 | 5 | * @param array $extra_config <p> |
|
| 1005 | * 'session_to_db' => false|true<br> |
||
| 1006 | 7 | * 'socket' => 'string (path)'<br> |
|
| 1007 | * 'ssl' => 'bool'<br> |
||
| 1008 | 7 | * 'clientkey' => 'string (path)'<br> |
|
| 1009 | * 'clientcert' => 'string (path)'<br> |
||
| 1010 | * 'cacert' => 'string (path)'<br> |
||
| 1011 | 7 | * </p> |
|
| 1012 | * |
||
| 1013 | * @return \voku\db\DB |
||
| 1014 | 7 | */ |
|
| 1015 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = '', $echo_on_error = '', $logger_class_name = '', $logger_level = '', $extra_config = array()) |
||
| 1071 | |||
| 1072 | 1 | /** |
|
| 1073 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1074 | 1 | * |
|
| 1075 | 1 | * @return \mysqli |
|
| 1076 | 1 | */ |
|
| 1077 | public function getLink() |
||
| 1081 | 1 | ||
| 1082 | 1 | /** |
|
| 1083 | 1 | * Get the current charset. |
|
| 1084 | 1 | * |
|
| 1085 | * @return string |
||
| 1086 | */ |
||
| 1087 | 1 | public function get_charset() |
|
| 1091 | |||
| 1092 | /** |
||
| 1093 | 1 | * Check if we are in a transaction. |
|
| 1094 | * |
||
| 1095 | 1 | * @return bool |
|
| 1096 | */ |
||
| 1097 | public function inTransaction() |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Execute a "insert"-query. |
||
| 1104 | * |
||
| 1105 | * @param string $table |
||
| 1106 | * @param array $data |
||
| 1107 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1108 | 1 | * |
|
| 1109 | 1 | * @return false|int <p>false on error</p> |
|
| 1110 | * |
||
| 1111 | * @throws QueryException |
||
| 1112 | 1 | */ |
|
| 1113 | 1 | public function insert($table, array $data = array(), $databaseName = null) |
|
| 1140 | |||
| 1141 | /** |
||
| 1142 | 4 | * Returns the auto generated id used in the last query. |
|
| 1143 | * |
||
| 1144 | * @return int|string |
||
| 1145 | */ |
||
| 1146 | public function insert_id() |
||
| 1150 | |||
| 1151 | 4 | /** |
|
| 1152 | * Check if db-connection is ready. |
||
| 1153 | * |
||
| 1154 | * @return boolean |
||
| 1155 | */ |
||
| 1156 | public function isReady() |
||
| 1160 | |||
| 1161 | 4 | /** |
|
| 1162 | * Get the last sql-error. |
||
| 1163 | * |
||
| 1164 | * @return string|false <p>false === there was no error</p> |
||
| 1165 | */ |
||
| 1166 | public function lastError() |
||
| 1172 | |||
| 1173 | /** |
||
| 1174 | * Execute a sql-multi-query. |
||
| 1175 | * |
||
| 1176 | * @param string $sql |
||
| 1177 | * |
||
| 1178 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1179 | 2 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
|
| 1180 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1181 | * "boolean" by only by e.g. "DROP"-queries<br /> |
||
| 1182 | 2 | * |
|
| 1183 | 1 | * @throws QueryException |
|
| 1184 | 1 | */ |
|
| 1185 | 1 | public function multi_query($sql) |
|
| 1252 | |||
| 1253 | 18 | /** |
|
| 1254 | * Pings a server connection, or tries to reconnect |
||
| 1255 | 18 | * if the connection has gone down. |
|
| 1256 | * |
||
| 1257 | * @return boolean |
||
| 1258 | */ |
||
| 1259 | 18 | public function ping() |
|
| 1273 | |||
| 1274 | 23 | /** |
|
| 1275 | * Get a new "Prepare"-Object for your sql-query. |
||
| 1276 | * |
||
| 1277 | 23 | * @param string $query |
|
| 1278 | * |
||
| 1279 | * @return Prepare |
||
| 1280 | 23 | */ |
|
| 1281 | public function prepare($query) |
||
| 1285 | 23 | ||
| 1286 | 23 | /** |
|
| 1287 | 23 | * Execute a sql-query and return the result-array for select-statements. |
|
| 1288 | 23 | * |
|
| 1289 | * @param string $query |
||
| 1290 | 23 | * |
|
| 1291 | 2 | * @return mixed |
|
| 1292 | 2 | * @deprecated |
|
| 1293 | * @throws \Exception |
||
| 1294 | 23 | */ |
|
| 1295 | 1 | public static function qry($query) |
|
| 1320 | 2 | ||
| 1321 | /** |
||
| 1322 | 23 | * Execute a sql-query. |
|
| 1323 | 2 | * |
|
| 1324 | 2 | * @param string $sql <p>The sql query-string.</p> |
|
| 1325 | * |
||
| 1326 | 23 | * @param array|boolean $params <p> |
|
| 1327 | 4 | * "array" of sql-query-parameters<br/> |
|
| 1328 | 4 | * "false" if you don't need any parameter (default)<br/> |
|
| 1329 | * </p> |
||
| 1330 | 23 | * |
|
| 1331 | 1 | * @return bool|int|Result <p> |
|
| 1332 | 1 | * "Result" by "<b>SELECT</b>"-queries<br /> |
|
| 1333 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1334 | 23 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 1335 | 4 | * "true" by e.g. "DROP"-queries<br /> |
|
| 1336 | 4 | * "false" on error |
|
| 1337 | * </p> |
||
| 1338 | 23 | * |
|
| 1339 | 1 | * @throws QueryException |
|
| 1340 | 1 | */ |
|
| 1341 | public function query($sql = '', $params = false) |
||
| 1414 | |||
| 1415 | 23 | /** |
|
| 1416 | 23 | * Error-handling for the sql-query. |
|
| 1417 | * |
||
| 1418 | 23 | * @param string $errorMessage |
|
| 1419 | * @param int $errorNumber |
||
| 1420 | * @param string $sql |
||
| 1421 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
||
| 1422 | * @param bool $sqlMultiQuery |
||
| 1423 | * |
||
| 1424 | * @throws QueryException |
||
| 1425 | * @throws DBGoneAwayException |
||
| 1426 | * |
||
| 1427 | * @return bool |
||
| 1428 | 26 | */ |
|
| 1429 | private function queryErrorHandling($errorMessage, $errorNumber, $sql, $sqlParams = false, $sqlMultiQuery = false) |
||
| 1469 | 1 | ||
| 1470 | /** |
||
| 1471 | 1 | * Quote && Escape e.g. a table name string. |
|
| 1472 | * |
||
| 1473 | * @param string $str |
||
| 1474 | 1 | * |
|
| 1475 | 1 | * @return string |
|
| 1476 | */ |
||
| 1477 | 1 | public function quote_string($str) |
|
| 1490 | 1 | ||
| 1491 | /** |
||
| 1492 | 1 | * Reconnect to the MySQL-Server. |
|
| 1493 | 1 | * |
|
| 1494 | 1 | * @param bool $checkViaPing |
|
| 1495 | * |
||
| 1496 | 1 | * @return bool |
|
| 1497 | */ |
||
| 1498 | public function reconnect($checkViaPing = false) |
||
| 1513 | |||
| 1514 | /** |
||
| 1515 | * Execute a "replace"-query. |
||
| 1516 | * |
||
| 1517 | 6 | * @param string $table |
|
| 1518 | * @param array $data |
||
| 1519 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1520 | 6 | * |
|
| 1521 | * @return false|int <p>false on error</p> |
||
| 1522 | 6 | * |
|
| 1523 | 1 | * @throws QueryException |
|
| 1524 | */ |
||
| 1525 | 1 | public function replace($table, array $data = array(), $databaseName = null) |
|
| 1566 | |||
| 1567 | 2 | /** |
|
| 1568 | * Rollback in a transaction and end the transaction. |
||
| 1569 | 2 | * |
|
| 1570 | 1 | * @return bool <p>Boolean true on success, false otherwise.</p> |
|
| 1571 | */ |
||
| 1572 | 1 | View Code Duplication | public function rollback() |
| 1586 | |||
| 1587 | 2 | /** |
|
| 1588 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 1589 | 2 | * |
|
| 1590 | * <p> |
||
| 1591 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
||
| 1592 | * 1. parse into "int" |
||
| 1593 | * </p><br /> |
||
| 1594 | * |
||
| 1595 | * <p> |
||
| 1596 | * <strong>float:</strong><br /> |
||
| 1597 | * 1. return "float" |
||
| 1598 | * </p><br /> |
||
| 1599 | * |
||
| 1600 | * <p> |
||
| 1601 | * <strong>string:</strong><br /> |
||
| 1602 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
||
| 1603 | 20 | * 2. trim whitespace<br /> |
|
| 1604 | * 3. trim '<br /> |
||
| 1605 | * 4. escape the string (and remove non utf-8 chars)<br /> |
||
| 1606 | 20 | * 5. trim ' again (because we maybe removed some chars)<br /> |
|
| 1607 | * 6. add ' around the new string<br /> |
||
| 1608 | 20 | * </p><br /> |
|
| 1609 | 1 | * |
|
| 1610 | * <p> |
||
| 1611 | 1 | * <strong>array:</strong><br /> |
|
| 1612 | * 1. return null |
||
| 1613 | * </p><br /> |
||
| 1614 | 20 | * |
|
| 1615 | 5 | * <p> |
|
| 1616 | 20 | * <strong>object:</strong><br /> |
|
| 1617 | 16 | * 1. return false |
|
| 1618 | 16 | * </p><br /> |
|
| 1619 | 1 | * |
|
| 1620 | * <p> |
||
| 1621 | * <strong>null:</strong><br /> |
||
| 1622 | 20 | * 1. return null |
|
| 1623 | * </p> |
||
| 1624 | * |
||
| 1625 | * @param mixed $var |
||
| 1626 | 20 | * |
|
| 1627 | * @return mixed |
||
| 1628 | 20 | */ |
|
| 1629 | public function secure($var) |
||
| 1655 | |||
| 1656 | /** |
||
| 1657 | * Execute a "select"-query. |
||
| 1658 | * |
||
| 1659 | * @param string $table |
||
| 1660 | * @param string|array $where |
||
| 1661 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1662 | * |
||
| 1663 | * @return false|Result <p>false on error</p> |
||
| 1664 | * |
||
| 1665 | * @throws QueryException |
||
| 1666 | 2 | */ |
|
| 1667 | View Code Duplication | public function select($table, $where = '1=1', $databaseName = null) |
|
| 1694 | |||
| 1695 | /** |
||
| 1696 | * Selects a different database than the one specified on construction. |
||
| 1697 | * |
||
| 1698 | * @param string $database <p>Database name to switch to.</p> |
||
| 1699 | * |
||
| 1700 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1701 | */ |
||
| 1702 | public function select_db($database) |
||
| 1710 | |||
| 1711 | /** |
||
| 1712 | * Set the current charset. |
||
| 1713 | * |
||
| 1714 | * @param string $charset |
||
| 1715 | * |
||
| 1716 | * @return bool |
||
| 1717 | */ |
||
| 1718 | public function set_charset($charset) |
||
| 1740 | |||
| 1741 | /** |
||
| 1742 | * Set the option to convert null to "''" (empty string). |
||
| 1743 | * |
||
| 1744 | * Used in secure() => select(), insert(), update(), delete() |
||
| 1745 | * |
||
| 1746 | * @param $bool |
||
| 1747 | */ |
||
| 1748 | public function set_convert_null_to_empty_string($bool) |
||
| 1752 | |||
| 1753 | /** |
||
| 1754 | * Enables or disables internal report functions |
||
| 1755 | * |
||
| 1756 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 1757 | * |
||
| 1758 | * @param int $flags <p> |
||
| 1759 | * <table> |
||
| 1760 | * Supported flags |
||
| 1761 | * <tr valign="top"> |
||
| 1762 | * <td>Name</td> |
||
| 1763 | * <td>Description</td> |
||
| 1764 | * </tr> |
||
| 1765 | * <tr valign="top"> |
||
| 1766 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 1767 | * <td>Turns reporting off</td> |
||
| 1768 | * </tr> |
||
| 1769 | * <tr valign="top"> |
||
| 1770 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 1771 | * <td>Report errors from mysqli function calls</td> |
||
| 1772 | * </tr> |
||
| 1773 | * <tr valign="top"> |
||
| 1774 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 1775 | * <td> |
||
| 1776 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 1777 | * instead of warnings |
||
| 1778 | * </td> |
||
| 1779 | * </tr> |
||
| 1780 | * <tr valign="top"> |
||
| 1781 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 1782 | * <td>Report if no index or bad index was used in a query</td> |
||
| 1783 | * </tr> |
||
| 1784 | * <tr valign="top"> |
||
| 1785 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 1786 | * <td>Set all options (report all)</td> |
||
| 1787 | * </tr> |
||
| 1788 | * </table> |
||
| 1789 | * </p> |
||
| 1790 | * |
||
| 1791 | * @return bool |
||
| 1792 | */ |
||
| 1793 | public function set_mysqli_report($flags) |
||
| 1797 | |||
| 1798 | /** |
||
| 1799 | * Show config errors by throw exceptions. |
||
| 1800 | * |
||
| 1801 | * @return bool |
||
| 1802 | * |
||
| 1803 | * @throws \InvalidArgumentException |
||
| 1804 | */ |
||
| 1805 | public function showConfigError() |
||
| 1833 | |||
| 1834 | /** |
||
| 1835 | * alias: "beginTransaction()" |
||
| 1836 | */ |
||
| 1837 | public function startTransaction() |
||
| 1841 | |||
| 1842 | /** |
||
| 1843 | * Execute a callback inside a transaction. |
||
| 1844 | * |
||
| 1845 | * @param callback $callback The callback to run inside the transaction |
||
| 1846 | * |
||
| 1847 | * @return bool Boolean true on success, false otherwise |
||
| 1848 | */ |
||
| 1849 | public function transact($callback) |
||
| 1862 | |||
| 1863 | /** |
||
| 1864 | * Execute a "update"-query. |
||
| 1865 | * |
||
| 1866 | * @param string $table |
||
| 1867 | * @param array $data |
||
| 1868 | * @param array|string $where |
||
| 1869 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1870 | * |
||
| 1871 | * @return false|int <p>false on error</p> |
||
| 1872 | * |
||
| 1873 | * @throws QueryException |
||
| 1874 | */ |
||
| 1875 | public function update($table, array $data = array(), $where = '1=1', $databaseName = null) |
||
| 1910 | |||
| 1911 | } |
||
| 1912 |
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.