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 AbstractConnection $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) |
|
97 | |||
98 | /** |
||
99 | * Prepares query for use. See NOTE. |
||
100 | * @param QueryBuilder $builder |
||
101 | * @return static |
||
102 | */ |
||
103 | 2 | public function prepare($builder = null) |
|
116 | |||
117 | /** |
||
118 | * @param $with |
||
119 | * @return static |
||
120 | */ |
||
121 | public function joinWith($with) |
||
127 | |||
128 | private function buildJoinWith() |
||
165 | |||
166 | /** |
||
167 | * @param ActiveRecord $model |
||
168 | * @param $with |
||
169 | */ |
||
170 | protected function joinWithRelations($model, $with) |
||
193 | |||
194 | /** |
||
195 | * Joins a parent query with a child query. |
||
196 | * The current query object will be modified accordingly. |
||
197 | * @param ActiveQuery $parent |
||
198 | * @param ActiveQuery $child |
||
199 | */ |
||
200 | private function joinWithRelation($parent, $child) |
||
208 | |||
209 | public function select($columns, $option = null) |
||
215 | |||
216 | /** |
||
217 | * @param array|string $columns |
||
218 | * @return $this |
||
219 | */ |
||
220 | public function addSelect($columns) |
||
234 | |||
235 | /** |
||
236 | * Executes query and returns a single row of result. |
||
237 | * |
||
238 | * @param AbstractConnection $db the DB connection used to create the DB command. |
||
239 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
240 | * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]], |
||
241 | * the query result may be either an array or an ActiveRecord object. Null will be returned |
||
242 | * if the query results in nothing. |
||
243 | */ |
||
244 | public function one($db = null) |
||
257 | |||
258 | /** |
||
259 | * Executes query and returns all results as an array. |
||
260 | * @param AbstractConnection $db the DB connection used to create the DB command. |
||
261 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
262 | * @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned. |
||
263 | */ |
||
264 | 2 | public function all($db = null) |
|
274 | |||
275 | 2 | public function populate($rows) |
|
293 | |||
294 | 2 | private function createModels($rows) |
|
317 | |||
318 | /** |
||
319 | * Populates joined relations from [[join]] array. |
||
320 | * |
||
321 | * @param ActiveRecord $model |
||
322 | * @param array $row |
||
323 | */ |
||
324 | 2 | public function populateJoinedRelations($model, array $row) |
|
381 | |||
382 | /** |
||
383 | * @param $relatedModel |
||
384 | */ |
||
385 | private function addInverseRelation($relatedModel) |
||
394 | } |
||
395 |
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..