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 |
||
20 | class QueryBuilder extends \yii\db\QueryBuilder |
||
21 | { |
||
22 | /** |
||
23 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
24 | */ |
||
25 | public $typeMap = [ |
||
26 | Schema::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
27 | Schema::TYPE_UPK => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
28 | Schema::TYPE_BIGPK => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
29 | Schema::TYPE_UBIGPK => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
30 | Schema::TYPE_CHAR => 'char(1)', |
||
31 | Schema::TYPE_STRING => 'varchar(255)', |
||
32 | Schema::TYPE_TEXT => 'text', |
||
33 | Schema::TYPE_SMALLINT => 'smallint(6)', |
||
34 | Schema::TYPE_INTEGER => 'int(11)', |
||
35 | Schema::TYPE_BIGINT => 'bigint(20)', |
||
36 | Schema::TYPE_FLOAT => 'float', |
||
37 | Schema::TYPE_DOUBLE => 'double', |
||
38 | Schema::TYPE_DECIMAL => 'decimal(10,0)', |
||
39 | Schema::TYPE_DATETIME => 'datetime', |
||
40 | Schema::TYPE_TIMESTAMP => 'timestamp', |
||
41 | Schema::TYPE_TIME => 'time', |
||
42 | Schema::TYPE_DATE => 'date', |
||
43 | Schema::TYPE_BINARY => 'blob', |
||
44 | Schema::TYPE_BOOLEAN => 'tinyint(1)', |
||
45 | Schema::TYPE_MONEY => 'decimal(19,4)', |
||
46 | ]; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Builds a SQL statement for renaming a column. |
||
51 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
52 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
53 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
54 | * @return string the SQL statement for renaming a DB column. |
||
55 | * @throws Exception |
||
56 | */ |
||
57 | public function renameColumn($table, $oldName, $newName) |
||
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | * @see https://bugs.mysql.com/bug.php?id=48875 |
||
89 | */ |
||
90 | 1 | public function createIndex($name, $table, $columns, $unique = false) |
|
98 | |||
99 | /** |
||
100 | * Builds a SQL statement for dropping a foreign key constraint. |
||
101 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
102 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
103 | * @return string the SQL statement for dropping a foreign key constraint. |
||
104 | */ |
||
105 | public function dropForeignKey($name, $table) |
||
110 | |||
111 | /** |
||
112 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
113 | * @param string $name the name of the primary key constraint to be removed. |
||
114 | * @param string $table the table that the primary key constraint will be removed from. |
||
115 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
116 | */ |
||
117 | 1 | public function dropPrimaryKey($name, $table) |
|
121 | |||
122 | /** |
||
123 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
124 | * The sequence will be reset such that the primary key of the next new row inserted |
||
125 | * will have the specified value or 1. |
||
126 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
127 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
128 | * the next new row's primary key will have a value 1. |
||
129 | * @return string the SQL statement for resetting sequence |
||
130 | * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. |
||
131 | */ |
||
132 | 2 | public function resetSequence($tableName, $value = null) |
|
151 | |||
152 | /** |
||
153 | * Builds a SQL statement for enabling or disabling integrity check. |
||
154 | * @param bool $check whether to turn on or off the integrity check. |
||
155 | * @param string $schema the schema of the tables. Meaningless for MySQL. |
||
156 | * @param string $table the table name. Meaningless for MySQL. |
||
157 | * @return string the SQL statement for checking integrity |
||
158 | */ |
||
159 | 4 | public function checkIntegrity($check = true, $schema = '', $table = '') |
|
163 | |||
164 | /** |
||
165 | * @inheritdoc |
||
166 | */ |
||
167 | 324 | public function buildLimit($limit, $offset) |
|
184 | |||
185 | /** |
||
186 | * @inheritdoc |
||
187 | */ |
||
188 | 141 | public function insert($table, $columns, &$params) |
|
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | * @since 2.0.8 |
||
235 | */ |
||
236 | 1 | public function addCommentOnColumn($table, $column, $comment) |
|
247 | |||
248 | /** |
||
249 | * @inheritdoc |
||
250 | * @since 2.0.8 |
||
251 | */ |
||
252 | 1 | public function addCommentOnTable($table, $comment) |
|
256 | |||
257 | /** |
||
258 | * @inheritdoc |
||
259 | * @since 2.0.8 |
||
260 | */ |
||
261 | 1 | public function dropCommentFromColumn($table, $column) |
|
265 | |||
266 | /** |
||
267 | * @inheritdoc |
||
268 | * @since 2.0.8 |
||
269 | */ |
||
270 | 1 | public function dropCommentFromTable($table) |
|
274 | |||
275 | |||
276 | /** |
||
277 | * Gets column definition. |
||
278 | * |
||
279 | * @param string $table table name |
||
280 | * @param string $column column name |
||
281 | * @return null|string the column definition |
||
282 | * @throws Exception in case when table does not contain column |
||
283 | */ |
||
284 | 1 | private function getColumnDefinition($table, $column) |
|
306 | } |
||
307 |