Complex classes like FakeEntityManager 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 FakeEntityManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class FakeEntityManager implements EntityManagerInterface |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Returns the cache API for managing the second level cache regions or NULL if the cache is not enabled. |
||
| 24 | * |
||
| 25 | * @return \Doctrine\ORM\Cache|null |
||
| 26 | */ |
||
| 27 | public function getCache() |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Gets the database connection object used by the EntityManager. |
||
| 34 | * |
||
| 35 | * @return \Doctrine\DBAL\Connection |
||
| 36 | */ |
||
| 37 | public function getConnection() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
||
| 44 | * |
||
| 45 | * Example: |
||
| 46 | * |
||
| 47 | * <code> |
||
| 48 | * $qb = $em->createQueryBuilder(); |
||
| 49 | * $expr = $em->getExpressionBuilder(); |
||
| 50 | * $qb->select('u')->from('User', 'u') |
||
| 51 | * ->where($expr->orX($expr->eq('u.id', 1), $expr->eq('u.id', 2))); |
||
| 52 | * </code> |
||
| 53 | * |
||
| 54 | * @return \Doctrine\ORM\Query\Expr |
||
| 55 | */ |
||
| 56 | public function getExpressionBuilder() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Starts a transaction on the underlying database connection. |
||
| 63 | * |
||
| 64 | * @return void |
||
| 65 | */ |
||
| 66 | public function beginTransaction() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Executes a function in a transaction. |
||
| 73 | * |
||
| 74 | * The function gets passed this EntityManager instance as an (optional) parameter. |
||
| 75 | * |
||
| 76 | * {@link flush} is invoked prior to transaction commit. |
||
| 77 | * |
||
| 78 | * If an exception occurs during execution of the function or flushing or transaction commit, |
||
| 79 | * the transaction is rolled back, the EntityManager closed and the exception re-thrown. |
||
| 80 | * |
||
| 81 | * @param callable $func The function to execute transactionally. |
||
| 82 | * |
||
| 83 | * @return mixed The non-empty value returned from the closure or true instead. |
||
| 84 | */ |
||
| 85 | public function transactional($func) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Commits a transaction on the underlying database connection. |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | public function commit() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Performs a rollback on the underlying database connection. |
||
| 102 | * |
||
| 103 | * @return void |
||
| 104 | */ |
||
| 105 | public function rollback() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Creates a new Query object. |
||
| 112 | * |
||
| 113 | * @param string $dql The DQL string. |
||
| 114 | * |
||
| 115 | * @return Query |
||
| 116 | */ |
||
| 117 | public function createQuery($dql = '') |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Creates a Query from a named query. |
||
| 124 | * |
||
| 125 | * @param string $name |
||
| 126 | * |
||
| 127 | * @return Query |
||
| 128 | */ |
||
| 129 | public function createNamedQuery($name) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Creates a native SQL query. |
||
| 136 | * |
||
| 137 | * @param string $sql |
||
| 138 | * @param ResultSetMapping $rsm The ResultSetMapping to use. |
||
| 139 | * |
||
| 140 | * @return NativeQuery |
||
| 141 | */ |
||
| 142 | public function createNativeQuery($sql, ResultSetMapping $rsm) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Creates a NativeQuery from a named native query. |
||
| 149 | * |
||
| 150 | * @param string $name |
||
| 151 | * |
||
| 152 | * @return NativeQuery |
||
| 153 | */ |
||
| 154 | public function createNamedNativeQuery($name) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Create a QueryBuilder instance |
||
| 161 | * |
||
| 162 | * @return QueryBuilder |
||
| 163 | */ |
||
| 164 | public function createQueryBuilder() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Gets a reference to the entity identified by the given type and identifier |
||
| 171 | * without actually loading it, if the entity is not yet loaded. |
||
| 172 | * |
||
| 173 | * @param string $entityName The name of the entity type. |
||
| 174 | * @param mixed $id The entity identifier. |
||
| 175 | * |
||
| 176 | * @return object The entity reference. |
||
| 177 | * |
||
| 178 | * @throws ORMException |
||
| 179 | */ |
||
| 180 | public function getReference($entityName, $id) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Gets a partial reference to the entity identified by the given type and identifier |
||
| 187 | * without actually loading it, if the entity is not yet loaded. |
||
| 188 | * |
||
| 189 | * The returned reference may be a partial object if the entity is not yet loaded/managed. |
||
| 190 | * If it is a partial object it will not initialize the rest of the entity state on access. |
||
| 191 | * Thus you can only ever safely access the identifier of an entity obtained through |
||
| 192 | * this method. |
||
| 193 | * |
||
| 194 | * The use-cases for partial references involve maintaining bidirectional associations |
||
| 195 | * without loading one side of the association or to update an entity without loading it. |
||
| 196 | * Note, however, that in the latter case the original (persistent) entity data will |
||
| 197 | * never be visible to the application (especially not event listeners) as it will |
||
| 198 | * never be loaded in the first place. |
||
| 199 | * |
||
| 200 | * @param string $entityName The name of the entity type. |
||
| 201 | * @param mixed $identifier The entity identifier. |
||
| 202 | * |
||
| 203 | * @return object The (partial) entity reference. |
||
| 204 | */ |
||
| 205 | public function getPartialReference($entityName, $identifier) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Closes the EntityManager. All entities that are currently managed |
||
| 212 | * by this EntityManager become detached. The EntityManager may no longer |
||
| 213 | * be used after it is closed. |
||
| 214 | * |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | public function close() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Creates a copy of the given entity. Can create a shallow or a deep copy. |
||
| 224 | * |
||
| 225 | * @param object $entity The entity to copy. |
||
| 226 | * @param boolean $deep FALSE for a shallow copy, TRUE for a deep copy. |
||
| 227 | * |
||
| 228 | * @return object The new entity. |
||
| 229 | * |
||
| 230 | * @throws \BadMethodCallException |
||
| 231 | */ |
||
| 232 | public function copy($entity, $deep = false) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Acquire a lock on the given entity. |
||
| 239 | * |
||
| 240 | * @param object $entity |
||
| 241 | * @param int $lockMode |
||
| 242 | * @param int|null $lockVersion |
||
| 243 | * |
||
| 244 | * @return void |
||
| 245 | * |
||
| 246 | * @throws OptimisticLockException |
||
| 247 | * @throws PessimisticLockException |
||
| 248 | */ |
||
| 249 | public function lock($entity, $lockMode, $lockVersion = null) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Gets the EventManager used by the EntityManager. |
||
| 256 | * |
||
| 257 | * @return \Doctrine\Common\EventManager |
||
| 258 | */ |
||
| 259 | public function getEventManager() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Gets the Configuration used by the EntityManager. |
||
| 266 | * |
||
| 267 | * @return Configuration |
||
| 268 | */ |
||
| 269 | public function getConfiguration() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Check if the Entity manager is open or closed. |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | public function isOpen() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Gets the UnitOfWork used by the EntityManager to coordinate operations. |
||
| 286 | * |
||
| 287 | * @return UnitOfWork |
||
| 288 | */ |
||
| 289 | public function getUnitOfWork() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Gets a hydrator for the given hydration mode. |
||
| 296 | * |
||
| 297 | * This method caches the hydrator instances which is used for all queries that don't |
||
| 298 | * selectively iterate over the result. |
||
| 299 | * |
||
| 300 | * @deprecated |
||
| 301 | * |
||
| 302 | * @param int $hydrationMode |
||
| 303 | * |
||
| 304 | * @return \Doctrine\ORM\Internal\Hydration\AbstractHydrator |
||
| 305 | */ |
||
| 306 | public function getHydrator($hydrationMode) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Create a new instance for the given hydration mode. |
||
| 313 | * |
||
| 314 | * @param int $hydrationMode |
||
| 315 | * |
||
| 316 | * @return \Doctrine\ORM\Internal\Hydration\AbstractHydrator |
||
| 317 | * |
||
| 318 | * @throws ORMException |
||
| 319 | */ |
||
| 320 | public function newHydrator($hydrationMode) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Gets the proxy factory used by the EntityManager to create entity proxies. |
||
| 327 | * |
||
| 328 | * @return \Doctrine\ORM\Proxy\ProxyFactory |
||
| 329 | */ |
||
| 330 | public function getProxyFactory() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Gets the enabled filters. |
||
| 337 | * |
||
| 338 | * @return \Doctrine\ORM\Query\FilterCollection The active filter collection. |
||
| 339 | */ |
||
| 340 | public function getFilters() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Checks whether the state of the filter collection is clean. |
||
| 347 | * |
||
| 348 | * @return boolean True, if the filter collection is clean. |
||
| 349 | */ |
||
| 350 | public function isFiltersStateClean() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Checks whether the Entity Manager has filters. |
||
| 357 | * |
||
| 358 | * @return boolean True, if the EM has a filter collection. |
||
| 359 | */ |
||
| 360 | public function hasFilters() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Finds an object by its identifier. |
||
| 367 | * |
||
| 368 | * This is just a convenient shortcut for getRepository($className)->find($id). |
||
| 369 | * |
||
| 370 | * @param string $className The class name of the object to find. |
||
| 371 | * @param mixed $id The identity of the object to find. |
||
| 372 | * |
||
| 373 | * @return object The found object. |
||
| 374 | */ |
||
| 375 | public function find($className, $id) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Tells the ObjectManager to make an instance managed and persistent. |
||
| 382 | * |
||
| 383 | * The object will be entered into the database as a result of the flush operation. |
||
| 384 | * |
||
| 385 | * NOTE: The persist operation always considers objects that are not yet known to |
||
| 386 | * this ObjectManager as NEW. Do not pass detached objects to the persist operation. |
||
| 387 | * |
||
| 388 | * @param object $object The instance to make managed and persistent. |
||
| 389 | * |
||
| 390 | * @return void |
||
| 391 | */ |
||
| 392 | public function persist($object) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Removes an object instance. |
||
| 399 | * |
||
| 400 | * A removed object will be removed from the database as a result of the flush operation. |
||
| 401 | * |
||
| 402 | * @param object $object The object instance to remove. |
||
| 403 | * |
||
| 404 | * @return void |
||
| 405 | */ |
||
| 406 | public function remove($object) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Merges the state of a detached object into the persistence context |
||
| 413 | * of this ObjectManager and returns the managed copy of the object. |
||
| 414 | * The object passed to merge will not become associated/managed with this ObjectManager. |
||
| 415 | * |
||
| 416 | * @param object $object |
||
| 417 | * |
||
| 418 | * @return object |
||
| 419 | */ |
||
| 420 | public function merge($object) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Clears the ObjectManager. All objects that are currently managed |
||
| 427 | * by this ObjectManager become detached. |
||
| 428 | * |
||
| 429 | * @param string|null $objectName if given, only objects of this type will get detached. |
||
| 430 | * |
||
| 431 | * @return void |
||
| 432 | */ |
||
| 433 | public function clear($objectName = null) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Detaches an object from the ObjectManager, causing a managed object to |
||
| 440 | * become detached. Unflushed changes made to the object if any |
||
| 441 | * (including removal of the object), will not be synchronized to the database. |
||
| 442 | * Objects which previously referenced the detached object will continue to |
||
| 443 | * reference it. |
||
| 444 | * |
||
| 445 | * @param object $object The object to detach. |
||
| 446 | * |
||
| 447 | * @return void |
||
| 448 | */ |
||
| 449 | public function detach($object) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Refreshes the persistent state of an object from the database, |
||
| 456 | * overriding any local changes that have not yet been persisted. |
||
| 457 | * |
||
| 458 | * @param object $object The object to refresh. |
||
| 459 | * |
||
| 460 | * @return void |
||
| 461 | */ |
||
| 462 | public function refresh($object) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Flushes all changes to objects that have been queued up to now to the database. |
||
| 469 | * This effectively synchronizes the in-memory state of managed objects with the |
||
| 470 | * database. |
||
| 471 | * |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | public function flush() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Gets the repository for a class. |
||
| 481 | * |
||
| 482 | * @param string $className |
||
| 483 | * |
||
| 484 | * @return \Doctrine\Common\Persistence\ObjectRepository |
||
| 485 | */ |
||
| 486 | public function getRepository($className) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Gets the metadata factory used to gather the metadata of classes. |
||
| 493 | * |
||
| 494 | * @return \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory |
||
| 495 | */ |
||
| 496 | public function getMetadataFactory() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 503 | * |
||
| 504 | * This method is a no-op for other objects. |
||
| 505 | * |
||
| 506 | * @param object $obj |
||
| 507 | * |
||
| 508 | * @return void |
||
| 509 | */ |
||
| 510 | public function initializeObject($obj) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Checks if the object is part of the current UnitOfWork and therefore managed. |
||
| 517 | * |
||
| 518 | * @param object $object |
||
| 519 | * |
||
| 520 | * @return bool |
||
| 521 | */ |
||
| 522 | public function contains($object) |
||
| 526 | |||
| 527 | function __call($name, $arguments) |
||
| 531 | |||
| 532 | public function getClassMetadata($className) |
||
| 536 | } |
||
| 537 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.