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 |
||
22 | class QueryBuilder extends \yii\db\QueryBuilder |
||
23 | { |
||
24 | public $typeMap = [ |
||
25 | Schema::TYPE_PK => 'integer NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) PRIMARY KEY', |
||
26 | Schema::TYPE_BIGPK => 'bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) PRIMARY KEY', |
||
27 | Schema::TYPE_STRING => 'varchar(255)', |
||
28 | Schema::TYPE_TEXT => 'clob', |
||
29 | Schema::TYPE_SMALLINT => 'smallint', |
||
30 | Schema::TYPE_INTEGER => 'integer', |
||
31 | Schema::TYPE_BIGINT => 'bigint', |
||
32 | Schema::TYPE_FLOAT => 'float', |
||
33 | Schema::TYPE_DOUBLE => 'double', |
||
34 | Schema::TYPE_DECIMAL => 'decimal(10,0)', |
||
35 | Schema::TYPE_DATETIME => 'timestamp', |
||
36 | Schema::TYPE_TIMESTAMP => 'timestamp', |
||
37 | Schema::TYPE_TIME => 'time', |
||
38 | Schema::TYPE_DATE => 'date', |
||
39 | Schema::TYPE_BINARY => 'blob', |
||
40 | Schema::TYPE_BOOLEAN => 'smallint', |
||
41 | Schema::TYPE_MONEY => 'decimal(19,4)', |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * Builds a SQL statement for truncating a DB table. |
||
46 | * @param string $table the table to be truncated. The name will be properly quoted by the method. |
||
47 | * @return string the SQL statement for truncating a DB table. |
||
48 | */ |
||
49 | public function truncateTable($table) |
||
53 | |||
54 | /** |
||
55 | * @inheritdoc |
||
56 | */ |
||
57 | public function resetSequence($tableName, $value = null) |
||
75 | |||
76 | /** |
||
77 | * Builds a SQL statement for enabling or disabling integrity check. |
||
78 | * @param boolean $check whether to turn on or off the integrity check. |
||
79 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
80 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
81 | * @return string the SQL statement for checking integrity |
||
82 | * @throws \yii\base\NotSupportedException if this is not supported by the underlying DBMS |
||
83 | * @see http://www-01.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0000998.html?cp=SSEPGG_10.5.0%2F2-12-7-227 |
||
84 | */ |
||
85 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | 97 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
|
163 | |||
164 | /** |
||
165 | * @inheritdoc |
||
166 | */ |
||
167 | 97 | public function buildLimit($limit, $offset) |
|
183 | |||
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | public function alterColumn($table, $column, $type) |
||
193 | |||
194 | /** |
||
195 | * @inheritdoc |
||
196 | */ |
||
197 | 4 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
|
221 | |||
222 | /** |
||
223 | * @inheritdoc |
||
224 | */ |
||
225 | public function insert($table, $columns, &$params) |
||
257 | |||
258 | /** |
||
259 | * Creates a SELECT EXISTS() SQL statement. |
||
260 | * @param string $rawSql the subquery in a raw form to select from. |
||
261 | * @return string the SELECT EXISTS() SQL statement. |
||
262 | * |
||
263 | * @since 2.0.8 |
||
264 | */ |
||
265 | public function selectExists($rawSql) |
||
269 | |||
270 | /** |
||
271 | * Builds a SQL command for adding comment to column |
||
272 | * |
||
273 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
274 | * @param string $column the name of the column to be commented. The column name will be properly quoted by the method. |
||
275 | * @return string the SQL statement for adding comment on column |
||
276 | * @since 2.0.8 |
||
277 | */ |
||
278 | public function dropCommentFromColumn($table, $column) |
||
282 | |||
283 | /** |
||
284 | * Builds a SQL command for adding comment to table |
||
285 | * |
||
286 | * @param string $table the table whose column is to be commented. The table name will be properly quoted by the method. |
||
287 | * @return string the SQL statement for adding comment on column |
||
288 | * @since 2.0.8 |
||
289 | */ |
||
290 | public function dropCommentFromTable($table) |
||
294 | } |
||
295 |