Complex classes like ActiveRelationTrait 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 ActiveRelationTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | trait ActiveRelationTrait |
||
25 | { |
||
26 | /** |
||
27 | * @var bool whether this query represents a relation to more than one record. |
||
28 | * This property is only used in relational context. If true, this relation will |
||
29 | * populate all query results into AR instances using [[Query::all()|all()]]. |
||
30 | * If false, only the first row of the results will be retrieved using [[Query::one()|one()]]. |
||
31 | */ |
||
32 | public $multiple; |
||
33 | /** |
||
34 | * @var ActiveRecord the primary model of a relational query. |
||
35 | * This is used only in lazy loading with dynamic query options. |
||
36 | */ |
||
37 | public $primaryModel; |
||
38 | /** |
||
39 | * @var array the columns of the primary and foreign tables that establish a relation. |
||
40 | * The array keys must be columns of the table for this relation, and the array values |
||
41 | * must be the corresponding columns from the primary table. |
||
42 | * Do not prefix or quote the column names as this will be done automatically by Yii. |
||
43 | * This property is only used in relational context. |
||
44 | */ |
||
45 | public $link; |
||
46 | /** |
||
47 | * @var array|object the query associated with the junction table. Please call [[via()]] |
||
48 | * to set this property instead of directly setting it. |
||
49 | * This property is only used in relational context. |
||
50 | * @see via() |
||
51 | */ |
||
52 | public $via; |
||
53 | /** |
||
54 | * @var string the name of the relation that is the inverse of this relation. |
||
55 | * For example, an order has a customer, which means the inverse of the "customer" relation |
||
56 | * is the "orders", and the inverse of the "orders" relation is the "customer". |
||
57 | * If this property is set, the primary record(s) will be referenced through the specified relation. |
||
58 | * For example, `$customer->orders[0]->customer` and `$customer` will be the same object, |
||
59 | * and accessing the customer of an order will not trigger new DB query. |
||
60 | * This property is only used in relational context. |
||
61 | * @see inverseOf() |
||
62 | */ |
||
63 | public $inverseOf; |
||
64 | |||
65 | private $viaMap; |
||
66 | |||
67 | /** |
||
68 | * Clones internal objects. |
||
69 | */ |
||
70 | 12 | public function __clone() |
|
80 | |||
81 | /** |
||
82 | * Specifies the relation associated with the junction table. |
||
83 | * |
||
84 | * Use this method to specify a pivot record/table when declaring a relation in the [[ActiveRecord]] class: |
||
85 | * |
||
86 | * ```php |
||
87 | * class Order extends ActiveRecord |
||
88 | * { |
||
89 | * public function getOrderItems() { |
||
90 | * return $this->hasMany(OrderItem::className(), ['order_id' => 'id']); |
||
91 | * } |
||
92 | * |
||
93 | * public function getItems() { |
||
94 | * return $this->hasMany(Item::className(), ['id' => 'item_id']) |
||
95 | * ->via('orderItems'); |
||
96 | * } |
||
97 | * } |
||
98 | * ``` |
||
99 | * |
||
100 | * @param string $relationName the relation name. This refers to a relation declared in [[primaryModel]]. |
||
101 | * @param callable $callable a PHP callback for customizing the relation associated with the junction table. |
||
102 | * Its signature should be `function($query)`, where `$query` is the query to be customized. |
||
103 | * @return $this the relation object itself. |
||
104 | */ |
||
105 | 72 | public function via($relationName, callable $callable = null) |
|
115 | |||
116 | /** |
||
117 | * Sets the name of the relation that is the inverse of this relation. |
||
118 | * For example, a customer has orders, which means the inverse of the "orders" relation is the "customer". |
||
119 | * If this property is set, the primary record(s) will be referenced through the specified relation. |
||
120 | * For example, `$customer->orders[0]->customer` and `$customer` will be the same object, |
||
121 | * and accessing the customer of an order will not trigger a new DB query. |
||
122 | * |
||
123 | * Use this method when declaring a relation in the [[ActiveRecord]] class, e.g. in Customer model: |
||
124 | * |
||
125 | * ```php |
||
126 | * public function getOrders() |
||
127 | * { |
||
128 | * return $this->hasMany(Order::className(), ['customer_id' => 'id'])->inverseOf('customer'); |
||
129 | * } |
||
130 | * ``` |
||
131 | * |
||
132 | * This also may be used for Order model, but with caution: |
||
133 | * |
||
134 | * ```php |
||
135 | * public function getCustomer() |
||
136 | * { |
||
137 | * return $this->hasOne(Customer::className(), ['id' => 'customer_id'])->inverseOf('orders'); |
||
138 | * } |
||
139 | * ``` |
||
140 | * |
||
141 | * in this case result will depend on how order(s) was loaded. |
||
142 | * Let's suppose customer has several orders. If only one order was loaded: |
||
143 | * |
||
144 | * ```php |
||
145 | * $orders = Order::find()->where(['id' => 1])->all(); |
||
146 | * $customerOrders = $orders[0]->customer->orders; |
||
147 | * ``` |
||
148 | * |
||
149 | * variable `$customerOrders` will contain only one order. If orders was loaded like this: |
||
150 | * |
||
151 | * ```php |
||
152 | * $orders = Order::find()->with('customer')->where(['customer_id' => 1])->all(); |
||
153 | * $customerOrders = $orders[0]->customer->orders; |
||
154 | * ``` |
||
155 | * |
||
156 | * variable `$customerOrders` will contain all orders of the customer. |
||
157 | * |
||
158 | * @param string $relationName the name of the relation that is the inverse of this relation. |
||
159 | * @return $this the relation object itself. |
||
160 | */ |
||
161 | 12 | public function inverseOf($relationName) |
|
166 | |||
167 | /** |
||
168 | * Finds the related records for the specified primary record. |
||
169 | * This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion. |
||
170 | * @param string $name the relation name |
||
171 | * @param ActiveRecordInterface|BaseActiveRecord $model the primary model |
||
172 | * @return mixed the related record(s) |
||
173 | * @throws InvalidArgumentException if the relation is invalid |
||
174 | */ |
||
175 | 73 | public function findFor($name, $model) |
|
187 | |||
188 | /** |
||
189 | * If applicable, populate the query's primary model into the related records' inverse relationship. |
||
190 | * @param array $result the array of related records as generated by [[populate()]] |
||
191 | * @since 2.0.9 |
||
192 | */ |
||
193 | 12 | private function addInverseRelations(&$result) |
|
215 | |||
216 | /** |
||
217 | * Finds the related records and populates them into the primary models. |
||
218 | * @param string $name the relation name |
||
219 | * @param array $primaryModels primary models |
||
220 | * @return array the related models |
||
221 | * @throws InvalidConfigException if [[link]] is invalid |
||
222 | */ |
||
223 | 90 | public function populateRelation($name, &$primaryModels) |
|
322 | |||
323 | /** |
||
324 | * @param ActiveRecordInterface[] $primaryModels primary models |
||
325 | * @param ActiveRecordInterface[] $models models |
||
326 | * @param string $primaryName the primary relation name |
||
327 | * @param string $name the relation name |
||
328 | */ |
||
329 | 9 | private function populateInverseRelation(&$primaryModels, $models, $primaryName, $name) |
|
384 | |||
385 | /** |
||
386 | * @param array $models |
||
387 | * @param array $link |
||
388 | * @param array $viaModels |
||
389 | * @param null|self $viaQuery |
||
390 | * @param bool $checkMultiple |
||
391 | * @return array |
||
392 | */ |
||
393 | 72 | private function buildBuckets($models, $link, $viaModels = null, $viaQuery = null, $checkMultiple = true) |
|
444 | |||
445 | /** |
||
446 | * @param array $map |
||
447 | * @param array $viaMap |
||
448 | * @return array |
||
449 | */ |
||
450 | 3 | private function mapVia($map, $viaMap) { |
|
459 | |||
460 | /** |
||
461 | * Indexes buckets by column name. |
||
462 | * |
||
463 | * @param array $buckets |
||
464 | * @param string|callable $indexBy the name of the column by which the query results should be indexed by. |
||
465 | * This can also be a callable (e.g. anonymous function) that returns the index value based on the given row data. |
||
466 | * @return array |
||
467 | */ |
||
468 | 15 | private function indexBuckets($buckets, $indexBy) |
|
481 | |||
482 | /** |
||
483 | * @param array $attributes the attributes to prefix |
||
484 | * @return array |
||
485 | */ |
||
486 | 175 | private function prefixKeyColumns($attributes) |
|
510 | |||
511 | /** |
||
512 | * @param array $models |
||
513 | */ |
||
514 | 175 | private function filterByModels($models) |
|
556 | |||
557 | /** |
||
558 | * @param ActiveRecordInterface|array $model |
||
559 | * @param array $attributes |
||
560 | * @return string |
||
561 | */ |
||
562 | 72 | private function getModelKey($model, $attributes) |
|
574 | |||
575 | /** |
||
576 | * @param mixed $value raw key value. |
||
577 | * @return string normalized key value. |
||
578 | */ |
||
579 | 72 | private function normalizeModelKey($value) |
|
588 | |||
589 | /** |
||
590 | * @param array $primaryModels either array of AR instances or arrays |
||
591 | * @return array |
||
592 | */ |
||
593 | 21 | private function findJunctionRows($primaryModels) |
|
608 | } |
||
609 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: