Complex classes like ObjectHydrator 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 ObjectHydrator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class ObjectHydrator extends AbstractHydrator |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | private $identifierMap = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | private $resultPointers = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $idTemplate = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var integer |
||
| 59 | */ |
||
| 60 | private $resultCounter = 0; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $rootAliases = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private $initializedCollections = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private $existingCollections = array(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritdoc} |
||
| 79 | */ |
||
| 80 | 657 | protected function prepare() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | 651 | protected function cleanup() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | 650 | protected function hydrateAllData() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Initializes a related collection. |
||
| 175 | * |
||
| 176 | * @param object $entity The entity to which the collection belongs. |
||
| 177 | * @param ClassMetadata $class |
||
| 178 | * @param string $fieldName The name of the field on the entity that holds the collection. |
||
| 179 | * @param string $parentDqlAlias Alias of the parent fetch joining this collection. |
||
| 180 | * |
||
| 181 | * @return \Doctrine\ORM\PersistentCollection |
||
| 182 | */ |
||
| 183 | 99 | private function initRelatedCollection($entity, $class, $fieldName, $parentDqlAlias) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Gets an entity instance. |
||
| 224 | * |
||
| 225 | * @param array $data The instance data. |
||
| 226 | * @param string $dqlAlias The DQL alias of the entity's class. |
||
| 227 | * |
||
| 228 | * @return object The entity. |
||
| 229 | * |
||
| 230 | * @throws HydrationException |
||
| 231 | */ |
||
| 232 | 586 | private function getEntity(array $data, $dqlAlias) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $className |
||
| 275 | * @param array $data |
||
| 276 | * |
||
| 277 | * @return mixed |
||
| 278 | */ |
||
| 279 | 34 | private function getEntityFromIdentityMap($className, array $data) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Hydrates a single row in an SQL result set. |
||
| 304 | * |
||
| 305 | * @internal |
||
| 306 | * First, the data of the row is split into chunks where each chunk contains data |
||
| 307 | * that belongs to a particular component/class. Afterwards, all these chunks |
||
| 308 | * are processed, one after the other. For each chunk of class data only one of the |
||
| 309 | * following code paths is executed: |
||
| 310 | * |
||
| 311 | * Path A: The data chunk belongs to a joined/associated object and the association |
||
| 312 | * is collection-valued. |
||
| 313 | * Path B: The data chunk belongs to a joined/associated object and the association |
||
| 314 | * is single-valued. |
||
| 315 | * Path C: The data chunk belongs to a root result element/object that appears in the topmost |
||
| 316 | * level of the hydrated result. A typical example are the objects of the type |
||
| 317 | * specified by the FROM clause in a DQL query. |
||
| 318 | * |
||
| 319 | * @param array $row The data of the row to process. |
||
| 320 | * @param array $result The result array to fill. |
||
| 321 | * |
||
| 322 | * @return void |
||
| 323 | */ |
||
| 324 | 621 | protected function hydrateRowData(array $row, array &$result) |
|
| 325 | { |
||
| 326 | // Initialize |
||
| 327 | 621 | $id = $this->idTemplate; // initialize the id-memory |
|
| 328 | 621 | $nonemptyComponents = array(); |
|
| 329 | // Split the row data into chunks of class data. |
||
| 330 | 621 | $rowData = $this->gatherRowData($row, $id, $nonemptyComponents); |
|
| 331 | |||
| 332 | // Hydrate the data chunks |
||
| 333 | 621 | foreach ($rowData['data'] as $dqlAlias => $data) { |
|
| 334 | 586 | $entityName = $this->_rsm->aliasMap[$dqlAlias]; |
|
| 335 | |||
| 336 | 586 | if (isset($this->_rsm->parentAliasMap[$dqlAlias])) { |
|
| 337 | // It's a joined result |
||
| 338 | |||
| 339 | 320 | $parentAlias = $this->_rsm->parentAliasMap[$dqlAlias]; |
|
| 340 | // we need the $path to save into the identifier map which entities were already |
||
| 341 | // seen for this parent-child relationship |
||
| 342 | 320 | $path = $parentAlias . '.' . $dqlAlias; |
|
| 343 | |||
| 344 | // We have a RIGHT JOIN result here. Doctrine cannot hydrate RIGHT JOIN Object-Graphs |
||
| 345 | 320 | if ( ! isset($nonemptyComponents[$parentAlias])) { |
|
| 346 | // TODO: Add special case code where we hydrate the right join objects into identity map at least |
||
| 347 | 2 | continue; |
|
| 348 | } |
||
| 349 | |||
| 350 | 320 | $parentClass = $this->_metadataCache[$this->_rsm->aliasMap[$parentAlias]]; |
|
| 351 | 320 | $relationField = $this->_rsm->relationMap[$dqlAlias]; |
|
| 352 | 320 | $relation = $parentClass->associationMappings[$relationField]; |
|
| 353 | 320 | $reflField = $parentClass->reflFields[$relationField]; |
|
| 354 | |||
| 355 | // Get a reference to the parent object to which the joined element belongs. |
||
| 356 | 320 | if ($this->_rsm->isMixed && isset($this->rootAliases[$parentAlias])) { |
|
| 357 | 17 | $first = reset($this->resultPointers); |
|
| 358 | 17 | $parentObject = $first[key($first)]; |
|
| 359 | 305 | } else if (isset($this->resultPointers[$parentAlias])) { |
|
| 360 | 305 | $parentObject = $this->resultPointers[$parentAlias]; |
|
| 361 | } else { |
||
| 362 | // Parent object of relation not found, mark as not-fetched again |
||
| 363 | 1 | $element = $this->getEntity($data, $dqlAlias); |
|
| 364 | |||
| 365 | // Update result pointer and provide initial fetch data for parent |
||
| 366 | 1 | $this->resultPointers[$dqlAlias] = $element; |
|
| 367 | 1 | $rowData['data'][$parentAlias][$relationField] = $element; |
|
| 368 | |||
| 369 | // Mark as not-fetched again |
||
| 370 | 1 | unset($this->_hints['fetched'][$parentAlias][$relationField]); |
|
| 371 | 1 | continue; |
|
| 372 | } |
||
| 373 | |||
| 374 | 320 | $oid = spl_object_hash($parentObject); |
|
| 375 | |||
| 376 | // Check the type of the relation (many or single-valued) |
||
| 377 | 320 | if ( ! ($relation['type'] & ClassMetadata::TO_ONE)) { |
|
| 378 | // PATH A: Collection-valued association |
||
| 379 | 100 | $reflFieldValue = $reflField->getValue($parentObject); |
|
| 380 | |||
| 381 | 100 | if (isset($nonemptyComponents[$dqlAlias])) { |
|
| 382 | 96 | $collKey = $oid . $relationField; |
|
| 383 | 96 | if (isset($this->initializedCollections[$collKey])) { |
|
| 384 | 44 | $reflFieldValue = $this->initializedCollections[$collKey]; |
|
| 385 | 96 | } else if ( ! isset($this->existingCollections[$collKey])) { |
|
| 386 | 96 | $reflFieldValue = $this->initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias); |
|
| 387 | } |
||
| 388 | |||
| 389 | 96 | $indexExists = isset($this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]]); |
|
| 390 | 96 | $index = $indexExists ? $this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] : false; |
|
| 391 | 96 | $indexIsValid = $index !== false ? isset($reflFieldValue[$index]) : false; |
|
| 392 | |||
| 393 | 96 | if ( ! $indexExists || ! $indexIsValid) { |
|
| 394 | 96 | if (isset($this->existingCollections[$collKey])) { |
|
| 395 | // Collection exists, only look for the element in the identity map. |
||
| 396 | 34 | if ($element = $this->getEntityFromIdentityMap($entityName, $data)) { |
|
| 397 | $this->resultPointers[$dqlAlias] = $element; |
||
| 398 | } else { |
||
| 399 | 34 | unset($this->resultPointers[$dqlAlias]); |
|
| 400 | } |
||
| 401 | } else { |
||
| 402 | 64 | $element = $this->getEntity($data, $dqlAlias); |
|
| 403 | |||
| 404 | 64 | if (isset($this->_rsm->indexByMap[$dqlAlias])) { |
|
| 405 | 11 | $indexValue = $row[$this->_rsm->indexByMap[$dqlAlias]]; |
|
| 406 | 11 | $reflFieldValue->hydrateSet($indexValue, $element); |
|
| 407 | 11 | $this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $indexValue; |
|
| 408 | } else { |
||
| 409 | 53 | $reflFieldValue->hydrateAdd($element); |
|
| 410 | 53 | $reflFieldValue->last(); |
|
| 411 | 53 | $this->identifierMap[$path][$id[$parentAlias]][$id[$dqlAlias]] = $reflFieldValue->key(); |
|
| 412 | } |
||
| 413 | // Update result pointer |
||
| 414 | 96 | $this->resultPointers[$dqlAlias] = $element; |
|
| 415 | } |
||
| 416 | } else { |
||
| 417 | // Update result pointer |
||
| 418 | 96 | $this->resultPointers[$dqlAlias] = $reflFieldValue[$index]; |
|
| 419 | } |
||
| 420 | 9 | } else if ( ! $reflFieldValue) { |
|
| 421 | 5 | $reflFieldValue = $this->initRelatedCollection($parentObject, $parentClass, $relationField, $parentAlias); |
|
|
|
|||
| 422 | 6 | } else if ($reflFieldValue instanceof PersistentCollection && $reflFieldValue->isInitialized() === false) { |
|
| 423 | 100 | $reflFieldValue->setInitialized(true); |
|
| 424 | } |
||
| 425 | |||
| 426 | } else { |
||
| 427 | // PATH B: Single-valued association |
||
| 428 | 240 | $reflFieldValue = $reflField->getValue($parentObject); |
|
| 429 | |||
| 430 | 240 | if ( ! $reflFieldValue || isset($this->_hints[Query::HINT_REFRESH]) || ($reflFieldValue instanceof Proxy && !$reflFieldValue->__isInitialized__)) { |
|
| 431 | // we only need to take action if this value is null, |
||
| 432 | // we refresh the entity or its an unitialized proxy. |
||
| 433 | 225 | if (isset($nonemptyComponents[$dqlAlias])) { |
|
| 434 | 116 | $element = $this->getEntity($data, $dqlAlias); |
|
| 435 | 115 | $reflField->setValue($parentObject, $element); |
|
| 436 | 115 | $this->_uow->setOriginalEntityProperty($oid, $relationField, $element); |
|
| 437 | 115 | $targetClass = $this->_metadataCache[$relation['targetEntity']]; |
|
| 438 | |||
| 439 | 115 | if ($relation['isOwningSide']) { |
|
| 440 | // TODO: Just check hints['fetched'] here? |
||
| 441 | // If there is an inverse mapping on the target class its bidirectional |
||
| 442 | 51 | if ($relation['inversedBy']) { |
|
| 443 | 34 | $inverseAssoc = $targetClass->associationMappings[$relation['inversedBy']]; |
|
| 444 | 34 | if ($inverseAssoc['type'] & ClassMetadata::TO_ONE) { |
|
| 445 | 12 | $targetClass->reflFields[$inverseAssoc['fieldName']]->setValue($element, $parentObject); |
|
| 446 | 34 | $this->_uow->setOriginalEntityProperty(spl_object_hash($element), $inverseAssoc['fieldName'], $parentObject); |
|
| 447 | } |
||
| 448 | 18 | } else if ($parentClass === $targetClass && $relation['mappedBy']) { |
|
| 449 | // Special case: bi-directional self-referencing one-one on the same class |
||
| 450 | 51 | $targetClass->reflFields[$relationField]->setValue($element, $parentObject); |
|
| 451 | } |
||
| 452 | } else { |
||
| 453 | // For sure bidirectional, as there is no inverse side in unidirectional mappings |
||
| 454 | 67 | $targetClass->reflFields[$relation['mappedBy']]->setValue($element, $parentObject); |
|
| 455 | 67 | $this->_uow->setOriginalEntityProperty(spl_object_hash($element), $relation['mappedBy'], $parentObject); |
|
| 456 | } |
||
| 457 | // Update result pointer |
||
| 458 | 115 | $this->resultPointers[$dqlAlias] = $element; |
|
| 459 | } else { |
||
| 460 | 130 | $this->_uow->setOriginalEntityProperty($oid, $relationField, null); |
|
| 461 | 224 | $reflField->setValue($parentObject, null); |
|
| 462 | } |
||
| 463 | // else leave $reflFieldValue null for single-valued associations |
||
| 464 | } else { |
||
| 465 | // Update result pointer |
||
| 466 | 319 | $this->resultPointers[$dqlAlias] = $reflFieldValue; |
|
| 467 | } |
||
| 468 | } |
||
| 469 | } else { |
||
| 470 | // PATH C: Its a root result element |
||
| 471 | 586 | $this->rootAliases[$dqlAlias] = true; // Mark as root alias |
|
| 472 | 586 | $entityKey = $this->_rsm->entityMappings[$dqlAlias] ?: 0; |
|
| 473 | |||
| 474 | // if this row has a NULL value for the root result id then make it a null result. |
||
| 475 | 586 | if ( ! isset($nonemptyComponents[$dqlAlias]) ) { |
|
| 476 | 3 | if ($this->_rsm->isMixed) { |
|
| 477 | 2 | $result[] = array($entityKey => null); |
|
| 478 | } else { |
||
| 479 | 1 | $result[] = null; |
|
| 480 | } |
||
| 481 | 3 | $resultKey = $this->resultCounter; |
|
| 482 | 3 | ++$this->resultCounter; |
|
| 483 | 3 | continue; |
|
| 484 | } |
||
| 485 | |||
| 486 | // check for existing result from the iterations before |
||
| 487 | 586 | if ( ! isset($this->identifierMap[$dqlAlias][$id[$dqlAlias]])) { |
|
| 488 | 586 | $element = $this->getEntity($data, $dqlAlias); |
|
| 489 | |||
| 490 | 584 | if ($this->_rsm->isMixed) { |
|
| 491 | 40 | $element = array($entityKey => $element); |
|
| 492 | } |
||
| 493 | |||
| 494 | 584 | if (isset($this->_rsm->indexByMap[$dqlAlias])) { |
|
| 495 | 26 | $resultKey = $row[$this->_rsm->indexByMap[$dqlAlias]]; |
|
| 496 | |||
| 497 | 26 | if (isset($this->_hints['collection'])) { |
|
| 498 | 10 | $this->_hints['collection']->hydrateSet($resultKey, $element); |
|
| 499 | } |
||
| 500 | |||
| 501 | 26 | $result[$resultKey] = $element; |
|
| 502 | } else { |
||
| 503 | 567 | $resultKey = $this->resultCounter; |
|
| 504 | 567 | ++$this->resultCounter; |
|
| 505 | |||
| 506 | 567 | if (isset($this->_hints['collection'])) { |
|
| 507 | 109 | $this->_hints['collection']->hydrateAdd($element); |
|
| 508 | } |
||
| 509 | |||
| 510 | 567 | $result[] = $element; |
|
| 511 | } |
||
| 512 | |||
| 513 | 584 | $this->identifierMap[$dqlAlias][$id[$dqlAlias]] = $resultKey; |
|
| 514 | |||
| 515 | // Update result pointer |
||
| 516 | 584 | $this->resultPointers[$dqlAlias] = $element; |
|
| 517 | |||
| 518 | } else { |
||
| 519 | // Update result pointer |
||
| 520 | 75 | $index = $this->identifierMap[$dqlAlias][$id[$dqlAlias]]; |
|
| 521 | 75 | $this->resultPointers[$dqlAlias] = $result[$index]; |
|
| 522 | 75 | $resultKey = $index; |
|
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | 584 | if (isset($this->_hints[Query::HINT_INTERNAL_ITERATION]) && $this->_hints[Query::HINT_INTERNAL_ITERATION]) { |
|
| 527 | 584 | $this->_uow->hydrationComplete(); |
|
| 528 | } |
||
| 529 | } |
||
| 530 | |||
| 531 | 618 | if ( ! isset($resultKey) ) { |
|
| 532 | 35 | $this->resultCounter++; |
|
| 533 | } |
||
| 534 | |||
| 535 | // Append scalar values to mixed result sets |
||
| 536 | 618 | if (isset($rowData['scalars'])) { |
|
| 537 | 54 | if ( ! isset($resultKey) ) { |
|
| 538 | 22 | $resultKey = (isset($this->_rsm->indexByMap['scalars'])) |
|
| 539 | 2 | ? $row[$this->_rsm->indexByMap['scalars']] |
|
| 540 | 22 | : $this->resultCounter - 1; |
|
| 541 | } |
||
| 542 | |||
| 543 | 54 | foreach ($rowData['scalars'] as $name => $value) { |
|
| 544 | 54 | $result[$resultKey][$name] = $value; |
|
| 545 | } |
||
| 546 | } |
||
| 547 | |||
| 548 | // Append new object to mixed result sets |
||
| 549 | 618 | if (isset($rowData['newObjects'])) { |
|
| 550 | 19 | if ( ! isset($resultKey) ) { |
|
| 551 | 13 | $resultKey = $this->resultCounter - 1; |
|
| 552 | } |
||
| 553 | |||
| 554 | |||
| 555 | 19 | $scalarCount = (isset($rowData['scalars'])? count($rowData['scalars']): 0); |
|
| 556 | |||
| 557 | 19 | foreach ($rowData['newObjects'] as $objIndex => $newObject) { |
|
| 558 | 19 | $class = $newObject['class']; |
|
| 559 | 19 | $args = $newObject['args']; |
|
| 560 | 19 | $obj = $class->newInstanceArgs($args); |
|
| 561 | |||
| 562 | 19 | if ($scalarCount == 0 && count($rowData['newObjects']) == 1 ) { |
|
| 563 | 10 | $result[$resultKey] = $obj; |
|
| 564 | |||
| 565 | 10 | continue; |
|
| 566 | } |
||
| 567 | |||
| 568 | 9 | $result[$resultKey][$objIndex] = $obj; |
|
| 569 | } |
||
| 570 | } |
||
| 571 | 618 | } |
|
| 572 | |||
| 573 | /** |
||
| 574 | * When executed in a hydrate() loop we may have to clear internal state to |
||
| 575 | * decrease memory consumption. |
||
| 576 | * |
||
| 577 | * @param mixed $eventArgs |
||
| 578 | * |
||
| 579 | * @return void |
||
| 580 | */ |
||
| 581 | 4 | public function onClear($eventArgs) |
|
| 589 | } |
||
| 590 |
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.