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 |
||
23 | class QueryBuilder extends \yii\db\QueryBuilder |
||
24 | { |
||
25 | /** |
||
26 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
27 | */ |
||
28 | public $typeMap = [ |
||
29 | Schema::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
30 | Schema::TYPE_UPK => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
31 | Schema::TYPE_BIGPK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
32 | Schema::TYPE_UBIGPK => 'integer UNSIGNED PRIMARY KEY AUTOINCREMENT NOT NULL', |
||
33 | Schema::TYPE_CHAR => 'char(1)', |
||
34 | Schema::TYPE_STRING => 'varchar(255)', |
||
35 | Schema::TYPE_TEXT => 'text', |
||
36 | Schema::TYPE_SMALLINT => 'smallint', |
||
37 | Schema::TYPE_INTEGER => 'integer', |
||
38 | Schema::TYPE_BIGINT => 'bigint', |
||
39 | Schema::TYPE_FLOAT => 'float', |
||
40 | Schema::TYPE_DOUBLE => 'double', |
||
41 | Schema::TYPE_DECIMAL => 'decimal(10,0)', |
||
42 | Schema::TYPE_DATETIME => 'datetime', |
||
43 | Schema::TYPE_TIMESTAMP => 'timestamp', |
||
44 | Schema::TYPE_TIME => 'time', |
||
45 | Schema::TYPE_DATE => 'date', |
||
46 | Schema::TYPE_BINARY => 'blob', |
||
47 | Schema::TYPE_BOOLEAN => 'boolean', |
||
48 | Schema::TYPE_MONEY => 'decimal(19,4)', |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | protected $likeEscapeCharacter = '\\'; |
||
55 | |||
56 | /** |
||
57 | * Generates a batch INSERT SQL statement. |
||
58 | * For example, |
||
59 | * |
||
60 | * ```php |
||
61 | * $connection->createCommand()->batchInsert('user', ['name', 'age'], [ |
||
62 | * ['Tom', 30], |
||
63 | * ['Jane', 20], |
||
64 | * ['Linda', 25], |
||
65 | * ])->execute(); |
||
66 | * ``` |
||
67 | * |
||
68 | * Note that the values in each row must match the corresponding column names. |
||
69 | * |
||
70 | * @param string $table the table that new rows will be inserted into. |
||
71 | * @param array $columns the column names |
||
72 | * @param array $rows the rows to be batch inserted into the table |
||
73 | * @return string the batch INSERT SQL statement |
||
74 | */ |
||
75 | 10 | public function batchInsert($table, $columns, $rows) |
|
124 | |||
125 | /** |
||
126 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
127 | * The sequence will be reset such that the primary key of the next new row inserted |
||
128 | * will have the specified value or 1. |
||
129 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
130 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
131 | * the next new row's primary key will have a value 1. |
||
132 | * @return string the SQL statement for resetting sequence |
||
133 | * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. |
||
134 | */ |
||
135 | 3 | public function resetSequence($tableName, $value = null) |
|
160 | |||
161 | /** |
||
162 | * Enables or disables integrity check. |
||
163 | * @param bool $check whether to turn on or off the integrity check. |
||
164 | * @param string $schema the schema of the tables. Meaningless for SQLite. |
||
165 | * @param string $table the table name. Meaningless for SQLite. |
||
166 | * @return string the SQL statement for checking integrity |
||
167 | * @throws NotSupportedException this is not supported by SQLite |
||
168 | */ |
||
169 | 2 | public function checkIntegrity($check = true, $schema = '', $table = '') |
|
173 | |||
174 | /** |
||
175 | * Builds a SQL statement for truncating a DB table. |
||
176 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
177 | * @return string the SQL statement for truncating a DB table. |
||
178 | */ |
||
179 | 1 | public function truncateTable($table) |
|
183 | |||
184 | /** |
||
185 | * Builds a SQL statement for dropping an index. |
||
186 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
187 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
188 | * @return string the SQL statement for dropping an index. |
||
189 | */ |
||
190 | public function dropIndex($name, $table) |
||
194 | |||
195 | /** |
||
196 | * Builds a SQL statement for dropping a DB column. |
||
197 | * @param string $table the table whose column is to be dropped. The name will be properly quoted by the method. |
||
198 | * @param string $column the name of the column to be dropped. The name will be properly quoted by the method. |
||
199 | * @return string the SQL statement for dropping a DB column. |
||
200 | * @throws NotSupportedException this is not supported by SQLite |
||
201 | */ |
||
202 | public function dropColumn($table, $column) |
||
206 | |||
207 | /** |
||
208 | * Builds a SQL statement for renaming a column. |
||
209 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
210 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
211 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
212 | * @return string the SQL statement for renaming a DB column. |
||
213 | * @throws NotSupportedException this is not supported by SQLite |
||
214 | */ |
||
215 | public function renameColumn($table, $oldName, $newName) |
||
219 | |||
220 | /** |
||
221 | * Builds a SQL statement for adding a foreign key constraint to an existing table. |
||
222 | * The method will properly quote the table and column names. |
||
223 | * @param string $name the name of the foreign key constraint. |
||
224 | * @param string $table the table that the foreign key constraint will be added to. |
||
225 | * @param string|array $columns the name of the column to that the constraint will be added on. |
||
226 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
227 | * @param string $refTable the table that the foreign key references to. |
||
228 | * @param string|array $refColumns the name of the column that the foreign key references to. |
||
229 | * If there are multiple columns, separate them with commas or use an array to represent them. |
||
230 | * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
231 | * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
||
232 | * @return string the SQL statement for adding a foreign key constraint to an existing table. |
||
233 | * @throws NotSupportedException this is not supported by SQLite |
||
234 | */ |
||
235 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
239 | |||
240 | /** |
||
241 | * Builds a SQL statement for dropping a foreign key constraint. |
||
242 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
243 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
244 | * @return string the SQL statement for dropping a foreign key constraint. |
||
245 | * @throws NotSupportedException this is not supported by SQLite |
||
246 | */ |
||
247 | public function dropForeignKey($name, $table) |
||
251 | |||
252 | /** |
||
253 | * Builds a SQL statement for renaming a DB table. |
||
254 | * |
||
255 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
256 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
257 | * @return string the SQL statement for renaming a DB table. |
||
258 | */ |
||
259 | 2 | public function renameTable($table, $newName) |
|
263 | |||
264 | /** |
||
265 | * Builds a SQL statement for changing the definition of a column. |
||
266 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
267 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
268 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
269 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
270 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
271 | * will become 'varchar(255) not null'. |
||
272 | * @return string the SQL statement for changing the definition of a column. |
||
273 | * @throws NotSupportedException this is not supported by SQLite |
||
274 | */ |
||
275 | public function alterColumn($table, $column, $type) |
||
279 | |||
280 | /** |
||
281 | * Builds a SQL statement for adding a primary key constraint to an existing table. |
||
282 | * @param string $name the name of the primary key constraint. |
||
283 | * @param string $table the table that the primary key constraint will be added to. |
||
284 | * @param string|array $columns comma separated string or array of columns that the primary key will consist of. |
||
285 | * @return string the SQL statement for adding a primary key constraint to an existing table. |
||
286 | * @throws NotSupportedException this is not supported by SQLite |
||
287 | */ |
||
288 | public function addPrimaryKey($name, $table, $columns) |
||
292 | |||
293 | /** |
||
294 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
295 | * @param string $name the name of the primary key constraint to be removed. |
||
296 | * @param string $table the table that the primary key constraint will be removed from. |
||
297 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
298 | * @throws NotSupportedException this is not supported by SQLite |
||
299 | */ |
||
300 | public function dropPrimaryKey($name, $table) |
||
304 | |||
305 | /** |
||
306 | * @inheritdoc |
||
307 | * @throws NotSupportedException |
||
308 | * @since 2.0.8 |
||
309 | */ |
||
310 | public function addCommentOnColumn($table, $column, $comment) |
||
314 | |||
315 | /** |
||
316 | * @inheritdoc |
||
317 | * @throws NotSupportedException |
||
318 | * @since 2.0.8 |
||
319 | */ |
||
320 | public function addCommentOnTable($table, $comment) |
||
324 | |||
325 | /** |
||
326 | * @inheritdoc |
||
327 | * @throws NotSupportedException |
||
328 | * @since 2.0.8 |
||
329 | */ |
||
330 | public function dropCommentFromColumn($table, $column) |
||
334 | |||
335 | /** |
||
336 | * @inheritdoc |
||
337 | * @throws NotSupportedException |
||
338 | * @since 2.0.8 |
||
339 | */ |
||
340 | public function dropCommentFromTable($table) |
||
344 | |||
345 | /** |
||
346 | * @inheritdoc |
||
347 | */ |
||
348 | 241 | public function buildLimit($limit, $offset) |
|
364 | |||
365 | /** |
||
366 | * @inheritdoc |
||
367 | * @throws NotSupportedException if `$columns` is an array |
||
368 | */ |
||
369 | 2 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
|
376 | |||
377 | /** |
||
378 | * Builds SQL for IN condition |
||
379 | * |
||
380 | * @param string $operator |
||
381 | * @param array $columns |
||
382 | * @param array $values |
||
383 | * @param array $params |
||
384 | * @return string SQL |
||
385 | */ |
||
386 | 5 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
409 | |||
410 | /** |
||
411 | * @inheritdoc |
||
412 | */ |
||
413 | 241 | public function build($query, $params = []) |
|
453 | |||
454 | /** |
||
455 | * @inheritdoc |
||
456 | */ |
||
457 | 241 | public function buildUnion($unions, &$params) |
|
476 | } |
||
477 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.