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 | 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 | 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 | public function createCommand($db = null) |
||
98 | |||
99 | /** |
||
100 | * Prepares query for use. See NOTE. |
||
101 | * @param QueryBuilder $builder |
||
102 | * @return static |
||
103 | */ |
||
104 | 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 | public function all($db = null) |
||
271 | |||
272 | public function populate($rows) |
||
290 | |||
291 | private function createModels($rows) |
||
314 | |||
315 | /** |
||
316 | * Populates joined relations from [[join]] array. |
||
317 | * |
||
318 | * @param ActiveRecord $model |
||
319 | * @param array $row |
||
320 | */ |
||
321 | public function populateJoinedRelations($model, array $row) |
||
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..