Complex classes like ActiveQuery 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 ActiveQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
73 | class ActiveQuery extends Query implements ActiveQueryInterface |
||
74 | { |
||
75 | use ActiveQueryTrait; |
||
76 | use ActiveRelationTrait; |
||
77 | |||
78 | /** |
||
79 | * @event Event an event that is triggered when the query is initialized via [[init()]]. |
||
80 | */ |
||
81 | const EVENT_INIT = 'init'; |
||
82 | |||
83 | /** |
||
84 | * @var string the SQL statement to be executed for retrieving AR records. |
||
85 | * This is set by [[ActiveRecord::findBySql()]]. |
||
86 | */ |
||
87 | public $sql; |
||
88 | /** |
||
89 | * @var string|array the join condition to be used when this query is used in a relational context. |
||
90 | * The condition will be used in the ON part when [[ActiveQuery::joinWith()]] is called. |
||
91 | * Otherwise, the condition will be used in the WHERE part of a query. |
||
92 | * Please refer to [[Query::where()]] on how to specify this parameter. |
||
93 | * @see onCondition() |
||
94 | */ |
||
95 | public $on; |
||
96 | /** |
||
97 | * @var array a list of relations that this query should be joined with |
||
98 | */ |
||
99 | public $joinWith; |
||
100 | |||
101 | |||
102 | /** |
||
103 | * Constructor. |
||
104 | * @param string $modelClass the model class associated with this query |
||
105 | * @param array $config configurations to be applied to the newly created query object |
||
106 | */ |
||
107 | 504 | public function __construct($modelClass, $config = []) |
|
112 | |||
113 | /** |
||
114 | * Initializes the object. |
||
115 | * This method is called at the end of the constructor. The default implementation will trigger |
||
116 | * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end |
||
117 | * to ensure triggering of the event. |
||
118 | */ |
||
119 | 504 | public function init() |
|
124 | |||
125 | /** |
||
126 | * Executes query and returns all results as an array. |
||
127 | * @param Connection $db the DB connection used to create the DB command. |
||
128 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
129 | * @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned. |
||
130 | */ |
||
131 | 198 | public function all($db = null) |
|
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | 400 | public function prepare($builder) |
|
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | 344 | public function populate($rows) |
|
227 | |||
228 | /** |
||
229 | * Removes duplicated models by checking their primary key values. |
||
230 | * This method is mainly called when a join query is performed, which may cause duplicated rows being returned. |
||
231 | * @param array $models the models to be checked |
||
232 | * @throws InvalidConfigException if model primary key is empty |
||
233 | * @return array the distinctive models |
||
234 | */ |
||
235 | 36 | private function removeDuplicatedModels($models) |
|
281 | |||
282 | /** |
||
283 | * Executes query and returns a single row of result. |
||
284 | * @param Connection|null $db the DB connection used to create the DB command. |
||
285 | * If `null`, the DB connection returned by [[modelClass]] will be used. |
||
286 | * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]], |
||
287 | * the query result may be either an array or an ActiveRecord object. `null` will be returned |
||
288 | * if the query results in nothing. |
||
289 | */ |
||
290 | 285 | public function one($db = null) |
|
300 | |||
301 | /** |
||
302 | * Creates a DB command that can be used to execute this query. |
||
303 | * @param Connection|null $db the DB connection used to create the DB command. |
||
304 | * If `null`, the DB connection returned by [[modelClass]] will be used. |
||
305 | * @return Command the created DB command instance. |
||
306 | */ |
||
307 | 400 | public function createCommand($db = null) |
|
327 | |||
328 | /** |
||
329 | * {@inheritdoc} |
||
330 | */ |
||
331 | 64 | protected function queryScalar($selectExpression, $db) |
|
351 | |||
352 | /** |
||
353 | * Joins with the specified relations. |
||
354 | * |
||
355 | * This method allows you to reuse existing relation definitions to perform JOIN queries. |
||
356 | * Based on the definition of the specified relation(s), the method will append one or multiple |
||
357 | * JOIN statements to the current query. |
||
358 | * |
||
359 | * If the `$eagerLoading` parameter is true, the method will also perform eager loading for the specified relations, |
||
360 | * which is equivalent to calling [[with()]] using the specified relations. |
||
361 | * |
||
362 | * Note that because a JOIN query will be performed, you are responsible to disambiguate column names. |
||
363 | * |
||
364 | * This method differs from [[with()]] in that it will build up and execute a JOIN SQL statement |
||
365 | * for the primary table. And when `$eagerLoading` is true, it will call [[with()]] in addition with the specified relations. |
||
366 | * |
||
367 | * @param string|array $with the relations to be joined. This can either be a string, representing a relation name or |
||
368 | * an array with the following semantics: |
||
369 | * |
||
370 | * - Each array element represents a single relation. |
||
371 | * - You may specify the relation name as the array key and provide an anonymous functions that |
||
372 | * can be used to modify the relation queries on-the-fly as the array value. |
||
373 | * - If a relation query does not need modification, you may use the relation name as the array value. |
||
374 | * |
||
375 | * The relation name may optionally contain an alias for the relation table (e.g. `books b`). |
||
376 | * |
||
377 | * Sub-relations can also be specified, see [[with()]] for the syntax. |
||
378 | * |
||
379 | * In the following you find some examples: |
||
380 | * |
||
381 | * ```php |
||
382 | * // find all orders that contain books, and eager loading "books" |
||
383 | * Order::find()->joinWith('books', true, 'INNER JOIN')->all(); |
||
384 | * // find all orders, eager loading "books", and sort the orders and books by the book names. |
||
385 | * Order::find()->joinWith([ |
||
386 | * 'books' => function (\yii\db\ActiveQuery $query) { |
||
387 | * $query->orderBy('item.name'); |
||
388 | * } |
||
389 | * ])->all(); |
||
390 | * // find all orders that contain books of the category 'Science fiction', using the alias "b" for the books table |
||
391 | * Order::find()->joinWith(['books b'], true, 'INNER JOIN')->where(['b.category' => 'Science fiction'])->all(); |
||
392 | * ``` |
||
393 | * |
||
394 | * The alias syntax is available since version 2.0.7. |
||
395 | * |
||
396 | * @param bool|array $eagerLoading whether to eager load the relations |
||
397 | * specified in `$with`. When this is a boolean, it applies to all |
||
398 | * relations specified in `$with`. Use an array to explicitly list which |
||
399 | * relations in `$with` need to be eagerly loaded. Note, that this does |
||
400 | * not mean, that the relations are populated from the query result. An |
||
401 | * extra query will still be performed to bring in the related data. |
||
402 | * Defaults to `true`. |
||
403 | * @param string|array $joinType the join type of the relations specified in `$with`. |
||
404 | * When this is a string, it applies to all relations specified in `$with`. Use an array |
||
405 | * in the format of `relationName => joinType` to specify different join types for different relations. |
||
406 | * @return $this the query object itself |
||
407 | */ |
||
408 | 57 | public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN') |
|
439 | |||
440 | 51 | private function buildJoinWith() |
|
483 | |||
484 | /** |
||
485 | * Inner joins with the specified relations. |
||
486 | * This is a shortcut method to [[joinWith()]] with the join type set as "INNER JOIN". |
||
487 | * Please refer to [[joinWith()]] for detailed usage of this method. |
||
488 | * @param string|array $with the relations to be joined with. |
||
489 | * @param bool|array $eagerLoading whether to eager load the relations. |
||
490 | * Note, that this does not mean, that the relations are populated from the |
||
491 | * query result. An extra query will still be performed to bring in the |
||
492 | * related data. |
||
493 | * @return $this the query object itself |
||
494 | * @see joinWith() |
||
495 | */ |
||
496 | 21 | public function innerJoinWith($with, $eagerLoading = true) |
|
500 | |||
501 | /** |
||
502 | * Modifies the current query by adding join fragments based on the given relations. |
||
503 | * @param ActiveRecord $model the primary model |
||
504 | * @param array $with the relations to be joined |
||
505 | * @param string|array $joinType the join type |
||
506 | */ |
||
507 | 51 | private function joinWithRelations($model, $with, $joinType) |
|
551 | |||
552 | /** |
||
553 | * Returns the join type based on the given join type parameter and the relation name. |
||
554 | * @param string|array $joinType the given join type(s) |
||
555 | * @param string $name relation name |
||
556 | * @return string the real join type |
||
557 | */ |
||
558 | 51 | private function getJoinType($joinType, $name) |
|
566 | |||
567 | /** |
||
568 | * Returns the table name and the table alias for [[modelClass]]. |
||
569 | * @return array the table name and the table alias. |
||
570 | * @internal |
||
571 | */ |
||
572 | 69 | private function getTableNameAndAlias() |
|
594 | |||
595 | /** |
||
596 | * Joins a parent query with a child query. |
||
597 | * The current query object will be modified accordingly. |
||
598 | * @param ActiveQuery $parent |
||
599 | * @param ActiveQuery $child |
||
600 | * @param string $joinType |
||
601 | */ |
||
602 | 51 | private function joinWithRelation($parent, $child, $joinType) |
|
668 | |||
669 | /** |
||
670 | * Sets the ON condition for a relational query. |
||
671 | * The condition will be used in the ON part when [[ActiveQuery::joinWith()]] is called. |
||
672 | * Otherwise, the condition will be used in the WHERE part of a query. |
||
673 | * |
||
674 | * Use this method to specify additional conditions when declaring a relation in the [[ActiveRecord]] class: |
||
675 | * |
||
676 | * ```php |
||
677 | * public function getActiveUsers() |
||
678 | * { |
||
679 | * return $this->hasMany(User::className(), ['id' => 'user_id']) |
||
680 | * ->onCondition(['active' => true]); |
||
681 | * } |
||
682 | * ``` |
||
683 | * |
||
684 | * Note that this condition is applied in case of a join as well as when fetching the related records. |
||
685 | * Thus only fields of the related table can be used in the condition. Trying to access fields of the primary |
||
686 | * record will cause an error in a non-join-query. |
||
687 | * |
||
688 | * @param string|array $condition the ON condition. Please refer to [[Query::where()]] on how to specify this parameter. |
||
689 | * @param array $params the parameters (name => value) to be bound to the query. |
||
690 | * @return $this the query object itself |
||
691 | */ |
||
692 | 21 | public function onCondition($condition, $params = []) |
|
698 | |||
699 | /** |
||
700 | * Adds an additional ON condition to the existing one. |
||
701 | * The new condition and the existing one will be joined using the 'AND' operator. |
||
702 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
703 | * on how to specify this parameter. |
||
704 | * @param array $params the parameters (name => value) to be bound to the query. |
||
705 | * @return $this the query object itself |
||
706 | * @see onCondition() |
||
707 | * @see orOnCondition() |
||
708 | */ |
||
709 | 6 | public function andOnCondition($condition, $params = []) |
|
719 | |||
720 | /** |
||
721 | * Adds an additional ON condition to the existing one. |
||
722 | * The new condition and the existing one will be joined using the 'OR' operator. |
||
723 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
724 | * on how to specify this parameter. |
||
725 | * @param array $params the parameters (name => value) to be bound to the query. |
||
726 | * @return $this the query object itself |
||
727 | * @see onCondition() |
||
728 | * @see andOnCondition() |
||
729 | */ |
||
730 | 6 | public function orOnCondition($condition, $params = []) |
|
740 | |||
741 | /** |
||
742 | * Specifies the junction table for a relational query. |
||
743 | * |
||
744 | * Use this method to specify a junction table when declaring a relation in the [[ActiveRecord]] class: |
||
745 | * |
||
746 | * ```php |
||
747 | * public function getItems() |
||
748 | * { |
||
749 | * return $this->hasMany(Item::className(), ['id' => 'item_id']) |
||
750 | * ->viaTable('order_item', ['order_id' => 'id']); |
||
751 | * } |
||
752 | * ``` |
||
753 | * |
||
754 | * @param string $tableName the name of the junction table. |
||
755 | * @param array $link the link between the junction table and the table associated with [[primaryModel]]. |
||
756 | * The keys of the array represent the columns in the junction table, and the values represent the columns |
||
757 | * in the [[primaryModel]] table. |
||
758 | * @param callable $callable a PHP callback for customizing the relation associated with the junction table. |
||
759 | * Its signature should be `function($query)`, where `$query` is the query to be customized. |
||
760 | * @return $this the query object itself |
||
761 | * @see via() |
||
762 | */ |
||
763 | 24 | public function viaTable($tableName, $link, callable $callable = null) |
|
780 | |||
781 | /** |
||
782 | * Define an alias for the table defined in [[modelClass]]. |
||
783 | * |
||
784 | * This method will adjust [[from]] so that an already defined alias will be overwritten. |
||
785 | * If none was defined, [[from]] will be populated with the given alias. |
||
786 | * |
||
787 | * @param string $alias the table alias. |
||
788 | * @return $this the query object itself |
||
789 | * @since 2.0.7 |
||
790 | */ |
||
791 | 24 | public function alias($alias) |
|
809 | |||
810 | /** |
||
811 | * {@inheritdoc} |
||
812 | * @since 2.0.12 |
||
813 | */ |
||
814 | 136 | public function getTablesUsedInFrom() |
|
822 | |||
823 | /** |
||
824 | * @return string primary table name |
||
825 | * @since 2.0.12 |
||
826 | */ |
||
827 | 415 | protected function getPrimaryTableName() |
|
833 | } |
||
834 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..