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 | 368 | 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 | 368 | 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 | 173 | public function all($db = null) |
|
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | 312 | public function prepare($builder) |
|
200 | |||
201 | /** |
||
202 | * @inheritdoc |
||
203 | */ |
||
204 | 265 | public function populate($rows) |
|
205 | { |
||
206 | 265 | if (empty($rows)) { |
|
207 | 49 | return []; |
|
208 | } |
||
209 | |||
210 | 259 | $models = $this->createModels($rows); |
|
211 | 259 | if (!empty($this->join) && $this->indexBy === null) { |
|
212 | 24 | $models = $this->removeDuplicatedModels($models); |
|
213 | 24 | } |
|
214 | 259 | if (!empty($this->with)) { |
|
215 | 69 | $this->findWith($this->with, $models); |
|
216 | 69 | } |
|
217 | |||
218 | 259 | if ($this->inverseOf !== null) { |
|
219 | 6 | $this->addInverseRelations($models); |
|
220 | 6 | } |
|
221 | |||
222 | 259 | if (!$this->asArray) { |
|
223 | 253 | foreach ($models as $model) { |
|
224 | 253 | $model->afterFind(); |
|
225 | 253 | } |
|
226 | 253 | } |
|
227 | |||
228 | 259 | 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 | 24 | 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 | 219 | 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 | 312 | public function createCommand($db = null) |
|
327 | |||
328 | /** |
||
329 | * @inheritdoc |
||
330 | */ |
||
331 | 64 | protected function queryScalar($selectExpression, $db) |
|
347 | |||
348 | /** |
||
349 | * Joins with the specified relations. |
||
350 | * |
||
351 | * This method allows you to reuse existing relation definitions to perform JOIN queries. |
||
352 | * Based on the definition of the specified relation(s), the method will append one or multiple |
||
353 | * JOIN statements to the current query. |
||
354 | * |
||
355 | * If the `$eagerLoading` parameter is true, the method will also perform eager loading for the specified relations, |
||
356 | * which is equivalent to calling [[with()]] using the specified relations. |
||
357 | * |
||
358 | * Note that because a JOIN query will be performed, you are responsible to disambiguate column names. |
||
359 | * |
||
360 | * This method differs from [[with()]] in that it will build up and execute a JOIN SQL statement |
||
361 | * for the primary table. And when `$eagerLoading` is true, it will call [[with()]] in addition with the specified relations. |
||
362 | * |
||
363 | * @param string|array $with the relations to be joined. This can either be a string, representing a relation name or |
||
364 | * an array with the following semantics: |
||
365 | * |
||
366 | * - Each array element represents a single relation. |
||
367 | * - You may specify the relation name as the array key and provide an anonymous functions that |
||
368 | * can be used to modify the relation queries on-the-fly as the array value. |
||
369 | * - If a relation query does not need modification, you may use the relation name as the array value. |
||
370 | * |
||
371 | * The relation name may optionally contain an alias for the relation table (e.g. `books b`). |
||
372 | * |
||
373 | * Sub-relations can also be specified, see [[with()]] for the syntax. |
||
374 | * |
||
375 | * In the following you find some examples: |
||
376 | * |
||
377 | * ```php |
||
378 | * // find all orders that contain books, and eager loading "books" |
||
379 | * Order::find()->joinWith('books', true, 'INNER JOIN')->all(); |
||
380 | * // find all orders, eager loading "books", and sort the orders and books by the book names. |
||
381 | * Order::find()->joinWith([ |
||
382 | * 'books' => function (\yii\db\ActiveQuery $query) { |
||
383 | * $query->orderBy('item.name'); |
||
384 | * } |
||
385 | * ])->all(); |
||
386 | * // find all orders that contain books of the category 'Science fiction', using the alias "b" for the books table |
||
387 | * Order::find()->joinWith(['books b'], true, 'INNER JOIN')->where(['b.category' => 'Science fiction'])->all(); |
||
388 | * ``` |
||
389 | * |
||
390 | * The alias syntax is available since version 2.0.7. |
||
391 | * |
||
392 | * @param bool|array $eagerLoading whether to eager load the relations specified in `$with`. |
||
393 | * When this is a boolean, it applies to all relations specified in `$with`. Use an array |
||
394 | * to explicitly list which relations in `$with` need to be eagerly loaded. Defaults to `true`. |
||
395 | * @param string|array $joinType the join type of the relations specified in `$with`. |
||
396 | * When this is a string, it applies to all relations specified in `$with`. Use an array |
||
397 | * in the format of `relationName => joinType` to specify different join types for different relations. |
||
398 | * @return $this the query object itself |
||
399 | */ |
||
400 | 42 | public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN') |
|
431 | |||
432 | 36 | private function buildJoinWith() |
|
473 | |||
474 | /** |
||
475 | * Inner joins with the specified relations. |
||
476 | * This is a shortcut method to [[joinWith()]] with the join type set as "INNER JOIN". |
||
477 | * Please refer to [[joinWith()]] for detailed usage of this method. |
||
478 | * @param string|array $with the relations to be joined with. |
||
479 | * @param bool|array $eagerLoading whether to eager loading the relations. |
||
480 | * @return $this the query object itself |
||
481 | * @see joinWith() |
||
482 | */ |
||
483 | 12 | public function innerJoinWith($with, $eagerLoading = true) |
|
487 | |||
488 | /** |
||
489 | * Modifies the current query by adding join fragments based on the given relations. |
||
490 | * @param ActiveRecord $model the primary model |
||
491 | * @param array $with the relations to be joined |
||
492 | * @param string|array $joinType the join type |
||
493 | */ |
||
494 | 36 | private function joinWithRelations($model, $with, $joinType) |
|
536 | |||
537 | /** |
||
538 | * Returns the join type based on the given join type parameter and the relation name. |
||
539 | * @param string|array $joinType the given join type(s) |
||
540 | * @param string $name relation name |
||
541 | * @return string the real join type |
||
542 | */ |
||
543 | 36 | private function getJoinType($joinType, $name) |
|
551 | |||
552 | /** |
||
553 | * Returns the table name and the table alias for [[modelClass]]. |
||
554 | * @return array the table name and the table alias. |
||
555 | * @internal |
||
556 | */ |
||
557 | 51 | private function getTableNameAndAlias() |
|
558 | { |
||
559 | 51 | if (empty($this->from)) { |
|
560 | /* @var $modelClass ActiveRecord */ |
||
561 | 45 | $modelClass = $this->modelClass; |
|
562 | 45 | $tableName = $modelClass::tableName(); |
|
563 | 45 | } else { |
|
564 | 42 | $tableName = ''; |
|
565 | 42 | foreach ($this->from as $alias => $tableName) { |
|
566 | 42 | if (is_string($alias)) { |
|
567 | 15 | return [$tableName, $alias]; |
|
568 | } else { |
||
569 | 36 | break; |
|
570 | } |
||
571 | 36 | } |
|
572 | } |
||
573 | |||
574 | 48 | if (preg_match('/^(.*?)\s+({{\w+}}|\w+)$/', $tableName, $matches)) { |
|
575 | 3 | $alias = $matches[2]; |
|
576 | 3 | } else { |
|
577 | 48 | $alias = $tableName; |
|
578 | } |
||
579 | |||
580 | 48 | return [$tableName, $alias]; |
|
581 | } |
||
582 | |||
583 | /** |
||
584 | * Joins a parent query with a child query. |
||
585 | * The current query object will be modified accordingly. |
||
586 | * @param ActiveQuery $parent |
||
587 | * @param ActiveQuery $child |
||
588 | * @param string $joinType |
||
589 | */ |
||
590 | 36 | private function joinWithRelation($parent, $child, $joinType) |
|
657 | |||
658 | /** |
||
659 | * Sets the ON condition for a relational query. |
||
660 | * The condition will be used in the ON part when [[ActiveQuery::joinWith()]] is called. |
||
661 | * Otherwise, the condition will be used in the WHERE part of a query. |
||
662 | * |
||
663 | * Use this method to specify additional conditions when declaring a relation in the [[ActiveRecord]] class: |
||
664 | * |
||
665 | * ```php |
||
666 | * public function getActiveUsers() |
||
667 | * { |
||
668 | * return $this->hasMany(User::className(), ['id' => 'user_id']) |
||
669 | * ->onCondition(['active' => true]); |
||
670 | * } |
||
671 | * ``` |
||
672 | * |
||
673 | * Note that this condition is applied in case of a join as well as when fetching the related records. |
||
674 | * Thus only fields of the related table can be used in the condition. Trying to access fields of the primary |
||
675 | * record will cause an error in a non-join-query. |
||
676 | * |
||
677 | * @param string|array $condition the ON condition. Please refer to [[Query::where()]] on how to specify this parameter. |
||
678 | * @param array $params the parameters (name => value) to be bound to the query. |
||
679 | * @return $this the query object itself |
||
680 | */ |
||
681 | 21 | public function onCondition($condition, $params = []) |
|
687 | |||
688 | /** |
||
689 | * Adds an additional ON condition to the existing one. |
||
690 | * The new condition and the existing one will be joined using the 'AND' operator. |
||
691 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
692 | * on how to specify this parameter. |
||
693 | * @param array $params the parameters (name => value) to be bound to the query. |
||
694 | * @return $this the query object itself |
||
695 | * @see onCondition() |
||
696 | * @see orOnCondition() |
||
697 | */ |
||
698 | 6 | public function andOnCondition($condition, $params = []) |
|
699 | { |
||
700 | 6 | if ($this->on === null) { |
|
701 | 3 | $this->on = $condition; |
|
702 | 3 | } else { |
|
703 | 3 | $this->on = ['and', $this->on, $condition]; |
|
704 | } |
||
705 | 6 | $this->addParams($params); |
|
706 | 6 | return $this; |
|
707 | } |
||
708 | |||
709 | /** |
||
710 | * Adds an additional ON condition to the existing one. |
||
711 | * The new condition and the existing one will be joined using the 'OR' operator. |
||
712 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
713 | * on how to specify this parameter. |
||
714 | * @param array $params the parameters (name => value) to be bound to the query. |
||
715 | * @return $this the query object itself |
||
716 | * @see onCondition() |
||
717 | * @see andOnCondition() |
||
718 | */ |
||
719 | 6 | public function orOnCondition($condition, $params = []) |
|
720 | { |
||
721 | 6 | if ($this->on === null) { |
|
722 | 3 | $this->on = $condition; |
|
723 | 3 | } else { |
|
724 | 3 | $this->on = ['or', $this->on, $condition]; |
|
725 | } |
||
726 | 6 | $this->addParams($params); |
|
727 | 6 | return $this; |
|
728 | } |
||
729 | |||
730 | /** |
||
731 | * Specifies the junction table for a relational query. |
||
732 | * |
||
733 | * Use this method to specify a junction table when declaring a relation in the [[ActiveRecord]] class: |
||
734 | * |
||
735 | * ```php |
||
736 | * public function getItems() |
||
737 | * { |
||
738 | * return $this->hasMany(Item::className(), ['id' => 'item_id']) |
||
739 | * ->viaTable('order_item', ['order_id' => 'id']); |
||
740 | * } |
||
741 | * ``` |
||
742 | * |
||
743 | * @param string $tableName the name of the junction table. |
||
744 | * @param array $link the link between the junction table and the table associated with [[primaryModel]]. |
||
745 | * The keys of the array represent the columns in the junction table, and the values represent the columns |
||
746 | * in the [[primaryModel]] table. |
||
747 | * @param callable $callable a PHP callback for customizing the relation associated with the junction table. |
||
748 | * Its signature should be `function($query)`, where `$query` is the query to be customized. |
||
749 | * @return $this the query object itself |
||
750 | * @see via() |
||
751 | */ |
||
752 | 24 | public function viaTable($tableName, $link, callable $callable = null) |
|
767 | |||
768 | /** |
||
769 | * Define an alias for the table defined in [[modelClass]]. |
||
770 | * |
||
771 | * This method will adjust [[from]] so that an already defined alias will be overwritten. |
||
772 | * If none was defined, [[from]] will be populated with the given alias. |
||
773 | * |
||
774 | * @param string $alias the table alias. |
||
775 | * @return $this the query object itself |
||
776 | * @since 2.0.7 |
||
777 | */ |
||
778 | 18 | public function alias($alias) |
|
797 | } |
||
798 |
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..