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  | 
            ||
| 34 | class EntityManager extends Doctrine\ORM\EntityManager implements Persistence\QueryExecutor, Persistence\Queryable  | 
            ||
| 35 | { | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * @var NonLockingUniqueInserter  | 
            ||
| 39 | */  | 
            ||
| 40 | private $nonLockingUniqueInserter;  | 
            ||
| 41 | |||
| 42 | /**  | 
            ||
| 43 | * @var Diagnostics\Panel  | 
            ||
| 44 | */  | 
            ||
| 45 | private $panel;  | 
            ||
| 46 | |||
| 47 | |||
| 48 | |||
| 49 | protected function __construct(Doctrine\DBAL\Connection $conn, Doctrine\ORM\Configuration $config, Doctrine\Common\EventManager $eventManager)  | 
            ||
| 57 | |||
| 58 | |||
| 59 | |||
| 60 | /**  | 
            ||
| 61 | * @internal  | 
            ||
| 62 | * @param Diagnostics\Panel $panel  | 
            ||
| 63 | */  | 
            ||
| 64 | public function bindTracyPanel(Diagnostics\Panel $panel)  | 
            ||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * @throws NotSupportedException  | 
            ||
| 73 | * @return \Kdyby\Doctrine\QueryBuilder  | 
            ||
| 74 | */  | 
            ||
| 75 | public function createQueryBuilder($alias = NULL, $indexBy = NULL)  | 
            ||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | /**  | 
            ||
| 92 | * @return \Kdyby\Doctrine\DqlSelection  | 
            ||
| 93 | */  | 
            ||
| 94 | public function createSelection()  | 
            ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | /**  | 
            ||
| 102 | 	 * {@inheritdoc} | 
            ||
| 103 | * @param string|array $entity  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 104 | * @return EntityManager  | 
            ||
| 105 | */  | 
            ||
| 106 | public function clear($entityName = null)  | 
            ||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | /**  | 
            ||
| 118 | 	 * {@inheritdoc} | 
            ||
| 119 | * @param object|array $entity  | 
            ||
| 120 | * @return EntityManager  | 
            ||
| 121 | */  | 
            ||
| 122 | public function remove($entity)  | 
            ||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | /**  | 
            ||
| 134 | 	 * {@inheritdoc} | 
            ||
| 135 | * @param object|array $entity  | 
            ||
| 136 | * @return EntityManager  | 
            ||
| 137 | */  | 
            ||
| 138 | public function persist($entity)  | 
            ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | /**  | 
            ||
| 150 | 	 * {@inheritdoc} | 
            ||
| 151 | * @param object|array $entity  | 
            ||
| 152 | * @return EntityManager  | 
            ||
| 153 | */  | 
            ||
| 154 | public function flush($entity = null)  | 
            ||
| 169 | |||
| 170 | |||
| 171 | |||
| 172 | public function close()  | 
            ||
| 180 | |||
| 181 | |||
| 182 | |||
| 183 | /**  | 
            ||
| 184 | * Tries to persist the given entity and returns FALSE if an unique  | 
            ||
| 185 | * constaint was violated.  | 
            ||
| 186 | *  | 
            ||
| 187 | * Warning: On success you must NOT use the passed entity further  | 
            ||
| 188 | * in your application. Use the returned one instead!  | 
            ||
| 189 | *  | 
            ||
| 190 | * @param $entity  | 
            ||
| 191 | * @throws \Doctrine\DBAL\DBALException  | 
            ||
| 192 | * @throws \Exception  | 
            ||
| 193 | * @return bool|object  | 
            ||
| 194 | */  | 
            ||
| 195 | public function safePersist($entity)  | 
            ||
| 203 | |||
| 204 | |||
| 205 | |||
| 206 | /**  | 
            ||
| 207 | * @param int $hydrationMode  | 
            ||
| 208 | * @return Doctrine\ORM\Internal\Hydration\AbstractHydrator  | 
            ||
| 209 | * @throws \Doctrine\ORM\ORMException  | 
            ||
| 210 | */  | 
            ||
| 211 | public function newHydrator($hydrationMode)  | 
            ||
| 221 | |||
| 222 | |||
| 223 | |||
| 224 | /**  | 
            ||
| 225 | * Factory method to create EntityManager instances.  | 
            ||
| 226 | *  | 
            ||
| 227 | * @param \Doctrine\DBAL\Connection|array $conn  | 
            ||
| 228 | * @param \Doctrine\ORM\Configuration $config  | 
            ||
| 229 | * @param \Doctrine\Common\EventManager $eventManager  | 
            ||
| 230 | * @throws \Doctrine\ORM\ORMException  | 
            ||
| 231 | * @throws \InvalidArgumentException  | 
            ||
| 232 | * @throws \Doctrine\ORM\ORMException  | 
            ||
| 233 | * @return EntityManager  | 
            ||
| 234 | */  | 
            ||
| 235 | public static function create($conn, Doctrine\ORM\Configuration $config, Doctrine\Common\EventManager $eventManager = NULL)  | 
            ||
| 260 | |||
| 261 | |||
| 262 | |||
| 263 | /****************** Kdyby\Persistence\QueryExecutor *****************/  | 
            ||
| 264 | |||
| 265 | |||
| 266 | |||
| 267 | /**  | 
            ||
| 268 | * @param \Kdyby\Persistence\Query|\Kdyby\Doctrine\QueryObject $queryObject  | 
            ||
| 269 | * @param int $hydrationMode  | 
            ||
| 270 | * @throws QueryException  | 
            ||
| 271 | * @return array|\Kdyby\Doctrine\ResultSet  | 
            ||
| 272 | */  | 
            ||
| 273 | public function fetch(Persistence\Query $queryObject, $hydrationMode = AbstractQuery::HYDRATE_OBJECT)  | 
            ||
| 282 | |||
| 283 | |||
| 284 | |||
| 285 | /**  | 
            ||
| 286 | * @param \Kdyby\Persistence\Query|\Kdyby\Doctrine\QueryObject $queryObject  | 
            ||
| 287 | *  | 
            ||
| 288 | * @throws InvalidStateException  | 
            ||
| 289 | * @throws QueryException  | 
            ||
| 290 | * @return object|NULL  | 
            ||
| 291 | */  | 
            ||
| 292 | public function fetchOne(Persistence\Query $queryObject)  | 
            ||
| 307 | |||
| 308 | |||
| 309 | |||
| 310 | /**  | 
            ||
| 311 | * @param \Exception $e  | 
            ||
| 312 | * @param \Kdyby\Persistence\Query $queryObject  | 
            ||
| 313 | *  | 
            ||
| 314 | * @throws \Exception  | 
            ||
| 315 | */  | 
            ||
| 316 | private function handleQueryException(\Exception $e, Persistence\Query $queryObject)  | 
            ||
| 322 | |||
| 323 | |||
| 324 | |||
| 325 | /*************************** Nette\Object ***************************/  | 
            ||
| 326 | |||
| 327 | |||
| 328 | |||
| 329 | /**  | 
            ||
| 330 | * Access to reflection.  | 
            ||
| 331 | *  | 
            ||
| 332 | * @return \Nette\Reflection\ClassType  | 
            ||
| 333 | */  | 
            ||
| 334 | public static function getReflection()  | 
            ||
| 338 | |||
| 339 | |||
| 340 | |||
| 341 | /**  | 
            ||
| 342 | * Call to undefined method.  | 
            ||
| 343 | *  | 
            ||
| 344 | * @param string $name  | 
            ||
| 345 | * @param array $args  | 
            ||
| 346 | *  | 
            ||
| 347 | * @throws \Nette\MemberAccessException  | 
            ||
| 348 | * @return mixed  | 
            ||
| 349 | */  | 
            ||
| 350 | public function __call($name, $args)  | 
            ||
| 354 | |||
| 355 | |||
| 356 | |||
| 357 | /**  | 
            ||
| 358 | * Call to undefined static method.  | 
            ||
| 359 | *  | 
            ||
| 360 | * @param string $name  | 
            ||
| 361 | * @param array $args  | 
            ||
| 362 | *  | 
            ||
| 363 | * @throws \Nette\MemberAccessException  | 
            ||
| 364 | * @return mixed  | 
            ||
| 365 | */  | 
            ||
| 366 | public static function __callStatic($name, $args)  | 
            ||
| 370 | |||
| 371 | |||
| 372 | |||
| 373 | /**  | 
            ||
| 374 | * Adding method to class.  | 
            ||
| 375 | *  | 
            ||
| 376 | * @param $name  | 
            ||
| 377 | * @param null $callback  | 
            ||
| 378 | *  | 
            ||
| 379 | * @throws \Nette\MemberAccessException  | 
            ||
| 380 | * @return callable|null  | 
            ||
| 381 | */  | 
            ||
| 382 | public static function extensionMethod($name, $callback = NULL)  | 
            ||
| 395 | |||
| 396 | |||
| 397 | |||
| 398 | /**  | 
            ||
| 399 | * Returns property value. Do not call directly.  | 
            ||
| 400 | *  | 
            ||
| 401 | * @param string $name  | 
            ||
| 402 | *  | 
            ||
| 403 | * @throws \Nette\MemberAccessException  | 
            ||
| 404 | * @return mixed  | 
            ||
| 405 | */  | 
            ||
| 406 | public function &__get($name)  | 
            ||
| 410 | |||
| 411 | |||
| 412 | |||
| 413 | /**  | 
            ||
| 414 | * Sets value of a property. Do not call directly.  | 
            ||
| 415 | *  | 
            ||
| 416 | * @param string $name  | 
            ||
| 417 | * @param mixed $value  | 
            ||
| 418 | *  | 
            ||
| 419 | * @throws \Nette\MemberAccessException  | 
            ||
| 420 | * @return void  | 
            ||
| 421 | */  | 
            ||
| 422 | public function __set($name, $value)  | 
            ||
| 426 | |||
| 427 | |||
| 428 | |||
| 429 | /**  | 
            ||
| 430 | * Is property defined?  | 
            ||
| 431 | *  | 
            ||
| 432 | * @param string $name  | 
            ||
| 433 | *  | 
            ||
| 434 | * @return bool  | 
            ||
| 435 | */  | 
            ||
| 436 | public function __isset($name)  | 
            ||
| 440 | |||
| 441 | |||
| 442 | |||
| 443 | /**  | 
            ||
| 444 | * Access to undeclared property.  | 
            ||
| 445 | *  | 
            ||
| 446 | * @param string $name  | 
            ||
| 447 | *  | 
            ||
| 448 | * @throws \Nette\MemberAccessException  | 
            ||
| 449 | * @return void  | 
            ||
| 450 | */  | 
            ||
| 451 | public function __unset($name)  | 
            ||
| 455 | |||
| 456 | }  | 
            ||
| 457 | 
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.