Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
18 | class ActiveQuery extends Query implements ActiveQueryInterface |
||
19 | { |
||
20 | use ActiveQueryTrait; |
||
21 | use ActiveRelationTrait; |
||
22 | |||
23 | /** |
||
24 | * @event Event an event that is triggered when the query is initialized via [[init()]]. |
||
25 | */ |
||
26 | const EVENT_INIT = 'init'; |
||
27 | |||
28 | /** |
||
29 | * @var array|null a list of relations that this query should be joined with |
||
30 | */ |
||
31 | public $joinWith = []; |
||
32 | |||
33 | /** |
||
34 | * Constructor. |
||
35 | * @param string $modelClass the model class associated with this query |
||
36 | * @param array $config configurations to be applied to the newly created query object |
||
37 | */ |
||
38 | 2 | public function __construct($modelClass, $config = []) |
|
44 | |||
45 | /** |
||
46 | * Initializes the object. |
||
47 | * This method is called at the end of the constructor. The default implementation will trigger |
||
48 | * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end |
||
49 | * to ensure triggering of the event. |
||
50 | */ |
||
51 | 2 | public function init() |
|
56 | |||
57 | /** |
||
58 | * Creates a DB command that can be used to execute this query. |
||
59 | * @param Connection $db the DB connection used to create the DB command. |
||
60 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
61 | * @return Command the created DB command instance |
||
62 | */ |
||
63 | 2 | public function createCommand($db = null) |
|
64 | { |
||
65 | 2 | if ($this->primaryModel !== null) { |
|
66 | // lazy loading |
||
67 | if (is_array($this->via)) { |
||
68 | // via relation |
||
69 | /* @var $viaQuery ActiveQuery */ |
||
70 | list($viaName, $viaQuery) = $this->via; |
||
71 | if ($viaQuery->multiple) { |
||
72 | $viaModels = $viaQuery->all(); |
||
73 | $this->primaryModel->populateRelation($viaName, $viaModels); |
||
74 | } else { |
||
75 | $model = $viaQuery->one(); |
||
76 | $this->primaryModel->populateRelation($viaName, $model); |
||
77 | $viaModels = $model === null ? [] : [$model]; |
||
78 | } |
||
79 | $this->filterByModels($viaModels); |
||
80 | } else { |
||
81 | $this->filterByModels([$this->primaryModel]); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /* @var $modelClass ActiveRecord */ |
||
86 | 2 | $modelClass = $this->modelClass; |
|
87 | |||
88 | 2 | if ($db === null) { |
|
89 | 2 | $db = $modelClass::getDb(); |
|
90 | 2 | } |
|
91 | 2 | if ($this->from === null) { |
|
92 | 2 | $this->from = $modelClass::from(); |
|
|
|||
93 | 2 | } |
|
94 | |||
95 | 2 | $commandConfig = $db->getQueryBuilder()->build($this); |
|
96 | |||
97 | 2 | return $db->createCommand($commandConfig); |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Prepares query for use. See NOTE. |
||
102 | * @param QueryBuilder $builder |
||
103 | * @return static |
||
104 | */ |
||
105 | 2 | public function prepare($builder = null) |
|
106 | { |
||
107 | 2 | $class = $this->modelClass; |
|
108 | 2 | $class::prepare($this, $builder); |
|
109 | |||
110 | // NOTE: because the same ActiveQuery may be used to build different SQL statements |
||
111 | // (e.g. by ActiveDataProvider, one for count query, the other for row data query, |
||
112 | // it is important to make sure the same ActiveQuery can be used to build SQL statements |
||
113 | // multiple times. |
||
114 | 2 | if (!empty($this->joinWith)) { |
|
115 | $this->buildJoinWith(); |
||
116 | $this->joinWith = null; |
||
117 | } |
||
118 | |||
119 | 2 | return $this; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param $with |
||
124 | * @return static |
||
125 | */ |
||
126 | public function joinWith($with) |
||
132 | |||
133 | private function buildJoinWith() |
||
134 | { |
||
135 | $join = $this->join; |
||
136 | $this->join = []; |
||
137 | |||
138 | $model = new $this->modelClass(); |
||
139 | |||
140 | foreach ($this->joinWith as $with) { |
||
141 | $this->joinWithRelations($model, $with); |
||
142 | |||
143 | foreach ($with as $name => $callback) { |
||
144 | if (is_int($name)) { |
||
145 | $this->innerJoin([$callback]); |
||
146 | } else { |
||
147 | $this->innerJoin([$name => $callback]); |
||
148 | } |
||
149 | |||
150 | unset($with[$name]); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | if (!empty($join)) { |
||
155 | // append explicit join to joinWith() |
||
156 | // https://github.com/yiisoft/yii2/issues/2880 |
||
157 | $this->join = empty($this->join) ? $join : array_merge($this->join, $join); |
||
158 | } |
||
159 | |||
160 | if (empty($this->select) || true) { |
||
161 | $this->addSelect(['*' => '*']); |
||
162 | foreach ($this->joinWith as $join) { |
||
163 | $key = array_shift(array_keys($join)); |
||
164 | $closure = array_shift($join); |
||
165 | |||
166 | $this->addSelect(is_int($key) ? $closure : $key); |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param ActiveRecord $model |
||
173 | * @param $with |
||
174 | */ |
||
175 | protected function joinWithRelations($model, $with) |
||
176 | { |
||
177 | foreach ($with as $name => $callback) { |
||
178 | if (is_int($name)) { |
||
179 | $name = $callback; |
||
180 | $callback = null; |
||
181 | } |
||
182 | |||
183 | $primaryModel = $model; |
||
184 | $parent = $this; |
||
185 | |||
186 | if (!isset($relations[$name])) { |
||
187 | $relations[$name] = $relation = $primaryModel->getRelation($name); |
||
188 | if ($callback !== null) { |
||
189 | call_user_func($callback, $relation); |
||
190 | } |
||
191 | if (!empty($relation->joinWith)) { |
||
192 | $relation->buildJoinWith(); |
||
193 | } |
||
194 | $this->joinWithRelation($parent, $relation); |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Joins a parent query with a child query. |
||
201 | * The current query object will be modified accordingly. |
||
202 | * @param ActiveQuery $parent |
||
203 | * @param ActiveQuery $child |
||
204 | */ |
||
205 | private function joinWithRelation($parent, $child) |
||
206 | { |
||
207 | if (!empty($child->join)) { |
||
208 | foreach ($child->join as $join) { |
||
209 | $this->join[] = $join; |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | |||
214 | public function select($columns, $option = null) |
||
220 | |||
221 | /** |
||
222 | * @param array|string $columns |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function addSelect($columns) |
||
226 | { |
||
227 | if (!is_array($columns)) { |
||
228 | $columns = (array) $columns; |
||
229 | } |
||
230 | |||
231 | if ($this->select === null) { |
||
232 | $this->select = $columns; |
||
233 | } else { |
||
234 | $this->select = array_merge($this->select, $columns); |
||
235 | } |
||
236 | |||
237 | return $this; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Executes query and returns a single row of result. |
||
242 | * |
||
243 | * @param Connection $db the DB connection used to create the DB command. |
||
244 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
245 | * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]], |
||
246 | * the query result may be either an array or an ActiveRecord object. Null will be returned |
||
247 | * if the query results in nothing. |
||
248 | */ |
||
249 | public function one($db = null) |
||
255 | |||
256 | /** |
||
257 | * Executes query and returns all results as an array. |
||
258 | * @param Connection $db the DB connection used to create the DB command. |
||
259 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
260 | * @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned. |
||
261 | */ |
||
262 | 2 | public function all($db = null) |
|
272 | |||
273 | 2 | public function populate($rows) |
|
274 | { |
||
275 | 2 | if (empty($rows)) { |
|
276 | return []; |
||
277 | } |
||
278 | |||
279 | 2 | $models = $this->createModels($rows); |
|
280 | |||
281 | 2 | if (!empty($this->with)) { |
|
282 | $this->findWith($this->with, $models); |
||
283 | } |
||
284 | |||
285 | 2 | foreach ($models as $model) { |
|
286 | 2 | $model->afterFind(); |
|
287 | 2 | } |
|
288 | |||
289 | 2 | return $models; |
|
290 | } |
||
291 | |||
292 | 2 | private function createModels($rows) |
|
293 | { |
||
294 | 2 | $models = []; |
|
295 | 2 | $class = $this->modelClass; |
|
296 | 2 | foreach ($rows as $row) { |
|
297 | 2 | $model = $class::instantiate($row); |
|
298 | 2 | $modelClass = get_class($model); |
|
299 | 2 | $modelClass::populateRecord($model, $row); |
|
300 | 2 | $this->populateJoinedRelations($model, $row); |
|
301 | 2 | if ($this->indexBy) { |
|
302 | View Code Duplication | if ($this->indexBy instanceof \Closure) { |
|
303 | $key = call_user_func($this->indexBy, $model); |
||
304 | } else { |
||
305 | $key = $model->{$this->indexBy}; |
||
306 | } |
||
307 | $models[$key] = $model; |
||
308 | } else { |
||
309 | 2 | $models[] = $model; |
|
310 | } |
||
311 | 2 | } |
|
312 | |||
313 | 2 | return $models; |
|
314 | } |
||
315 | |||
316 | /** |
||
317 | * Populates joined relations from [[join]] array. |
||
318 | * |
||
319 | * @param ActiveRecord $model |
||
320 | * @param array $row |
||
321 | */ |
||
322 | 2 | public function populateJoinedRelations($model, array $row) |
|
323 | { |
||
324 | 2 | foreach ($row as $key => $value) { |
|
325 | 2 | if (empty($this->join) || !is_array($value) || $model->hasAttribute($key)) { |
|
326 | 2 | continue; |
|
327 | } |
||
328 | foreach ($this->join as $join) { |
||
329 | $name = array_shift(array_keys($join)); |
||
330 | $closure = array_shift($join); |
||
331 | |||
332 | if (is_int($name)) { |
||
333 | $name = $closure; |
||
334 | $closure = null; |
||
335 | } |
||
336 | if ($name !== $key) { |
||
337 | continue; |
||
338 | } |
||
339 | if ($model->isRelationPopulated($name)) { |
||
340 | continue 2; |
||
341 | } |
||
342 | $records = []; |
||
343 | $relation = $model->getRelation($name); |
||
344 | $relationClass = $relation->modelClass; |
||
345 | if ($closure !== null) { |
||
346 | call_user_func($closure, $relation); |
||
347 | } |
||
348 | $relation->prepare(); |
||
349 | |||
350 | if ($relation->multiple) { |
||
351 | foreach ($value as $item) { |
||
352 | $relatedModel = $relationClass::instantiate($item); |
||
353 | $relatedModelClass = get_class($relatedModel); |
||
354 | $relatedModelClass::populateRecord($relatedModel, $item); |
||
355 | $relation->populateJoinedRelations($relatedModel, $item); |
||
356 | $relation->addInverseRelation($relatedModel, $model); |
||
357 | if ($relation->indexBy !== null) { |
||
358 | $index = is_string($relation->indexBy) |
||
359 | ? $relatedModel[$relation->indexBy] |
||
360 | : call_user_func($relation->indexBy, $relatedModel); |
||
361 | $records[$index] = $relatedModel; |
||
362 | } else { |
||
363 | $records[] = $relatedModel; |
||
364 | } |
||
365 | } |
||
366 | } else { |
||
367 | $relatedModel = $relationClass::instantiate($value); |
||
368 | $relatedModelClass = get_class($relatedModel); |
||
369 | $relatedModelClass::populateRecord($relatedModel, $value); |
||
370 | $relation->populateJoinedRelations($relatedModel, $value); |
||
371 | $relation->addInverseRelation($relatedModel, $model); |
||
372 | $records = $relatedModel; |
||
373 | } |
||
374 | |||
375 | $model->populateRelation($name, $records); |
||
376 | } |
||
377 | 2 | } |
|
378 | 2 | } |
|
379 | |||
380 | /** |
||
381 | * @param $relatedModel |
||
382 | */ |
||
383 | private function addInverseRelation($relatedModel) |
||
392 | } |
||
393 |
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..