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 | 519 | 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 | 519 | 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 | 204 | public function all($db = null) |
|
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | 406 | public function prepare($builder) |
|
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | 350 | public function populate($rows) |
|
235 | |||
236 | /** |
||
237 | * Removes duplicated models by checking their primary key values. |
||
238 | * This method is mainly called when a join query is performed, which may cause duplicated rows being returned. |
||
239 | * @param array $models the models to be checked |
||
240 | * @throws InvalidConfigException if model primary key is empty |
||
241 | * @return array the distinctive models |
||
242 | */ |
||
243 | 36 | private function removeDuplicatedModels($models) |
|
289 | |||
290 | /** |
||
291 | * Executes query and returns a single row of result. |
||
292 | * @param Connection|null $db the DB connection used to create the DB command. |
||
293 | * If `null`, the DB connection returned by [[modelClass]] will be used. |
||
294 | * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]], |
||
295 | * the query result may be either an array or an ActiveRecord object. `null` will be returned |
||
296 | * if the query results in nothing. |
||
297 | */ |
||
298 | 285 | public function one($db = null) |
|
308 | |||
309 | /** |
||
310 | * Creates a DB command that can be used to execute this query. |
||
311 | * @param Connection|null $db the DB connection used to create the DB command. |
||
312 | * If `null`, the DB connection returned by [[modelClass]] will be used. |
||
313 | * @return Command the created DB command instance. |
||
314 | */ |
||
315 | 406 | public function createCommand($db = null) |
|
335 | |||
336 | /** |
||
337 | * {@inheritdoc} |
||
338 | */ |
||
339 | 64 | protected function queryScalar($selectExpression, $db) |
|
359 | |||
360 | /** |
||
361 | * Joins with the specified relations. |
||
362 | * |
||
363 | * This method allows you to reuse existing relation definitions to perform JOIN queries. |
||
364 | * Based on the definition of the specified relation(s), the method will append one or multiple |
||
365 | * JOIN statements to the current query. |
||
366 | * |
||
367 | * If the `$eagerLoading` parameter is true, the method will also perform eager loading for the specified relations, |
||
368 | * which is equivalent to calling [[with()]] using the specified relations. |
||
369 | * |
||
370 | * Note that because a JOIN query will be performed, you are responsible to disambiguate column names. |
||
371 | * |
||
372 | * This method differs from [[with()]] in that it will build up and execute a JOIN SQL statement |
||
373 | * for the primary table. And when `$eagerLoading` is true, it will call [[with()]] in addition with the specified relations. |
||
374 | * |
||
375 | * @param string|array $with the relations to be joined. This can either be a string, representing a relation name or |
||
376 | * an array with the following semantics: |
||
377 | * |
||
378 | * - Each array element represents a single relation. |
||
379 | * - You may specify the relation name as the array key and provide an anonymous functions that |
||
380 | * can be used to modify the relation queries on-the-fly as the array value. |
||
381 | * - If a relation query does not need modification, you may use the relation name as the array value. |
||
382 | * |
||
383 | * The relation name may optionally contain an alias for the relation table (e.g. `books b`). |
||
384 | * |
||
385 | * Sub-relations can also be specified, see [[with()]] for the syntax. |
||
386 | * |
||
387 | * In the following you find some examples: |
||
388 | * |
||
389 | * ```php |
||
390 | * // find all orders that contain books, and eager loading "books" |
||
391 | * Order::find()->joinWith('books', true, 'INNER JOIN')->all(); |
||
392 | * // find all orders, eager loading "books", and sort the orders and books by the book names. |
||
393 | * Order::find()->joinWith([ |
||
394 | * 'books' => function (\yii\db\ActiveQuery $query) { |
||
395 | * $query->orderBy('item.name'); |
||
396 | * } |
||
397 | * ])->all(); |
||
398 | * // find all orders that contain books of the category 'Science fiction', using the alias "b" for the books table |
||
399 | * Order::find()->joinWith(['books b'], true, 'INNER JOIN')->where(['b.category' => 'Science fiction'])->all(); |
||
400 | * ``` |
||
401 | * |
||
402 | * The alias syntax is available since version 2.0.7. |
||
403 | * |
||
404 | * @param bool|array $eagerLoading whether to eager load the relations |
||
405 | * specified in `$with`. When this is a boolean, it applies to all |
||
406 | * relations specified in `$with`. Use an array to explicitly list which |
||
407 | * relations in `$with` need to be eagerly loaded. Note, that this does |
||
408 | * not mean, that the relations are populated from the query result. An |
||
409 | * extra query will still be performed to bring in the related data. |
||
410 | * Defaults to `true`. |
||
411 | * @param string|array $joinType the join type of the relations specified in `$with`. |
||
412 | * When this is a string, it applies to all relations specified in `$with`. Use an array |
||
413 | * in the format of `relationName => joinType` to specify different join types for different relations. |
||
414 | * @return $this the query object itself |
||
415 | */ |
||
416 | 57 | public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN') |
|
447 | |||
448 | 51 | private function buildJoinWith() |
|
491 | |||
492 | /** |
||
493 | * Inner joins with the specified relations. |
||
494 | * This is a shortcut method to [[joinWith()]] with the join type set as "INNER JOIN". |
||
495 | * Please refer to [[joinWith()]] for detailed usage of this method. |
||
496 | * @param string|array $with the relations to be joined with. |
||
497 | * @param bool|array $eagerLoading whether to eager load the relations. |
||
498 | * Note, that this does not mean, that the relations are populated from the |
||
499 | * query result. An extra query will still be performed to bring in the |
||
500 | * related data. |
||
501 | * @return $this the query object itself |
||
502 | * @see joinWith() |
||
503 | */ |
||
504 | 21 | public function innerJoinWith($with, $eagerLoading = true) |
|
508 | |||
509 | /** |
||
510 | * Modifies the current query by adding join fragments based on the given relations. |
||
511 | * @param ActiveRecord $model the primary model |
||
512 | * @param array $with the relations to be joined |
||
513 | * @param string|array $joinType the join type |
||
514 | */ |
||
515 | 51 | private function joinWithRelations($model, $with, $joinType) |
|
559 | |||
560 | /** |
||
561 | * Returns the join type based on the given join type parameter and the relation name. |
||
562 | * @param string|array $joinType the given join type(s) |
||
563 | * @param string $name relation name |
||
564 | * @return string the real join type |
||
565 | */ |
||
566 | 51 | private function getJoinType($joinType, $name) |
|
574 | |||
575 | /** |
||
576 | * Returns the table name and the table alias for [[modelClass]]. |
||
577 | * @return array the table name and the table alias. |
||
578 | * @since 2.0.16 |
||
579 | */ |
||
580 | 69 | protected function getTableNameAndAlias() |
|
603 | |||
604 | /** |
||
605 | * Joins a parent query with a child query. |
||
606 | * The current query object will be modified accordingly. |
||
607 | * @param ActiveQuery $parent |
||
608 | * @param ActiveQuery $child |
||
609 | * @param string $joinType |
||
610 | */ |
||
611 | 51 | private function joinWithRelation($parent, $child, $joinType) |
|
677 | |||
678 | /** |
||
679 | * Sets the ON condition for a relational query. |
||
680 | * The condition will be used in the ON part when [[ActiveQuery::joinWith()]] is called. |
||
681 | * Otherwise, the condition will be used in the WHERE part of a query. |
||
682 | * |
||
683 | * Use this method to specify additional conditions when declaring a relation in the [[ActiveRecord]] class: |
||
684 | * |
||
685 | * ```php |
||
686 | * public function getActiveUsers() |
||
687 | * { |
||
688 | * return $this->hasMany(User::className(), ['id' => 'user_id']) |
||
689 | * ->onCondition(['active' => true]); |
||
690 | * } |
||
691 | * ``` |
||
692 | * |
||
693 | * Note that this condition is applied in case of a join as well as when fetching the related records. |
||
694 | * Thus only fields of the related table can be used in the condition. Trying to access fields of the primary |
||
695 | * record will cause an error in a non-join-query. |
||
696 | * |
||
697 | * @param string|array $condition the ON condition. Please refer to [[Query::where()]] on how to specify this parameter. |
||
698 | * @param array $params the parameters (name => value) to be bound to the query. |
||
699 | * @return $this the query object itself |
||
700 | */ |
||
701 | 21 | public function onCondition($condition, $params = []) |
|
707 | |||
708 | /** |
||
709 | * Adds an additional ON condition to the existing one. |
||
710 | * The new condition and the existing one will be joined using the 'AND' operator. |
||
711 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
712 | * on how to specify this parameter. |
||
713 | * @param array $params the parameters (name => value) to be bound to the query. |
||
714 | * @return $this the query object itself |
||
715 | * @see onCondition() |
||
716 | * @see orOnCondition() |
||
717 | */ |
||
718 | 6 | public function andOnCondition($condition, $params = []) |
|
728 | |||
729 | /** |
||
730 | * Adds an additional ON condition to the existing one. |
||
731 | * The new condition and the existing one will be joined using the 'OR' operator. |
||
732 | * @param string|array $condition the new ON condition. Please refer to [[where()]] |
||
733 | * on how to specify this parameter. |
||
734 | * @param array $params the parameters (name => value) to be bound to the query. |
||
735 | * @return $this the query object itself |
||
736 | * @see onCondition() |
||
737 | * @see andOnCondition() |
||
738 | */ |
||
739 | 6 | public function orOnCondition($condition, $params = []) |
|
749 | |||
750 | /** |
||
751 | * Specifies the junction table for a relational query. |
||
752 | * |
||
753 | * Use this method to specify a junction table when declaring a relation in the [[ActiveRecord]] class: |
||
754 | * |
||
755 | * ```php |
||
756 | * public function getItems() |
||
757 | * { |
||
758 | * return $this->hasMany(Item::className(), ['id' => 'item_id']) |
||
759 | * ->viaTable('order_item', ['order_id' => 'id']); |
||
760 | * } |
||
761 | * ``` |
||
762 | * |
||
763 | * @param string $tableName the name of the junction table. |
||
764 | * @param array $link the link between the junction table and the table associated with [[primaryModel]]. |
||
765 | * The keys of the array represent the columns in the junction table, and the values represent the columns |
||
766 | * in the [[primaryModel]] table. |
||
767 | * @param callable $callable a PHP callback for customizing the relation associated with the junction table. |
||
768 | * Its signature should be `function($query)`, where `$query` is the query to be customized. |
||
769 | * @return $this the query object itself |
||
770 | * @throws InvalidConfigException when query is not initialized properly |
||
771 | * @see via() |
||
772 | */ |
||
773 | 27 | public function viaTable($tableName, $link, callable $callable = null) |
|
774 | { |
||
775 | 27 | $modelClass = $this->primaryModel ? get_class($this->primaryModel) : $this->modelClass; |
|
776 | 27 | $relation = new self($modelClass, [ |
|
777 | 27 | 'from' => [$tableName], |
|
778 | 27 | 'link' => $link, |
|
779 | 'multiple' => true, |
||
780 | 'asArray' => true, |
||
781 | ]); |
||
782 | 27 | $this->via = $relation; |
|
783 | 27 | if ($callable !== null) { |
|
784 | 6 | call_user_func($callable, $relation); |
|
785 | } |
||
786 | |||
787 | 27 | return $this; |
|
788 | } |
||
789 | |||
790 | /** |
||
791 | * Define an alias for the table defined in [[modelClass]]. |
||
792 | * |
||
793 | * This method will adjust [[from]] so that an already defined alias will be overwritten. |
||
794 | * If none was defined, [[from]] will be populated with the given alias. |
||
795 | * |
||
796 | * @param string $alias the table alias. |
||
797 | * @return $this the query object itself |
||
798 | * @since 2.0.7 |
||
799 | */ |
||
800 | 24 | public function alias($alias) |
|
818 | |||
819 | /** |
||
820 | * {@inheritdoc} |
||
821 | * @since 2.0.12 |
||
822 | */ |
||
823 | 136 | public function getTablesUsedInFrom() |
|
831 | |||
832 | /** |
||
833 | * @return string primary table name |
||
834 | * @since 2.0.12 |
||
835 | */ |
||
836 | 421 | protected function getPrimaryTableName() |
|
842 | } |
||
843 |
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..