Complex classes like QueryBuilder 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 QueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class QueryBuilder extends \yii\db\QueryBuilder |
||
26 | { |
||
27 | /** |
||
28 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
29 | */ |
||
30 | public $typeMap = [ |
||
31 | Schema::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
32 | Schema::TYPE_UPK => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
33 | Schema::TYPE_BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
34 | Schema::TYPE_UBIGPK => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
35 | Schema::TYPE_CHAR => 'char(1)', |
||
36 | Schema::TYPE_STRING => 'varchar(255)', |
||
37 | Schema::TYPE_TEXT => 'text', |
||
38 | Schema::TYPE_TINYINT => 'tinyint', |
||
39 | Schema::TYPE_SMALLINT => 'smallint', |
||
40 | Schema::TYPE_INTEGER => 'integer', |
||
41 | Schema::TYPE_BIGINT => 'bigint', |
||
42 | Schema::TYPE_FLOAT => 'float', |
||
43 | Schema::TYPE_DOUBLE => 'double', |
||
44 | Schema::TYPE_DECIMAL => 'decimal(10,0)', |
||
45 | Schema::TYPE_DATETIME => 'datetime', |
||
46 | Schema::TYPE_TIMESTAMP => 'timestamp', |
||
47 | Schema::TYPE_TIME => 'time', |
||
48 | Schema::TYPE_DATE => 'date', |
||
49 | Schema::TYPE_BINARY => 'blob', |
||
50 | Schema::TYPE_BOOLEAN => 'boolean', |
||
51 | Schema::TYPE_MONEY => 'decimal(19,4)', |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 400 | protected function defaultExpressionBuilders() |
|
64 | |||
65 | /** |
||
66 | * @inheritdoc |
||
67 | * @see https://stackoverflow.com/questions/15277373/sqlite-upsert-update-or-insert/15277374#15277374 |
||
68 | */ |
||
69 | 22 | public function upsert($table, $insertColumns, $updateColumns, &$params) |
|
110 | |||
111 | /** |
||
112 | * Generates a batch INSERT SQL statement. |
||
113 | * |
||
114 | * For example, |
||
115 | * |
||
116 | * ```php |
||
117 | * $connection->createCommand()->batchInsert('user', ['name', 'age'], [ |
||
118 | * ['Tom', 30], |
||
119 | * ['Jane', 20], |
||
120 | * ['Linda', 25], |
||
121 | * ])->execute(); |
||
122 | * ``` |
||
123 | * |
||
124 | * Note that the values in each row must match the corresponding column names. |
||
125 | * |
||
126 | * @param string $table the table that new rows will be inserted into. |
||
127 | * @param array $columns the column names |
||
128 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
129 | * @return string the batch INSERT SQL statement |
||
130 | */ |
||
131 | 14 | public function batchInsert($table, $columns, $rows, &$params = []) |
|
185 | |||
186 | /** |
||
187 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
188 | * The sequence will be reset such that the primary key of the next new row inserted |
||
189 | * will have the specified value or 1. |
||
190 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
191 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
192 | * the next new row's primary key will have a value 1. |
||
193 | * @return string the SQL statement for resetting sequence |
||
194 | * @throws InvalidArgumentException if the table does not exist or there is no sequence associated with the table. |
||
195 | */ |
||
196 | 5 | public function resetSequence($tableName, $value = null) |
|
218 | |||
219 | /** |
||
220 | * Enables or disables integrity check. |
||
221 | * @param bool $check whether to turn on or off the integrity check. |
||
222 | * @param string $schema the schema of the tables. Meaningless for SQLite. |
||
223 | * @param string $table the table name. Meaningless for SQLite. |
||
224 | * @return string the SQL statement for checking integrity |
||
225 | * @throws NotSupportedException this is not supported by SQLite |
||
226 | */ |
||
227 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
231 | |||
232 | /** |
||
233 | * Builds a SQL statement for truncating a DB table. |
||
234 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
235 | * @return string the SQL statement for truncating a DB table. |
||
236 | */ |
||
237 | 1 | public function truncateTable($table) |
|
241 | |||
242 | /** |
||
243 | * Builds a SQL statement for dropping an index. |
||
244 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
245 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
246 | * @return string the SQL statement for dropping an index. |
||
247 | */ |
||
248 | 2 | public function dropIndex($name, $table) |
|
252 | |||
253 | /** |
||
254 | * Builds a SQL statement for dropping a DB column. |
||
255 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
256 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
257 | * @return string the SQL statement for dropping a DB column. |
||
258 | * @throws NotSupportedException this is not supported by SQLite |
||
259 | */ |
||
260 | public function dropColumn($table, $column) |
||
264 | |||
265 | /** |
||
266 | * Builds a SQL statement for renaming a column. |
||
267 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
268 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
269 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
270 | * @return string the SQL statement for renaming a DB column. |
||
271 | * @throws NotSupportedException this is not supported by SQLite |
||
272 | */ |
||
273 | public function renameColumn($table, $oldName, $newName) |
||
277 | |||
278 | /** |
||
279 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
280 | * The method will properly quote the table and column names. |
||
281 | * @param string $name the name of the foreign key constraint. |
||
282 | * @param string $table the table that the foreign key constraint will be added to. |
||
283 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
284 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
285 | * @param string $refTable the table that the foreign key references to. |
||
286 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
287 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
288 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
289 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
290 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
291 | * @throws NotSupportedException this is not supported by SQLite |
||
292 | */ |
||
293 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
297 | |||
298 | /** |
||
299 | * Builds a SQL statement for dropping a foreign key constraint. |
||
300 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
301 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
302 | * @return string the SQL statement for dropping a foreign key constraint. |
||
303 | * @throws NotSupportedException this is not supported by SQLite |
||
304 | */ |
||
305 | public function dropForeignKey($name, $table) |
||
309 | |||
310 | /** |
||
311 | * Builds a SQL statement for renaming a DB table. |
||
312 | * |
||
313 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
314 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
315 | * @return string the SQL statement for renaming a DB table. |
||
316 | */ |
||
317 | 2 | public function renameTable($table, $newName) |
|
321 | |||
322 | /** |
||
323 | * Builds a SQL statement for changing the definition of a column. |
||
324 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
325 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
326 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
327 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
328 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
329 | * will become 'varchar(255) not null'. |
||
330 | * @return string the SQL statement for changing the definition of a column. |
||
331 | * @throws NotSupportedException this is not supported by SQLite |
||
332 | */ |
||
333 | public function alterColumn($table, $column, $type) |
||
337 | |||
338 | /** |
||
339 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
340 | * @param string $name the name of the primary key constraint. |
||
341 | * @param string $table the table that the primary key constraint will be added to. |
||
342 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
343 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
344 | * @throws NotSupportedException this is not supported by SQLite |
||
345 | */ |
||
346 | public function addPrimaryKey($name, $table, $columns) |
||
350 | |||
351 | /** |
||
352 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
353 | * @param string $name the name of the primary key constraint to be removed. |
||
354 | * @param string $table the table that the primary key constraint will be removed from. |
||
355 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
356 | * @throws NotSupportedException this is not supported by SQLite |
||
357 | */ |
||
358 | public function dropPrimaryKey($name, $table) |
||
362 | |||
363 | /** |
||
364 | * @inheritDoc |
||
365 | * @throws NotSupportedException this is not supported by SQLite. |
||
366 | */ |
||
367 | public function addUnique($name, $table, $columns) |
||
371 | |||
372 | /** |
||
373 | * @inheritDoc |
||
374 | * @throws NotSupportedException this is not supported by SQLite. |
||
375 | */ |
||
376 | public function dropUnique($name, $table) |
||
380 | |||
381 | /** |
||
382 | * @inheritDoc |
||
383 | * @throws NotSupportedException this is not supported by SQLite. |
||
384 | */ |
||
385 | public function addCheck($name, $table, $expression) |
||
389 | |||
390 | /** |
||
391 | * @inheritDoc |
||
392 | * @throws NotSupportedException this is not supported by SQLite. |
||
393 | */ |
||
394 | public function dropCheck($name, $table) |
||
398 | |||
399 | /** |
||
400 | * @inheritDoc |
||
401 | * @throws NotSupportedException this is not supported by SQLite. |
||
402 | */ |
||
403 | public function addDefaultValue($name, $table, $column, $value) |
||
407 | |||
408 | /** |
||
409 | * @inheritDoc |
||
410 | * @throws NotSupportedException this is not supported by SQLite. |
||
411 | */ |
||
412 | public function dropDefaultValue($name, $table) |
||
416 | |||
417 | /** |
||
418 | * {@inheritdoc} |
||
419 | * @throws NotSupportedException |
||
420 | * @since 2.0.8 |
||
421 | */ |
||
422 | public function addCommentOnColumn($table, $column, $comment) |
||
426 | |||
427 | /** |
||
428 | * {@inheritdoc} |
||
429 | * @throws NotSupportedException |
||
430 | * @since 2.0.8 |
||
431 | */ |
||
432 | public function addCommentOnTable($table, $comment) |
||
436 | |||
437 | /** |
||
438 | * {@inheritdoc} |
||
439 | * @throws NotSupportedException |
||
440 | * @since 2.0.8 |
||
441 | */ |
||
442 | public function dropCommentFromColumn($table, $column) |
||
446 | |||
447 | /** |
||
448 | * {@inheritdoc} |
||
449 | * @throws NotSupportedException |
||
450 | * @since 2.0.8 |
||
451 | */ |
||
452 | public function dropCommentFromTable($table) |
||
456 | |||
457 | /** |
||
458 | * {@inheritdoc} |
||
459 | */ |
||
460 | 339 | public function buildLimit($limit, $offset) |
|
476 | |||
477 | /** |
||
478 | * {@inheritdoc} |
||
479 | */ |
||
480 | 339 | public function build($query, $params = []) |
|
520 | |||
521 | /** |
||
522 | * {@inheritdoc} |
||
523 | */ |
||
524 | 339 | public function buildUnion($unions, &$params) |
|
543 | } |
||
544 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.