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 |
||
| 25 | class QueryBuilder extends \yii\db\QueryBuilder |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
| 29 | */ |
||
| 30 | public $typeMap = [ |
||
| 31 | Schema::TYPE_PK => 'NUMBER(10) NOT NULL PRIMARY KEY', |
||
| 32 | Schema::TYPE_UPK => 'NUMBER(10) UNSIGNED NOT NULL PRIMARY KEY', |
||
| 33 | Schema::TYPE_BIGPK => 'NUMBER(20) NOT NULL PRIMARY KEY', |
||
| 34 | Schema::TYPE_UBIGPK => 'NUMBER(20) UNSIGNED NOT NULL PRIMARY KEY', |
||
| 35 | Schema::TYPE_CHAR => 'CHAR(1)', |
||
| 36 | Schema::TYPE_STRING => 'VARCHAR2(255)', |
||
| 37 | Schema::TYPE_TEXT => 'CLOB', |
||
| 38 | Schema::TYPE_SMALLINT => 'NUMBER(5)', |
||
| 39 | Schema::TYPE_INTEGER => 'NUMBER(10)', |
||
| 40 | Schema::TYPE_BIGINT => 'NUMBER(20)', |
||
| 41 | Schema::TYPE_FLOAT => 'NUMBER', |
||
| 42 | Schema::TYPE_DOUBLE => 'NUMBER', |
||
| 43 | Schema::TYPE_DECIMAL => 'NUMBER', |
||
| 44 | Schema::TYPE_DATETIME => 'TIMESTAMP', |
||
| 45 | Schema::TYPE_TIMESTAMP => 'TIMESTAMP', |
||
| 46 | Schema::TYPE_TIME => 'TIMESTAMP', |
||
| 47 | Schema::TYPE_DATE => 'DATE', |
||
| 48 | Schema::TYPE_BINARY => 'BLOB', |
||
| 49 | Schema::TYPE_BOOLEAN => 'NUMBER(1)', |
||
| 50 | Schema::TYPE_MONEY => 'NUMBER(19,4)', |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | protected function defaultExpressionBuilders() |
||
| 57 | { |
||
| 58 | return array_merge(parent::defaultExpressionBuilders(), [ |
||
| 59 | 'yii\db\conditions\InCondition' => 'yii\db\oci\conditions\InConditionBuilder', |
||
| 60 | 'yii\db\conditions\LikeCondition' => 'yii\db\oci\conditions\LikeConditionBuilder', |
||
| 61 | ]); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Builds a SQL statement for renaming a DB table. |
||
| 97 | * |
||
| 98 | * @param string $table the table to be renamed. The name will be properly quoted by the method. |
||
| 99 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
| 100 | * @return string the SQL statement for renaming a DB table. |
||
| 101 | */ |
||
| 102 | public function renameTable($table, $newName) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Builds a SQL statement for changing the definition of a column. |
||
| 109 | * |
||
| 110 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
| 111 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
| 112 | * @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column type (if any) |
||
| 113 | * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
||
| 114 | * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
||
| 115 | * @return string the SQL statement for changing the definition of a column. |
||
| 116 | */ |
||
| 117 | public function alterColumn($table, $column, $type) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Builds a SQL statement for dropping an index. |
||
| 126 | * |
||
| 127 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
| 128 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
| 129 | * @return string the SQL statement for dropping an index. |
||
| 130 | */ |
||
| 131 | public function dropIndex($name, $table) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | public function resetSequence($table, $value = null) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | */ |
||
| 165 | public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | protected function prepareInsertValues($table, $columns, $params = []) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @inheritdoc |
||
| 203 | * @see https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF01606 |
||
| 204 | */ |
||
| 205 | public function upsert($table, $insertColumns, $updateColumns, &$params) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Generates a batch INSERT SQL statement. |
||
| 269 | * |
||
| 270 | * For example, |
||
| 271 | * |
||
| 272 | * ```php |
||
| 273 | * $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
||
| 274 | * ['Tom', 30], |
||
| 275 | * ['Jane', 20], |
||
| 276 | * ['Linda', 25], |
||
| 277 | * ]); |
||
| 278 | * ``` |
||
| 279 | * |
||
| 280 | * Note that the values in each row must match the corresponding column names. |
||
| 281 | * |
||
| 282 | * @param string $table the table that new rows will be inserted into. |
||
| 283 | * @param array $columns the column names |
||
| 284 | * @param array|\Generator $rows the rows to be batch inserted into the table |
||
| 285 | * @return string the batch INSERT SQL statement |
||
| 286 | */ |
||
| 287 | public function batchInsert($table, $columns, $rows) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * {@inheritdoc} |
||
| 337 | * @since 2.0.8 |
||
| 338 | */ |
||
| 339 | public function selectExists($rawSql) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * {@inheritdoc} |
||
| 346 | * @since 2.0.8 |
||
| 347 | */ |
||
| 348 | public function dropCommentFromColumn($table, $column) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * {@inheritdoc} |
||
| 355 | * @since 2.0.8 |
||
| 356 | */ |
||
| 357 | public function dropCommentFromTable($table) |
||
| 361 | } |
||
| 362 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.