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 | 237 | public function __construct($modelClass, $config = []) |
|
108 | { |
||
109 | 237 | $this->modelClass = $modelClass; |
|
110 | 237 | parent::__construct($config); |
|
111 | 237 | } |
|
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 | 237 | public function init() |
|
120 | { |
||
121 | 237 | parent::init(); |
|
122 | 237 | $this->trigger(self::EVENT_INIT); |
|
123 | 237 | } |
|
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 | 144 | public function all($db = null) |
|
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | 226 | public function prepare($builder) |
|
200 | |||
201 | /** |
||
202 | * @inheritdoc |
||
203 | */ |
||
204 | 200 | public function populate($rows) |
|
225 | |||
226 | /** |
||
227 | * Removes duplicated models by checking their primary key values. |
||
228 | * This method is mainly called when a join query is performed, which may cause duplicated rows being returned. |
||
229 | * @param array $models the models to be checked |
||
230 | * @throws InvalidConfigException if model primary key is empty |
||
231 | * @return array the distinctive models |
||
232 | */ |
||
233 | 21 | private function removeDuplicatedModels($models) |
|
279 | |||
280 | /** |
||
281 | * Executes query and returns a single row of result. |
||
282 | * @param Connection $db the DB connection used to create the DB command. |
||
283 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
284 | * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]], |
||
285 | * the query result may be either an array or an ActiveRecord object. Null will be returned |
||
286 | * if the query results in nothing. |
||
287 | */ |
||
288 | 159 | public function one($db = null) |
|
298 | |||
299 | /** |
||
300 | * Creates a DB command that can be used to execute this query. |
||
301 | * @param Connection $db the DB connection used to create the DB command. |
||
302 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
303 | * @return Command the created DB command instance. |
||
304 | */ |
||
305 | 229 | public function createCommand($db = null) |
|
322 | |||
323 | /** |
||
324 | * @inheritdoc |
||
325 | */ |
||
326 | 55 | protected function queryScalar($selectExpression, $db) |
|
342 | |||
343 | /** |
||
344 | * Joins with the specified relations. |
||
345 | * |
||
346 | * This method allows you to reuse existing relation definitions to perform JOIN queries. |
||
347 | * Based on the definition of the specified relation(s), the method will append one or multiple |
||
348 | * JOIN statements to the current query. |
||
349 | * |
||
350 | * If the `$eagerLoading` parameter is true, the method will also perform eager loading for the specified relations, |
||
351 | * which is equivalent to calling [[with()]] using the specified relations. |
||
352 | * |
||
353 | * Note that because a JOIN query will be performed, you are responsible to disambiguate column names. |
||
354 | * You may specify the table names manually or use the table alias syntax described in the [Guide about alias handling](db-querybuilder#aliases) |
||
355 | * for this. |
||
356 | * |
||
357 | * This method differs from [[with()]] in that it will build up and execute a JOIN SQL statement |
||
358 | * for the primary table. And when `$eagerLoading` is true, it will call [[with()]] in addition with the specified relations. |
||
359 | * |
||
360 | * @param string|array $with the relations to be joined. This can either be a string, representing a relation name or |
||
361 | * an array with the following semantics: |
||
362 | * |
||
363 | * - Each array element represents a single relation. |
||
364 | * - You may specify the relation name as the array key and provide an anonymous functions that |
||
365 | * can be used to modify the relation queries on-the-fly as the array value. |
||
366 | * - If a relation query does not need modification, you may use the relation name as the array value. |
||
367 | * |
||
368 | * The relation name may optionally contain an alias for the relation table (e.g. `books b`). |
||
369 | * |
||
370 | * Sub-relations can also be specified, see [[with()]] for the syntax. |
||
371 | * |
||
372 | * In the following you find some examples: |
||
373 | * |
||
374 | * ```php |
||
375 | * // find all orders that contain books, and eager loading "books" |
||
376 | * Order::find()->joinWith('books', true, 'INNER JOIN')->all(); |
||
377 | * // find all orders, eager loading "books", and sort the orders and books by the book names. |
||
378 | * Order::find()->joinWith([ |
||
379 | * 'books' => function (\yii\db\ActiveQuery $query) { |
||
380 | * $query->orderBy('item.name'); |
||
381 | * } |
||
382 | * ])->all(); |
||
383 | * // find all orders that contain books of the category 'Science fiction', using the alias "b" for the books table |
||
384 | * Order::find()->joinWith(['books b'], true, 'INNER JOIN')->where(['b.category' => 'Science fiction'])->all(); |
||
385 | * ``` |
||
386 | * |
||
387 | * The alias syntax is available since version 2.0.7. |
||
388 | * |
||
389 | * @param boolean|array $eagerLoading whether to eager load the relations specified in `$with`. |
||
390 | * When this is a boolean, it applies to all relations specified in `$with`. Use an array |
||
391 | * to explicitly list which relations in `$with` need to be eagerly loaded. Defaults to `true`. |
||
392 | * @param string|array $joinType the join type of the relations specified in `$with`. |
||
393 | * When this is a string, it applies to all relations specified in `$with`. Use an array |
||
394 | * in the format of `relationName => joinType` to specify different join types for different relations. |
||
395 | * @return $this the query object itself |
||
396 | */ |
||
397 | 21 | public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN') |
|
428 | |||
429 | 21 | private function buildJoinWith() |
|
470 | |||
471 | /** |
||
472 | * Inner joins with the specified relations. |
||
473 | * This is a shortcut method to [[joinWith()]] with the join type set as "INNER JOIN". |
||
474 | * Please refer to [[joinWith()]] for detailed usage of this method. |
||
475 | * @param string|array $with the relations to be joined with. |
||
476 | * @param boolean|array $eagerLoading whether to eager loading the relations. |
||
477 | * @return $this the query object itself |
||
478 | * @see joinWith() |
||
479 | */ |
||
480 | 12 | public function innerJoinWith($with, $eagerLoading = true) |
|
484 | |||
485 | /** |
||
486 | * Modifies the current query by adding join fragments based on the given relations. |
||
487 | * @param ActiveRecord $model the primary model |
||
488 | * @param array $with the relations to be joined |
||
489 | * @param string|array $joinType the join type |
||
490 | */ |
||
491 | 21 | private function joinWithRelations($model, $with, $joinType) |
|
533 | |||
534 | /** |
||
535 | * Returns the join type based on the given join type parameter and the relation name. |
||
536 | * @param string|array $joinType the given join type(s) |
||
537 | * @param string $name relation name |
||
538 | * @return string the real join type |
||
539 | */ |
||
540 | 21 | private function getJoinType($joinType, $name) |
|
548 | |||
549 | /** |
||
550 | * Returns the table name and the table alias for [[modelClass]]. |
||
551 | * @param ActiveQuery $query |
||
552 | * @return array the table name and the table alias. |
||
553 | */ |
||
554 | 24 | private function getQueryTableName($query) |
|
579 | |||
580 | /** |
||
581 | * Joins a parent query with a child query. |
||
582 | * The current query object will be modified accordingly. |
||
583 | * @param ActiveQuery $parent |
||
584 | * @param ActiveQuery $child |
||
585 | * @param string $joinType |
||
586 | */ |
||
587 | 21 | private function joinWithRelation($parent, $child, $joinType) |
|
655 | |||
656 | /** |
||
657 | * Sets the ON condition for a relational query. |
||
658 | * The condition will be used in the ON part when [[ActiveQuery::joinWith()]] is called. |
||
659 | * Otherwise, the condition will be used in the WHERE part of a query. |
||
660 | * |
||
661 | * Use this method to specify additional conditions when declaring a relation in the [[ActiveRecord]] class: |
||
662 | * |
||
663 | * ```php |
||
664 | * public function getActiveUsers() |
||
665 | * { |
||
666 | * return $this->hasMany(User::className(), ['id' => 'user_id']) |
||
667 | * ->onCondition(['active' => true]); |
||
668 | * } |
||
669 | * ``` |
||
670 | * |
||
671 | * Note that this condition is applied in case of a join as well as when fetching the related records. |
||
672 | * Thus only fields of the related table can be used in the condition. Trying to access fields of the primary |
||
673 | * record will cause an error in a non-join-query. |
||
674 | * |
||
675 | * @param string|array $condition the ON condition. Please refer to [[Query::where()]] on how to specify this parameter. |
||
676 | * @param array $params the parameters (name => value) to be bound to the query. |
||
677 | * @return $this the query object itself |
||
678 | */ |
||
679 | 15 | public function onCondition($condition, $params = []) |
|
685 | |||
686 | /** |
||
687 | * Adds an additional ON condition to the existing one. |
||
688 | * The new condition and the existing one will be joined using the 'AND' operator. |
||
689 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
690 | * on how to specify this parameter. |
||
691 | * @param array $params the parameters (name => value) to be bound to the query. |
||
692 | * @return $this the query object itself |
||
693 | * @see onCondition() |
||
694 | * @see orOnCondition() |
||
695 | */ |
||
696 | public function andOnCondition($condition, $params = []) |
||
706 | |||
707 | /** |
||
708 | * Adds an additional ON condition to the existing one. |
||
709 | * The new condition and the existing one will be joined using the 'OR' operator. |
||
710 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
711 | * on how to specify this parameter. |
||
712 | * @param array $params the parameters (name => value) to be bound to the query. |
||
713 | * @return $this the query object itself |
||
714 | * @see onCondition() |
||
715 | * @see andOnCondition() |
||
716 | */ |
||
717 | public function orOnCondition($condition, $params = []) |
||
727 | |||
728 | /** |
||
729 | * Specifies the junction table for a relational query. |
||
730 | * |
||
731 | * Use this method to specify a junction table when declaring a relation in the [[ActiveRecord]] class: |
||
732 | * |
||
733 | * ```php |
||
734 | * public function getItems() |
||
735 | * { |
||
736 | * return $this->hasMany(Item::className(), ['id' => 'item_id']) |
||
737 | * ->viaTable('order_item', ['order_id' => 'id']); |
||
738 | * } |
||
739 | * ``` |
||
740 | * |
||
741 | * @param string $tableName the name of the junction table. |
||
742 | * @param array $link the link between the junction table and the table associated with [[primaryModel]]. |
||
743 | * The keys of the array represent the columns in the junction table, and the values represent the columns |
||
744 | * in the [[primaryModel]] table. |
||
745 | * @param callable $callable a PHP callback for customizing the relation associated with the junction table. |
||
746 | * Its signature should be `function($query)`, where `$query` is the query to be customized. |
||
747 | * @return $this the query object itself |
||
748 | * @see via() |
||
749 | */ |
||
750 | 21 | public function viaTable($tableName, $link, callable $callable = null) |
|
765 | |||
766 | /** |
||
767 | * Define an alias for the table defined in [[modelClass]]. |
||
768 | * |
||
769 | * This method will adjust [[from]] so that an already defined alias will be overwritten. |
||
770 | * If none was defined, [[from]] will be populated with the given alias. |
||
771 | * |
||
772 | * @param string $alias the table alias. |
||
773 | * @return $this the query object itself |
||
774 | * @since 2.0.7 |
||
775 | */ |
||
776 | 15 | public function alias($alias) |
|
796 | } |
||
797 |
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..