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 | 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 | 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 | public function all($db = null) |
||
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | public function prepare($builder) |
||
200 | |||
201 | /** |
||
202 | * @inheritdoc |
||
203 | */ |
||
204 | public function populate($rows) |
||
205 | { |
||
206 | if (empty($rows)) { |
||
207 | return []; |
||
208 | } |
||
209 | |||
210 | $models = $this->createModels($rows); |
||
211 | if (!empty($this->join) && $this->indexBy === null) { |
||
212 | $models = $this->removeDuplicatedModels($models); |
||
213 | } |
||
214 | if (!empty($this->with)) { |
||
215 | $this->findWith($this->with, $models); |
||
216 | } |
||
217 | |||
218 | if ($this->inverseOf !== null) { |
||
219 | $this->addInverseRelations($models); |
||
220 | } |
||
221 | |||
222 | if (!$this->asArray) { |
||
223 | foreach ($models as $model) { |
||
224 | $model->afterFind(); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | return $models; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Removes duplicated models by checking their primary key values. |
||
233 | * This method is mainly called when a join query is performed, which may cause duplicated rows being returned. |
||
234 | * @param array $models the models to be checked |
||
235 | * @throws InvalidConfigException if model primary key is empty |
||
236 | * @return array the distinctive models |
||
237 | */ |
||
238 | private function removeDuplicatedModels($models) |
||
284 | |||
285 | /** |
||
286 | * Executes query and returns a single row of result. |
||
287 | * @param Connection|null $db the DB connection used to create the DB command. |
||
288 | * If `null`, the DB connection returned by [[modelClass]] will be used. |
||
289 | * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]], |
||
290 | * the query result may be either an array or an ActiveRecord object. `null` will be returned |
||
291 | * if the query results in nothing. |
||
292 | */ |
||
293 | public function one($db = null) |
||
303 | |||
304 | /** |
||
305 | * Creates a DB command that can be used to execute this query. |
||
306 | * @param Connection|null $db the DB connection used to create the DB command. |
||
307 | * If `null`, the DB connection returned by [[modelClass]] will be used. |
||
308 | * @return Command the created DB command instance. |
||
309 | */ |
||
310 | public function createCommand($db = null) |
||
327 | |||
328 | /** |
||
329 | * @inheritdoc |
||
330 | */ |
||
331 | protected function queryScalar($selectExpression, $db) |
||
349 | |||
350 | /** |
||
351 | * Joins with the specified relations. |
||
352 | * |
||
353 | * This method allows you to reuse existing relation definitions to perform JOIN queries. |
||
354 | * Based on the definition of the specified relation(s), the method will append one or multiple |
||
355 | * JOIN statements to the current query. |
||
356 | * |
||
357 | * If the `$eagerLoading` parameter is true, the method will also perform eager loading for the specified relations, |
||
358 | * which is equivalent to calling [[with()]] using the specified relations. |
||
359 | * |
||
360 | * Note that because a JOIN query will be performed, you are responsible to disambiguate column names. |
||
361 | * |
||
362 | * This method differs from [[with()]] in that it will build up and execute a JOIN SQL statement |
||
363 | * for the primary table. And when `$eagerLoading` is true, it will call [[with()]] in addition with the specified relations. |
||
364 | * |
||
365 | * @param string|array $with the relations to be joined. This can either be a string, representing a relation name or |
||
366 | * an array with the following semantics: |
||
367 | * |
||
368 | * - Each array element represents a single relation. |
||
369 | * - You may specify the relation name as the array key and provide an anonymous functions that |
||
370 | * can be used to modify the relation queries on-the-fly as the array value. |
||
371 | * - If a relation query does not need modification, you may use the relation name as the array value. |
||
372 | * |
||
373 | * The relation name may optionally contain an alias for the relation table (e.g. `books b`). |
||
374 | * |
||
375 | * Sub-relations can also be specified, see [[with()]] for the syntax. |
||
376 | * |
||
377 | * In the following you find some examples: |
||
378 | * |
||
379 | * ```php |
||
380 | * // find all orders that contain books, and eager loading "books" |
||
381 | * Order::find()->joinWith('books', true, 'INNER JOIN')->all(); |
||
382 | * // find all orders, eager loading "books", and sort the orders and books by the book names. |
||
383 | * Order::find()->joinWith([ |
||
384 | * 'books' => function (\yii\db\ActiveQuery $query) { |
||
385 | * $query->orderBy('item.name'); |
||
386 | * } |
||
387 | * ])->all(); |
||
388 | * // find all orders that contain books of the category 'Science fiction', using the alias "b" for the books table |
||
389 | * Order::find()->joinWith(['books b'], true, 'INNER JOIN')->where(['b.category' => 'Science fiction'])->all(); |
||
390 | * ``` |
||
391 | * |
||
392 | * The alias syntax is available since version 2.0.7. |
||
393 | * |
||
394 | * @param bool|array $eagerLoading whether to eager load the relations specified in `$with`. |
||
395 | * When this is a boolean, it applies to all relations specified in `$with`. Use an array |
||
396 | * to explicitly list which relations in `$with` need to be eagerly loaded. Defaults to `true`. |
||
397 | * @param string|array $joinType the join type of the relations specified in `$with`. |
||
398 | * When this is a string, it applies to all relations specified in `$with`. Use an array |
||
399 | * in the format of `relationName => joinType` to specify different join types for different relations. |
||
400 | * @return $this the query object itself |
||
401 | */ |
||
402 | public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN') |
||
433 | |||
434 | private function buildJoinWith() |
||
475 | |||
476 | /** |
||
477 | * Inner joins with the specified relations. |
||
478 | * This is a shortcut method to [[joinWith()]] with the join type set as "INNER JOIN". |
||
479 | * Please refer to [[joinWith()]] for detailed usage of this method. |
||
480 | * @param string|array $with the relations to be joined with. |
||
481 | * @param bool|array $eagerLoading whether to eager loading the relations. |
||
482 | * @return $this the query object itself |
||
483 | * @see joinWith() |
||
484 | */ |
||
485 | public function innerJoinWith($with, $eagerLoading = true) |
||
489 | |||
490 | /** |
||
491 | * Modifies the current query by adding join fragments based on the given relations. |
||
492 | * @param ActiveRecord $model the primary model |
||
493 | * @param array $with the relations to be joined |
||
494 | * @param string|array $joinType the join type |
||
495 | */ |
||
496 | private function joinWithRelations($model, $with, $joinType) |
||
538 | |||
539 | /** |
||
540 | * Returns the join type based on the given join type parameter and the relation name. |
||
541 | * @param string|array $joinType the given join type(s) |
||
542 | * @param string $name relation name |
||
543 | * @return string the real join type |
||
544 | */ |
||
545 | private function getJoinType($joinType, $name) |
||
553 | |||
554 | /** |
||
555 | * Returns the table name and the table alias for [[modelClass]]. |
||
556 | * @return array the table name and the table alias. |
||
557 | * @internal |
||
558 | */ |
||
559 | private function getTableNameAndAlias() |
||
584 | |||
585 | /** |
||
586 | * Joins a parent query with a child query. |
||
587 | * The current query object will be modified accordingly. |
||
588 | * @param ActiveQuery $parent |
||
589 | * @param ActiveQuery $child |
||
590 | * @param string $joinType |
||
591 | */ |
||
592 | private function joinWithRelation($parent, $child, $joinType) |
||
659 | |||
660 | /** |
||
661 | * Sets the ON condition for a relational query. |
||
662 | * The condition will be used in the ON part when [[ActiveQuery::joinWith()]] is called. |
||
663 | * Otherwise, the condition will be used in the WHERE part of a query. |
||
664 | * |
||
665 | * Use this method to specify additional conditions when declaring a relation in the [[ActiveRecord]] class: |
||
666 | * |
||
667 | * ```php |
||
668 | * public function getActiveUsers() |
||
669 | * { |
||
670 | * return $this->hasMany(User::className(), ['id' => 'user_id']) |
||
671 | * ->onCondition(['active' => true]); |
||
672 | * } |
||
673 | * ``` |
||
674 | * |
||
675 | * Note that this condition is applied in case of a join as well as when fetching the related records. |
||
676 | * Thus only fields of the related table can be used in the condition. Trying to access fields of the primary |
||
677 | * record will cause an error in a non-join-query. |
||
678 | * |
||
679 | * @param string|array $condition the ON condition. Please refer to [[Query::where()]] on how to specify this parameter. |
||
680 | * @param array $params the parameters (name => value) to be bound to the query. |
||
681 | * @return $this the query object itself |
||
682 | */ |
||
683 | public function onCondition($condition, $params = []) |
||
689 | |||
690 | /** |
||
691 | * Adds an additional ON condition to the existing one. |
||
692 | * The new condition and the existing one will be joined using the 'AND' operator. |
||
693 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
694 | * on how to specify this parameter. |
||
695 | * @param array $params the parameters (name => value) to be bound to the query. |
||
696 | * @return $this the query object itself |
||
697 | * @see onCondition() |
||
698 | * @see orOnCondition() |
||
699 | */ |
||
700 | public function andOnCondition($condition, $params = []) |
||
710 | |||
711 | /** |
||
712 | * Adds an additional ON condition to the existing one. |
||
713 | * The new condition and the existing one will be joined using the 'OR' operator. |
||
714 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
715 | * on how to specify this parameter. |
||
716 | * @param array $params the parameters (name => value) to be bound to the query. |
||
717 | * @return $this the query object itself |
||
718 | * @see onCondition() |
||
719 | * @see andOnCondition() |
||
720 | */ |
||
721 | public function orOnCondition($condition, $params = []) |
||
731 | |||
732 | /** |
||
733 | * Specifies the junction table for a relational query. |
||
734 | * |
||
735 | * Use this method to specify a junction table when declaring a relation in the [[ActiveRecord]] class: |
||
736 | * |
||
737 | * ```php |
||
738 | * public function getItems() |
||
739 | * { |
||
740 | * return $this->hasMany(Item::className(), ['id' => 'item_id']) |
||
741 | * ->viaTable('order_item', ['order_id' => 'id']); |
||
742 | * } |
||
743 | * ``` |
||
744 | * |
||
745 | * @param string $tableName the name of the junction table. |
||
746 | * @param array $link the link between the junction table and the table associated with [[primaryModel]]. |
||
747 | * The keys of the array represent the columns in the junction table, and the values represent the columns |
||
748 | * in the [[primaryModel]] table. |
||
749 | * @param callable $callable a PHP callback for customizing the relation associated with the junction table. |
||
750 | * Its signature should be `function($query)`, where `$query` is the query to be customized. |
||
751 | * @return $this the query object itself |
||
752 | * @see via() |
||
753 | */ |
||
754 | public function viaTable($tableName, $link, callable $callable = null) |
||
769 | |||
770 | /** |
||
771 | * Define an alias for the table defined in [[modelClass]]. |
||
772 | * |
||
773 | * This method will adjust [[from]] so that an already defined alias will be overwritten. |
||
774 | * If none was defined, [[from]] will be populated with the given alias. |
||
775 | * |
||
776 | * @param string $alias the table alias. |
||
777 | * @return $this the query object itself |
||
778 | * @since 2.0.7 |
||
779 | */ |
||
780 | public function alias($alias) |
||
799 | } |
||
800 |
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..