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 |
||
17 | class ActiveQuery extends Query implements ActiveQueryInterface |
||
18 | { |
||
19 | use ActiveQueryTrait; |
||
20 | use ActiveRelationTrait; |
||
21 | |||
22 | /** |
||
23 | * @event Event an event that is triggered when the query is initialized via [[init()]]. |
||
24 | */ |
||
25 | const EVENT_INIT = 'init'; |
||
26 | |||
27 | /** |
||
28 | * @var array|null a list of relations that this query should be joined with |
||
29 | */ |
||
30 | public $joinWith = []; |
||
31 | |||
32 | /** |
||
33 | * Constructor. |
||
34 | * @param string $modelClass the model class associated with this query |
||
35 | * @param array $config configurations to be applied to the newly created query object |
||
36 | */ |
||
37 | 2 | public function __construct($modelClass, $config = []) |
|
43 | |||
44 | /** |
||
45 | * Initializes the object. |
||
46 | * This method is called at the end of the constructor. The default implementation will trigger |
||
47 | * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end |
||
48 | * to ensure triggering of the event. |
||
49 | */ |
||
50 | 2 | public function init() |
|
55 | |||
56 | /** |
||
57 | * Creates a DB command that can be used to execute this query. |
||
58 | * @param Connection $db the DB connection used to create the DB command. |
||
59 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
60 | * @return Command the created DB command instance |
||
61 | */ |
||
62 | 2 | public function createCommand($db = null) |
|
98 | |||
99 | /** |
||
100 | * Prepares query for use. See NOTE. |
||
101 | * @param QueryBuilder $builder |
||
102 | * @return static |
||
103 | */ |
||
104 | 2 | public function prepare($builder = null) |
|
120 | |||
121 | /** |
||
122 | * @param $with |
||
123 | * @return static |
||
124 | */ |
||
125 | public function joinWith($with) |
||
131 | |||
132 | private function buildJoinWith() |
||
169 | |||
170 | /** |
||
171 | * @param ActiveRecord $model |
||
172 | * @param $with |
||
173 | */ |
||
174 | protected function joinWithRelations($model, $with) |
||
197 | |||
198 | /** |
||
199 | * Joins a parent query with a child query. |
||
200 | * The current query object will be modified accordingly. |
||
201 | * @param ActiveQuery $parent |
||
202 | * @param ActiveQuery $child |
||
203 | */ |
||
204 | private function joinWithRelation($parent, $child) |
||
212 | |||
213 | public function select($columns, $option = null) |
||
219 | |||
220 | /** |
||
221 | * @param array|string $columns |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function addSelect($columns) |
||
238 | |||
239 | /** |
||
240 | * Executes query and returns a single row of result. |
||
241 | * |
||
242 | * @param Connection $db the DB connection used to create the DB command. |
||
243 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
244 | * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]], |
||
245 | * the query result may be either an array or an ActiveRecord object. Null will be returned |
||
246 | * if the query results in nothing. |
||
247 | */ |
||
248 | public function one($db = null) |
||
254 | |||
255 | /** |
||
256 | * Executes query and returns all results as an array. |
||
257 | * @param Connection $db the DB connection used to create the DB command. |
||
258 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
259 | * @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned. |
||
260 | */ |
||
261 | 2 | public function all($db = null) |
|
262 | { |
||
263 | 2 | if ($this->asArray) { |
|
264 | return parent::all($db); |
||
265 | } |
||
266 | |||
267 | 2 | $rows = $this->searchAll(); |
|
268 | |||
269 | 2 | return $this->populate($rows); |
|
270 | } |
||
271 | |||
272 | 2 | public function populate($rows) |
|
273 | { |
||
274 | 2 | if (empty($rows)) { |
|
275 | return []; |
||
276 | } |
||
277 | |||
278 | 2 | $models = $this->createModels($rows); |
|
279 | |||
280 | 2 | if (!empty($this->with)) { |
|
281 | $this->findWith($this->with, $models); |
||
282 | } |
||
283 | |||
284 | 2 | foreach ($models as $model) { |
|
285 | 2 | $model->afterFind(); |
|
286 | 2 | } |
|
287 | |||
288 | 2 | return $models; |
|
289 | } |
||
290 | |||
291 | 2 | private function createModels($rows) |
|
292 | { |
||
293 | 2 | $models = []; |
|
294 | 2 | $class = $this->modelClass; |
|
295 | 2 | foreach ($rows as $row) { |
|
296 | 2 | $model = $class::instantiate($row); |
|
297 | 2 | $modelClass = get_class($model); |
|
298 | 2 | $modelClass::populateRecord($model, $row); |
|
299 | 2 | $this->populateJoinedRelations($model, $row); |
|
300 | 2 | if ($this->indexBy) { |
|
301 | View Code Duplication | if ($this->indexBy instanceof \Closure) { |
|
302 | $key = call_user_func($this->indexBy, $model); |
||
303 | } else { |
||
304 | $key = $model->{$this->indexBy}; |
||
305 | } |
||
306 | $models[$key] = $model; |
||
307 | } else { |
||
308 | 2 | $models[] = $model; |
|
309 | } |
||
310 | 2 | } |
|
311 | |||
312 | 2 | return $models; |
|
313 | } |
||
314 | |||
315 | /** |
||
316 | * Populates joined relations from [[join]] array. |
||
317 | * |
||
318 | * @param ActiveRecord $model |
||
319 | * @param array $row |
||
320 | */ |
||
321 | 2 | public function populateJoinedRelations($model, array $row) |
|
322 | { |
||
323 | 2 | foreach ($row as $key => $value) { |
|
324 | 2 | if (empty($this->join) || !is_array($value) || $model->hasAttribute($key)) { |
|
325 | 2 | continue; |
|
326 | } |
||
327 | foreach ($this->join as $join) { |
||
328 | $name = array_shift(array_keys($join)); |
||
329 | $closure = array_shift($join); |
||
330 | |||
331 | if (is_int($name)) { |
||
332 | $name = $closure; |
||
333 | $closure = null; |
||
334 | } |
||
335 | if ($name !== $key) { |
||
336 | continue; |
||
337 | } |
||
338 | if ($model->isRelationPopulated($name)) { |
||
339 | continue 2; |
||
340 | } |
||
341 | $records = []; |
||
342 | $relation = $model->getRelation($name); |
||
343 | $relationClass = $relation->modelClass; |
||
344 | if ($closure !== null) { |
||
345 | call_user_func($closure, $relation); |
||
346 | } |
||
347 | $relation->prepare(); |
||
348 | |||
349 | if ($relation->multiple) { |
||
350 | foreach ($value as $item) { |
||
351 | $relatedModel = $relationClass::instantiate($item); |
||
352 | $relatedModelClass = get_class($relatedModel); |
||
353 | $relatedModelClass::populateRecord($relatedModel, $item); |
||
354 | $relation->populateJoinedRelations($relatedModel, $item); |
||
355 | $relation->addInverseRelation($relatedModel, $model); |
||
356 | if ($relation->indexBy !== null) { |
||
357 | $index = is_string($relation->indexBy) |
||
358 | ? $relatedModel[$relation->indexBy] |
||
359 | : call_user_func($relation->indexBy, $relatedModel); |
||
360 | $records[$index] = $relatedModel; |
||
361 | } else { |
||
362 | $records[] = $relatedModel; |
||
363 | } |
||
364 | } |
||
365 | } else { |
||
366 | $relatedModel = $relationClass::instantiate($value); |
||
367 | $relatedModelClass = get_class($relatedModel); |
||
368 | $relatedModelClass::populateRecord($relatedModel, $value); |
||
369 | $relation->populateJoinedRelations($relatedModel, $value); |
||
370 | $relation->addInverseRelation($relatedModel, $model); |
||
371 | $records = $relatedModel; |
||
372 | } |
||
373 | |||
374 | $model->populateRelation($name, $records); |
||
375 | } |
||
376 | 2 | } |
|
377 | 2 | } |
|
378 | |||
379 | /** |
||
380 | * @param $relatedModel |
||
381 | */ |
||
382 | private function addInverseRelation($relatedModel) |
||
391 | } |
||
392 |
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..