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) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Prepares query for use. See NOTE. |
||
| 102 | * @param QueryBuilder $builder |
||
| 103 | * @return static |
||
| 104 | */ |
||
| 105 | 2 | public function prepare($builder = null) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param $with |
||
| 121 | * @return static |
||
| 122 | */ |
||
| 123 | public function joinWith($with) |
||
| 129 | |||
| 130 | private function buildJoinWith() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param ActiveRecord $model |
||
| 170 | * @param $with |
||
| 171 | */ |
||
| 172 | protected function joinWithRelations($model, $with) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Joins a parent query with a child query. |
||
| 198 | * The current query object will be modified accordingly. |
||
| 199 | * @param ActiveQuery $parent |
||
| 200 | * @param ActiveQuery $child |
||
| 201 | */ |
||
| 202 | private function joinWithRelation($parent, $child) |
||
| 210 | |||
| 211 | public function select($columns, $option = null) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param array|string $columns |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | public function addSelect($columns) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Executes query and returns a single row of result. |
||
| 239 | * |
||
| 240 | * @param AbstractConnection $db the DB connection used to create the DB command. |
||
| 241 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
| 242 | * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]], |
||
| 243 | * the query result may be either an array or an ActiveRecord object. Null will be returned |
||
| 244 | * if the query results in nothing. |
||
| 245 | */ |
||
| 246 | public function one($db = null) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Executes query and returns all results as an array. |
||
| 259 | * @param AbstractConnection $db the DB connection used to create the DB command. |
||
| 260 | * If null, the DB connection returned by [[modelClass]] will be used. |
||
| 261 | * @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned. |
||
| 262 | */ |
||
| 263 | 2 | public function all($db = null) |
|
| 264 | { |
||
| 265 | 2 | if ($this->asArray) { |
|
| 266 | return parent::all($db); |
||
| 267 | } |
||
| 268 | |||
| 269 | 2 | $rows = $this->searchAll(); |
|
| 270 | |||
| 271 | 2 | return $this->populate($rows); |
|
| 272 | } |
||
| 273 | |||
| 274 | 2 | public function populate($rows) |
|
| 275 | { |
||
| 276 | 2 | if (empty($rows)) { |
|
| 277 | return []; |
||
| 278 | } |
||
| 279 | |||
| 280 | 2 | $models = $this->createModels($rows); |
|
| 281 | |||
| 282 | 2 | if (!empty($this->with)) { |
|
| 283 | $this->findWith($this->with, $models); |
||
| 284 | } |
||
| 285 | |||
| 286 | 2 | foreach ($models as $model) { |
|
| 287 | 2 | $model->afterFind(); |
|
| 288 | 2 | } |
|
| 289 | |||
| 290 | 2 | return $models; |
|
| 291 | } |
||
| 292 | |||
| 293 | 2 | private function createModels($rows) |
|
| 294 | { |
||
| 295 | 2 | $models = []; |
|
| 296 | 2 | $class = $this->modelClass; |
|
| 297 | 2 | foreach ($rows as $row) { |
|
| 298 | 2 | $model = $class::instantiate($row); |
|
| 299 | 2 | $modelClass = get_class($model); |
|
| 300 | 2 | $modelClass::populateRecord($model, $row); |
|
| 301 | 2 | $this->populateJoinedRelations($model, $row); |
|
| 302 | 2 | if ($this->indexBy) { |
|
| 303 | View Code Duplication | if ($this->indexBy instanceof \Closure) { |
|
| 304 | $key = call_user_func($this->indexBy, $model); |
||
| 305 | } else { |
||
| 306 | $key = $model->{$this->indexBy}; |
||
| 307 | } |
||
| 308 | $models[$key] = $model; |
||
| 309 | } else { |
||
| 310 | 2 | $models[] = $model; |
|
| 311 | } |
||
| 312 | 2 | } |
|
| 313 | |||
| 314 | 2 | return $models; |
|
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Populates joined relations from [[join]] array. |
||
| 319 | * |
||
| 320 | * @param ActiveRecord $model |
||
| 321 | * @param array $row |
||
| 322 | */ |
||
| 323 | 2 | public function populateJoinedRelations($model, array $row) |
|
| 324 | { |
||
| 325 | 2 | foreach ($row as $key => $value) { |
|
| 326 | 2 | if (empty($this->join) || !is_array($value) || $model->hasAttribute($key)) { |
|
| 327 | 2 | continue; |
|
| 328 | } |
||
| 329 | foreach ($this->join as $join) { |
||
| 330 | $name = array_shift(array_keys($join)); |
||
| 331 | $closure = array_shift($join); |
||
| 332 | |||
| 333 | if (is_int($name)) { |
||
| 334 | $name = $closure; |
||
| 335 | $closure = null; |
||
| 336 | } |
||
| 337 | if ($name !== $key) { |
||
| 338 | continue; |
||
| 339 | } |
||
| 340 | if ($model->isRelationPopulated($name)) { |
||
| 341 | continue 2; |
||
| 342 | } |
||
| 343 | $records = []; |
||
| 344 | $relation = $model->getRelation($name); |
||
| 345 | $relationClass = $relation->modelClass; |
||
| 346 | if ($closure !== null) { |
||
| 347 | call_user_func($closure, $relation); |
||
| 348 | } |
||
| 349 | $relation->prepare(); |
||
| 350 | |||
| 351 | if ($relation->multiple) { |
||
| 352 | foreach ($value as $item) { |
||
| 353 | $relatedModel = $relationClass::instantiate($item); |
||
| 354 | $relatedModelClass = get_class($relatedModel); |
||
| 355 | $relatedModelClass::populateRecord($relatedModel, $item); |
||
| 356 | $relation->populateJoinedRelations($relatedModel, $item); |
||
| 357 | $relation->addInverseRelation($relatedModel, $model); |
||
| 358 | if ($relation->indexBy !== null) { |
||
| 359 | $index = is_string($relation->indexBy) |
||
| 360 | ? $relatedModel[$relation->indexBy] |
||
| 361 | : call_user_func($relation->indexBy, $relatedModel); |
||
| 362 | $records[$index] = $relatedModel; |
||
| 363 | } else { |
||
| 364 | $records[] = $relatedModel; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } else { |
||
| 368 | $relatedModel = $relationClass::instantiate($value); |
||
| 369 | $relatedModelClass = get_class($relatedModel); |
||
| 370 | $relatedModelClass::populateRecord($relatedModel, $value); |
||
| 371 | $relation->populateJoinedRelations($relatedModel, $value); |
||
| 372 | $relation->addInverseRelation($relatedModel, $model); |
||
| 373 | $records = $relatedModel; |
||
| 374 | } |
||
| 375 | |||
| 376 | $model->populateRelation($name, $records); |
||
| 377 | } |
||
| 378 | 2 | } |
|
| 379 | 2 | } |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @param $relatedModel |
||
| 383 | */ |
||
| 384 | private function addInverseRelation($relatedModel) |
||
| 393 | } |
||
| 394 |
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..