| Conditions | 22 |
| Paths | 240 |
| Total Lines | 102 |
| Lines | 35 |
| Ratio | 34.31 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 261 | public function execute(): array |
||
| 262 | { |
||
| 263 | $modelClass = $this->model; |
||
| 264 | $driver = $modelClass::getDriver(); |
||
| 265 | |||
| 266 | $eagerLoadedProperties = []; |
||
| 267 | // instantiate a model so that initialize() is called and properties are filled in |
||
| 268 | // otherwise this empty model is not used |
||
| 269 | $model = new $modelClass(); |
||
|
|
|||
| 270 | $ids = []; |
||
| 271 | foreach ($this->eagerLoaded as $k) { |
||
| 272 | $eagerLoadedProperties[$k] = $modelClass::definition()->get($k); |
||
| 273 | $ids[$k] = []; |
||
| 274 | } |
||
| 275 | |||
| 276 | // fetch the models matching the query |
||
| 277 | /** @var Model[] $models */ |
||
| 278 | $models = []; |
||
| 279 | foreach ($driver->queryModels($this) as $j => $row) { |
||
| 280 | // type-cast the values because they came from the database |
||
| 281 | foreach ($row as $k => &$v) { |
||
| 282 | if ($property = $modelClass::definition()->get($k)) { |
||
| 283 | $v = Type::cast($property, $v); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | // create the model and cache the loaded values |
||
| 288 | $models[] = new $modelClass($row); |
||
| 289 | |||
| 290 | // capture any local ids for eager loading relationships |
||
| 291 | foreach ($this->eagerLoaded as $k) { |
||
| 292 | $localKey = $eagerLoadedProperties[$k]['local_key']; |
||
| 293 | if (isset($row[$localKey])) { |
||
| 294 | $ids[$k][$j] = $row[$localKey]; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | // hydrate the eager loaded relationships |
||
| 300 | foreach ($this->eagerLoaded as $k) { |
||
| 301 | $property = $eagerLoadedProperties[$k]; |
||
| 302 | $relationModelClass = $property->getForeignModelClass(); |
||
| 303 | $type = $property->getRelationshipType(); |
||
| 304 | |||
| 305 | if (Relationship::BELONGS_TO == $type) { |
||
| 306 | $relationships = $this->fetchRelationships($relationModelClass, $ids[$k], $property->getForeignKey(), false); |
||
| 307 | |||
| 308 | foreach ($ids[$k] as $j => $id) { |
||
| 309 | if (isset($relationships[$id])) { |
||
| 310 | $models[$j]->setRelation($k, $relationships[$id]); |
||
| 311 | // older style properties do not support this type of hydration |
||
| 312 | if (!$property->isPersisted()) { |
||
| 313 | $models[$j]->hydrateValue($k, $relationships[$id]); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } elseif (Relationship::HAS_ONE == $type) { |
||
| 318 | $relationships = $this->fetchRelationships($relationModelClass, $ids[$k], $property->getForeignKey(), false); |
||
| 319 | |||
| 320 | View Code Duplication | foreach ($ids[$k] as $j => $id) { |
|
| 321 | if (isset($relationships[$id])) { |
||
| 322 | $models[$j]->setRelation($k, $relationships[$id]); |
||
| 323 | // older style properties do not support this type of hydration |
||
| 324 | if (!$property->isPersisted()) { |
||
| 325 | $models[$j]->hydrateValue($k, $relationships[$id]); |
||
| 326 | } |
||
| 327 | } else { |
||
| 328 | // when using has one eager loading we must |
||
| 329 | // explicitly mark the relationship as null |
||
| 330 | // for models not found during eager loading |
||
| 331 | // or else it will trigger another DB call |
||
| 332 | $models[$j]->clearRelation($k); |
||
| 333 | |||
| 334 | // older style properties do not support this type of hydration |
||
| 335 | if (!$property->isPersisted()) { |
||
| 336 | $models[$j]->hydrateValue($k, null); |
||
| 337 | } |
||
| 338 | } |
||
| 339 | } |
||
| 340 | } elseif (Relationship::HAS_MANY == $type) { |
||
| 341 | $relationships = $this->fetchRelationships($relationModelClass, $ids[$k], $property->getForeignKey(), true); |
||
| 342 | |||
| 343 | View Code Duplication | foreach ($ids[$k] as $j => $id) { |
|
| 344 | if (isset($relationships[$id])) { |
||
| 345 | $models[$j]->setRelationCollection($k, $relationships[$id]); |
||
| 346 | // older style properties do not support this type of hydration |
||
| 347 | if (!$property->isPersisted()) { |
||
| 348 | $models[$j]->hydrateValue($k, $relationships[$id]); |
||
| 349 | } |
||
| 350 | } else { |
||
| 351 | $models[$j]->setRelationCollection($k, []); |
||
| 352 | // older style properties do not support this type of hydration |
||
| 353 | if (!$property->isPersisted()) { |
||
| 354 | $models[$j]->hydrateValue($k, []); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | } |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | return $models; |
||
| 362 | } |
||
| 363 | |||
| 525 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.