Complex classes like EntityManager 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 EntityManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 63 | /* final */class EntityManager implements EntityManagerInterface |
||
| 64 | { |
||
| 65 | /** |
||
| 66 | * The used Configuration. |
||
| 67 | * |
||
| 68 | * @var \Doctrine\ORM\Configuration |
||
| 69 | */ |
||
| 70 | private $config; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The database connection used by the EntityManager. |
||
| 74 | * |
||
| 75 | * @var \Doctrine\DBAL\Connection |
||
| 76 | */ |
||
| 77 | private $conn; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The metadata factory, used to retrieve the ORM metadata of entity classes. |
||
| 81 | * |
||
| 82 | * @var \Doctrine\ORM\Mapping\ClassMetadataFactory |
||
| 83 | */ |
||
| 84 | private $metadataFactory; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The UnitOfWork used to coordinate object-level transactions. |
||
| 88 | * |
||
| 89 | * @var \Doctrine\ORM\UnitOfWork |
||
| 90 | */ |
||
| 91 | private $unitOfWork; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The event manager that is the central point of the event system. |
||
| 95 | * |
||
| 96 | * @var \Doctrine\Common\EventManager |
||
| 97 | */ |
||
| 98 | private $eventManager; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The proxy factory used to create dynamic proxies. |
||
| 102 | * |
||
| 103 | * @var \Doctrine\ORM\Proxy\ProxyFactory |
||
| 104 | */ |
||
| 105 | private $proxyFactory; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The repository factory used to create dynamic repositories. |
||
| 109 | * |
||
| 110 | * @var \Doctrine\ORM\Repository\RepositoryFactory |
||
| 111 | */ |
||
| 112 | private $repositoryFactory; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The expression builder instance used to generate query expressions. |
||
| 116 | * |
||
| 117 | * @var \Doctrine\ORM\Query\Expr |
||
| 118 | */ |
||
| 119 | private $expressionBuilder; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Whether the EntityManager is closed or not. |
||
| 123 | * |
||
| 124 | * @var bool |
||
| 125 | */ |
||
| 126 | private $closed = false; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Collection of query filters. |
||
| 130 | * |
||
| 131 | * @var \Doctrine\ORM\Query\FilterCollection |
||
| 132 | */ |
||
| 133 | private $filterCollection; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var \Doctrine\ORM\Cache The second level cache regions API. |
||
| 137 | */ |
||
| 138 | private $cache; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Creates a new EntityManager that operates on the given database connection |
||
| 142 | * and uses the given Configuration and EventManager implementations. |
||
| 143 | * |
||
| 144 | * @param \Doctrine\DBAL\Connection $conn |
||
| 145 | * @param \Doctrine\ORM\Configuration $config |
||
| 146 | * @param \Doctrine\Common\EventManager $eventManager |
||
| 147 | */ |
||
| 148 | 2301 | protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritDoc} |
||
| 178 | */ |
||
| 179 | 1793 | public function getConnection() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Gets the metadata factory used to gather the metadata of classes. |
||
| 186 | * |
||
| 187 | * @return \Doctrine\ORM\Mapping\ClassMetadataFactory |
||
| 188 | */ |
||
| 189 | 2301 | public function getMetadataFactory() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritDoc} |
||
| 196 | */ |
||
| 197 | 17 | public function getExpressionBuilder() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritDoc} |
||
| 208 | */ |
||
| 209 | 1 | public function beginTransaction() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritDoc} |
||
| 216 | */ |
||
| 217 | 212 | public function getCache() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritDoc} |
||
| 224 | */ |
||
| 225 | 4 | public function transactional($func) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritDoc} |
||
| 250 | */ |
||
| 251 | public function commit() |
||
| 252 | { |
||
| 253 | $this->conn->commit(); |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritDoc} |
||
| 258 | */ |
||
| 259 | public function rollback() |
||
| 260 | { |
||
| 261 | $this->conn->rollBack(); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns the ORM metadata descriptor for a class. |
||
| 266 | * |
||
| 267 | * The class name must be the fully-qualified class name without a leading backslash |
||
| 268 | * (as it is returned by get_class($obj)) or an aliased class name. |
||
| 269 | * |
||
| 270 | * Examples: |
||
| 271 | * MyProject\Domain\User |
||
| 272 | * sales:PriceRequest |
||
| 273 | * |
||
| 274 | * Internal note: Performance-sensitive method. |
||
| 275 | * |
||
| 276 | * @param string $className |
||
| 277 | * |
||
| 278 | * @return \Doctrine\ORM\Mapping\ClassMetadata |
||
| 279 | */ |
||
| 280 | 1858 | public function getClassMetadata($className) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritDoc} |
||
| 287 | */ |
||
| 288 | 865 | public function createQuery($dql = '') |
|
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritDoc} |
||
| 301 | */ |
||
| 302 | 1 | public function createNamedQuery($name) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * {@inheritDoc} |
||
| 309 | */ |
||
| 310 | 19 | public function createNativeQuery($sql, ResultSetMapping $rsm) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * {@inheritDoc} |
||
| 322 | */ |
||
| 323 | 1 | public function createNamedNativeQuery($name) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritDoc} |
||
| 332 | */ |
||
| 333 | 110 | public function createQueryBuilder() |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Flushes all changes to objects that have been queued up to now to the database. |
||
| 340 | * This effectively synchronizes the in-memory state of managed objects with the |
||
| 341 | * database. |
||
| 342 | * |
||
| 343 | * If an entity is explicitly passed to this method only this entity and |
||
| 344 | * the cascade-persist semantics + scheduled inserts/removals are synchronized. |
||
| 345 | * |
||
| 346 | * @param null|object|array $entity |
||
| 347 | * |
||
| 348 | * @return void |
||
| 349 | * |
||
| 350 | * @throws \Doctrine\ORM\OptimisticLockException If a version check on an entity that |
||
| 351 | * makes use of optimistic locking fails. |
||
| 352 | * @throws ORMException |
||
| 353 | */ |
||
| 354 | 971 | public function flush($entity = null) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Finds an Entity by its identifier. |
||
| 363 | * |
||
| 364 | * @param string $entityName The class name of the entity to find. |
||
| 365 | * @param mixed $id The identity of the entity to find. |
||
| 366 | * @param integer|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants |
||
| 367 | * or NULL if no specific lock mode should be used |
||
| 368 | * during the search. |
||
| 369 | * @param integer|null $lockVersion The version of the entity to find when using |
||
| 370 | * optimistic locking. |
||
| 371 | * |
||
| 372 | * @return object|null The entity instance or NULL if the entity can not be found. |
||
| 373 | * |
||
| 374 | * @throws OptimisticLockException |
||
| 375 | * @throws ORMInvalidArgumentException |
||
| 376 | * @throws TransactionRequiredException |
||
| 377 | * @throws ORMException |
||
| 378 | */ |
||
| 379 | 277 | public function find($entityName, $id, $lockMode = null, $lockVersion = null) |
|
| 380 | { |
||
| 381 | 277 | $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
|
| 382 | |||
| 383 | 277 | if ( ! is_array($id)) { |
|
| 384 | 261 | if ($class->isIdentifierComposite) { |
|
|
|
|||
| 385 | throw ORMInvalidArgumentException::invalidCompositeIdentifier(); |
||
| 386 | } |
||
| 387 | |||
| 388 | 261 | $id = array($class->identifier[0] => $id); |
|
| 389 | } |
||
| 390 | |||
| 391 | 277 | foreach ($id as $i => $value) { |
|
| 392 | 277 | if (is_object($value) && $this->metadataFactory->hasMetadataFor(ClassUtils::getClass($value))) { |
|
| 393 | 1 | $id[$i] = $this->unitOfWork->getSingleIdentifierValue($value); |
|
| 394 | |||
| 395 | 1 | if ($id[$i] === null) { |
|
| 396 | 277 | throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); |
|
| 397 | } |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | 276 | $sortedId = array(); |
|
| 402 | |||
| 403 | 276 | foreach ($class->identifier as $identifier) { |
|
| 404 | 276 | if ( ! isset($id[$identifier])) { |
|
| 405 | 233 | throw ORMException::missingIdentifierField($class->name, $identifier); |
|
| 406 | } |
||
| 407 | |||
| 408 | 43 | $sortedId[$identifier] = $id[$identifier]; |
|
| 409 | 43 | unset($id[$identifier]); |
|
| 410 | } |
||
| 411 | |||
| 412 | 43 | if ($id) { |
|
| 413 | 1 | throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id)); |
|
| 414 | } |
||
| 415 | |||
| 416 | 42 | $unitOfWork = $this->getUnitOfWork(); |
|
| 417 | |||
| 418 | // Check identity map first |
||
| 419 | 42 | if (($entity = $unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) { |
|
| 420 | 2 | if ( ! ($entity instanceof $class->name)) { |
|
| 421 | return null; |
||
| 422 | } |
||
| 423 | |||
| 424 | switch (true) { |
||
| 425 | 2 | case LockMode::OPTIMISTIC === $lockMode: |
|
| 426 | $this->lock($entity, $lockMode, $lockVersion); |
||
| 427 | break; |
||
| 428 | |||
| 429 | 2 | case LockMode::NONE === $lockMode: |
|
| 430 | 2 | case LockMode::PESSIMISTIC_READ === $lockMode: |
|
| 431 | 2 | case LockMode::PESSIMISTIC_WRITE === $lockMode: |
|
| 432 | $persister = $unitOfWork->getEntityPersister($class->name); |
||
| 433 | $persister->refresh($sortedId, $entity, $lockMode); |
||
| 434 | break; |
||
| 435 | } |
||
| 436 | |||
| 437 | 2 | return $entity; // Hit! |
|
| 438 | } |
||
| 439 | |||
| 440 | 41 | $persister = $unitOfWork->getEntityPersister($class->name); |
|
| 441 | |||
| 442 | switch (true) { |
||
| 443 | 41 | case LockMode::OPTIMISTIC === $lockMode: |
|
| 444 | 1 | if ( ! $class->isVersioned) { |
|
| 445 | 1 | throw OptimisticLockException::notVersioned($class->name); |
|
| 446 | } |
||
| 447 | |||
| 448 | $entity = $persister->load($sortedId); |
||
| 449 | |||
| 450 | $unitOfWork->lock($entity, $lockMode, $lockVersion); |
||
| 451 | |||
| 452 | return $entity; |
||
| 453 | |||
| 454 | 40 | case LockMode::PESSIMISTIC_READ === $lockMode: |
|
| 455 | 39 | case LockMode::PESSIMISTIC_WRITE === $lockMode: |
|
| 456 | 2 | if ( ! $this->getConnection()->isTransactionActive()) { |
|
| 457 | 2 | throw TransactionRequiredException::transactionRequired(); |
|
| 458 | } |
||
| 459 | |||
| 460 | return $persister->load($sortedId, null, null, array(), $lockMode); |
||
| 461 | |||
| 462 | default: |
||
| 463 | 38 | return $persister->loadById($sortedId); |
|
| 464 | } |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * {@inheritDoc} |
||
| 469 | */ |
||
| 470 | 46 | public function getReference($entityName, $id) |
|
| 471 | { |
||
| 472 | 46 | $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
|
| 473 | |||
| 474 | 46 | if ( ! is_array($id)) { |
|
| 475 | 43 | $id = array($class->identifier[0] => $id); |
|
| 476 | } |
||
| 477 | |||
| 478 | 46 | $sortedId = array(); |
|
| 479 | |||
| 480 | 46 | foreach ($class->identifier as $identifier) { |
|
| 481 | 46 | if ( ! isset($id[$identifier])) { |
|
| 482 | 39 | throw ORMException::missingIdentifierField($class->name, $identifier); |
|
| 483 | } |
||
| 484 | |||
| 485 | 7 | $sortedId[$identifier] = $id[$identifier]; |
|
| 486 | 7 | unset($id[$identifier]); |
|
| 487 | } |
||
| 488 | |||
| 489 | 7 | if ($id) { |
|
| 490 | 1 | throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id)); |
|
| 491 | } |
||
| 492 | |||
| 493 | // Check identity map first, if its already in there just return it. |
||
| 494 | 6 | if (($entity = $this->unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) { |
|
| 495 | return ($entity instanceof $class->name) ? $entity : null; |
||
| 496 | } |
||
| 497 | |||
| 498 | 6 | if ($class->subClasses) { |
|
| 499 | return $this->find($entityName, $sortedId); |
||
| 500 | } |
||
| 501 | |||
| 502 | 6 | $entity = $this->proxyFactory->getProxy($class->name, $sortedId); |
|
| 503 | |||
| 504 | 6 | $this->unitOfWork->registerManaged($entity, $sortedId, array()); |
|
| 505 | |||
| 506 | 6 | return $entity; |
|
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * {@inheritDoc} |
||
| 511 | */ |
||
| 512 | 3 | public function getPartialReference($entityName, $identifier) |
|
| 513 | { |
||
| 514 | 3 | $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
|
| 515 | |||
| 516 | // Check identity map first, if its already in there just return it. |
||
| 517 | 3 | if (($entity = $this->unitOfWork->tryGetById($identifier, $class->rootEntityName)) !== false) { |
|
| 518 | return ($entity instanceof $class->name) ? $entity : null; |
||
| 519 | } |
||
| 520 | |||
| 521 | 3 | if ( ! is_array($identifier)) { |
|
| 522 | 3 | $identifier = array($class->identifier[0] => $identifier); |
|
| 523 | } |
||
| 524 | |||
| 525 | 3 | $entity = $class->newInstance(); |
|
| 526 | |||
| 527 | 3 | $class->setIdentifierValues($entity, $identifier); |
|
| 528 | |||
| 529 | 3 | $this->unitOfWork->registerManaged($entity, $identifier, array()); |
|
| 530 | 1 | $this->unitOfWork->markReadOnly($entity); |
|
| 531 | |||
| 532 | 1 | return $entity; |
|
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Clears the EntityManager. All entities that are currently managed |
||
| 537 | * by this EntityManager become detached. |
||
| 538 | * |
||
| 539 | * @param string|null $entityName if given, only entities of this type will get detached |
||
| 540 | * |
||
| 541 | * @return void |
||
| 542 | */ |
||
| 543 | 1177 | public function clear($entityName = null) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * {@inheritDoc} |
||
| 550 | */ |
||
| 551 | 8 | public function close() |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Tells the EntityManager to make an instance managed and persistent. |
||
| 560 | * |
||
| 561 | * The entity will be entered into the database at or before transaction |
||
| 562 | * commit or as a result of the flush operation. |
||
| 563 | * |
||
| 564 | * NOTE: The persist operation always considers entities that are not yet known to |
||
| 565 | * this EntityManager as NEW. Do not pass detached entities to the persist operation. |
||
| 566 | * |
||
| 567 | * @param object $entity The instance to make managed and persistent. |
||
| 568 | * |
||
| 569 | * @return void |
||
| 570 | * |
||
| 571 | * @throws ORMInvalidArgumentException |
||
| 572 | * @throws ORMException |
||
| 573 | */ |
||
| 574 | 969 | public function persist($entity) |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Removes an entity instance. |
||
| 587 | * |
||
| 588 | * A removed entity will be removed from the database at or before transaction commit |
||
| 589 | * or as a result of the flush operation. |
||
| 590 | * |
||
| 591 | * @param object $entity The entity instance to remove. |
||
| 592 | * |
||
| 593 | * @return void |
||
| 594 | * |
||
| 595 | * @throws ORMInvalidArgumentException |
||
| 596 | * @throws ORMException |
||
| 597 | */ |
||
| 598 | 15 | public function remove($entity) |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Refreshes the persistent state of an entity from the database, |
||
| 611 | * overriding any local changes that have not yet been persisted. |
||
| 612 | * |
||
| 613 | * @param object $entity The entity to refresh. |
||
| 614 | * |
||
| 615 | * @return void |
||
| 616 | * |
||
| 617 | * @throws ORMInvalidArgumentException |
||
| 618 | * @throws ORMException |
||
| 619 | */ |
||
| 620 | 14 | public function refresh($entity) |
|
| 621 | { |
||
| 622 | 14 | if ( ! is_object($entity)) { |
|
| 623 | 1 | throw ORMInvalidArgumentException::invalidObject('EntityManager#refresh()', $entity); |
|
| 624 | } |
||
| 625 | |||
| 626 | 13 | $this->errorIfClosed(); |
|
| 627 | |||
| 628 | 12 | $this->unitOfWork->refresh($entity); |
|
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Detaches an entity from the EntityManager, causing a managed entity to |
||
| 633 | * become detached. Unflushed changes made to the entity if any |
||
| 634 | * (including removal of the entity), will not be synchronized to the database. |
||
| 635 | * Entities which previously referenced the detached entity will continue to |
||
| 636 | * reference it. |
||
| 637 | * |
||
| 638 | * @param object $entity The entity to detach. |
||
| 639 | * |
||
| 640 | * @return void |
||
| 641 | * |
||
| 642 | * @throws ORMInvalidArgumentException |
||
| 643 | */ |
||
| 644 | 9 | public function detach($entity) |
|
| 652 | |||
| 653 | /** |
||
| 654 | * Merges the state of a detached entity into the persistence context |
||
| 655 | * of this EntityManager and returns the managed copy of the entity. |
||
| 656 | * The entity passed to merge will not become associated/managed with this EntityManager. |
||
| 657 | * |
||
| 658 | * @param object $entity The detached entity to merge into the persistence context. |
||
| 659 | * |
||
| 660 | * @return object The managed copy of the entity. |
||
| 661 | * |
||
| 662 | * @throws ORMInvalidArgumentException |
||
| 663 | * @throws ORMException |
||
| 664 | */ |
||
| 665 | 26 | public function merge($entity) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * {@inheritDoc} |
||
| 678 | * |
||
| 679 | * @todo Implementation need. This is necessary since $e2 = clone $e1; throws an E_FATAL when access anything on $e: |
||
| 680 | * Fatal error: Maximum function nesting level of '100' reached, aborting! |
||
| 681 | */ |
||
| 682 | public function copy($entity, $deep = false) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * {@inheritDoc} |
||
| 689 | */ |
||
| 690 | 7 | public function lock($entity, $lockMode, $lockVersion = null) |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Gets the repository for an entity class. |
||
| 697 | * |
||
| 698 | * @param string $entityName The name of the entity. |
||
| 699 | * |
||
| 700 | * @return \Doctrine\ORM\EntityRepository The repository class. |
||
| 701 | */ |
||
| 702 | 133 | public function getRepository($entityName) |
|
| 706 | |||
| 707 | /** |
||
| 708 | * Determines whether an entity instance is managed in this EntityManager. |
||
| 709 | * |
||
| 710 | * @param object $entity |
||
| 711 | * |
||
| 712 | * @return boolean TRUE if this EntityManager currently manages the given entity, FALSE otherwise. |
||
| 713 | */ |
||
| 714 | 12 | public function contains($entity) |
|
| 720 | |||
| 721 | /** |
||
| 722 | * {@inheritDoc} |
||
| 723 | */ |
||
| 724 | 2301 | public function getEventManager() |
|
| 728 | |||
| 729 | /** |
||
| 730 | * {@inheritDoc} |
||
| 731 | */ |
||
| 732 | 2301 | public function getConfiguration() |
|
| 736 | |||
| 737 | /** |
||
| 738 | * Throws an exception if the EntityManager is closed or currently not active. |
||
| 739 | * |
||
| 740 | * @return void |
||
| 741 | * |
||
| 742 | * @throws ORMException If the EntityManager is closed. |
||
| 743 | */ |
||
| 744 | 984 | private function errorIfClosed() |
|
| 750 | |||
| 751 | /** |
||
| 752 | * {@inheritDoc} |
||
| 753 | */ |
||
| 754 | 1 | public function isOpen() |
|
| 758 | |||
| 759 | /** |
||
| 760 | * {@inheritDoc} |
||
| 761 | */ |
||
| 762 | 2301 | public function getUnitOfWork() |
|
| 766 | |||
| 767 | /** |
||
| 768 | * {@inheritDoc} |
||
| 769 | */ |
||
| 770 | public function getHydrator($hydrationMode) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * {@inheritDoc} |
||
| 777 | */ |
||
| 778 | 458 | public function newHydrator($hydrationMode) |
|
| 804 | |||
| 805 | /** |
||
| 806 | * {@inheritDoc} |
||
| 807 | */ |
||
| 808 | 5 | public function getProxyFactory() |
|
| 812 | |||
| 813 | /** |
||
| 814 | * {@inheritDoc} |
||
| 815 | */ |
||
| 816 | public function initializeObject($obj) |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Factory method to create EntityManager instances. |
||
| 823 | * |
||
| 824 | * @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
||
| 825 | * @param Configuration $config The Configuration instance to use. |
||
| 826 | * @param EventManager $eventManager The EventManager instance to use. |
||
| 827 | * |
||
| 828 | * @return EntityManager The created EntityManager. |
||
| 829 | * |
||
| 830 | * @throws \InvalidArgumentException |
||
| 831 | * @throws ORMException |
||
| 832 | */ |
||
| 833 | 1189 | public static function create($connection, Configuration $config, EventManager $eventManager = null) |
|
| 834 | { |
||
| 835 | 1189 | if ( ! $config->getMetadataDriverImpl()) { |
|
| 836 | throw ORMException::missingMappingDriverImpl(); |
||
| 837 | } |
||
| 838 | |||
| 839 | 1189 | $connection = static::createConnection($connection, $config, $eventManager); |
|
| 840 | |||
| 841 | 1189 | return new EntityManager($connection, $config, $connection->getEventManager()); |
|
| 842 | } |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Factory method to create Connection instances. |
||
| 846 | * |
||
| 847 | * @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
||
| 848 | * @param Configuration $config The Configuration instance to use. |
||
| 849 | * @param EventManager $eventManager The EventManager instance to use. |
||
| 850 | * |
||
| 851 | * @return Connection |
||
| 852 | * |
||
| 853 | * @throws \InvalidArgumentException |
||
| 854 | * @throws ORMException |
||
| 855 | */ |
||
| 856 | 1189 | protected static function createConnection($connection, Configuration $config, EventManager $eventManager = null) |
|
| 857 | { |
||
| 858 | 1189 | if (is_array($connection)) { |
|
| 859 | return DriverManager::getConnection($connection, $config, $eventManager ?: new EventManager()); |
||
| 860 | } |
||
| 861 | |||
| 862 | 1189 | if ( ! $connection instanceof Connection) { |
|
| 863 | throw new \InvalidArgumentException("Invalid argument: " . $connection); |
||
| 864 | } |
||
| 865 | |||
| 866 | 1189 | if ($eventManager !== null && $connection->getEventManager() !== $eventManager) { |
|
| 867 | throw ORMException::mismatchedEventManager(); |
||
| 868 | } |
||
| 869 | |||
| 870 | 1189 | return $connection; |
|
| 871 | } |
||
| 872 | |||
| 873 | /** |
||
| 874 | * {@inheritDoc} |
||
| 875 | */ |
||
| 876 | 140 | public function getFilters() |
|
| 884 | |||
| 885 | /** |
||
| 886 | * {@inheritDoc} |
||
| 887 | */ |
||
| 888 | 29 | public function isFiltersStateClean() |
|
| 892 | |||
| 893 | /** |
||
| 894 | * {@inheritDoc} |
||
| 895 | */ |
||
| 896 | 675 | public function hasFilters() |
|
| 900 | } |
||
| 901 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: