Complex classes like EntityManagerMock 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 EntityManagerMock, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class EntityManagerMock implements EntityManagerInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var \Doctrine\ORM\UnitOfWork|null |
||
| 28 | */ |
||
| 29 | private $uowMock; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \Doctrine\ORM\Proxy\Factory\ProxyFactory|null |
||
| 33 | */ |
||
| 34 | private $proxyFactoryMock; |
||
| 35 | |||
| 36 | /** @var EntityManagerInterface $em */ |
||
| 37 | private $em; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * EntityManagerMock constructor. |
||
| 41 | * |
||
| 42 | * @param EntityManagerInterface $em |
||
| 43 | */ |
||
| 44 | public function __construct(EntityManagerInterface $em) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritdoc} |
||
| 51 | */ |
||
| 52 | public function getUnitOfWork() |
||
| 56 | |||
| 57 | /* Mock API */ |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Sets a (mock) UnitOfWork that will be returned when getUnitOfWork() is called. |
||
| 61 | * |
||
| 62 | * @param \Doctrine\ORM\UnitOfWork $uow |
||
| 63 | * |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | public function setUnitOfWork($uow) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param \Doctrine\ORM\Proxy\Factory\ProxyFactory $proxyFactory |
||
| 73 | * |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | public function setProxyFactory($proxyFactory) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return \Doctrine\ORM\Proxy\Factory\ProxyFactory |
||
| 83 | */ |
||
| 84 | public function getProxyFactory() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Mock factory method to create an EntityManager. |
||
| 91 | * |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | public static function create($conn, Configuration $config = null, EventManager $eventManager = null) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns the cache API for managing the second level cache regions or NULL if the cache is not enabled. |
||
| 110 | * |
||
| 111 | * @return \Doctrine\ORM\Cache|null |
||
| 112 | */ |
||
| 113 | public function getCache() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Gets the database connection object used by the EntityManager. |
||
| 120 | * |
||
| 121 | * @return \Doctrine\DBAL\Connection |
||
| 122 | */ |
||
| 123 | public function getConnection() |
||
| 127 | |||
| 128 | |||
| 129 | /** |
||
| 130 | * @return Query\Expr |
||
| 131 | */ |
||
| 132 | public function getExpressionBuilder() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Gets an IdentifierFlattener used for converting Entities into an array of identifier values. |
||
| 139 | * |
||
| 140 | * @return IdentifierFlattener |
||
| 141 | */ |
||
| 142 | public function getIdentifierFlattener() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Starts a transaction on the underlying database connection. |
||
| 149 | * |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | public function beginTransaction() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Executes a function in a transaction. |
||
| 159 | * |
||
| 160 | * The function gets passed this EntityManager instance as an (optional) parameter. |
||
| 161 | * |
||
| 162 | * {@link flush} is invoked prior to transaction commit. |
||
| 163 | * |
||
| 164 | * If an exception occurs during execution of the function or flushing or transaction commit, |
||
| 165 | * the transaction is rolled back, the EntityManager closed and the exception re-thrown. |
||
| 166 | * |
||
| 167 | * @param callable $func The function to execute transactionally. |
||
| 168 | * |
||
| 169 | * @return mixed The value returned from the closure. |
||
| 170 | * |
||
| 171 | * @throws \Throwable |
||
| 172 | */ |
||
| 173 | public function transactional(callable $func) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Commits a transaction on the underlying database connection. |
||
| 180 | * |
||
| 181 | * @return void |
||
| 182 | */ |
||
| 183 | public function commit() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Performs a rollback on the underlying database connection. |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | public function rollback() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Creates a new Query object. |
||
| 200 | * |
||
| 201 | * @param string $dql The DQL string. |
||
| 202 | * |
||
| 203 | * @return Query |
||
| 204 | */ |
||
| 205 | public function createQuery($dql = '') |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Creates a Query from a named query. |
||
| 212 | * |
||
| 213 | * @param string $name |
||
| 214 | * |
||
| 215 | * @return Query |
||
| 216 | */ |
||
| 217 | public function createNamedQuery($name) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Creates a native SQL query. |
||
| 224 | * |
||
| 225 | * @param string $sql |
||
| 226 | * @param ResultSetMapping $rsm The ResultSetMapping to use. |
||
| 227 | * |
||
| 228 | * @return NativeQuery |
||
| 229 | */ |
||
| 230 | public function createNativeQuery($sql, ResultSetMapping $rsm) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Creates a NativeQuery from a named native query. |
||
| 237 | * |
||
| 238 | * @param string $name |
||
| 239 | * |
||
| 240 | * @return NativeQuery |
||
| 241 | */ |
||
| 242 | public function createNamedNativeQuery($name) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Create a QueryBuilder instance |
||
| 249 | * |
||
| 250 | * @return QueryBuilder |
||
| 251 | */ |
||
| 252 | public function createQueryBuilder() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Gets a reference to the entity identified by the given type and identifier |
||
| 259 | * without actually loading it, if the entity is not yet loaded. |
||
| 260 | * |
||
| 261 | * @param string $entityName The name of the entity type. |
||
| 262 | * @param mixed $id The entity identifier. |
||
| 263 | * |
||
| 264 | * @return object The entity reference. |
||
| 265 | * |
||
| 266 | * @throws ORMException |
||
| 267 | */ |
||
| 268 | public function getReference($entityName, $id) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Gets a partial reference to the entity identified by the given type and identifier |
||
| 275 | * without actually loading it, if the entity is not yet loaded. |
||
| 276 | * |
||
| 277 | * The returned reference may be a partial object if the entity is not yet loaded/managed. |
||
| 278 | * If it is a partial object it will not initialize the rest of the entity state on access. |
||
| 279 | * Thus you can only ever safely access the identifier of an entity obtained through |
||
| 280 | * this method. |
||
| 281 | * |
||
| 282 | * The use-cases for partial references involve maintaining bidirectional associations |
||
| 283 | * without loading one side of the association or to update an entity without loading it. |
||
| 284 | * Note, however, that in the latter case the original (persistent) entity data will |
||
| 285 | * never be visible to the application (especially not event listeners) as it will |
||
| 286 | * never be loaded in the first place. |
||
| 287 | * |
||
| 288 | * @param string $entityName The name of the entity type. |
||
| 289 | * @param mixed $identifier The entity identifier. |
||
| 290 | * |
||
| 291 | * @return object The (partial) entity reference. |
||
| 292 | */ |
||
| 293 | public function getPartialReference($entityName, $identifier) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Closes the EntityManager. All entities that are currently managed |
||
| 300 | * by this EntityManager become detached. The EntityManager may no longer |
||
| 301 | * be used after it is closed. |
||
| 302 | * |
||
| 303 | * @return void |
||
| 304 | */ |
||
| 305 | public function close() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Creates a copy of the given entity. Can create a shallow or a deep copy. |
||
| 312 | * |
||
| 313 | * @param object $entity The entity to copy. |
||
| 314 | * @param boolean $deep FALSE for a shallow copy, TRUE for a deep copy. |
||
| 315 | * |
||
| 316 | * @return object The new entity. |
||
| 317 | * |
||
| 318 | * @throws \BadMethodCallException |
||
| 319 | */ |
||
| 320 | public function copy($entity, $deep = false) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Acquire a lock on the given entity. |
||
| 327 | * |
||
| 328 | * @param object $entity |
||
| 329 | * @param int $lockMode |
||
| 330 | * @param int|null $lockVersion |
||
| 331 | * |
||
| 332 | * @return void |
||
| 333 | * |
||
| 334 | * @throws OptimisticLockException |
||
| 335 | * @throws PessimisticLockException |
||
| 336 | */ |
||
| 337 | public function lock($entity, $lockMode, $lockVersion = null) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Gets the EventManager used by the EntityManager. |
||
| 344 | * |
||
| 345 | * @return \Doctrine\Common\EventManager |
||
| 346 | */ |
||
| 347 | public function getEventManager() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Gets the Configuration used by the EntityManager. |
||
| 354 | * |
||
| 355 | * @return Configuration |
||
| 356 | */ |
||
| 357 | public function getConfiguration() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Check if the Entity manager is open or closed. |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | public function isOpen() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Gets a hydrator for the given hydration mode. |
||
| 374 | * |
||
| 375 | * This method caches the hydrator instances which is used for all queries that don't |
||
| 376 | * selectively iterate over the result. |
||
| 377 | * |
||
| 378 | * @deprecated |
||
| 379 | * |
||
| 380 | * @param int $hydrationMode |
||
| 381 | * |
||
| 382 | * @return \Doctrine\ORM\Internal\Hydration\AbstractHydrator |
||
| 383 | */ |
||
| 384 | public function getHydrator($hydrationMode) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Create a new instance for the given hydration mode. |
||
| 391 | * |
||
| 392 | * @param int $hydrationMode |
||
| 393 | * |
||
| 394 | * @return \Doctrine\ORM\Internal\Hydration\AbstractHydrator |
||
| 395 | * |
||
| 396 | * @throws ORMException |
||
| 397 | */ |
||
| 398 | public function newHydrator($hydrationMode) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Gets the enabled filters. |
||
| 405 | * |
||
| 406 | * @return \Doctrine\ORM\Query\FilterCollection The active filter collection. |
||
| 407 | */ |
||
| 408 | public function getFilters() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Checks whether the state of the filter collection is clean. |
||
| 415 | * |
||
| 416 | * @return boolean True, if the filter collection is clean. |
||
| 417 | */ |
||
| 418 | public function isFiltersStateClean() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Checks whether the Entity Manager has filters. |
||
| 425 | * |
||
| 426 | * @return boolean True, if the EM has a filter collection. |
||
| 427 | */ |
||
| 428 | public function hasFilters() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Finds an object by its identifier. |
||
| 435 | * |
||
| 436 | * This is just a convenient shortcut for getRepository($className)->find($id). |
||
| 437 | * |
||
| 438 | * @param string $className The class name of the object to find. |
||
| 439 | * @param mixed $id The identity of the object to find. |
||
| 440 | * |
||
| 441 | * @return object The found object. |
||
| 442 | */ |
||
| 443 | public function find($className, $id) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Tells the ObjectManager to make an instance managed and persistent. |
||
| 450 | * |
||
| 451 | * The object will be entered into the database as a result of the flush operation. |
||
| 452 | * |
||
| 453 | * NOTE: The persist operation always considers objects that are not yet known to |
||
| 454 | * this ObjectManager as NEW. Do not pass detached objects to the persist operation. |
||
| 455 | * |
||
| 456 | * @param object $object The instance to make managed and persistent. |
||
| 457 | * |
||
| 458 | * @return void |
||
| 459 | */ |
||
| 460 | public function persist($object) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Removes an object instance. |
||
| 467 | * |
||
| 468 | * A removed object will be removed from the database as a result of the flush operation. |
||
| 469 | * |
||
| 470 | * @param object $object The object instance to remove. |
||
| 471 | * |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | public function remove($object) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Merges the state of a detached object into the persistence context |
||
| 481 | * of this ObjectManager and returns the managed copy of the object. |
||
| 482 | * The object passed to merge will not become associated/managed with this ObjectManager. |
||
| 483 | * |
||
| 484 | * @param object $object |
||
| 485 | * |
||
| 486 | * @return object |
||
| 487 | */ |
||
| 488 | public function merge($object) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Clears the ObjectManager. All objects that are currently managed |
||
| 495 | * by this ObjectManager become detached. |
||
| 496 | * |
||
| 497 | * @param string|null $objectName if given, only objects of this type will get detached. |
||
| 498 | * |
||
| 499 | * @return void |
||
| 500 | */ |
||
| 501 | public function clear($objectName = null) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Detaches an object from the ObjectManager, causing a managed object to |
||
| 508 | * become detached. Unflushed changes made to the object if any |
||
| 509 | * (including removal of the object), will not be synchronized to the database. |
||
| 510 | * Objects which previously referenced the detached object will continue to |
||
| 511 | * reference it. |
||
| 512 | * |
||
| 513 | * @param object $object The object to detach. |
||
| 514 | * |
||
| 515 | * @return void |
||
| 516 | */ |
||
| 517 | public function detach($object) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Refreshes the persistent state of an object from the database, |
||
| 524 | * overriding any local changes that have not yet been persisted. |
||
| 525 | * |
||
| 526 | * @param object $object The object to refresh. |
||
| 527 | * |
||
| 528 | * @return void |
||
| 529 | */ |
||
| 530 | public function refresh($object) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Flushes all changes to objects that have been queued up to now to the database. |
||
| 537 | * This effectively synchronizes the in-memory state of managed objects with the |
||
| 538 | * database. |
||
| 539 | * |
||
| 540 | * @return void |
||
| 541 | */ |
||
| 542 | public function flush() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Gets the repository for a class. |
||
| 549 | * |
||
| 550 | * @param string $className |
||
| 551 | * |
||
| 552 | * @return \Doctrine\Common\Persistence\ObjectRepository |
||
| 553 | */ |
||
| 554 | public function getRepository($className) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Gets the metadata factory used to gather the metadata of classes. |
||
| 561 | * |
||
| 562 | * @return \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory |
||
| 563 | */ |
||
| 564 | public function getMetadataFactory() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 571 | * |
||
| 572 | * This method is a no-op for other objects. |
||
| 573 | * |
||
| 574 | * @param object $obj |
||
| 575 | * |
||
| 576 | * @return void |
||
| 577 | */ |
||
| 578 | public function initializeObject($obj) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Checks if the object is part of the current UnitOfWork and therefore managed. |
||
| 585 | * |
||
| 586 | * @param object $object |
||
| 587 | * |
||
| 588 | * @return bool |
||
| 589 | */ |
||
| 590 | public function contains($object) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param string $className |
||
| 597 | * @return Mapping\ClassMetadata |
||
| 598 | */ |
||
| 599 | public function getClassMetadata($className) : Mapping\ClassMetadata |
||
| 603 | } |
||
| 604 |
This method has been deprecated.