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 | * 'clientkey' => 'string (path)'<br> |
||
| 137 | 4 | * 'clientcert' => 'string (path)'<br> |
|
| 138 | * 'cacert' => 'string (path)'<br> |
||
| 139 | 4 | * </p> |
|
| 140 | */ |
||
| 141 | 4 | protected function __construct($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config = array()) |
|
| 192 | |||
| 193 | /** |
||
| 194 | 7 | * Prevent the instance from being cloned. |
|
| 195 | * |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | 10 | private function __clone() |
|
| 201 | |||
| 202 | 10 | /** |
|
| 203 | * __destruct |
||
| 204 | 10 | * |
|
| 205 | 10 | */ |
|
| 206 | public function __destruct() |
||
| 213 | 10 | ||
| 214 | /** |
||
| 215 | 10 | * __wakeup |
|
| 216 | 10 | * |
|
| 217 | * @return void |
||
| 218 | 10 | */ |
|
| 219 | public function __wakeup() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Load the config from the constructor. |
||
| 226 | * |
||
| 227 | * @param string $hostname |
||
| 228 | * @param string $username |
||
| 229 | * @param string $password |
||
| 230 | 10 | * @param string $database |
|
| 231 | * @param int|string $port <p>default is (int)3306</p> |
||
| 232 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 233 | * @param boolean|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 234 | 10 | * @param boolean|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
|
| 235 | 10 | * @param string $logger_class_name |
|
| 236 | 9 | * @param string $logger_level |
|
| 237 | 9 | * @param array $extra_config <p> |
|
| 238 | 8 | * 'session_to_db' => false|true<br> |
|
| 239 | 10 | * 'socket' => 'string (path)'<br> |
|
| 240 | * 'clientkey' => 'string (path)'<br> |
||
| 241 | 3 | * 'clientcert' => 'string (path)'<br> |
|
| 242 | 1 | * 'cacert' => 'string (path)'<br> |
|
| 243 | * </p> |
||
| 244 | * |
||
| 245 | 2 | * @return bool |
|
| 246 | 1 | */ |
|
| 247 | private function _loadConfig($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $extra_config) |
||
| 316 | 45 | ||
| 317 | /** |
||
| 318 | * Parses arrays with value pairs and generates SQL to use in queries. |
||
| 319 | * |
||
| 320 | * @param array $arrayPair |
||
| 321 | * @param string $glue <p>This is the separator.</p> |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | * |
||
| 325 | * @internal |
||
| 326 | 2 | */ |
|
| 327 | public function _parseArrayPair($arrayPair, $glue = ',') |
||
| 473 | 33 | ||
| 474 | 33 | /** |
|
| 475 | * _parseQueryParams |
||
| 476 | 33 | * |
|
| 477 | * @param string $sql |
||
| 478 | 33 | * @param array $params |
|
| 479 | 33 | * |
|
| 480 | 28 | * @return string |
|
| 481 | 28 | */ |
|
| 482 | 24 | private function _parseQueryParams($sql, array $params) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * Gets the number of affected rows in a previous MySQL operation. |
||
| 506 | 8 | * |
|
| 507 | 8 | * @return int |
|
| 508 | 8 | */ |
|
| 509 | public function affected_rows() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Begins a transaction, by turning off auto commit. |
||
| 516 | * |
||
| 517 | * @return bool <p>This will return true or false indicating success of transaction</p> |
||
| 518 | */ |
||
| 519 | public function beginTransaction() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Clear the errors in "_debug->_errors". |
||
| 540 | 3 | * |
|
| 541 | 3 | * @return bool |
|
| 542 | 3 | */ |
|
| 543 | public function clearErrors() |
||
| 547 | 3 | ||
| 548 | 3 | /** |
|
| 549 | 3 | * Closes a previously opened database connection. |
|
| 550 | 3 | * |
|
| 551 | * @return bool |
||
| 552 | 3 | */ |
|
| 553 | public function close() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Open a new connection to the MySQL server. |
||
| 571 | * |
||
| 572 | * @return bool |
||
| 573 | * |
||
| 574 | * @throws DBConnectException |
||
| 575 | */ |
||
| 576 | public function connect() |
||
| 652 | 33 | ||
| 653 | 33 | /** |
|
| 654 | 33 | * Execute a "delete"-query. |
|
| 655 | * |
||
| 656 | * @param string $table |
||
| 657 | * @param string|array $where |
||
| 658 | 33 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 659 | * |
||
| 660 | 33 | * @return false|int <p>false on error</p> |
|
| 661 | 33 | * |
|
| 662 | * * @throws QueryException |
||
| 663 | 33 | */ |
|
| 664 | 33 | View Code Duplication | public function delete($table, $where, $databaseName = null) |
| 691 | 1 | ||
| 692 | 1 | /** |
|
| 693 | * Ends a transaction and commits if no errors, then ends autocommit. |
||
| 694 | 1 | * |
|
| 695 | 1 | * @return bool <p>This will return true or false indicating success of transactions.</p> |
|
| 696 | */ |
||
| 697 | public function endTransaction() |
||
| 711 | 33 | ||
| 712 | /** |
||
| 713 | * Get all errors from "$this->_errors". |
||
| 714 | 3 | * |
|
| 715 | 3 | * @return array|false <p>false === on errors</p> |
|
| 716 | 3 | */ |
|
| 717 | 3 | public function errors() |
|
| 723 | |||
| 724 | 33 | /** |
|
| 725 | 9 | * Escape: Use "mysqli_real_escape_string" and clean non UTF-8 chars + some extra optional stuff. |
|
| 726 | 9 | * |
|
| 727 | * @param mixed $var boolean: convert into "integer"<br /> |
||
| 728 | 33 | * int: int (don't change it)<br /> |
|
| 729 | * float: float (don't change it)<br /> |
||
| 730 | 1 | * null: null (don't change it)<br /> |
|
| 731 | 1 | * array: run escape() for every key => value<br /> |
|
| 732 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 733 | 33 | * @param bool $stripe_non_utf8 |
|
| 734 | * @param bool $html_entity_decode |
||
| 735 | 33 | * @param bool|null $convert_array <strong>false</strong> => Keep the array.<br /> |
|
| 736 | * <strong>true</strong> => Convert to string var1,var2,var3...<br /> |
||
| 737 | 33 | * <strong>null</strong> => Convert the array into null, every time. |
|
| 738 | * |
||
| 739 | * @return mixed |
||
| 740 | */ |
||
| 741 | 3 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = false, $convert_array = false) |
|
| 855 | |||
| 856 | 3 | /** |
|
| 857 | * Execute select/insert/update/delete sql-queries. |
||
| 858 | * |
||
| 859 | 3 | * @param string $query sql-query |
|
| 860 | 3 | * @param bool $useCache use cache? |
|
| 861 | 3 | * @param int $cacheTTL cache-ttl in seconds |
|
| 862 | 3 | * |
|
| 863 | * @return mixed "array" by "<b>SELECT</b>"-queries<br /> |
||
| 864 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 865 | 3 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
|
| 866 | * "true" by e.g. "DROP"-queries<br /> |
||
| 867 | * "false" on error |
||
| 868 | * |
||
| 869 | * @throws QueryException |
||
| 870 | */ |
||
| 871 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600) |
||
| 918 | |||
| 919 | 1 | /** |
|
| 920 | * Get all table-names via "SHOW TABLES". |
||
| 921 | 1 | * |
|
| 922 | 1 | * @return array |
|
| 923 | 1 | */ |
|
| 924 | 1 | public function getAllTables() |
|
| 931 | 3 | ||
| 932 | /** |
||
| 933 | * @return Debug |
||
| 934 | */ |
||
| 935 | public function getDebugger() |
||
| 939 | 1 | ||
| 940 | /** |
||
| 941 | 1 | * Get errors from "$this->_errors". |
|
| 942 | * |
||
| 943 | * @return array |
||
| 944 | */ |
||
| 945 | public function getErrors() |
||
| 949 | |||
| 950 | /** |
||
| 951 | * getInstance() |
||
| 952 | * |
||
| 953 | * @param string $hostname |
||
| 954 | * @param string $username |
||
| 955 | * @param string $password |
||
| 956 | * @param string $database |
||
| 957 | * @param int|string $port <p>default is (int)3306</p> |
||
| 958 | * @param string $charset <p>default is 'utf8' or 'utf8mb4' (if supported)</p> |
||
| 959 | * @param bool|string $exit_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 960 | * @param bool|string $echo_on_error <p>Use a empty string "" or false to disable it.</p> |
||
| 961 | * @param string $logger_class_name |
||
| 962 | * @param string $logger_level |
||
| 963 | * @param array $extra_config <p> |
||
| 964 | * 'session_to_db' => false|true<br> |
||
| 965 | * 'socket' => 'string (path)'<br> |
||
| 966 | * 'clientkey' => 'string (path)'<br> |
||
| 967 | * 'clientcert' => 'string (path)'<br> |
||
| 968 | * 'cacert' => 'string (path)'<br> |
||
| 969 | * </p> |
||
| 970 | * |
||
| 971 | * @return \voku\db\DB |
||
| 972 | */ |
||
| 973 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = '', $echo_on_error = '', $logger_class_name = '', $logger_level = '', $extra_config = array()) |
||
| 1029 | 1 | ||
| 1030 | /** |
||
| 1031 | * Get the mysqli-link (link identifier returned by mysqli-connect). |
||
| 1032 | * |
||
| 1033 | * @return \mysqli |
||
| 1034 | */ |
||
| 1035 | public function getLink() |
||
| 1039 | 1 | ||
| 1040 | /** |
||
| 1041 | 1 | * Get the current charset. |
|
| 1042 | * |
||
| 1043 | * @return string |
||
| 1044 | */ |
||
| 1045 | public function get_charset() |
||
| 1049 | |||
| 1050 | /** |
||
| 1051 | * Check if we are in a transaction. |
||
| 1052 | * |
||
| 1053 | * @return bool |
||
| 1054 | */ |
||
| 1055 | public function inTransaction() |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Execute a "insert"-query. |
||
| 1062 | 1 | * |
|
| 1063 | 1 | * @param string $table |
|
| 1064 | * @param array $data |
||
| 1065 | 1 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 1066 | * |
||
| 1067 | * @return false|int <p>false on error</p> |
||
| 1068 | 1 | * |
|
| 1069 | 1 | * @throws QueryException |
|
| 1070 | 1 | */ |
|
| 1071 | public function insert($table, array $data = array(), $databaseName = null) |
||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * Returns the auto generated id used in the last query. |
||
| 1101 | * |
||
| 1102 | * @return int|string |
||
| 1103 | */ |
||
| 1104 | public function insert_id() |
||
| 1108 | 1 | ||
| 1109 | 1 | /** |
|
| 1110 | * Check if db-connection is ready. |
||
| 1111 | * |
||
| 1112 | 1 | * @return boolean |
|
| 1113 | 1 | */ |
|
| 1114 | public function isReady() |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * Get the last sql-error. |
||
| 1121 | * |
||
| 1122 | 1 | * @return string|false <p>false === there was no error</p> |
|
| 1123 | */ |
||
| 1124 | 1 | public function lastError() |
|
| 1130 | |||
| 1131 | /** |
||
| 1132 | 4 | * Execute a sql-multi-query. |
|
| 1133 | * |
||
| 1134 | 4 | * @param string $sql |
|
| 1135 | * |
||
| 1136 | 4 | * @return false|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
|
| 1137 | 1 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
|
| 1138 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1139 | 1 | * "boolean" by only by e.g. "DROP"-queries<br /> |
|
| 1140 | * |
||
| 1141 | * @throws QueryException |
||
| 1142 | 4 | */ |
|
| 1143 | public function multi_query($sql) |
||
| 1211 | 2 | ||
| 1212 | /** |
||
| 1213 | * Pings a server connection, or tries to reconnect |
||
| 1214 | 2 | * if the connection has gone down. |
|
| 1215 | * |
||
| 1216 | 2 | * @return boolean |
|
| 1217 | 2 | */ |
|
| 1218 | 2 | public function ping() |
|
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Selects a different database than the one specified on construction. |
||
| 1235 | * |
||
| 1236 | 21 | * @param string $database <p>Database name to switch to.</p> |
|
| 1237 | * |
||
| 1238 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1239 | 21 | */ |
|
| 1240 | public function select_db($database) |
||
| 1248 | 3 | ||
| 1249 | /** |
||
| 1250 | 3 | * Get a new "Prepare"-Object for your sql-query. |
|
| 1251 | * |
||
| 1252 | * @param string $query |
||
| 1253 | 18 | * |
|
| 1254 | * @return Prepare |
||
| 1255 | 18 | */ |
|
| 1256 | public function prepare($query) |
||
| 1260 | |||
| 1261 | 18 | /** |
|
| 1262 | * Execute a sql-query and return the result-array for select-statements. |
||
| 1263 | * |
||
| 1264 | * @param $query |
||
| 1265 | * |
||
| 1266 | * @return mixed |
||
| 1267 | * @deprecated |
||
| 1268 | * @throws \Exception |
||
| 1269 | */ |
||
| 1270 | public static function qry($query) |
||
| 1295 | 1 | ||
| 1296 | 1 | /** |
|
| 1297 | * Execute a sql-query. |
||
| 1298 | 23 | * |
|
| 1299 | 1 | * @param string $sql <p>The sql query-string.</p> |
|
| 1300 | 1 | * |
|
| 1301 | * @param array|boolean $params <p> |
||
| 1302 | 23 | * "array" of sql-query-parameters<br/> |
|
| 1303 | 1 | * "false" if you don't need any parameter (default)<br/> |
|
| 1304 | 1 | * </p> |
|
| 1305 | * |
||
| 1306 | 23 | * @return bool|int|Result <p> |
|
| 1307 | 1 | * "Result" by "<b>SELECT</b>"-queries<br /> |
|
| 1308 | 1 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
|
| 1309 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1310 | 23 | * "true" by e.g. "DROP"-queries<br /> |
|
| 1311 | 1 | * "false" on error |
|
| 1312 | 1 | * </p> |
|
| 1313 | * |
||
| 1314 | 23 | * @throws QueryException |
|
| 1315 | 1 | */ |
|
| 1316 | 1 | public function query($sql = '', $params = false) |
|
| 1390 | 23 | ||
| 1391 | 23 | /** |
|
| 1392 | * Error-handling for the sql-query. |
||
| 1393 | 23 | * |
|
| 1394 | * @param string $errorMessage |
||
| 1395 | 23 | * @param int $errorNumber |
|
| 1396 | * @param string $sql |
||
| 1397 | 23 | * @param array|bool $sqlParams <p>false if there wasn't any parameter</p> |
|
| 1398 | 1 | * @param bool $sqlMultiQuery |
|
| 1399 | 23 | * |
|
| 1400 | 23 | * @throws QueryException |
|
| 1401 | 23 | * @throws DBGoneAwayException |
|
| 1402 | * |
||
| 1403 | 23 | * @return bool |
|
| 1404 | 1 | */ |
|
| 1405 | 1 | private function queryErrorHandling($errorMessage, $errorNumber, $sql, $sqlParams = false, $sqlMultiQuery = false) |
|
| 1445 | |||
| 1446 | /** |
||
| 1447 | 1 | * Quote && Escape e.g. a table name string. |
|
| 1448 | * |
||
| 1449 | 1 | * @param string $str |
|
| 1450 | * |
||
| 1451 | * @return string |
||
| 1452 | */ |
||
| 1453 | public function quote_string($str) |
||
| 1466 | 1 | ||
| 1467 | /** |
||
| 1468 | 1 | * Reconnect to the MySQL-Server. |
|
| 1469 | 1 | * |
|
| 1470 | * @param bool $checkViaPing |
||
| 1471 | 1 | * |
|
| 1472 | * @return bool |
||
| 1473 | */ |
||
| 1474 | 1 | public function reconnect($checkViaPing = false) |
|
| 1489 | |||
| 1490 | 1 | /** |
|
| 1491 | * Execute a "replace"-query. |
||
| 1492 | 1 | * |
|
| 1493 | 1 | * @param string $table |
|
| 1494 | 1 | * @param array $data |
|
| 1495 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1496 | 1 | * |
|
| 1497 | * @return false|int <p>false on error</p> |
||
| 1498 | * |
||
| 1499 | * @throws QueryException |
||
| 1500 | 1 | */ |
|
| 1501 | public function replace($table, array $data = array(), $databaseName = null) |
||
| 1542 | |||
| 1543 | /** |
||
| 1544 | 6 | * Rollback in a transaction and end the transaction. |
|
| 1545 | * |
||
| 1546 | * @return bool <p>Boolean true on success, false otherwise.</p> |
||
| 1547 | */ |
||
| 1548 | 6 | public function rollback() |
|
| 1560 | |||
| 1561 | /** |
||
| 1562 | * Commits the current transaction and end the transaction. |
||
| 1563 | * |
||
| 1564 | 2 | * @return bool <p>Boolean true on success, false otherwise.</p> |
|
| 1565 | */ |
||
| 1566 | public function commit() |
||
| 1579 | 2 | ||
| 1580 | 1 | /** |
|
| 1581 | * Execute a callback inside a transaction. |
||
| 1582 | * |
||
| 1583 | 2 | * @param callback $callback The callback to run inside the transaction |
|
| 1584 | * |
||
| 1585 | * @return bool Boolean true on success, false otherwise |
||
| 1586 | */ |
||
| 1587 | 2 | public function transact($callback) |
|
| 1598 | |||
| 1599 | /** |
||
| 1600 | * Try to secure a variable, so can you use it in sql-queries. |
||
| 1601 | * |
||
| 1602 | * <p> |
||
| 1603 | 20 | * <strong>int:</strong> (also strings that contains only an int-value)<br /> |
|
| 1604 | * 1. parse into "int" |
||
| 1605 | * </p><br /> |
||
| 1606 | 20 | * |
|
| 1607 | * <p> |
||
| 1608 | 20 | * <strong>float:</strong><br /> |
|
| 1609 | 1 | * 1. return "float" |
|
| 1610 | * </p><br /> |
||
| 1611 | 1 | * |
|
| 1612 | * <p> |
||
| 1613 | * <strong>string:</strong><br /> |
||
| 1614 | 20 | * 1. check if the string isn't a default mysql-time-function e.g. 'CURDATE()'<br /> |
|
| 1615 | 5 | * 2. trim whitespace<br /> |
|
| 1616 | 20 | * 3. trim '<br /> |
|
| 1617 | 16 | * 4. escape the string (and remove non utf-8 chars)<br /> |
|
| 1618 | 16 | * 5. trim ' again (because we maybe removed some chars)<br /> |
|
| 1619 | 1 | * 6. add ' around the new string<br /> |
|
| 1620 | * </p><br /> |
||
| 1621 | * |
||
| 1622 | 20 | * <p> |
|
| 1623 | * <strong>array:</strong><br /> |
||
| 1624 | * 1. return null |
||
| 1625 | * </p><br /> |
||
| 1626 | 20 | * |
|
| 1627 | * <p> |
||
| 1628 | 20 | * <strong>object:</strong><br /> |
|
| 1629 | * 1. return false |
||
| 1630 | * </p><br /> |
||
| 1631 | * |
||
| 1632 | * <p> |
||
| 1633 | * <strong>null:</strong><br /> |
||
| 1634 | * 1. return null |
||
| 1635 | * </p> |
||
| 1636 | 1 | * |
|
| 1637 | * @param mixed $var |
||
| 1638 | 1 | * |
|
| 1639 | * @return mixed |
||
| 1640 | 1 | */ |
|
| 1641 | public function secure($var) |
||
| 1667 | |||
| 1668 | 2 | /** |
|
| 1669 | * Execute a "select"-query. |
||
| 1670 | 2 | * |
|
| 1671 | 2 | * @param string $table |
|
| 1672 | 2 | * @param string|array $where |
|
| 1673 | 2 | * @param string|null $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
|
| 1674 | * |
||
| 1675 | * @return false|Result <p>false on error</p> |
||
| 1676 | * |
||
| 1677 | * @throws QueryException |
||
| 1678 | */ |
||
| 1679 | View Code Duplication | public function select($table, $where = '1=1', $databaseName = null) |
|
| 1706 | |||
| 1707 | /** |
||
| 1708 | * Set the current charset. |
||
| 1709 | * |
||
| 1710 | * @param string $charset |
||
| 1711 | * |
||
| 1712 | * @return bool |
||
| 1713 | */ |
||
| 1714 | public function set_charset($charset) |
||
| 1736 | |||
| 1737 | /** |
||
| 1738 | * Set the option to convert null to "''" (empty string). |
||
| 1739 | * |
||
| 1740 | * Used in secure() => select(), insert(), update(), delete() |
||
| 1741 | * |
||
| 1742 | * @param $bool |
||
| 1743 | */ |
||
| 1744 | public function set_convert_null_to_empty_string($bool) |
||
| 1748 | |||
| 1749 | /** |
||
| 1750 | * Enables or disables internal report functions |
||
| 1751 | * |
||
| 1752 | * @link http://php.net/manual/en/function.mysqli-report.php |
||
| 1753 | * |
||
| 1754 | * @param int $flags <p> |
||
| 1755 | * <table> |
||
| 1756 | * Supported flags |
||
| 1757 | * <tr valign="top"> |
||
| 1758 | * <td>Name</td> |
||
| 1759 | * <td>Description</td> |
||
| 1760 | * </tr> |
||
| 1761 | * <tr valign="top"> |
||
| 1762 | * <td><b>MYSQLI_REPORT_OFF</b></td> |
||
| 1763 | * <td>Turns reporting off</td> |
||
| 1764 | * </tr> |
||
| 1765 | * <tr valign="top"> |
||
| 1766 | * <td><b>MYSQLI_REPORT_ERROR</b></td> |
||
| 1767 | * <td>Report errors from mysqli function calls</td> |
||
| 1768 | * </tr> |
||
| 1769 | * <tr valign="top"> |
||
| 1770 | * <td><b>MYSQLI_REPORT_STRICT</b></td> |
||
| 1771 | * <td> |
||
| 1772 | * Throw <b>mysqli_sql_exception</b> for errors |
||
| 1773 | * instead of warnings |
||
| 1774 | * </td> |
||
| 1775 | * </tr> |
||
| 1776 | * <tr valign="top"> |
||
| 1777 | * <td><b>MYSQLI_REPORT_INDEX</b></td> |
||
| 1778 | * <td>Report if no index or bad index was used in a query</td> |
||
| 1779 | * </tr> |
||
| 1780 | * <tr valign="top"> |
||
| 1781 | * <td><b>MYSQLI_REPORT_ALL</b></td> |
||
| 1782 | * <td>Set all options (report all)</td> |
||
| 1783 | * </tr> |
||
| 1784 | * </table> |
||
| 1785 | * </p> |
||
| 1786 | * |
||
| 1787 | * @return bool |
||
| 1788 | */ |
||
| 1789 | public function set_mysqli_report($flags) |
||
| 1793 | |||
| 1794 | /** |
||
| 1795 | * Show config errors by throw exceptions. |
||
| 1796 | * |
||
| 1797 | * @return bool |
||
| 1798 | * |
||
| 1799 | * @throws \InvalidArgumentException |
||
| 1800 | */ |
||
| 1801 | public function showConfigError() |
||
| 1829 | |||
| 1830 | /** |
||
| 1831 | * alias: "beginTransaction()" |
||
| 1832 | */ |
||
| 1833 | public function startTransaction() |
||
| 1837 | |||
| 1838 | /** |
||
| 1839 | * Execute a "update"-query. |
||
| 1840 | * |
||
| 1841 | * @param string $table |
||
| 1842 | * @param array $data |
||
| 1843 | * @param array|string $where |
||
| 1844 | * @param null|string $databaseName <p>Use <strong>null</strong> if you will use the current database.</p> |
||
| 1845 | * |
||
| 1846 | * @return false|int <p>false on error</p> |
||
| 1847 | * |
||
| 1848 | * @throws QueryException |
||
| 1849 | */ |
||
| 1850 | public function update($table, array $data = array(), $where = '1=1', $databaseName = null) |
||
| 1885 | |||
| 1886 | /** |
||
| 1887 | * @param null|string $sql |
||
| 1888 | * @param array $bindings |
||
| 1889 | * |
||
| 1890 | * @return bool|int|Result|DB <p> |
||
| 1891 | * "DB" by "$sql" === null<br /> |
||
| 1892 | * "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 1893 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 1894 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1895 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1896 | * "false" on error |
||
| 1897 | * </p> |
||
| 1898 | */ |
||
| 1899 | public function __invoke($sql = null, array $bindings = array()) |
||
| 1903 | |||
| 1904 | } |
||
| 1905 |
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.