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 | 6 | protected function alterTableDb(array $alterDef, array $options = []) { |
|
23 | 6 | $this->alterTableMigrate($alterDef, $options); |
|
24 | 6 | } |
|
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 | 6 | private function alterTableMigrate(array $alterDef, array $options = []) { |
|
33 | 6 | $table = $alterDef['name']; |
|
34 | 6 | $currentDef = $this->fetchTableDef($table); |
|
35 | |||
36 | // Merge the table definitions if we aren't dropping stuff. |
||
37 | 6 | if (!self::val(Db::OPTION_DROP, $options)) { |
|
38 | 5 | $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 | 6 | 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 | 6 | $tmpTable = $table.'_'.time(); |
|
51 | |||
52 | // Rename the current table. |
||
53 | 6 | $this->renameTable($table, $tmpTable); |
|
54 | |||
55 | // Create the new table. |
||
56 | 6 | $this->createTableDb($tableDef, $options); |
|
57 | |||
58 | // Figure out the columns that we can insert. |
||
59 | 6 | $columns = array_keys(array_intersect_key($tableDef['columns'], $currentDef['columns'])); |
|
60 | |||
61 | // Build the insert/select statement. |
||
62 | 6 | $sql = 'insert into '.$this->prefixTable($table)."\n". |
|
63 | 6 | $this->bracketList($columns, '`')."\n". |
|
64 | 6 | $this->buildSelect($tmpTable, [], ['columns' => $columns]); |
|
65 | |||
66 | 6 | $this->queryDefine($sql); |
|
67 | |||
68 | // Drop the temp table. |
||
69 | 6 | $this->dropTable($tmpTable); |
|
70 | 6 | } |
|
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 | 6 | private function renameTable($old, $new) { |
|
79 | $renameSql = 'alter table '. |
||
80 | 6 | $this->prefixTable($old). |
|
81 | 6 | ' rename to '. |
|
82 | 6 | $this->prefixTable($new); |
|
83 | 6 | $this->queryDefine($renameSql); |
|
84 | 6 | } |
|
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 | 5 | private function mergeTableDefs(array $tableDef, array $alterDef) { |
|
94 | 5 | $result = $tableDef; |
|
95 | |||
96 | 5 | 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 | 5 | $result['columns'] = array_merge($result['columns'], $alterDef['def']['columns']); |
|
109 | 5 | $result['indexes'] = array_merge($result['indexes'], $alterDef['add']['indexes']); |
|
110 | |||
111 | 5 | 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 | 29 | protected function buildInsert($table, array $row, $options = []) { |
|
129 | 29 | if (self::val(Db::OPTION_UPSERT, $options)) { |
|
130 | throw new \Exception("Upsert is not supported."); |
||
131 | 29 | } elseif (self::val(Db::OPTION_IGNORE, $options)) { |
|
132 | 2 | $sql = 'insert or ignore into '; |
|
133 | 28 | } elseif (self::val(Db::OPTION_REPLACE, $options)) { |
|
134 | 2 | $sql = 'insert or replace into '; |
|
135 | } else { |
||
136 | 27 | $sql = 'insert into '; |
|
137 | } |
||
138 | 29 | $sql .= $this->prefixTable($table); |
|
139 | |||
140 | // Add the list of values. |
||
141 | $sql .= |
||
142 | 29 | "\n".$this->bracketList(array_keys($row), '`'). |
|
143 | 29 | "\nvalues".$this->bracketList($row, "'"); |
|
144 | |||
145 | 29 | return $sql; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | 1 | protected function buildLike(string $column, $value): string { |
|
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 = []): string { |
|
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 | 13 | protected function columnDefString($name, array $cdef) { |
|
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | 13 | protected function nativeDbType(array $type) { |
|
221 | 13 | static $translations = ['bool' => 'boolean', 'byte' => 'tinyint', 'short' => 'smallint', 'long' => 'bigint']; |
|
222 | |||
223 | // Translate the dbtype to a MySQL native type. |
||
224 | 13 | if (isset($translations[$type['dbtype']])) { |
|
250 | |||
251 | /** |
||
252 | * {@inheritdoc} |
||
253 | */ |
||
254 | 13 | protected function createTableDb(array $tableDef, array $options = []) { |
|
292 | |||
293 | /** |
||
294 | * Create an index. |
||
295 | * |
||
296 | * @param string $table The name of the table to create the index on. |
||
297 | * @param array $indexDef The index definition. |
||
298 | * @param array $options Additional options for the index creation. |
||
299 | */ |
||
300 | 7 | public function createIndex($table, array $indexDef, $options = []) { |
|
312 | |||
313 | /** |
||
314 | * Force a value into the appropriate php type based on its Sqlite type. |
||
315 | * |
||
316 | * @param mixed $value The value to force. |
||
317 | * @param string $type The sqlite type name. |
||
318 | * @return mixed Returns $value cast to the appropriate type. |
||
319 | */ |
||
320 | 3 | protected function forceType($value, $type) { |
|
335 | |||
336 | /** |
||
337 | * Get the columns for a table.. |
||
338 | * |
||
339 | * @param string $table The table to get the columns for. |
||
340 | * @return array|null Returns an array of columns. |
||
341 | */ |
||
342 | 6 | protected function fetchColumnDefsDb(string $table) { |
|
380 | |||
381 | /** |
||
382 | * Get the indexes for a table. |
||
383 | * |
||
384 | * @param string $table The name of the table to get the indexes for or an empty string to get all indexes. |
||
385 | * @return array|null |
||
386 | */ |
||
387 | 5 | protected function fetchIndexesDb($table = '') { |
|
412 | |||
413 | /** |
||
414 | * Get the primary or secondary keys from the given rows. |
||
415 | * |
||
416 | * @param string $table The name of the table. |
||
417 | * @param array $row The row to examine. |
||
418 | * @param bool $quick Whether or not to quickly look for <tablename>ID for the primary key. |
||
419 | * @return array|null Returns the primary keys and values from {@link $rows} or null if the primary key isn't found. |
||
420 | */ |
||
421 | 2 | private function getPKValue($table, array $row, $quick = false) { |
|
440 | |||
441 | /** |
||
442 | * Get the all of table names in the database. |
||
443 | * |
||
444 | * @return array Returns an array of table names. |
||
445 | */ |
||
446 | 1 | protected function fetchTableNamesDb() { |
|
466 | |||
467 | /** |
||
468 | * {@inheritdoc} |
||
469 | */ |
||
470 | 9 | public function insert(string $table, array $row, array $options = []) { |
|
499 | |||
500 | /** |
||
501 | * Optionally quote a where value. |
||
502 | * |
||
503 | * @param mixed $value The value to quote. |
||
504 | * @param string $column The name of the column being operated on. |
||
505 | * @return string Returns the value, optionally quoted. |
||
506 | * @internal param bool $quote Whether or not to quote the value. |
||
507 | */ |
||
508 | 37 | public function quote($value, string $column = ''): string { |
|
524 | } |
||
525 |
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.