Complex classes like MySqlDb 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 MySqlDb, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class MySqlDb extends Db { |
||
| 19 | const MYSQL_DATE_FORMAT = 'Y-m-d H:i:s'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $dbname; |
||
| 25 | |||
| 26 | protected static $map = [ |
||
| 27 | Db::OP_GT => '>', |
||
| 28 | Db::OP_GTE => '>=', |
||
| 29 | Db::OP_LT => '<', |
||
| 30 | Db::OP_LTE => '<=', |
||
| 31 | Db::OP_LIKE => 'like', |
||
| 32 | Db::OP_AND => 'and', |
||
| 33 | Db::OP_OR => 'or', |
||
| 34 | ]; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | 7 | protected function dropTableDb($table, array $options = []) { |
|
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | 10 | protected function fetchTableDefDb($table) { |
|
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | 6 | protected function fetchColumnDefsDb($table) { |
|
| 73 | 6 | $rows = $this->get( |
|
| 74 | 6 | new Identifier('information_schema', 'COLUMNS'), |
|
| 75 | [ |
||
| 76 | 6 | 'TABLE_SCHEMA' => $this->getDbName(), |
|
| 77 | 6 | 'TABLE_NAME' => $this->prefixTable($table, false) |
|
| 78 | ], |
||
| 79 | [ |
||
| 80 | 6 | Db::OPTION_FETCH_MODE => PDO::FETCH_ASSOC, |
|
| 81 | 'order' => ['TABLE_NAME', 'ORDINAL_POSITION'] |
||
| 82 | ] |
||
| 83 | ); |
||
| 84 | |||
| 85 | 6 | $columns = []; |
|
| 86 | 6 | foreach ($rows as $row) { |
|
| 87 | 6 | $columnType = $row['COLUMN_TYPE']; |
|
| 88 | 6 | if ($columnType === 'tinyint(1)') { |
|
| 89 | 1 | $columnType = 'bool'; |
|
| 90 | } |
||
| 91 | 6 | $column = Db::typeDef($columnType); |
|
| 92 | 6 | if ($column === null) { |
|
| 93 | throw new \Exception("Unknown type '$columnType'.", 500); |
||
| 94 | } |
||
| 95 | |||
| 96 | 6 | $column['allowNull'] = strcasecmp($row['IS_NULLABLE'], 'YES') === 0; |
|
| 97 | |||
| 98 | 6 | if (($default = $row['COLUMN_DEFAULT']) !== null) { |
|
| 99 | 4 | $column['default'] = $this->forceType($default, $column['type']); |
|
| 100 | } |
||
| 101 | |||
| 102 | 6 | if ($row['EXTRA'] === 'auto_increment') { |
|
| 103 | 1 | $column['autoIncrement'] = true; |
|
| 104 | } |
||
| 105 | |||
| 106 | 6 | if ($row['COLUMN_KEY'] === 'PRI') { |
|
| 107 | 3 | $column['primary'] = true; |
|
| 108 | } |
||
| 109 | |||
| 110 | 6 | $columns[$row['COLUMN_NAME']] = $column; |
|
| 111 | } |
||
| 112 | |||
| 113 | 6 | return $columns; |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | 78 | public function get($table, array $where, array $options = []) { |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Build a sql select statement. |
||
| 127 | * |
||
| 128 | * @param string|Identifier $table The name of the main table. |
||
| 129 | * @param array $where The where filter. |
||
| 130 | * @param array $options An array of additional query options. |
||
| 131 | * @return string Returns the select statement as a string. |
||
| 132 | * @see Db::get() |
||
| 133 | */ |
||
| 134 | 82 | protected function buildSelect($table, array $where, array $options = []) { |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Build a where clause from a where array. |
||
| 196 | * |
||
| 197 | * @param array $where There where string. |
||
| 198 | * This is an array in the form `['column' => 'value']` with more advanced options for non-equality comparisons. |
||
| 199 | * @param string $op The logical operator to join multiple field comparisons. |
||
| 200 | * @return string The where string. |
||
| 201 | */ |
||
| 202 | 84 | protected function buildWhere($where, $op = Db::OP_AND) { |
|
| 203 | 84 | $map = static::$map; |
|
| 204 | 84 | $strop = $map[$op]; |
|
| 205 | |||
| 206 | 84 | $result = ''; |
|
| 207 | 84 | foreach ($where as $column => $value) { |
|
| 208 | 55 | $btcolumn = $this->escape($column); |
|
| 209 | |||
| 210 | 55 | if (is_array($value)) { |
|
| 211 | 32 | if (is_numeric($column)) { |
|
| 212 | // This is a bracketed expression. |
||
| 213 | 4 | $result .= (empty($result) ? '' : "\n $strop "). |
|
| 214 | 4 | "(\n ". |
|
| 215 | 4 | $this->buildWhere($value, $op). |
|
| 216 | 4 | "\n )"; |
|
| 217 | 32 | } elseif (in_array($column, [Db::OP_AND, Db::OP_OR])) { |
|
| 218 | // This is an AND/OR expression. |
||
| 219 | 4 | $result .= (empty($result) ? '' : "\n $strop "). |
|
| 220 | 4 | "(\n ". |
|
| 221 | 4 | $this->buildWhere($value, $column). |
|
| 222 | 4 | "\n )"; |
|
| 223 | } else { |
||
| 224 | 28 | if (isset($value[0])) { |
|
| 225 | // This is a short in syntax. |
||
| 226 | 2 | $value = [Db::OP_IN => $value]; |
|
| 227 | } |
||
| 228 | |||
| 229 | 28 | foreach ($value as $vop => $rval) { |
|
| 230 | 28 | if ($result) { |
|
| 231 | 6 | $result .= "\n $strop "; |
|
| 232 | } |
||
| 233 | |||
| 234 | switch ($vop) { |
||
| 235 | 28 | case Db::OP_AND: |
|
| 236 | 28 | case Db::OP_OR: |
|
| 237 | 4 | if (is_numeric($column)) { |
|
| 238 | $innerWhere = $rval; |
||
| 239 | } else { |
||
| 240 | 4 | $innerWhere = [$column => $rval]; |
|
| 241 | } |
||
| 242 | $result .= "(\n ". |
||
| 243 | 4 | $this->buildWhere($innerWhere, $vop). |
|
|
|
|||
| 244 | 4 | "\n )"; |
|
| 245 | 4 | break; |
|
| 246 | 28 | case Db::OP_EQ: |
|
| 247 | 6 | if ($rval === null) { |
|
| 248 | $result .= "$btcolumn is null"; |
||
| 249 | 6 | } elseif (is_array($rval)) { |
|
| 250 | 2 | $result .= "$btcolumn in ".$this->bracketList($rval); |
|
| 251 | } else { |
||
| 252 | 4 | $result .= "$btcolumn = ".$this->quote($rval); |
|
| 253 | } |
||
| 254 | 6 | break; |
|
| 255 | 24 | case Db::OP_GT: |
|
| 256 | 22 | case Db::OP_GTE: |
|
| 257 | 20 | case Db::OP_LT: |
|
| 258 | 14 | case Db::OP_LTE: |
|
| 259 | 12 | $result .= "$btcolumn {$map[$vop]} ".$this->quote($rval); |
|
| 260 | 12 | break; |
|
| 261 | 12 | case Db::OP_LIKE: |
|
| 262 | 2 | $result .= $this->buildLike($btcolumn, $rval); |
|
| 263 | 2 | break; |
|
| 264 | 10 | case Db::OP_IN: |
|
| 265 | // Quote the in values. |
||
| 266 | 4 | $rval = array_map([$this, 'quote'], (array)$rval); |
|
| 267 | 4 | $result .= "$btcolumn in (".implode(', ', $rval).')'; |
|
| 268 | 4 | break; |
|
| 269 | 6 | case Db::OP_NEQ: |
|
| 270 | 6 | if ($rval === null) { |
|
| 271 | 2 | $result .= "$btcolumn is not null"; |
|
| 272 | 4 | } elseif (is_array($rval)) { |
|
| 273 | 2 | $result .= "$btcolumn not in ".$this->bracketList($rval); |
|
| 274 | } else { |
||
| 275 | 2 | $result .= "$btcolumn <> ".$this->quote($rval); |
|
| 276 | } |
||
| 277 | 32 | break; |
|
| 278 | } |
||
| 279 | } |
||
| 280 | } |
||
| 281 | } else { |
||
| 282 | 29 | if ($result) { |
|
| 283 | 10 | $result .= "\n $strop "; |
|
| 284 | } |
||
| 285 | |||
| 286 | // This is just an equality operator. |
||
| 287 | 29 | if ($value === null) { |
|
| 288 | 2 | $result .= "$btcolumn is null"; |
|
| 289 | } else { |
||
| 290 | 55 | $result .= "$btcolumn = ".$this->quote($value); |
|
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | 84 | return $result; |
|
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Build a like expression. |
||
| 299 | * |
||
| 300 | * @param string $column The column name. |
||
| 301 | * @param mixed $value The right-hand value. |
||
| 302 | * @return string Returns the like expression. |
||
| 303 | * @internal param bool $quotevals Whether or not to quote the values. |
||
| 304 | */ |
||
| 305 | 1 | protected function buildLike($column, $value) { |
|
| 306 | 1 | return "$column like ".$this->quote($value); |
|
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Convert an array into a bracketed list suitable for MySQL clauses. |
||
| 311 | * |
||
| 312 | * @param array $row The row to expand. |
||
| 313 | * @param string $quote The quotes to surroud the items with. There are two special cases. |
||
| 314 | * ' (single quote) |
||
| 315 | * : The row will be passed through {@link PDO::quote()}. |
||
| 316 | * ` (backticks) |
||
| 317 | * : The row will be passed through {@link MySqlDb::backtick()}. |
||
| 318 | * @return string Returns the bracket list. |
||
| 319 | */ |
||
| 320 | 75 | public function bracketList($row, $quote = "'") { |
|
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * Get the current database name. |
||
| 338 | * |
||
| 339 | * @return mixed |
||
| 340 | */ |
||
| 341 | 6 | private function getDbName() { |
|
| 342 | 6 | if (!isset($this->dbname)) { |
|
| 343 | $this->dbname = $this->getPDO()->query('select database()')->fetchColumn(); |
||
| 344 | } |
||
| 345 | 6 | return $this->dbname; |
|
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * {@inheritdoc} |
||
| 350 | */ |
||
| 351 | 18 | protected function nativeDbType(array $type) { |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Parse a column type string and return it in a way that is suitable for a create/alter table statement. |
||
| 370 | * |
||
| 371 | * @param string $typeString The string to parse. |
||
| 372 | * @return string Returns a canonical string. |
||
| 373 | */ |
||
| 374 | protected function columnTypeString($typeString) { |
||
| 375 | $type = null; |
||
| 376 | |||
| 377 | if (substr($typeString, 0, 4) === 'enum') { |
||
| 378 | // This is an enum which will come in as an array. |
||
| 379 | if (preg_match_all("`'([^']+)'`", $typeString, $matches)) { |
||
| 380 | $type = $matches[1]; |
||
| 381 | } |
||
| 382 | } else { |
||
| 383 | if (preg_match('`([a-z]+)\s*(?:\((\d+(?:\s*,\s*\d+)*)\))?\s*(unsigned)?`', $typeString, $matches)) { |
||
| 384 | // var_dump($matches); |
||
| 385 | $str = $matches[1]; |
||
| 386 | $length = self::val(2, $matches); |
||
| 387 | $unsigned = self::val(3, $matches); |
||
| 388 | |||
| 389 | if (substr($str, 0, 1) == 'u') { |
||
| 390 | $unsigned = true; |
||
| 391 | $str = substr($str, 1); |
||
| 392 | } |
||
| 393 | |||
| 394 | // Remove the length from types without real lengths. |
||
| 395 | if (in_array($str, array('tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double'))) { |
||
| 396 | $length = null; |
||
| 397 | } |
||
| 398 | |||
| 399 | $type = $str; |
||
| 400 | if ($length) { |
||
| 401 | $length = str_replace(' ', '', $length); |
||
| 402 | $type .= "($length)"; |
||
| 403 | } |
||
| 404 | if ($unsigned) { |
||
| 405 | $type .= ' unsigned'; |
||
| 406 | } |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | if (!$type) { |
||
| 411 | debug_print_backtrace(); |
||
| 412 | trigger_error("Couldn't parse type $typeString", E_USER_ERROR); |
||
| 413 | } |
||
| 414 | |||
| 415 | return $type; |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Get the indexes from the database. |
||
| 420 | * |
||
| 421 | * @param string $table The name of the table to get the indexes for. |
||
| 422 | * @return array|null |
||
| 423 | */ |
||
| 424 | 5 | protected function fetchIndexesDb($table = '') { |
|
| 460 | |||
| 461 | /** |
||
| 462 | * {@inheritdoc} |
||
| 463 | */ |
||
| 464 | 1 | protected function fetchTableNamesDb() { |
|
| 465 | // Get the table names. |
||
| 466 | 1 | $tables = $this->get( |
|
| 467 | 1 | new Identifier('information_schema', 'TABLES'), |
|
| 468 | [ |
||
| 469 | 1 | 'TABLE_SCHEMA' => $this->getDbName(), |
|
| 470 | 1 | 'TABLE_NAME' => [Db::OP_LIKE => $this->escapeLike($this->getPx()).'%'] |
|
| 471 | ], |
||
| 472 | [ |
||
| 473 | 1 | 'columns' => ['TABLE_NAME'], |
|
| 474 | 'fetchMode' => PDO::FETCH_ASSOC |
||
| 475 | ] |
||
| 476 | ); |
||
| 477 | |||
| 478 | 1 | return $tables->fetchAll(PDO::FETCH_COLUMN); |
|
| 479 | } |
||
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | 17 | public function insert($table, array $row, array $options = []) { |
|
| 485 | 17 | $sql = $this->buildInsert($table, $row, $options); |
|
| 486 | 17 | $id = $this->queryID($sql, [], $options); |
|
| 487 | 17 | if (is_numeric($id)) { |
|
| 488 | 17 | return (int)$id; |
|
| 489 | } else { |
||
| 490 | return $id; |
||
| 491 | } |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Build an insert statement. |
||
| 496 | * |
||
| 497 | * @param string|Identifier $table The name of the table to insert to. |
||
| 498 | * @param array $row The row to insert. |
||
| 499 | * @param array $options An array of options for the insert. See {@link Db::insert} for the options. |
||
| 500 | * @return string Returns the the sql string of the insert statement. |
||
| 501 | */ |
||
| 502 | 33 | protected function buildInsert($table, array $row, $options = []) { |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Build an upsert statement. |
||
| 524 | * |
||
| 525 | * An upsert statement is an insert on duplicate key statement in MySQL. |
||
| 526 | * |
||
| 527 | * @param string|Identifier $table The name of the table to update. |
||
| 528 | * @param array $row The row to insert or update. |
||
| 529 | * @param array $options An array of additional query options. |
||
| 530 | * @return string Returns the upsert statement as a string. |
||
| 531 | */ |
||
| 532 | 2 | protected function buildUpsert($table, array $row, $options = []) { |
|
| 546 | |||
| 547 | /** |
||
| 548 | * {@inheritdoc} |
||
| 549 | */ |
||
| 550 | 54 | public function load($table, $rows, array $options = []) { |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Make a valid PDO parameter name from a string. |
||
| 579 | * |
||
| 580 | * This method replaces invalid placeholder characters with underscores. |
||
| 581 | * |
||
| 582 | * @param string $name The name to replace. |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | 44 | protected function paramName($name) { |
|
| 589 | |||
| 590 | /** |
||
| 591 | * {@inheritdoc} |
||
| 592 | */ |
||
| 593 | 8 | public function update($table, array $set, array $where, array $options = []) { |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Build a sql update statement. |
||
| 602 | * |
||
| 603 | * @param string|Identifier $table The name of the table to update. |
||
| 604 | * @param array $set An array of columns to set. |
||
| 605 | * @param array $where The where filter. |
||
| 606 | * @param array $options Additional options for the query. |
||
| 607 | * @return string Returns the update statement as a string. |
||
| 608 | */ |
||
| 609 | 3 | protected function buildUpdate($table, array $set, array $where, array $options = []) { |
|
| 629 | |||
| 630 | /** |
||
| 631 | * {@inheritdoc} |
||
| 632 | */ |
||
| 633 | 30 | public function delete($table, array $where, array $options = []) { |
|
| 634 | 30 | if (self::val(Db::OPTION_TRUNCATE, $options)) { |
|
| 635 | if (!empty($where)) { |
||
| 636 | throw new \InvalidArgumentException("You cannot truncate $table with a where filter.", 500); |
||
| 637 | } |
||
| 638 | $sql = 'truncate table '.$this->prefixTable($table); |
||
| 639 | } else { |
||
| 640 | 30 | $sql = 'delete from '.$this->prefixTable($table); |
|
| 641 | |||
| 642 | 30 | if (!empty($where)) { |
|
| 643 | $sql .= "\nwhere ".$this->buildWhere($where); |
||
| 644 | } |
||
| 645 | } |
||
| 646 | 30 | return $this->queryModify($sql, [], $options); |
|
| 647 | } |
||
| 648 | |||
| 649 | /** |
||
| 650 | * {@inheritdoc} |
||
| 651 | */ |
||
| 652 | 18 | protected function createTableDb(array $tableDef, array $options = []) { |
|
| 653 | 18 | $table = $tableDef['name']; |
|
| 654 | |||
| 655 | // The table doesn't exist so this is a create table. |
||
| 656 | 18 | $parts = array(); |
|
| 657 | 18 | foreach ($tableDef['columns'] as $name => $cdef) { |
|
| 658 | 18 | $parts[] = $this->columnDefString($name, $cdef); |
|
| 659 | } |
||
| 660 | |||
| 661 | 18 | foreach (self::val('indexes', $tableDef, []) as $index) { |
|
| 662 | 17 | $indexDef = $this->indexDefString($table, $index); |
|
| 663 | 17 | if (!empty($indexDef)) { |
|
| 664 | 17 | $parts[] = $indexDef; |
|
| 665 | } |
||
| 666 | } |
||
| 667 | |||
| 668 | 18 | $tableName = $this->prefixTable($table); |
|
| 669 | 18 | $sql = "create table $tableName (\n ". |
|
| 670 | 18 | implode(",\n ", $parts). |
|
| 671 | 18 | "\n)"; |
|
| 672 | |||
| 673 | 18 | if (self::val('collate', $options)) { |
|
| 674 | $sql .= "\n collate {$options['collate']}"; |
||
| 675 | } |
||
| 676 | |||
| 677 | 18 | $this->queryDefine($sql, $options); |
|
| 678 | 18 | } |
|
| 679 | |||
| 680 | /** |
||
| 681 | * Construct a column definition string from an array defintion. |
||
| 682 | * |
||
| 683 | * @param string $name The name of the column. |
||
| 684 | * @param array $cdef The column definition. |
||
| 685 | * @return string Returns a string representing the column definition. |
||
| 686 | */ |
||
| 687 | 18 | protected function columnDefString($name, array $cdef) { |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Return the SDL string that defines an index. |
||
| 707 | * |
||
| 708 | * @param string $table The name of the table that the index is on. |
||
| 709 | * @param array $def The index definition. This definition should have the following keys. |
||
| 710 | * |
||
| 711 | * columns |
||
| 712 | * : An array of columns in the index. |
||
| 713 | * type |
||
| 714 | * : One of "index", "unique", or "primary". |
||
| 715 | * @return null|string Returns the index string or null if the index is not correct. |
||
| 716 | */ |
||
| 717 | 17 | protected function indexDefString($table, array $def) { |
|
| 729 | |||
| 730 | /** |
||
| 731 | * {@inheritdoc} |
||
| 732 | */ |
||
| 733 | 5 | protected function alterTableDb(array $alterDef, array $options = []) { |
|
| 734 | 5 | $table = $alterDef['name']; |
|
| 735 | 5 | $columnOrders = $this->getColumnOrders($alterDef['def']['columns']); |
|
| 736 | 5 | $parts = []; |
|
| 737 | |||
| 738 | // Add the columns and indexes. |
||
| 739 | 5 | foreach ($alterDef['add']['columns'] as $cname => $cdef) { |
|
| 740 | // Figure out the order of the column. |
||
| 741 | 3 | $pos = self::val($cname, $columnOrders, ''); |
|
| 742 | 3 | $parts[] = 'add '.$this->columnDefString($cname, $cdef).$pos; |
|
| 743 | } |
||
| 744 | 5 | foreach ($alterDef['add']['indexes'] as $ixdef) { |
|
| 745 | 3 | $parts[] = 'add '.$this->indexDefString($table, $ixdef); |
|
| 746 | } |
||
| 747 | |||
| 748 | // Alter the columns. |
||
| 749 | 5 | foreach ($alterDef['alter']['columns'] as $cname => $cdef) { |
|
| 750 | 2 | $parts[] = 'modify '.$this->columnDefString($cname, $cdef); |
|
| 751 | } |
||
| 752 | |||
| 753 | // Drop the columns and indexes. |
||
| 754 | 5 | foreach ($alterDef['drop']['columns'] as $cname => $_) { |
|
| 755 | $parts[] = 'drop '.$this->escape($cname); |
||
| 756 | } |
||
| 757 | 5 | foreach ($alterDef['drop']['indexes'] as $ixdef) { |
|
| 758 | 2 | $parts[] = 'drop index '.$this->escape($ixdef['name']); |
|
| 759 | } |
||
| 760 | |||
| 761 | 5 | if (empty($parts)) { |
|
| 762 | return false; |
||
| 763 | } |
||
| 764 | |||
| 765 | $sql = 'alter '. |
||
| 766 | 5 | (self::val(Db::OPTION_IGNORE, $options) ? 'ignore ' : ''). |
|
| 767 | 5 | 'table '.$this->prefixTable($table)."\n ". |
|
| 768 | 5 | implode(",\n ", $parts); |
|
| 769 | |||
| 770 | 5 | $this->queryDefine($sql, $options); |
|
| 771 | 5 | } |
|
| 772 | |||
| 773 | /** |
||
| 774 | * Get an array of column orders so that added columns can be slotted into their correct spot. |
||
| 775 | * |
||
| 776 | * @param array $cdefs An array of column definitions. |
||
| 777 | * @return array Returns an array of column orders suitable for an `alter table` statement. |
||
| 778 | */ |
||
| 779 | 5 | private function getColumnOrders($cdefs) { |
|
| 789 | |||
| 790 | /** |
||
| 791 | * Force a value into the appropriate php type based on its SQL type. |
||
| 792 | * |
||
| 793 | * @param mixed $value The value to force. |
||
| 794 | * @param string $type The sqlite type name. |
||
| 795 | * @return mixed Returns $value cast to the appropriate type. |
||
| 796 | */ |
||
| 797 | 4 | protected function forceType($value, $type) { |
|
| 798 | 4 | $type = strtolower($type); |
|
| 799 | |||
| 800 | 4 | if ($type === 'null') { |
|
| 801 | return null; |
||
| 802 | 4 | } elseif ($type === 'boolean') { |
|
| 803 | 1 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
| 804 | 3 | } elseif (in_array($type, ['int', 'integer', 'tinyint', 'smallint', |
|
| 805 | 'mediumint', 'bigint', 'unsigned big int', 'int2', 'int8', 'boolean'])) { |
||
| 806 | 3 | return filter_var($value, FILTER_VALIDATE_INT); |
|
| 807 | } elseif (in_array($type, ['real', 'double', 'double precision', 'float', |
||
| 808 | 'numeric', 'number', 'decimal(10,5)'])) { |
||
| 809 | return filter_var($value, FILTER_VALIDATE_FLOAT); |
||
| 810 | } else { |
||
| 811 | return (string)$value; |
||
| 812 | } |
||
| 813 | } |
||
| 814 | |||
| 815 | 42 | public function quote($value, $column = '') { |
|
| 816 | 42 | if (is_bool($value)) { |
|
| 817 | 1 | return (string)(int)$value; |
|
| 818 | 42 | } elseif ($value instanceof \DateTimeInterface) { |
|
| 819 | 1 | $value = $value->format(self::MYSQL_DATE_FORMAT); |
|
| 820 | } |
||
| 821 | |||
| 822 | 42 | return parent::quote($value, $column); |
|
| 823 | } |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Convert a value into something usable as a PDO parameter. |
||
| 827 | * |
||
| 828 | * @param mixed $value The value to convert. |
||
| 829 | * @return mixed Returns the converted value or the value itself if it's fine. |
||
| 830 | */ |
||
| 831 | 44 | private function argValue($value) { |
|
| 840 | } |
||
| 841 |
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.