Complex classes like SqliteDb 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 SqliteDb, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class SqliteDb extends MySqlDb { |
||
| 19 | /** |
||
| 20 | * {@inheritdoc} |
||
| 21 | */ |
||
| 22 | 5 | protected function alterTableDb(array $alterDef, array $options = []) { |
|
| 23 | 5 | $this->alterTableMigrate($alterDef, $options); |
|
| 24 | 5 | } |
|
| 25 | |||
| 26 | /** |
||
| 27 | * Alter a table by creating a new table and copying the old table's data to it. |
||
| 28 | * |
||
| 29 | * @param array $alterDef The new definition. |
||
| 30 | * @param array $options An array of options for the migration. |
||
| 31 | */ |
||
| 32 | 5 | private function alterTableMigrate(array $alterDef, array $options = []) { |
|
| 33 | 5 | $table = $alterDef['name']; |
|
| 34 | 5 | $currentDef = $this->fetchTableDef($table); |
|
| 35 | |||
| 36 | // Merge the table definitions if we aren't dropping stuff. |
||
| 37 | 5 | if (!self::val(Db::OPTION_DROP, $options)) { |
|
| 38 | 4 | $tableDef = $this->mergeTableDefs($currentDef, $alterDef); |
|
|
|
|||
| 39 | } else { |
||
| 40 | 1 | $tableDef = $alterDef['def']; |
|
| 41 | } |
||
| 42 | |||
| 43 | // Drop all of the indexes on the current table. |
||
| 44 | 5 | foreach (self::val('indexes', $currentDef, []) as $indexDef) { |
|
| 45 | 4 | if (self::val('type', $indexDef, Db::INDEX_IX) === Db::INDEX_IX) { |
|
| 46 | 4 | $this->dropIndex($indexDef['name']); |
|
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | 5 | $tmpTable = $table.'_'.time(); |
|
| 51 | |||
| 52 | // Rename the current table. |
||
| 53 | 5 | $this->renameTable($table, $tmpTable); |
|
| 54 | |||
| 55 | // Create the new table. |
||
| 56 | 5 | $this->createTableDb($tableDef, $options); |
|
| 57 | |||
| 58 | // Figure out the columns that we can insert. |
||
| 59 | 5 | $columns = array_keys(array_intersect_key($tableDef['columns'], $currentDef['columns'])); |
|
| 60 | |||
| 61 | // Build the insert/select statement. |
||
| 62 | 5 | $sql = 'insert into '.$this->prefixTable($table)."\n". |
|
| 63 | 5 | $this->bracketList($columns, '`')."\n". |
|
| 64 | 5 | $this->buildSelect($tmpTable, [], ['columns' => $columns]); |
|
| 65 | |||
| 66 | 5 | $this->queryDefine($sql); |
|
| 67 | |||
| 68 | // Drop the temp table. |
||
| 69 | 5 | $this->dropTable($tmpTable); |
|
| 70 | 5 | } |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Rename a table. |
||
| 74 | * |
||
| 75 | * @param string $old The old name of the table. |
||
| 76 | * @param string $new The new name of the table. |
||
| 77 | */ |
||
| 78 | 5 | private function renameTable($old, $new) { |
|
| 79 | $renameSql = 'alter table '. |
||
| 80 | 5 | $this->prefixTable($old). |
|
| 81 | 5 | ' rename to '. |
|
| 82 | 5 | $this->prefixTable($new); |
|
| 83 | 5 | $this->queryDefine($renameSql); |
|
| 84 | 5 | } |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Merge a table def with its alter def so that no columns/indexes are lost in an alter. |
||
| 88 | * |
||
| 89 | * @param array $tableDef The table def. |
||
| 90 | * @param array $alterDef The alter def. |
||
| 91 | * @return array The new table def. |
||
| 92 | */ |
||
| 93 | 4 | private function mergeTableDefs(array $tableDef, array $alterDef) { |
|
| 94 | 4 | $result = $tableDef; |
|
| 95 | |||
| 96 | 4 | if ($this->findPrimaryKeyIndex($alterDef['add']['indexes'])) { |
|
| 97 | 2 | $remove = null; |
|
| 98 | 2 | foreach ($result['indexes'] as $i => $index) { |
|
| 99 | 2 | if ($index['type'] === Db::INDEX_PK) { |
|
| 100 | 2 | $remove = $i; |
|
| 101 | } |
||
| 102 | } |
||
| 103 | 2 | if ($remove !== null) { |
|
| 104 | 2 | unset($result['indexes'][$i]); |
|
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | 4 | $result['columns'] = array_merge($result['columns'], $alterDef['def']['columns']); |
|
| 109 | 4 | $result['indexes'] = array_merge($result['indexes'], $alterDef['add']['indexes']); |
|
| 110 | |||
| 111 | 4 | return $result; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Drop an index. |
||
| 116 | * |
||
| 117 | * @param string $index The name of the index to drop. |
||
| 118 | */ |
||
| 119 | 2 | protected function dropIndex($index) { |
|
| 120 | $sql = 'drop index if exists '. |
||
| 121 | 2 | $this->escape($index); |
|
| 122 | 2 | $this->queryDefine($sql); |
|
| 123 | 2 | } |
|
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | 28 | protected function buildInsert($table, array $row, $options = []) { |
|
| 129 | 28 | if (self::val(Db::OPTION_UPSERT, $options)) { |
|
| 130 | throw new \Exception("Upsert is not supported."); |
||
| 131 | 28 | } elseif (self::val(Db::OPTION_IGNORE, $options)) { |
|
| 132 | 2 | $sql = 'insert or ignore into '; |
|
| 133 | 27 | } elseif (self::val(Db::OPTION_REPLACE, $options)) { |
|
| 134 | 2 | $sql = 'insert or replace into '; |
|
| 135 | } else { |
||
| 136 | 26 | $sql = 'insert into '; |
|
| 137 | } |
||
| 138 | 28 | $sql .= $this->prefixTable($table); |
|
| 139 | |||
| 140 | // Add the list of values. |
||
| 141 | $sql .= |
||
| 142 | 28 | "\n".$this->bracketList(array_keys($row), '`'). |
|
| 143 | 28 | "\nvalues".$this->bracketList($row, "'"); |
|
| 144 | |||
| 145 | 28 | return $sql; |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | 1 | protected function buildLike($column, $value) { |
|
| 152 | 1 | return "$column like ".$this->quote($value)." escape '\\'"; |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | 5 | protected function buildUpdate($table, array $set, array $where, array $options = []) { |
|
| 159 | $sql = 'update '. |
||
| 160 | 5 | (empty($options[Db::OPTION_IGNORE]) ? '' : 'or ignore '). |
|
| 161 | 5 | $this->prefixTable($table). |
|
| 162 | 5 | "\nset\n "; |
|
| 163 | |||
| 164 | 5 | $parts = []; |
|
| 165 | 5 | foreach ($set as $key => $value) { |
|
| 166 | 5 | $escapedKey = $this->escape($key); |
|
| 167 | 5 | $parts[] = "$escapedKey = ".$this->quote($value, $escapedKey); |
|
| 168 | } |
||
| 169 | 5 | $sql .= implode(",\n ", $parts); |
|
| 170 | |||
| 171 | 5 | if (!empty($where)) { |
|
| 172 | 5 | $sql .= "\nwhere ".$this->buildWhere($where, Db::OP_AND); |
|
| 173 | } |
||
| 174 | |||
| 175 | 5 | return $sql; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Construct a column definition string from an array defintion. |
||
| 180 | * |
||
| 181 | * @param string $name The name of the column. |
||
| 182 | * @param array $cdef The column definition. |
||
| 183 | * @return string Returns a string representing the column definition. |
||
| 184 | */ |
||
| 185 | 12 | protected function columnDefString($name, array $cdef) { |
|
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | 12 | protected function nativeDbType(array $type) { |
|
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | 12 | protected function createTableDb(array $tableDef, array $options = []) { |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Create an index. |
||
| 285 | * |
||
| 286 | * @param string $table The name of the table to create the index on. |
||
| 287 | * @param array $indexDef The index definition. |
||
| 288 | * @param array $options Additional options for the index creation. |
||
| 289 | */ |
||
| 290 | 7 | public function createIndex($table, array $indexDef, $options = []) { |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Force a value into the appropriate php type based on its Sqlite type. |
||
| 305 | * |
||
| 306 | * @param mixed $value The value to force. |
||
| 307 | * @param string $type The sqlite type name. |
||
| 308 | * @return mixed Returns $value cast to the appropriate type. |
||
| 309 | */ |
||
| 310 | 3 | protected function forceType($value, $type) { |
|
| 311 | 3 | $type = strtolower($type); |
|
| 312 | |||
| 313 | 3 | if ($type === 'null') { |
|
| 314 | return null; |
||
| 315 | 3 | } elseif (in_array($type, ['int', 'integer', 'tinyint', 'smallint', |
|
| 316 | 'mediumint', 'bigint', 'unsigned big int', 'int2', 'int8', 'boolean'])) { |
||
| 317 | 3 | return (int)filter_var($value, FILTER_VALIDATE_INT); |
|
| 318 | } elseif (in_array($type, ['real', 'double', 'double precision', 'float', |
||
| 319 | 'numeric', 'decimal(10,5)'])) { |
||
| 320 | return filter_var($value, FILTER_VALIDATE_FLOAT); |
||
| 321 | } else { |
||
| 322 | return (string)$value; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get the columns for a table.. |
||
| 328 | * |
||
| 329 | * @param string $table The table to get the columns for. |
||
| 330 | * @return array|null Returns an array of columns. |
||
| 331 | */ |
||
| 332 | 5 | protected function fetchColumnDefsDb($table) { |
|
| 333 | 5 | $cdefs = $this->query('pragma table_info('.$this->prefixTable($table, false).')')->fetchAll(PDO::FETCH_ASSOC); |
|
| 334 | 5 | if (empty($cdefs)) { |
|
| 335 | 3 | return null; |
|
| 336 | } |
||
| 337 | |||
| 338 | 5 | $columns = []; |
|
| 339 | 5 | $pk = []; |
|
| 340 | 5 | foreach ($cdefs as $cdef) { |
|
| 341 | 5 | $column = Db::typeDef($cdef['type']); |
|
| 342 | 5 | if ($column === null) { |
|
| 343 | throw new \Exception("Unknown type '$columnType'.", 500); |
||
| 344 | } |
||
| 345 | 5 | $column['allowNull'] = !filter_var($cdef['notnull'], FILTER_VALIDATE_BOOLEAN); |
|
| 346 | |||
| 347 | 5 | if ($cdef['pk']) { |
|
| 348 | 2 | $pk[] = $cdef['name']; |
|
| 349 | 2 | if (strcasecmp($cdef['type'], 'integer') === 0) { |
|
| 350 | $column['autoIncrement'] = true; |
||
| 351 | } else { |
||
| 352 | 2 | $column['primary'] = true; |
|
| 353 | } |
||
| 354 | } |
||
| 355 | 5 | if ($cdef['dflt_value'] !== null) { |
|
| 356 | 3 | $column['default'] = $this->forceType($cdef['dflt_value'], $column['type']); |
|
| 357 | } |
||
| 358 | 5 | $columns[$cdef['name']] = $column; |
|
| 359 | } |
||
| 360 | // $tdef = ['columns' => $columns]; |
||
| 361 | // if (!empty($pk)) { |
||
| 362 | // $tdef['indexes'][Db::INDEX_PK] = [ |
||
| 363 | // 'columns' => $pk, |
||
| 364 | // 'type' => Db::INDEX_PK |
||
| 365 | // ]; |
||
| 366 | // } |
||
| 367 | // $this->tables[$table] = $tdef; |
||
| 368 | 5 | return $columns; |
|
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get the indexes for a table. |
||
| 373 | * |
||
| 374 | * @param string $table The name of the table to get the indexes for or an empty string to get all indexes. |
||
| 375 | * @return array|null |
||
| 376 | */ |
||
| 377 | 5 | protected function fetchIndexesDb($table = '') { |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Get the primary or secondary keys from the given rows. |
||
| 405 | * |
||
| 406 | * @param string $table The name of the table. |
||
| 407 | * @param array $row The row to examine. |
||
| 408 | * @param bool $quick Whether or not to quickly look for <tablename>ID for the primary key. |
||
| 409 | * @return array|null Returns the primary keys and values from {@link $rows} or null if the primary key isn't found. |
||
| 410 | */ |
||
| 411 | 2 | private function getPKValue($table, array $row, $quick = false) { |
|
| 412 | 2 | if ($quick && isset($row[$table.'ID'])) { |
|
| 413 | 1 | return [$table.'ID' => $row[$table.'ID']]; |
|
| 414 | } |
||
| 415 | |||
| 416 | 1 | $tdef = $this->fetchTableDef($table); |
|
| 417 | 1 | $cols = []; |
|
| 418 | 1 | foreach ($tdef['columns'] as $name => $cdef) { |
|
| 419 | 1 | if (empty($cdef['primary'])) { |
|
| 420 | 1 | break; |
|
| 421 | } |
||
| 422 | 1 | if (!array_key_exists($name, $row)) { |
|
| 423 | return null; |
||
| 424 | } |
||
| 425 | |||
| 426 | 1 | $cols[$name] = $row[$name]; |
|
| 427 | } |
||
| 428 | 1 | return $cols; |
|
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get the all of table names in the database. |
||
| 433 | * |
||
| 434 | * @return array Returns an array of table names. |
||
| 435 | */ |
||
| 436 | 1 | protected function fetchTableNamesDb() { |
|
| 437 | // Get the table names. |
||
| 438 | 1 | $tables = $this->get( |
|
| 439 | 1 | new Identifier('sqlite_master'), |
|
| 440 | [ |
||
| 441 | 1 | 'type' => 'table', |
|
| 442 | 1 | 'name' => [Db::OP_LIKE => $this->escapeLike($this->getPx()).'%'] |
|
| 443 | ], |
||
| 444 | [ |
||
| 445 | 1 | 'columns' => ['name'] |
|
| 446 | ] |
||
| 447 | 1 | )->fetchAll(PDO::FETCH_COLUMN); |
|
| 448 | |||
| 449 | // Remove internal tables. |
||
| 450 | 1 | $tables = array_filter($tables, function ($name) { |
|
| 451 | 1 | return substr($name, 0, 7) !== 'sqlite_'; |
|
| 452 | 1 | }); |
|
| 453 | |||
| 454 | 1 | return $tables; |
|
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * {@inheritdoc} |
||
| 459 | */ |
||
| 460 | 8 | public function insert($table, array $row, array $options = []) { |
|
| 461 | // Sqlite doesn't support upsert so do upserts manually. |
||
| 462 | 8 | if (self::val(Db::OPTION_UPSERT, $options)) { |
|
| 463 | 2 | unset($options[Db::OPTION_UPSERT]); |
|
| 464 | |||
| 465 | 2 | $keys = $this->getPKValue($table, $row, true); |
|
| 466 | 2 | if (empty($keys)) { |
|
| 467 | throw new \Exception("Cannot upsert with no key.", 500); |
||
| 468 | } |
||
| 469 | // Try updating first. |
||
| 470 | 2 | $updated = $this->update( |
|
| 471 | 2 | $table, |
|
| 472 | 2 | array_diff_key($row, $keys), |
|
| 473 | 2 | $keys, |
|
| 474 | 2 | $options |
|
| 475 | ); |
||
| 476 | 2 | if ($updated) { |
|
| 477 | // Updated. |
||
| 478 | 2 | if (count($keys) === 1) { |
|
| 479 | 1 | return array_pop($keys); |
|
| 480 | } else { |
||
| 481 | 1 | return true; |
|
| 482 | } |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | 8 | $result = parent::insert($table, $row, $options); |
|
| 487 | 8 | return $result; |
|
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Optionally quote a where value. |
||
| 492 | * |
||
| 493 | * @param mixed $value The value to quote. |
||
| 494 | * @param string $column The name of the column being operated on. |
||
| 495 | * @return string Returns the value, optionally quoted. |
||
| 496 | * @internal param bool $quote Whether or not to quote the value. |
||
| 497 | */ |
||
| 498 | 36 | public function quote($value, $column = '') { |
|
| 514 | } |
||
| 515 |
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.