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 | /** |
||
25 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
26 | */ |
||
27 | public $typeMap = [ |
||
28 | Schema::TYPE_PK => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
29 | Schema::TYPE_UPK => 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
30 | Schema::TYPE_BIGPK => 'bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
31 | Schema::TYPE_UBIGPK => 'bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||
32 | Schema::TYPE_CHAR => 'char(1)', |
||
33 | Schema::TYPE_STRING => 'varchar(255)', |
||
34 | Schema::TYPE_TEXT => 'text', |
||
35 | Schema::TYPE_TINYINT => 'tinyint(3)', |
||
36 | Schema::TYPE_SMALLINT => 'smallint(6)', |
||
37 | Schema::TYPE_INTEGER => 'int(11)', |
||
38 | Schema::TYPE_BIGINT => 'bigint(20)', |
||
39 | Schema::TYPE_FLOAT => 'float', |
||
40 | Schema::TYPE_DOUBLE => 'double', |
||
41 | Schema::TYPE_DECIMAL => 'decimal(10,0)', |
||
42 | Schema::TYPE_DATE => 'date', |
||
43 | Schema::TYPE_BINARY => 'blob', |
||
44 | Schema::TYPE_BOOLEAN => 'tinyint(1)', |
||
45 | Schema::TYPE_MONEY => 'decimal(19,4)', |
||
46 | Schema::TYPE_JSON => 'json' |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | 509 | public function init() |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 509 | protected function defaultExpressionBuilders() |
|
68 | |||
69 | /** |
||
70 | * Builds a SQL statement for renaming a column. |
||
71 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
72 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
73 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
74 | * @return string the SQL statement for renaming a DB column. |
||
75 | * @throws Exception |
||
76 | */ |
||
77 | public function renameColumn($table, $oldName, $newName) |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | * @see https://bugs.mysql.com/bug.php?id=48875 |
||
109 | */ |
||
110 | 12 | public function createIndex($name, $table, $columns, $unique = false) |
|
118 | |||
119 | /** |
||
120 | * Builds a SQL statement for dropping a foreign key constraint. |
||
121 | * @param string $name the name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
||
122 | * @param string $table the table whose foreign is to be dropped. The name will be properly quoted by the method. |
||
123 | * @return string the SQL statement for dropping a foreign key constraint. |
||
124 | */ |
||
125 | 3 | public function dropForeignKey($name, $table) |
|
130 | |||
131 | /** |
||
132 | * Builds a SQL statement for removing a primary key constraint to an existing table. |
||
133 | * @param string $name the name of the primary key constraint to be removed. |
||
134 | * @param string $table the table that the primary key constraint will be removed from. |
||
135 | * @return string the SQL statement for removing a primary key constraint from an existing table. |
||
136 | */ |
||
137 | 2 | public function dropPrimaryKey($name, $table) |
|
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 2 | public function dropUnique($name, $table) |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | * @throws NotSupportedException this is not supported by MySQL. |
||
153 | */ |
||
154 | public function addCheck($name, $table, $expression) |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | * @throws NotSupportedException this is not supported by MySQL. |
||
162 | */ |
||
163 | public function dropCheck($name, $table) |
||
167 | |||
168 | /** |
||
169 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
170 | * The sequence will be reset such that the primary key of the next new row inserted |
||
171 | * will have the specified value or 1. |
||
172 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
173 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
174 | * the next new row's primary key will have a value 1. |
||
175 | * @return string the SQL statement for resetting sequence |
||
176 | * @throws InvalidArgumentException if the table does not exist or there is no sequence associated with the table. |
||
177 | */ |
||
178 | 11 | public function resetSequence($tableName, $value = null) |
|
197 | |||
198 | /** |
||
199 | * Builds a SQL statement for enabling or disabling integrity check. |
||
200 | * @param bool $check whether to turn on or off the integrity check. |
||
201 | * @param string $schema the schema of the tables. Meaningless for MySQL. |
||
202 | * @param string $table the table name. Meaningless for MySQL. |
||
203 | * @return string the SQL statement for checking integrity |
||
204 | */ |
||
205 | 4 | public function checkIntegrity($check = true, $schema = '', $table = '') |
|
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | 405 | public function buildLimit($limit, $offset) |
|
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | 405 | protected function hasLimit($limit) |
|
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | 405 | protected function hasOffset($offset) |
|
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | 203 | protected function prepareInsertValues($table, $columns, $params = []) |
|
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | * @see https://downloads.mysql.com/docs/refman-5.1-en.pdf |
||
272 | */ |
||
273 | 38 | public function upsert($table, $insertColumns, $updateColumns, &$params) |
|
293 | |||
294 | /** |
||
295 | * {@inheritdoc} |
||
296 | * @since 2.0.8 |
||
297 | */ |
||
298 | 2 | public function addCommentOnColumn($table, $column, $comment) |
|
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | * @since 2.0.8 |
||
314 | */ |
||
315 | 1 | public function addCommentOnTable($table, $comment) |
|
319 | |||
320 | /** |
||
321 | * {@inheritdoc} |
||
322 | * @since 2.0.8 |
||
323 | */ |
||
324 | 2 | public function dropCommentFromColumn($table, $column) |
|
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | * @since 2.0.8 |
||
332 | */ |
||
333 | 1 | public function dropCommentFromTable($table) |
|
337 | |||
338 | |||
339 | /** |
||
340 | * Gets column definition. |
||
341 | * |
||
342 | * @param string $table table name |
||
343 | * @param string $column column name |
||
344 | * @return null|string the column definition |
||
345 | * @throws Exception in case when table does not contain column |
||
346 | */ |
||
347 | 2 | private function getColumnDefinition($table, $column) |
|
370 | |||
371 | /** |
||
372 | * Checks the ability to use fractional seconds. |
||
373 | * |
||
374 | * @return bool |
||
375 | * @see https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html |
||
376 | */ |
||
377 | 509 | private function supportsFractionalSeconds() |
|
378 | { |
||
379 | 509 | $version = $this->db->getSlavePdo()->getAttribute(\PDO::ATTR_SERVER_VERSION); |
|
380 | 509 | return version_compare($version, '5.6.4', '>='); |
|
381 | } |
||
382 | |||
383 | /** |
||
384 | * Returns the map for default time type. |
||
385 | * If the version of MySQL is lower than 5.6.4, then the types will be without fractional seconds, |
||
386 | * otherwise with fractional seconds. |
||
387 | * |
||
388 | * @return array |
||
389 | */ |
||
390 | 509 | private function defaultTimeTypeMap() |
|
408 | } |
||
409 |