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 IDENTITY PRIMARY KEY', |
||
27 | Schema::TYPE_UPK => 'int IDENTITY PRIMARY KEY', |
||
28 | Schema::TYPE_BIGPK => 'bigint IDENTITY PRIMARY KEY', |
||
29 | Schema::TYPE_UBIGPK => 'bigint IDENTITY PRIMARY KEY', |
||
30 | Schema::TYPE_CHAR => 'nchar(1)', |
||
31 | Schema::TYPE_STRING => 'nvarchar(255)', |
||
32 | Schema::TYPE_TEXT => 'nvarchar(max)', |
||
33 | Schema::TYPE_SMALLINT => 'smallint', |
||
34 | Schema::TYPE_INTEGER => 'int', |
||
35 | Schema::TYPE_BIGINT => 'bigint', |
||
36 | Schema::TYPE_FLOAT => 'float', |
||
37 | Schema::TYPE_DOUBLE => 'float', |
||
38 | Schema::TYPE_DECIMAL => 'decimal', |
||
39 | Schema::TYPE_DATETIME => 'datetime', |
||
40 | Schema::TYPE_TIMESTAMP => 'datetime', |
||
41 | Schema::TYPE_TIME => 'time', |
||
42 | Schema::TYPE_DATE => 'date', |
||
43 | Schema::TYPE_BINARY => 'varbinary(max)', |
||
44 | Schema::TYPE_BOOLEAN => 'bit', |
||
45 | Schema::TYPE_MONEY => 'decimal(19,4)', |
||
46 | ]; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * @inheritdoc |
||
51 | */ |
||
52 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
65 | |||
66 | /** |
||
67 | * Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2012 or newer. |
||
68 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
69 | * @param array $orderBy the order by columns. See [[\yii\db\Query::orderBy]] for more details on how to specify this parameter. |
||
70 | * @param int $limit the limit number. See [[\yii\db\Query::limit]] for more details. |
||
71 | * @param int $offset the offset number. See [[\yii\db\Query::offset]] for more details. |
||
72 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
73 | */ |
||
74 | protected function newBuildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
92 | |||
93 | /** |
||
94 | * Builds the ORDER BY/LIMIT/OFFSET clauses for SQL SERVER 2005 to 2008. |
||
95 | * @param string $sql the existing SQL (without ORDER BY/LIMIT/OFFSET) |
||
96 | * @param array $orderBy the order by columns. See [[\yii\db\Query::orderBy]] for more details on how to specify this parameter. |
||
97 | * @param int $limit the limit number. See [[\yii\db\Query::limit]] for more details. |
||
98 | * @param int $offset the offset number. See [[\yii\db\Query::offset]] for more details. |
||
99 | * @return string the SQL completed with ORDER BY/LIMIT/OFFSET (if any) |
||
100 | */ |
||
101 | protected function oldBuildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
122 | |||
123 | /** |
||
124 | * Builds a SQL statement for renaming a DB table. |
||
125 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
126 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
127 | * @return string the SQL statement for renaming a DB table. |
||
128 | */ |
||
129 | public function renameTable($oldName, $newName) |
||
133 | |||
134 | /** |
||
135 | * Builds a SQL statement for renaming a column. |
||
136 | * @param string $table the table whose column is to be renamed. The name will be properly quoted by the method. |
||
137 | * @param string $oldName the old name of the column. The name will be properly quoted by the method. |
||
138 | * @param string $newName the new name of the column. The name will be properly quoted by the method. |
||
139 | * @return string the SQL statement for renaming a DB column. |
||
140 | */ |
||
141 | public function renameColumn($table, $oldName, $newName) |
||
148 | |||
149 | /** |
||
150 | * Builds a SQL statement for changing the definition of a column. |
||
151 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
152 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
153 | * @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column type (if any) |
||
154 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
155 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
156 | * @return string the SQL statement for changing the definition of a column. |
||
157 | */ |
||
158 | public function alterColumn($table, $column, $type) |
||
167 | |||
168 | /** |
||
169 | * Builds a SQL statement for enabling or disabling integrity check. |
||
170 | * @param bool $check whether to turn on or off the integrity check. |
||
171 | * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||
172 | * @param string $table the table name. Defaults to empty string, meaning that no table will be changed. |
||
173 | * @return string the SQL statement for checking integrity |
||
174 | * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. |
||
175 | */ |
||
176 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
189 | |||
190 | /** |
||
191 | * @inheritdoc |
||
192 | * @since 2.0.8 |
||
193 | */ |
||
194 | public function addCommentOnColumn($table, $column, $comment) |
||
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | * @since 2.0.8 |
||
202 | */ |
||
203 | public function addCommentOnTable($table, $comment) |
||
207 | |||
208 | /** |
||
209 | * @inheritdoc |
||
210 | * @since 2.0.8 |
||
211 | */ |
||
212 | public function dropCommentFromColumn($table, $column) |
||
216 | |||
217 | /** |
||
218 | * @inheritdoc |
||
219 | * @since 2.0.8 |
||
220 | */ |
||
221 | public function dropCommentFromTable($table) |
||
225 | |||
226 | /** |
||
227 | * Returns an array of column names given model name |
||
228 | * |
||
229 | * @param string $modelClass name of the model class |
||
230 | * @return array|null array of column names |
||
231 | */ |
||
232 | protected function getAllColumnNames($modelClass = null) |
||
242 | |||
243 | /** |
||
244 | * @var bool whether MSSQL used is old. |
||
245 | */ |
||
246 | private $_oldMssql; |
||
247 | |||
248 | /** |
||
249 | * @return bool whether the version of the MSSQL being used is older than 2012. |
||
250 | * @throws \yii\base\InvalidConfigException |
||
251 | * @throws \yii\db\Exception |
||
252 | */ |
||
253 | protected function isOldMssql() |
||
262 | |||
263 | /** |
||
264 | * @inheritdoc |
||
265 | * @throws NotSupportedException if `$columns` is an array |
||
266 | */ |
||
267 | protected function buildSubqueryInCondition($operator, $columns, $values, &$params) |
||
274 | |||
275 | /** |
||
276 | * Builds SQL for IN condition |
||
277 | * |
||
278 | * @param string $operator |
||
279 | * @param array $columns |
||
280 | * @param array $values |
||
281 | * @param array $params |
||
282 | * @return string SQL |
||
283 | */ |
||
284 | protected function buildCompositeInCondition($operator, $columns, $values, &$params) |
||
307 | |||
308 | /** |
||
309 | * @inheritdoc |
||
310 | * @since 2.0.8 |
||
311 | */ |
||
312 | public function selectExists($rawSql) |
||
316 | |||
317 | /** |
||
318 | * Normalizes data to be saved into the table, performing extra preparations and type converting, if necessary. |
||
319 | * @param string $table the table that data will be saved into. |
||
320 | * @param array $columns the column data (name => value) to be saved into the table. |
||
321 | * @return array normalized columns |
||
322 | */ |
||
323 | private function normalizeTableRowData($table, $columns, &$params) |
||
337 | |||
338 | /** |
||
339 | * @inheritdoc |
||
340 | */ |
||
341 | public function insert($table, $columns, &$params) |
||
345 | |||
346 | /** |
||
347 | * @inheritdoc |
||
348 | */ |
||
349 | public function update($table, $columns, $condition, &$params) |
||
353 | } |
||
354 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: