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 |
||
| 13 | class DB |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | public $query_count = 0; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | protected $exit_on_error = true; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | protected $echo_on_error = true; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $css_mysql_box_border = '3px solid red'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $css_mysql_box_bg = '#FFCCCC'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \mysqli |
||
| 43 | */ |
||
| 44 | protected $link = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | protected $connected = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $mysqlDefaultTimeFunctions; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $logger_class_name; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | * |
||
| 64 | * 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL' |
||
| 65 | */ |
||
| 66 | private $logger_level; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | private $hostname = ''; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $username = ''; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | private $password = ''; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $database = ''; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | private $port = 3306; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | private $charset = 'utf8'; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $socket = ''; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private $_errors = array(); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | private $session_to_db = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | private $_in_transaction = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * __construct() |
||
| 120 | * |
||
| 121 | * @param string $hostname |
||
| 122 | * @param string $username |
||
| 123 | * @param string $password |
||
| 124 | * @param string $database |
||
| 125 | * @param int $port |
||
| 126 | * @param string $charset |
||
| 127 | * @param boolean|string $exit_on_error use a empty string "" or false to disable it |
||
| 128 | * @param boolean|string $echo_on_error use a empty string "" or false to disable it |
||
| 129 | * @param string $logger_class_name |
||
| 130 | * @param string $logger_level 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL' |
||
| 131 | * @param boolean|string $session_to_db use a empty string "" or false to disable it |
||
| 132 | */ |
||
| 133 | 10 | protected function __construct($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $session_to_db) |
|
| 134 | { |
||
| 135 | 10 | $this->connected = false; |
|
| 136 | |||
| 137 | 10 | $this->_loadConfig( |
|
| 138 | 10 | $hostname, |
|
| 139 | 10 | $username, |
|
| 140 | 10 | $password, |
|
| 141 | 10 | $database, |
|
| 142 | 10 | $port, |
|
| 143 | 10 | $charset, |
|
| 144 | 10 | $exit_on_error, |
|
| 145 | 10 | $echo_on_error, |
|
| 146 | 10 | $logger_class_name, |
|
| 147 | 10 | $logger_level, |
|
| 148 | $session_to_db |
||
| 149 | 10 | ); |
|
| 150 | |||
| 151 | 7 | $this->connect(); |
|
| 152 | |||
| 153 | 4 | $this->mysqlDefaultTimeFunctions = array( |
|
| 154 | // Returns the current date |
||
| 155 | 4 | 'CURDATE()', |
|
| 156 | // CURRENT_DATE | Synonyms for CURDATE() |
||
| 157 | 4 | 'CURRENT_DATE()', |
|
| 158 | // CURRENT_TIME | Synonyms for CURTIME() |
||
| 159 | 4 | 'CURRENT_TIME()', |
|
| 160 | // CURRENT_TIMESTAMP | Synonyms for NOW() |
||
| 161 | 4 | 'CURRENT_TIMESTAMP()', |
|
| 162 | // Returns the current time |
||
| 163 | 4 | 'CURTIME()', |
|
| 164 | // Synonym for NOW() |
||
| 165 | 4 | 'LOCALTIME()', |
|
| 166 | // Synonym for NOW() |
||
| 167 | 4 | 'LOCALTIMESTAMP()', |
|
| 168 | // Returns the current date and time |
||
| 169 | 4 | 'NOW()', |
|
| 170 | // Returns the time at which the function executes |
||
| 171 | 4 | 'SYSDATE()', |
|
| 172 | // Returns a UNIX timestamp |
||
| 173 | 4 | 'UNIX_TIMESTAMP()', |
|
| 174 | // Returns the current UTC date |
||
| 175 | 4 | 'UTC_DATE()', |
|
| 176 | // Returns the current UTC time |
||
| 177 | 4 | 'UTC_TIME()', |
|
| 178 | // Returns the current UTC date and time |
||
| 179 | 4 | 'UTC_TIMESTAMP()', |
|
| 180 | ); |
||
| 181 | 4 | } |
|
| 182 | |||
| 183 | /** |
||
| 184 | * load the config |
||
| 185 | * |
||
| 186 | * @param string $hostname |
||
| 187 | * @param string $username |
||
| 188 | * @param string $password |
||
| 189 | * @param string $database |
||
| 190 | * @param int $port |
||
| 191 | * @param string $charset |
||
| 192 | * @param boolean|string $exit_on_error use a empty string "" or false to disable it |
||
| 193 | * @param boolean|string $echo_on_error use a empty string "" or false to disable it |
||
| 194 | * @param string $logger_class_name |
||
| 195 | * @param string $logger_level |
||
| 196 | * @param boolean|string $session_to_db use a empty string "" or false to disable it |
||
| 197 | * |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | 10 | private function _loadConfig($hostname, $username, $password, $database, $port, $charset, $exit_on_error, $echo_on_error, $logger_class_name, $logger_level, $session_to_db) |
|
| 201 | { |
||
| 202 | 10 | $this->hostname = (string)$hostname; |
|
| 203 | 10 | $this->username = (string)$username; |
|
| 204 | 10 | $this->password = (string)$password; |
|
| 205 | 10 | $this->database = (string)$database; |
|
| 206 | |||
| 207 | 10 | if ($charset) { |
|
| 208 | 4 | $this->charset = (string)$charset; |
|
| 209 | 4 | } |
|
| 210 | |||
| 211 | 10 | if ($port) { |
|
| 212 | 4 | $this->port = (int)$port; |
|
| 213 | 4 | } else { |
|
| 214 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
| 215 | 7 | $this->port = @ini_get('mysqli.default_port'); |
|
| 216 | } |
||
| 217 | |||
| 218 | 10 | if (!$this->socket) { |
|
| 219 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
| 220 | 10 | $this->socket = @ini_get('mysqli.default_socket'); |
|
| 221 | 10 | } |
|
| 222 | |||
| 223 | 10 | if ($exit_on_error === true || $exit_on_error === false) { |
|
| 224 | 10 | $this->exit_on_error = (boolean)$exit_on_error; |
|
| 225 | 10 | } |
|
| 226 | |||
| 227 | 10 | if ($echo_on_error === true || $echo_on_error === false) { |
|
| 228 | 10 | $this->echo_on_error = (boolean)$echo_on_error; |
|
| 229 | 10 | } |
|
| 230 | |||
| 231 | 10 | $this->logger_class_name = (string)$logger_class_name; |
|
| 232 | 10 | $this->logger_level = (string)$logger_level; |
|
| 233 | |||
| 234 | 10 | $this->session_to_db = (boolean)$session_to_db; |
|
| 235 | |||
| 236 | 10 | return $this->showConfigError(); |
|
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * show config error and throw a exception |
||
| 241 | */ |
||
| 242 | 10 | public function showConfigError() |
|
| 243 | { |
||
| 244 | |||
| 245 | if ( |
||
| 246 | 10 | !$this->hostname |
|
| 247 | 10 | || |
|
| 248 | 9 | !$this->username |
|
| 249 | 9 | || |
|
| 250 | 8 | !$this->database |
|
| 251 | 10 | ) { |
|
| 252 | |||
| 253 | 3 | if (!$this->hostname) { |
|
| 254 | 1 | throw new \Exception('no-sql-hostname'); |
|
| 255 | } |
||
| 256 | |||
| 257 | 2 | if (!$this->username) { |
|
| 258 | 1 | throw new \Exception('no-sql-username'); |
|
| 259 | } |
||
| 260 | |||
| 261 | 1 | if (!$this->database) { |
|
| 262 | 1 | throw new \Exception('no-sql-database'); |
|
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | 7 | return true; |
|
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * connect |
||
| 271 | * |
||
| 272 | * @return boolean |
||
| 273 | */ |
||
| 274 | 8 | public function connect() |
|
| 275 | { |
||
| 276 | 8 | if ($this->isReady()) { |
|
| 277 | 1 | return true; |
|
| 278 | } |
||
| 279 | |||
| 280 | 8 | mysqli_report(MYSQLI_REPORT_STRICT); |
|
| 281 | try { |
||
| 282 | /** @noinspection PhpUsageOfSilenceOperatorInspection */ |
||
| 283 | 8 | $this->link = @mysqli_connect( |
|
| 284 | 8 | $this->hostname, |
|
| 285 | 8 | $this->username, |
|
| 286 | 8 | $this->password, |
|
| 287 | 8 | $this->database, |
|
| 288 | 8 | $this->port, |
|
| 289 | 8 | $this->socket |
|
| 290 | 8 | ); |
|
| 291 | 8 | } catch (\Exception $e) { |
|
| 292 | 3 | $this->_displayError('Error connecting to mysql server: ' . $e->getMessage(), true); |
|
| 293 | } |
||
| 294 | 5 | mysqli_report(MYSQLI_REPORT_OFF); |
|
| 295 | |||
| 296 | 5 | if (!$this->link) { |
|
| 297 | $this->_displayError('Error connecting to mysql server: ' . mysqli_connect_error(), true); |
||
| 298 | } else { |
||
| 299 | 5 | $this->set_charset($this->charset); |
|
| 300 | 5 | $this->connected = true; |
|
| 301 | } |
||
| 302 | |||
| 303 | 5 | return $this->isReady(); |
|
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * check if db-connection is ready |
||
| 308 | * |
||
| 309 | * @return boolean |
||
| 310 | */ |
||
| 311 | 29 | public function isReady() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * _displayError |
||
| 318 | * |
||
| 319 | * @param string $e |
||
| 320 | * @param null|boolean $force_exception_after_error |
||
| 321 | * |
||
| 322 | * @throws \Exception |
||
| 323 | */ |
||
| 324 | 18 | private function _displayError($e, $force_exception_after_error = null) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * try to get the file & line from the current sql-query |
||
| 371 | * |
||
| 372 | * @return array will return array['file'] and array['line'] |
||
| 373 | */ |
||
| 374 | 19 | private function getFileAndLineFromSql() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * wrapper for a "Logger"-Class |
||
| 415 | * |
||
| 416 | * @param string[] $log [method, text, type] e.g.: array('error', 'this is a error', 'sql') |
||
| 417 | */ |
||
| 418 | 20 | private function logger($log) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * check for developer |
||
| 448 | * |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | 18 | private function checkForDev() |
|
| 480 | |||
| 481 | /** |
||
| 482 | * execute a sql-query and |
||
| 483 | * return the result-array for select-statements |
||
| 484 | * |
||
| 485 | * ----------------------- |
||
| 486 | * |
||
| 487 | * e.g.: |
||
| 488 | * $retcode = DB::qry("UPDATE user_extension |
||
| 489 | * SET |
||
| 490 | * user_name='?' |
||
| 491 | * WHERE user_uid_fk='?' |
||
| 492 | * ", |
||
| 493 | * $userName, |
||
| 494 | * (int)$uid |
||
| 495 | * ); |
||
| 496 | * |
||
| 497 | * ----------------------- |
||
| 498 | * |
||
| 499 | * @param $query |
||
| 500 | * |
||
| 501 | * @return array|bool|int|\voku\db\Result |
||
| 502 | * @deprecated |
||
| 503 | * @throws \Exception |
||
| 504 | */ |
||
| 505 | public static function qry($query) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * getInstance() |
||
| 538 | * |
||
| 539 | * @param string $hostname |
||
| 540 | * @param string $username |
||
| 541 | * @param string $password |
||
| 542 | * @param string $database |
||
| 543 | * @param string $port default is '3306' |
||
| 544 | * @param string $charset default is 'utf8', but if you need 4-byte chars, then your tables need |
||
| 545 | * the 'utf8mb4'-charset |
||
| 546 | * @param bool|string $exit_on_error use a empty string "" or false to disable it |
||
| 547 | * @param bool|string $echo_on_error use a empty string "" or false to disable it |
||
| 548 | * @param string $logger_class_name |
||
| 549 | * @param string $logger_level |
||
| 550 | * @param bool|string $session_to_db use a empty string "" or false to disable it |
||
| 551 | * |
||
| 552 | * @return \voku\db\DB |
||
| 553 | */ |
||
| 554 | 39 | public static function getInstance($hostname = '', $username = '', $password = '', $database = '', $port = '', $charset = '', $exit_on_error = '', $echo_on_error = '', $logger_class_name = '', $logger_level = '', $session_to_db = '') |
|
| 600 | |||
| 601 | /** |
||
| 602 | * run a sql-query |
||
| 603 | * |
||
| 604 | * @param string $sql sql-query string |
||
| 605 | * |
||
| 606 | * @param array|boolean $params a "array" of sql-query-parameters |
||
| 607 | * "false" if you don't need any parameter |
||
| 608 | * |
||
| 609 | * @return bool|int|Result "Result" by "<b>SELECT</b>"-queries<br /> |
||
| 610 | * "int" (insert_id) by "<b>INSERT / REPLACE</b>"-queries<br /> |
||
| 611 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 612 | * "true" by e.g. "DROP"-queries<br /> |
||
| 613 | * "false" on error |
||
| 614 | * |
||
| 615 | * @throws \Exception |
||
| 616 | */ |
||
| 617 | 22 | public function query($sql = '', $params = false) |
|
| 684 | |||
| 685 | /** |
||
| 686 | * _parseQueryParams |
||
| 687 | * |
||
| 688 | * @param string $sql |
||
| 689 | * @param array $params |
||
| 690 | * |
||
| 691 | * @return string |
||
| 692 | */ |
||
| 693 | 1 | private function _parseQueryParams($sql, array $params) |
|
| 715 | |||
| 716 | /** |
||
| 717 | * secure |
||
| 718 | * |
||
| 719 | * @param mixed $var |
||
| 720 | * |
||
| 721 | * @return string | null |
||
| 722 | */ |
||
| 723 | 14 | public function secure($var) |
|
| 764 | |||
| 765 | /** |
||
| 766 | * escape |
||
| 767 | * |
||
| 768 | * @param array|float|int|string|boolean $var boolean: convert into "integer"<br /> |
||
| 769 | * int: convert into "integer"<br /> |
||
| 770 | * float: convert into "float" and replace "," with "."<br /> |
||
| 771 | * array: run escape() for every key => value<br /> |
||
| 772 | * string: run UTF8::cleanup() and mysqli_real_escape_string()<br /> |
||
| 773 | * @param bool $stripe_non_utf8 |
||
| 774 | * @param bool $html_entity_decode |
||
| 775 | * @param bool $array_to_string |
||
| 776 | * |
||
| 777 | * @return array|bool|float|int|string |
||
| 778 | */ |
||
| 779 | 17 | public function escape($var = '', $stripe_non_utf8 = true, $html_entity_decode = true, $array_to_string = false) |
|
| 838 | |||
| 839 | /** |
||
| 840 | * getLink |
||
| 841 | * |
||
| 842 | * @return \mysqli |
||
| 843 | */ |
||
| 844 | 17 | public function getLink() |
|
| 848 | |||
| 849 | /** |
||
| 850 | * _logQuery |
||
| 851 | * |
||
| 852 | * @param String $sql sql-query |
||
| 853 | * @param int $duration |
||
| 854 | * @param int $results result counter |
||
| 855 | * |
||
| 856 | * @return bool |
||
| 857 | */ |
||
| 858 | 21 | private function _logQuery($sql, $duration, $results) |
|
| 888 | |||
| 889 | /** |
||
| 890 | * insert_id |
||
| 891 | * |
||
| 892 | * @return int|string |
||
| 893 | */ |
||
| 894 | 15 | public function insert_id() |
|
| 898 | |||
| 899 | /** |
||
| 900 | * affected_rows |
||
| 901 | * |
||
| 902 | * @return int |
||
| 903 | */ |
||
| 904 | 6 | public function affected_rows() |
|
| 908 | |||
| 909 | /** |
||
| 910 | * query error-handling |
||
| 911 | * |
||
| 912 | * @param string $errorMsg |
||
| 913 | * @param string $sql |
||
| 914 | * @param array|bool $sqlParams false if there wasn't any parameter |
||
| 915 | * |
||
| 916 | * @throws \Exception |
||
| 917 | */ |
||
| 918 | 9 | protected function queryErrorHandling($errorMsg, $sql, $sqlParams = false) |
|
| 944 | |||
| 945 | /** |
||
| 946 | * send a error mail to the admin / dev |
||
| 947 | * |
||
| 948 | * @param string $subject |
||
| 949 | * @param string $htmlBody |
||
| 950 | * @param int $priority |
||
| 951 | */ |
||
| 952 | 9 | private function mailToAdmin($subject, $htmlBody, $priority = 3) |
|
| 983 | |||
| 984 | /** |
||
| 985 | * reconnect |
||
| 986 | * |
||
| 987 | * @param bool $checkViaPing |
||
| 988 | * |
||
| 989 | * @return bool |
||
| 990 | */ |
||
| 991 | 2 | public function reconnect($checkViaPing = false) |
|
| 1006 | |||
| 1007 | /** |
||
| 1008 | * ping |
||
| 1009 | * |
||
| 1010 | * @return boolean |
||
| 1011 | */ |
||
| 1012 | 3 | public function ping() |
|
| 1025 | |||
| 1026 | /** |
||
| 1027 | * can handel select/insert/update/delete queries |
||
| 1028 | * |
||
| 1029 | * @param string $query sql-query |
||
| 1030 | * @param bool $useCache use cache? |
||
| 1031 | * @param int $cacheTTL cache-ttl in seconds |
||
| 1032 | * |
||
| 1033 | * @return bool|int|array "array" by "<b>SELECT</b>"-queries<br /> |
||
| 1034 | * "int" (insert_id) by "<b>INSERT</b>"-queries<br /> |
||
| 1035 | * "int" (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1036 | * "true" by e.g. "DROP"-queries<br /> |
||
| 1037 | * "false" on error |
||
| 1038 | * |
||
| 1039 | */ |
||
| 1040 | 3 | public static function execSQL($query, $useCache = false, $cacheTTL = 3600) |
|
| 1084 | |||
| 1085 | /** |
||
| 1086 | * get charset |
||
| 1087 | * |
||
| 1088 | * @return string |
||
| 1089 | */ |
||
| 1090 | 1 | public function get_charset() |
|
| 1094 | |||
| 1095 | /** |
||
| 1096 | * set charset |
||
| 1097 | * |
||
| 1098 | * @param string $charset |
||
| 1099 | * |
||
| 1100 | * @return bool |
||
| 1101 | */ |
||
| 1102 | 6 | public function set_charset($charset) |
|
| 1114 | |||
| 1115 | /** |
||
| 1116 | * __wakeup |
||
| 1117 | * |
||
| 1118 | * @return void |
||
| 1119 | */ |
||
| 1120 | 1 | public function __wakeup() |
|
| 1124 | |||
| 1125 | /** |
||
| 1126 | * get the names of all tables |
||
| 1127 | * |
||
| 1128 | * @return array |
||
| 1129 | */ |
||
| 1130 | 1 | public function getAllTables() |
|
| 1137 | |||
| 1138 | /** |
||
| 1139 | * run a sql-multi-query |
||
| 1140 | * |
||
| 1141 | * @param string $sql |
||
| 1142 | * |
||
| 1143 | * @return bool|Result[] "Result"-Array by "<b>SELECT</b>"-queries<br /> |
||
| 1144 | * "boolean" by only "<b>INSERT</b>"-queries<br /> |
||
| 1145 | * "boolean" by only (affected_rows) by "<b>UPDATE / DELETE</b>"-queries<br /> |
||
| 1146 | * "boolean" by only by e.g. "DROP"-queries<br /> |
||
| 1147 | * |
||
| 1148 | * @throws \Exception |
||
| 1149 | */ |
||
| 1150 | 1 | public function multi_query($sql) |
|
| 1218 | |||
| 1219 | /** |
||
| 1220 | * alias for "beginTransaction()" |
||
| 1221 | */ |
||
| 1222 | 1 | public function startTransaction() |
|
| 1226 | |||
| 1227 | /** |
||
| 1228 | * Begins a transaction, by turning off auto commit |
||
| 1229 | * |
||
| 1230 | * @return boolean this will return true or false indicating success of transaction |
||
| 1231 | */ |
||
| 1232 | 4 | public function beginTransaction() |
|
| 1252 | |||
| 1253 | /** |
||
| 1254 | * clear errors |
||
| 1255 | * |
||
| 1256 | * @return bool |
||
| 1257 | */ |
||
| 1258 | 4 | public function clearErrors() |
|
| 1264 | |||
| 1265 | /** |
||
| 1266 | * Check if in transaction |
||
| 1267 | * |
||
| 1268 | * @return boolean |
||
| 1269 | */ |
||
| 1270 | 4 | public function inTransaction() |
|
| 1274 | |||
| 1275 | /** |
||
| 1276 | * Ends a transaction and commits if no errors, then ends autocommit |
||
| 1277 | * |
||
| 1278 | * @return boolean this will return true or false indicating success of transactions |
||
| 1279 | */ |
||
| 1280 | 2 | public function endTransaction() |
|
| 1296 | |||
| 1297 | /** |
||
| 1298 | * get all errors |
||
| 1299 | * |
||
| 1300 | * @return array false === on errors |
||
| 1301 | */ |
||
| 1302 | 2 | public function errors() |
|
| 1306 | |||
| 1307 | /** |
||
| 1308 | * rollback in a transaction |
||
| 1309 | */ |
||
| 1310 | 2 | public function rollback() |
|
| 1323 | |||
| 1324 | /** |
||
| 1325 | * insert |
||
| 1326 | * |
||
| 1327 | * @param string $table |
||
| 1328 | * @param array $data |
||
| 1329 | * |
||
| 1330 | * @return false|int false on error |
||
| 1331 | */ |
||
| 1332 | 14 | public function insert($table, $data = array()) |
|
| 1354 | |||
| 1355 | /** |
||
| 1356 | * Parses arrays with value pairs and generates SQL to use in queries |
||
| 1357 | * |
||
| 1358 | * @param array $arrayPair |
||
| 1359 | * @param string $glue this is the separator |
||
| 1360 | * |
||
| 1361 | * @return string |
||
| 1362 | */ |
||
| 1363 | 12 | private function _parseArrayPair($arrayPair, $glue = ',') |
|
| 1472 | |||
| 1473 | /** |
||
| 1474 | * Quote && Escape e.g. a table name string |
||
| 1475 | * |
||
| 1476 | * @param string $str |
||
| 1477 | * |
||
| 1478 | * @return string |
||
| 1479 | */ |
||
| 1480 | 14 | public function quote_string($str) |
|
| 1484 | |||
| 1485 | /** |
||
| 1486 | * get errors |
||
| 1487 | * |
||
| 1488 | * @return array |
||
| 1489 | */ |
||
| 1490 | 1 | public function getErrors() |
|
| 1494 | |||
| 1495 | /** |
||
| 1496 | * replace |
||
| 1497 | * |
||
| 1498 | * @param string $table |
||
| 1499 | * @param array $data |
||
| 1500 | * |
||
| 1501 | * @return false|int false on error |
||
| 1502 | */ |
||
| 1503 | 1 | public function replace($table, $data = array()) |
|
| 1540 | |||
| 1541 | /** |
||
| 1542 | * update |
||
| 1543 | * |
||
| 1544 | * @param string $table |
||
| 1545 | * @param array $data |
||
| 1546 | * @param array|string $where |
||
| 1547 | * |
||
| 1548 | * @return false|int false on error |
||
| 1549 | */ |
||
| 1550 | 5 | public function update($table, $data = array(), $where = '1=1') |
|
| 1580 | |||
| 1581 | /** |
||
| 1582 | * delete |
||
| 1583 | * |
||
| 1584 | * @param string $table |
||
| 1585 | * @param string|array $where |
||
| 1586 | * |
||
| 1587 | * @return false|int false on error |
||
| 1588 | */ |
||
| 1589 | 1 | View Code Duplication | public function delete($table, $where) |
| 1612 | |||
| 1613 | /** |
||
| 1614 | * select |
||
| 1615 | * |
||
| 1616 | * @param string $table |
||
| 1617 | * @param string|array $where |
||
| 1618 | * |
||
| 1619 | * @return false|Result false on error |
||
| 1620 | */ |
||
| 1621 | 13 | View Code Duplication | public function select($table, $where = '1=1') |
| 1642 | |||
| 1643 | /** |
||
| 1644 | * get the last error |
||
| 1645 | * |
||
| 1646 | * @return string false on error |
||
| 1647 | */ |
||
| 1648 | 1 | public function lastError() |
|
| 1652 | |||
| 1653 | /** |
||
| 1654 | * __destruct |
||
| 1655 | * |
||
| 1656 | */ |
||
| 1657 | 1 | public function __destruct() |
|
| 1664 | |||
| 1665 | /** |
||
| 1666 | * close |
||
| 1667 | */ |
||
| 1668 | 3 | public function close() |
|
| 1676 | |||
| 1677 | /** |
||
| 1678 | * prevent the instance from being cloned |
||
| 1679 | * |
||
| 1680 | * @return void |
||
| 1681 | */ |
||
| 1682 | private function __clone() |
||
| 1685 | |||
| 1686 | } |
||
| 1687 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: