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 |
||
24 | class QueryBuilder extends \yii\db\QueryBuilder |
||
25 | { |
||
26 | /** |
||
27 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
28 | */ |
||
29 | public $typeMap = [ |
||
30 | Schema::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
31 | Schema::TYPE_UPK => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
32 | Schema::TYPE_BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
33 | Schema::TYPE_UBIGPK => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
34 | Schema::TYPE_CHAR => 'char(1)', |
||
35 | Schema::TYPE_STRING => 'varchar(255)', |
||
36 | Schema::TYPE_TEXT => 'text', |
||
37 | Schema::TYPE_TINYINT => 'tinyint', |
||
38 | Schema::TYPE_SMALLINT => 'smallint', |
||
39 | Schema::TYPE_INTEGER => 'integer', |
||
40 | Schema::TYPE_BIGINT => 'bigint', |
||
41 | Schema::TYPE_FLOAT => 'float', |
||
42 | Schema::TYPE_DOUBLE => 'double', |
||
43 | Schema::TYPE_DECIMAL => 'decimal(10,0)', |
||
44 | Schema::TYPE_DATETIME => 'datetime', |
||
45 | Schema::TYPE_TIMESTAMP => 'timestamp', |
||
46 | Schema::TYPE_TIME => 'time', |
||
47 | Schema::TYPE_DATE => 'date', |
||
48 | Schema::TYPE_BINARY => 'blob', |
||
49 | Schema::TYPE_BOOLEAN => 'boolean', |
||
50 | Schema::TYPE_MONEY => 'decimal(19,4)', |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * {@inheritdoc} |
||
55 | */ |
||
56 | 400 | protected function defaultExpressionBuilders() |
|
63 | |||
64 | /** |
||
65 | * @inheritdoc |
||
66 | * @see https://stackoverflow.com/questions/15277373/sqlite-upsert-update-or-insert/15277374#15277374 |
||
67 | */ |
||
68 | 22 | public function upsert($table, $insertColumns, $updateColumns, &$params) |
|
109 | |||
110 | /** |
||
111 | * Generates a batch INSERT SQL statement. |
||
112 | * |
||
113 | * For example, |
||
114 | * |
||
115 | * ```php |
||
116 | * $connection->createCommand()->batchInsert('user', ['name', 'age'], [ |
||
117 | * ['Tom', 30], |
||
118 | * ['Jane', 20], |
||
119 | * ['Linda', 25], |
||
120 | * ])->execute(); |
||
121 | * ``` |
||
122 | * |
||
123 | * Note that the values in each row must match the corresponding column names. |
||
124 | * |
||
125 | * @param string $table the table that new rows will be inserted into. |
||
126 | * @param array $columns the column names |
||
127 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
128 | * @return string the batch INSERT SQL statement |
||
129 | */ |
||
130 | 14 | public function batchInsert($table, $columns, $rows) |
|
182 | |||
183 | /** |
||
184 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
185 | * The sequence will be reset such that the primary key of the next new row inserted |
||
186 | * will have the specified value or 1. |
||
187 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
188 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
189 | * the next new row's primary key will have a value 1. |
||
190 | * @return string the SQL statement for resetting sequence |
||
191 | * @throws InvalidArgumentException if the table does not exist or there is no sequence associated with the table. |
||
192 | */ |
||
193 | 5 | public function resetSequence($tableName, $value = null) |
|
215 | |||
216 | /** |
||
217 | * Enables or disables integrity check. |
||
218 | * @param bool $check whether to turn on or off the integrity check. |
||
219 | * @param string $schema the schema of the tables. Meaningless for SQLite. |
||
220 | * @param string $table the table name. Meaningless for SQLite. |
||
221 | * @return string the SQL statement for checking integrity |
||
222 | * @throws NotSupportedException this is not supported by SQLite |
||
223 | */ |
||
224 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
228 | |||
229 | /** |
||
230 | * Builds a SQL statement for truncating a DB table. |
||
231 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
232 | * @return string the SQL statement for truncating a DB table. |
||
233 | */ |
||
234 | 1 | public function truncateTable($table) |
|
238 | |||
239 | /** |
||
240 | * Builds a SQL statement for dropping an index. |
||
241 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
242 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
243 | * @return string the SQL statement for dropping an index. |
||
244 | */ |
||
245 | 2 | public function dropIndex($name, $table) |
|
249 | |||
250 | /** |
||
251 | * Builds a SQL statement for dropping a DB column. |
||
252 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
253 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
254 | * @return string the SQL statement for dropping a DB column. |
||
255 | * @throws NotSupportedException this is not supported by SQLite |
||
256 | */ |
||
257 | public function dropColumn($table, $column) |
||
261 | |||
262 | /** |
||
263 | * Builds a SQL statement for renaming a column. |
||
264 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
265 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
266 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
267 | * @return string the SQL statement for renaming a DB column. |
||
268 | * @throws NotSupportedException this is not supported by SQLite |
||
269 | */ |
||
270 | public function renameColumn($table, $oldName, $newName) |
||
274 | |||
275 | /** |
||
276 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
277 | * The method will properly quote the table and column names. |
||
278 | * @param string $name the name of the foreign key constraint. |
||
279 | * @param string $table the table that the foreign key constraint will be added to. |
||
280 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
281 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
282 | * @param string $refTable the table that the foreign key references to. |
||
283 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
284 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
285 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
286 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
287 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
288 | * @throws NotSupportedException this is not supported by SQLite |
||
289 | */ |
||
290 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
294 | |||
295 | /** |
||
296 | * Builds a SQL statement for dropping a foreign key constraint. |
||
297 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
298 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
299 | * @return string the SQL statement for dropping a foreign key constraint. |
||
300 | * @throws NotSupportedException this is not supported by SQLite |
||
301 | */ |
||
302 | public function dropForeignKey($name, $table) |
||
306 | |||
307 | /** |
||
308 | * Builds a SQL statement for renaming a DB table. |
||
309 | * |
||
310 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
311 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
312 | * @return string the SQL statement for renaming a DB table. |
||
313 | */ |
||
314 | 2 | public function renameTable($table, $newName) |
|
318 | |||
319 | /** |
||
320 | * Builds a SQL statement for changing the definition of a column. |
||
321 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
322 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
323 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
324 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
325 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
326 | * will become 'varchar(255) not null'. |
||
327 | * @return string the SQL statement for changing the definition of a column. |
||
328 | * @throws NotSupportedException this is not supported by SQLite |
||
329 | */ |
||
330 | public function alterColumn($table, $column, $type) |
||
334 | |||
335 | /** |
||
336 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
337 | * @param string $name the name of the primary key constraint. |
||
338 | * @param string $table the table that the primary key constraint will be added to. |
||
339 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
340 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
341 | * @throws NotSupportedException this is not supported by SQLite |
||
342 | */ |
||
343 | public function addPrimaryKey($name, $table, $columns) |
||
347 | |||
348 | /** |
||
349 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
350 | * @param string $name the name of the primary key constraint to be removed. |
||
351 | * @param string $table the table that the primary key constraint will be removed from. |
||
352 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
353 | * @throws NotSupportedException this is not supported by SQLite |
||
354 | */ |
||
355 | public function dropPrimaryKey($name, $table) |
||
359 | |||
360 | /** |
||
361 | * @inheritDoc |
||
362 | * @throws NotSupportedException this is not supported by SQLite. |
||
363 | */ |
||
364 | public function addUnique($name, $table, $columns) |
||
368 | |||
369 | /** |
||
370 | * @inheritDoc |
||
371 | * @throws NotSupportedException this is not supported by SQLite. |
||
372 | */ |
||
373 | public function dropUnique($name, $table) |
||
377 | |||
378 | /** |
||
379 | * @inheritDoc |
||
380 | * @throws NotSupportedException this is not supported by SQLite. |
||
381 | */ |
||
382 | public function addCheck($name, $table, $expression) |
||
386 | |||
387 | /** |
||
388 | * @inheritDoc |
||
389 | * @throws NotSupportedException this is not supported by SQLite. |
||
390 | */ |
||
391 | public function dropCheck($name, $table) |
||
395 | |||
396 | /** |
||
397 | * @inheritDoc |
||
398 | * @throws NotSupportedException this is not supported by SQLite. |
||
399 | */ |
||
400 | public function addDefaultValue($name, $table, $column, $value) |
||
404 | |||
405 | /** |
||
406 | * @inheritDoc |
||
407 | * @throws NotSupportedException this is not supported by SQLite. |
||
408 | */ |
||
409 | public function dropDefaultValue($name, $table) |
||
413 | |||
414 | /** |
||
415 | * {@inheritdoc} |
||
416 | * @throws NotSupportedException |
||
417 | * @since 2.0.8 |
||
418 | */ |
||
419 | public function addCommentOnColumn($table, $column, $comment) |
||
423 | |||
424 | /** |
||
425 | * {@inheritdoc} |
||
426 | * @throws NotSupportedException |
||
427 | * @since 2.0.8 |
||
428 | */ |
||
429 | public function addCommentOnTable($table, $comment) |
||
433 | |||
434 | /** |
||
435 | * {@inheritdoc} |
||
436 | * @throws NotSupportedException |
||
437 | * @since 2.0.8 |
||
438 | */ |
||
439 | public function dropCommentFromColumn($table, $column) |
||
443 | |||
444 | /** |
||
445 | * {@inheritdoc} |
||
446 | * @throws NotSupportedException |
||
447 | * @since 2.0.8 |
||
448 | */ |
||
449 | public function dropCommentFromTable($table) |
||
453 | |||
454 | /** |
||
455 | * {@inheritdoc} |
||
456 | */ |
||
457 | 339 | public function buildLimit($limit, $offset) |
|
473 | |||
474 | /** |
||
475 | * {@inheritdoc} |
||
476 | */ |
||
477 | 339 | public function build($query, $params = []) |
|
502 | |||
503 | /** |
||
504 | * {@inheritdoc} |
||
505 | */ |
||
506 | 339 | public function buildUnion($unions, &$params) |
|
525 | } |
||
526 |
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.