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 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 400 | protected function defaultExpressionBuilders() |
|
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | * @see https://stackoverflow.com/questions/15277373/sqlite-upsert-update-or-insert/15277374#15277374 |
||
69 | */ |
||
70 | 22 | public function upsert($table, $insertColumns, $updateColumns, &$params) |
|
111 | |||
112 | /** |
||
113 | * Generates a batch INSERT SQL statement. |
||
114 | * |
||
115 | * For example, |
||
116 | * |
||
117 | * ```php |
||
118 | * $connection->createCommand()->batchInsert('user', ['name', 'age'], [ |
||
119 | * ['Tom', 30], |
||
120 | * ['Jane', 20], |
||
121 | * ['Linda', 25], |
||
122 | * ])->execute(); |
||
123 | * ``` |
||
124 | * |
||
125 | * Note that the values in each row must match the corresponding column names. |
||
126 | * |
||
127 | * @param string $table the table that new rows will be inserted into. |
||
128 | * @param array $columns the column names |
||
129 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
130 | * @return string the batch INSERT SQL statement |
||
131 | */ |
||
132 | 14 | public function batchInsert($table, $columns, $rows, &$params = []) |
|
186 | |||
187 | /** |
||
188 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
189 | * The sequence will be reset such that the primary key of the next new row inserted |
||
190 | * will have the specified value or 1. |
||
191 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
192 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
193 | * the next new row's primary key will have a value 1. |
||
194 | * @return string the SQL statement for resetting sequence |
||
195 | * @throws InvalidArgumentException if the table does not exist or there is no sequence associated with the table. |
||
196 | */ |
||
197 | 5 | public function resetSequence($tableName, $value = null) |
|
219 | |||
220 | /** |
||
221 | * Enables or disables integrity check. |
||
222 | * @param bool $check whether to turn on or off the integrity check. |
||
223 | * @param string $schema the schema of the tables. Meaningless for SQLite. |
||
224 | * @param string $table the table name. Meaningless for SQLite. |
||
225 | * @return string the SQL statement for checking integrity |
||
226 | * @throws NotSupportedException this is not supported by SQLite |
||
227 | */ |
||
228 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
232 | |||
233 | /** |
||
234 | * Builds a SQL statement for truncating a DB table. |
||
235 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
236 | * @return string the SQL statement for truncating a DB table. |
||
237 | */ |
||
238 | 1 | public function truncateTable($table) |
|
242 | |||
243 | /** |
||
244 | * Builds a SQL statement for dropping an index. |
||
245 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
246 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
247 | * @return string the SQL statement for dropping an index. |
||
248 | */ |
||
249 | 2 | public function dropIndex($name, $table) |
|
253 | |||
254 | /** |
||
255 | * Builds a SQL statement for dropping a DB column. |
||
256 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
257 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
258 | * @return string the SQL statement for dropping a DB column. |
||
259 | * @throws NotSupportedException this is not supported by SQLite |
||
260 | */ |
||
261 | public function dropColumn($table, $column) |
||
265 | |||
266 | /** |
||
267 | * Builds a SQL statement for renaming a column. |
||
268 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
269 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
270 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
271 | * @return string the SQL statement for renaming a DB column. |
||
272 | * @throws NotSupportedException this is not supported by SQLite |
||
273 | */ |
||
274 | public function renameColumn($table, $oldName, $newName) |
||
278 | |||
279 | /** |
||
280 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
281 | * The method will properly quote the table and column names. |
||
282 | * @param string $name the name of the foreign key constraint. |
||
283 | * @param string $table the table that the foreign key constraint will be added to. |
||
284 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
285 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
286 | * @param string $refTable the table that the foreign key references to. |
||
287 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
288 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
289 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
290 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
291 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
292 | * @throws NotSupportedException this is not supported by SQLite |
||
293 | */ |
||
294 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
298 | |||
299 | /** |
||
300 | * Builds a SQL statement for dropping a foreign key constraint. |
||
301 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
302 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
303 | * @return string the SQL statement for dropping a foreign key constraint. |
||
304 | * @throws NotSupportedException this is not supported by SQLite |
||
305 | */ |
||
306 | public function dropForeignKey($name, $table) |
||
310 | |||
311 | /** |
||
312 | * Builds a SQL statement for renaming a DB table. |
||
313 | * |
||
314 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
315 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
316 | * @return string the SQL statement for renaming a DB table. |
||
317 | */ |
||
318 | 2 | public function renameTable($table, $newName) |
|
322 | |||
323 | /** |
||
324 | * Builds a SQL statement for changing the definition of a column. |
||
325 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
326 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
327 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
328 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
329 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
330 | * will become 'varchar(255) not null'. |
||
331 | * @return string the SQL statement for changing the definition of a column. |
||
332 | * @throws NotSupportedException this is not supported by SQLite |
||
333 | */ |
||
334 | public function alterColumn($table, $column, $type) |
||
338 | |||
339 | /** |
||
340 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
341 | * @param string $name the name of the primary key constraint. |
||
342 | * @param string $table the table that the primary key constraint will be added to. |
||
343 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
344 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
345 | * @throws NotSupportedException this is not supported by SQLite |
||
346 | */ |
||
347 | public function addPrimaryKey($name, $table, $columns) |
||
351 | |||
352 | /** |
||
353 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
354 | * @param string $name the name of the primary key constraint to be removed. |
||
355 | * @param string $table the table that the primary key constraint will be removed from. |
||
356 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
357 | * @throws NotSupportedException this is not supported by SQLite |
||
358 | */ |
||
359 | public function dropPrimaryKey($name, $table) |
||
363 | |||
364 | /** |
||
365 | * {@inheritdoc} |
||
366 | * @throws NotSupportedException this is not supported by SQLite. |
||
367 | */ |
||
368 | public function addUnique($name, $table, $columns) |
||
372 | |||
373 | /** |
||
374 | * {@inheritdoc} |
||
375 | * @throws NotSupportedException this is not supported by SQLite. |
||
376 | */ |
||
377 | public function dropUnique($name, $table) |
||
381 | |||
382 | /** |
||
383 | * {@inheritdoc} |
||
384 | * @throws NotSupportedException this is not supported by SQLite. |
||
385 | */ |
||
386 | public function addCheck($name, $table, $expression) |
||
390 | |||
391 | /** |
||
392 | * {@inheritdoc} |
||
393 | * @throws NotSupportedException this is not supported by SQLite. |
||
394 | */ |
||
395 | public function dropCheck($name, $table) |
||
399 | |||
400 | /** |
||
401 | * {@inheritdoc} |
||
402 | * @throws NotSupportedException this is not supported by SQLite. |
||
403 | */ |
||
404 | public function addDefaultValue($name, $table, $column, $value) |
||
408 | |||
409 | /** |
||
410 | * {@inheritdoc} |
||
411 | * @throws NotSupportedException this is not supported by SQLite. |
||
412 | */ |
||
413 | public function dropDefaultValue($name, $table) |
||
417 | |||
418 | /** |
||
419 | * {@inheritdoc} |
||
420 | * @throws NotSupportedException |
||
421 | * @since 2.0.8 |
||
422 | */ |
||
423 | public function addCommentOnColumn($table, $column, $comment) |
||
427 | |||
428 | /** |
||
429 | * {@inheritdoc} |
||
430 | * @throws NotSupportedException |
||
431 | * @since 2.0.8 |
||
432 | */ |
||
433 | public function addCommentOnTable($table, $comment) |
||
437 | |||
438 | /** |
||
439 | * {@inheritdoc} |
||
440 | * @throws NotSupportedException |
||
441 | * @since 2.0.8 |
||
442 | */ |
||
443 | public function dropCommentFromColumn($table, $column) |
||
447 | |||
448 | /** |
||
449 | * {@inheritdoc} |
||
450 | * @throws NotSupportedException |
||
451 | * @since 2.0.8 |
||
452 | */ |
||
453 | public function dropCommentFromTable($table) |
||
457 | |||
458 | /** |
||
459 | * {@inheritdoc} |
||
460 | */ |
||
461 | 339 | public function buildLimit($limit, $offset) |
|
477 | |||
478 | /** |
||
479 | * {@inheritdoc} |
||
480 | */ |
||
481 | 339 | public function build($query, $params = []) |
|
506 | |||
507 | /** |
||
508 | * {@inheritdoc} |
||
509 | */ |
||
510 | 339 | public function buildUnion($unions, &$params) |
|
529 | } |
||
530 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.