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 | public function getEM() |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | public function getUnitOfWork() |
||
| 61 | |||
| 62 | /* Mock API */ |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Sets a (mock) UnitOfWork that will be returned when getUnitOfWork() is called. |
||
| 66 | * |
||
| 67 | * @param \Doctrine\ORM\UnitOfWork $uow |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | public function setUnitOfWork($uow) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param \Doctrine\ORM\Proxy\Factory\ProxyFactory $proxyFactory |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | public function setProxyFactory($proxyFactory) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return \Doctrine\ORM\Proxy\Factory\ProxyFactory |
||
| 88 | */ |
||
| 89 | public function getProxyFactory() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Mock factory method to create an EntityManager. |
||
| 96 | * |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public static function create($conn, Configuration $config = null, EventManager $eventManager = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns the cache API for managing the second level cache regions or NULL if the cache is not enabled. |
||
| 117 | * |
||
| 118 | * @return \Doctrine\ORM\Cache|null |
||
| 119 | */ |
||
| 120 | public function getCache() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Gets the database connection object used by the EntityManager. |
||
| 127 | * |
||
| 128 | * @return \Doctrine\DBAL\Connection |
||
| 129 | */ |
||
| 130 | public function getConnection() |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * @return Query\Expr |
||
| 138 | */ |
||
| 139 | public function getExpressionBuilder() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Gets an IdentifierFlattener used for converting Entities into an array of identifier values. |
||
| 146 | * |
||
| 147 | * @return IdentifierFlattener |
||
| 148 | */ |
||
| 149 | public function getIdentifierFlattener() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Starts a transaction on the underlying database connection. |
||
| 156 | * |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | public function beginTransaction() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Executes a function in a transaction. |
||
| 166 | * |
||
| 167 | * The function gets passed this EntityManager instance as an (optional) parameter. |
||
| 168 | * |
||
| 169 | * {@link flush} is invoked prior to transaction commit. |
||
| 170 | * |
||
| 171 | * If an exception occurs during execution of the function or flushing or transaction commit, |
||
| 172 | * the transaction is rolled back, the EntityManager closed and the exception re-thrown. |
||
| 173 | * |
||
| 174 | * @param callable $func The function to execute transactionally. |
||
| 175 | * |
||
| 176 | * @return mixed The value returned from the closure. |
||
| 177 | * |
||
| 178 | * @throws \Throwable |
||
| 179 | */ |
||
| 180 | public function transactional(callable $func) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Commits a transaction on the underlying database connection. |
||
| 187 | * |
||
| 188 | * @return void |
||
| 189 | */ |
||
| 190 | public function commit() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Performs a rollback on the underlying database connection. |
||
| 197 | * |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | public function rollback() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Creates a new Query object. |
||
| 207 | * |
||
| 208 | * @param string $dql The DQL string. |
||
| 209 | * |
||
| 210 | * @return Query |
||
| 211 | */ |
||
| 212 | public function createQuery($dql = '') |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Creates a Query from a named query. |
||
| 219 | * |
||
| 220 | * @param string $name |
||
| 221 | * |
||
| 222 | * @return Query |
||
| 223 | */ |
||
| 224 | public function createNamedQuery($name) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Creates a native SQL query. |
||
| 231 | * |
||
| 232 | * @param string $sql |
||
| 233 | * @param ResultSetMapping $rsm The ResultSetMapping to use. |
||
| 234 | * |
||
| 235 | * @return NativeQuery |
||
| 236 | */ |
||
| 237 | public function createNativeQuery($sql, ResultSetMapping $rsm) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Creates a NativeQuery from a named native query. |
||
| 244 | * |
||
| 245 | * @param string $name |
||
| 246 | * |
||
| 247 | * @return NativeQuery |
||
| 248 | */ |
||
| 249 | public function createNamedNativeQuery($name) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Create a QueryBuilder instance |
||
| 256 | * |
||
| 257 | * @return QueryBuilder |
||
| 258 | */ |
||
| 259 | public function createQueryBuilder() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Gets a reference to the entity identified by the given type and identifier |
||
| 266 | * without actually loading it, if the entity is not yet loaded. |
||
| 267 | * |
||
| 268 | * @param string $entityName The name of the entity type. |
||
| 269 | * @param mixed $id The entity identifier. |
||
| 270 | * |
||
| 271 | * @return object The entity reference. |
||
| 272 | * |
||
| 273 | * @throws ORMException |
||
| 274 | */ |
||
| 275 | public function getReference($entityName, $id) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Gets a partial reference to the entity identified by the given type and identifier |
||
| 282 | * without actually loading it, if the entity is not yet loaded. |
||
| 283 | * |
||
| 284 | * The returned reference may be a partial object if the entity is not yet loaded/managed. |
||
| 285 | * If it is a partial object it will not initialize the rest of the entity state on access. |
||
| 286 | * Thus you can only ever safely access the identifier of an entity obtained through |
||
| 287 | * this method. |
||
| 288 | * |
||
| 289 | * The use-cases for partial references involve maintaining bidirectional associations |
||
| 290 | * without loading one side of the association or to update an entity without loading it. |
||
| 291 | * Note, however, that in the latter case the original (persistent) entity data will |
||
| 292 | * never be visible to the application (especially not event listeners) as it will |
||
| 293 | * never be loaded in the first place. |
||
| 294 | * |
||
| 295 | * @param string $entityName The name of the entity type. |
||
| 296 | * @param mixed $identifier The entity identifier. |
||
| 297 | * |
||
| 298 | * @return object The (partial) entity reference. |
||
| 299 | */ |
||
| 300 | public function getPartialReference($entityName, $identifier) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Closes the EntityManager. All entities that are currently managed |
||
| 307 | * by this EntityManager become detached. The EntityManager may no longer |
||
| 308 | * be used after it is closed. |
||
| 309 | * |
||
| 310 | * @return void |
||
| 311 | */ |
||
| 312 | public function close() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Creates a copy of the given entity. Can create a shallow or a deep copy. |
||
| 319 | * |
||
| 320 | * @param object $entity The entity to copy. |
||
| 321 | * @param boolean $deep FALSE for a shallow copy, TRUE for a deep copy. |
||
| 322 | * |
||
| 323 | * @return object The new entity. |
||
| 324 | * |
||
| 325 | * @throws \BadMethodCallException |
||
| 326 | */ |
||
| 327 | public function copy($entity, $deep = false) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Acquire a lock on the given entity. |
||
| 334 | * |
||
| 335 | * @param object $entity |
||
| 336 | * @param int $lockMode |
||
| 337 | * @param int|null $lockVersion |
||
| 338 | * |
||
| 339 | * @return void |
||
| 340 | * |
||
| 341 | * @throws OptimisticLockException |
||
| 342 | * @throws PessimisticLockException |
||
| 343 | */ |
||
| 344 | public function lock($entity, $lockMode, $lockVersion = null) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Gets the EventManager used by the EntityManager. |
||
| 351 | * |
||
| 352 | * @return \Doctrine\Common\EventManager |
||
| 353 | */ |
||
| 354 | public function getEventManager() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Gets the Configuration used by the EntityManager. |
||
| 361 | * |
||
| 362 | * @return Configuration |
||
| 363 | */ |
||
| 364 | public function getConfiguration() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Check if the Entity manager is open or closed. |
||
| 371 | * |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | public function isOpen() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Gets a hydrator for the given hydration mode. |
||
| 381 | * |
||
| 382 | * This method caches the hydrator instances which is used for all queries that don't |
||
| 383 | * selectively iterate over the result. |
||
| 384 | * |
||
| 385 | * @deprecated |
||
| 386 | * |
||
| 387 | * @param int $hydrationMode |
||
| 388 | * |
||
| 389 | * @return \Doctrine\ORM\Internal\Hydration\AbstractHydrator |
||
| 390 | */ |
||
| 391 | public function getHydrator($hydrationMode) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Create a new instance for the given hydration mode. |
||
| 398 | * |
||
| 399 | * @param int $hydrationMode |
||
| 400 | * |
||
| 401 | * @return \Doctrine\ORM\Internal\Hydration\AbstractHydrator |
||
| 402 | * |
||
| 403 | * @throws ORMException |
||
| 404 | */ |
||
| 405 | public function newHydrator($hydrationMode) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Gets the enabled filters. |
||
| 412 | * |
||
| 413 | * @return \Doctrine\ORM\Query\FilterCollection The active filter collection. |
||
| 414 | */ |
||
| 415 | public function getFilters() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Checks whether the state of the filter collection is clean. |
||
| 422 | * |
||
| 423 | * @return boolean True, if the filter collection is clean. |
||
| 424 | */ |
||
| 425 | public function isFiltersStateClean() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Checks whether the Entity Manager has filters. |
||
| 432 | * |
||
| 433 | * @return boolean True, if the EM has a filter collection. |
||
| 434 | */ |
||
| 435 | public function hasFilters() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Finds an object by its identifier. |
||
| 442 | * |
||
| 443 | * This is just a convenient shortcut for getRepository($className)->find($id). |
||
| 444 | * |
||
| 445 | * @param string $className The class name of the object to find. |
||
| 446 | * @param mixed $id The identity of the object to find. |
||
| 447 | * |
||
| 448 | * @return object The found object. |
||
| 449 | */ |
||
| 450 | public function find($className, $id) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Tells the ObjectManager to make an instance managed and persistent. |
||
| 457 | * |
||
| 458 | * The object will be entered into the database as a result of the flush operation. |
||
| 459 | * |
||
| 460 | * NOTE: The persist operation always considers objects that are not yet known to |
||
| 461 | * this ObjectManager as NEW. Do not pass detached objects to the persist operation. |
||
| 462 | * |
||
| 463 | * @param object $object The instance to make managed and persistent. |
||
| 464 | * |
||
| 465 | * @return void |
||
| 466 | */ |
||
| 467 | public function persist($object) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Removes an object instance. |
||
| 474 | * |
||
| 475 | * A removed object will be removed from the database as a result of the flush operation. |
||
| 476 | * |
||
| 477 | * @param object $object The object instance to remove. |
||
| 478 | * |
||
| 479 | * @return void |
||
| 480 | */ |
||
| 481 | public function remove($object) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Merges the state of a detached object into the persistence context |
||
| 488 | * of this ObjectManager and returns the managed copy of the object. |
||
| 489 | * The object passed to merge will not become associated/managed with this ObjectManager. |
||
| 490 | * |
||
| 491 | * @param object $object |
||
| 492 | * |
||
| 493 | * @return object |
||
| 494 | */ |
||
| 495 | public function merge($object) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Clears the ObjectManager. All objects that are currently managed |
||
| 502 | * by this ObjectManager become detached. |
||
| 503 | * |
||
| 504 | * @param string|null $objectName if given, only objects of this type will get detached. |
||
| 505 | * |
||
| 506 | * @return void |
||
| 507 | */ |
||
| 508 | public function clear($objectName = null) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Detaches an object from the ObjectManager, causing a managed object to |
||
| 515 | * become detached. Unflushed changes made to the object if any |
||
| 516 | * (including removal of the object), will not be synchronized to the database. |
||
| 517 | * Objects which previously referenced the detached object will continue to |
||
| 518 | * reference it. |
||
| 519 | * |
||
| 520 | * @param object $object The object to detach. |
||
| 521 | * |
||
| 522 | * @return void |
||
| 523 | */ |
||
| 524 | public function detach($object) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Refreshes the persistent state of an object from the database, |
||
| 531 | * overriding any local changes that have not yet been persisted. |
||
| 532 | * |
||
| 533 | * @param object $object The object to refresh. |
||
| 534 | * |
||
| 535 | * @return void |
||
| 536 | */ |
||
| 537 | public function refresh($object) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Flushes all changes to objects that have been queued up to now to the database. |
||
| 544 | * This effectively synchronizes the in-memory state of managed objects with the |
||
| 545 | * database. |
||
| 546 | * |
||
| 547 | * @return void |
||
| 548 | */ |
||
| 549 | public function flush() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Gets the repository for a class. |
||
| 556 | * |
||
| 557 | * @param string $className |
||
| 558 | * |
||
| 559 | * @return \Doctrine\Common\Persistence\ObjectRepository |
||
| 560 | */ |
||
| 561 | public function getRepository($className) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Gets the metadata factory used to gather the metadata of classes. |
||
| 568 | * |
||
| 569 | * @return \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory |
||
| 570 | */ |
||
| 571 | public function getMetadataFactory() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 578 | * |
||
| 579 | * This method is a no-op for other objects. |
||
| 580 | * |
||
| 581 | * @param object $obj |
||
| 582 | * |
||
| 583 | * @return void |
||
| 584 | */ |
||
| 585 | public function initializeObject($obj) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Checks if the object is part of the current UnitOfWork and therefore managed. |
||
| 592 | * |
||
| 593 | * @param object $object |
||
| 594 | * |
||
| 595 | * @return bool |
||
| 596 | */ |
||
| 597 | public function contains($object) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @param string $className |
||
| 604 | * @return Mapping\ClassMetadata |
||
| 605 | */ |
||
| 606 | public function getClassMetadata($className) : Mapping\ClassMetadata |
||
| 610 | } |
||
| 611 |
This method has been deprecated.