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 |
||
| 22 | class EntityManager |
||
| 23 | { |
||
| 24 | const OPT_CONNECTION = 'connection'; |
||
| 25 | const OPT_TABLE_NAME_TEMPLATE = 'tableNameTemplate'; |
||
| 26 | const OPT_NAMING_SCHEME_TABLE = 'namingSchemeTable'; |
||
| 27 | const OPT_NAMING_SCHEME_COLUMN = 'namingSchemeColumn'; |
||
| 28 | const OPT_NAMING_SCHEME_METHODS = 'namingSchemeMethods'; |
||
| 29 | const OPT_QUOTING_CHARACTER = 'quotingChar'; |
||
| 30 | const OPT_IDENTIFIER_DIVIDER = 'identifierDivider'; |
||
| 31 | const OPT_BOOLEAN_TRUE = 'true'; |
||
| 32 | const OPT_BOOLEAN_FALSE = 'false'; |
||
| 33 | const OPT_DBAL_CLASS = 'dbalClass'; |
||
| 34 | |||
| 35 | /** @deprecated */ |
||
| 36 | const OPT_MYSQL_BOOLEAN_TRUE = 'mysqlTrue'; |
||
| 37 | /** @deprecated */ |
||
| 38 | const OPT_MYSQL_BOOLEAN_FALSE = 'mysqlFalse'; |
||
| 39 | /** @deprecated */ |
||
| 40 | const OPT_SQLITE_BOOLEAN_TRUE = 'sqliteTrue'; |
||
| 41 | /** @deprecated */ |
||
| 42 | const OPT_SQLITE_BOOLEAN_FALSE = 'sqliteFalse'; |
||
| 43 | /** @deprecated */ |
||
| 44 | const OPT_PGSQL_BOOLEAN_TRUE = 'pgsqlTrue'; |
||
| 45 | /** @deprecated */ |
||
| 46 | const OPT_PGSQL_BOOLEAN_FALSE = 'pgsqlFalse'; |
||
| 47 | |||
| 48 | /** Connection to database |
||
| 49 | * @var \PDO|callable|DbConfig */ |
||
| 50 | protected $connection; |
||
| 51 | |||
| 52 | /** The Database Abstraction Layer |
||
| 53 | * @var Dbal */ |
||
| 54 | protected $dbal; |
||
| 55 | |||
| 56 | /** The Namer instance |
||
| 57 | * @var Namer */ |
||
| 58 | protected $namer; |
||
| 59 | |||
| 60 | /** The Entity map |
||
| 61 | * @var Entity[][] */ |
||
| 62 | protected $map = []; |
||
| 63 | |||
| 64 | /** The options set for this instance |
||
| 65 | * @var array */ |
||
| 66 | protected $options = []; |
||
| 67 | |||
| 68 | /** Already fetched column descriptions |
||
| 69 | * @var Table[]|Column[][] */ |
||
| 70 | protected $descriptions = []; |
||
| 71 | |||
| 72 | /** Mapping for EntityManager instances |
||
| 73 | * @var EntityManager[string]|EntityManager[string][string] */ |
||
| 74 | protected static $emMapping = [ |
||
| 75 | 'byClass' => [], |
||
| 76 | 'byNameSpace' => [], |
||
| 77 | 'byParent' => [], |
||
| 78 | 'last' => null, |
||
| 79 | ]; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Constructor |
||
| 83 | * |
||
| 84 | * @param array $options Options for the new EntityManager |
||
| 85 | * @throws InvalidConfiguration |
||
| 86 | */ |
||
| 87 | 736 | public function __construct($options = []) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Get an instance of the EntityManager. |
||
| 98 | * |
||
| 99 | * If no class is given it gets $class from backtrace. |
||
| 100 | * |
||
| 101 | * It first gets tries the EntityManager for the Namespace of $class, then for the parents of $class. If no |
||
| 102 | * EntityManager is found it returns the last created EntityManager (null if no EntityManager got created). |
||
| 103 | * |
||
| 104 | * @param string $class |
||
| 105 | * @return EntityManager |
||
| 106 | */ |
||
| 107 | 290 | public static function getInstance($class = null) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Get the instance by NameSpace mapping |
||
| 130 | * |
||
| 131 | * @param $class |
||
| 132 | * @return EntityManager |
||
| 133 | */ |
||
| 134 | 288 | private static function getInstanceByNameSpace($class) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Get the instance by Parent class mapping |
||
| 147 | * |
||
| 148 | * @param $class |
||
| 149 | * @return EntityManager |
||
| 150 | */ |
||
| 151 | 289 | private static function getInstanceByParent($class) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Define $this EntityManager as the default EntityManager for $nameSpace |
||
| 170 | * |
||
| 171 | * @param $nameSpace |
||
| 172 | * @return self |
||
| 173 | */ |
||
| 174 | 2 | public function defineForNamespace($nameSpace) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Define $this EntityManager as the default EntityManager for subClasses of $class |
||
| 182 | * |
||
| 183 | * @param $class |
||
| 184 | * @return self |
||
| 185 | */ |
||
| 186 | 2 | public function defineForParent($class) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Set $option to $value |
||
| 194 | * |
||
| 195 | * @param string $option One of OPT_* constants |
||
| 196 | * @param mixed $value |
||
| 197 | * @return self |
||
| 198 | */ |
||
| 199 | 9 | public function setOption($option, $value) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Get $option |
||
| 225 | * |
||
| 226 | * @param $option |
||
| 227 | * @return mixed |
||
| 228 | */ |
||
| 229 | 4 | public function getOption($option) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Add connection after instantiation |
||
| 236 | * |
||
| 237 | * The connection can be an array of parameters for DbConfig::__construct(), a callable function that returns a PDO |
||
| 238 | * instance, an instance of DbConfig or a PDO instance itself. |
||
| 239 | * |
||
| 240 | * When it is not a PDO instance the connection get established on first use. |
||
| 241 | * |
||
| 242 | * @param mixed $connection A configuration for (or a) PDO instance |
||
| 243 | * @throws InvalidConfiguration |
||
| 244 | */ |
||
| 245 | 10 | public function setConnection($connection) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Get the pdo connection. |
||
| 266 | * |
||
| 267 | * @return \PDO |
||
| 268 | * @throws NoConnection |
||
| 269 | */ |
||
| 270 | 9 | public function getConnection() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Get the Datbase Abstraction Layer |
||
| 301 | * |
||
| 302 | * @return Dbal |
||
| 303 | */ |
||
| 304 | 11 | public function getDbal() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get the Namer instance |
||
| 324 | * |
||
| 325 | * @return Namer |
||
| 326 | * @codeCoverageIgnore trivial code... |
||
| 327 | */ |
||
| 328 | public function getNamer() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Synchronizing $entity with database |
||
| 339 | * |
||
| 340 | * If $reset is true it also calls reset() on $entity. |
||
| 341 | * |
||
| 342 | * @param Entity $entity |
||
| 343 | * @param bool $reset Reset entities current data |
||
| 344 | * @return bool |
||
| 345 | * @throws IncompletePrimaryKey |
||
| 346 | * @throws InvalidConfiguration |
||
| 347 | * @throws NoConnection |
||
| 348 | * @throws NoEntity |
||
| 349 | */ |
||
| 350 | 13 | public function sync(Entity $entity, $reset = false) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Insert $entity in database |
||
| 373 | * |
||
| 374 | * Returns boolean if it is not auto incremented or the value of auto incremented column otherwise. |
||
| 375 | * |
||
| 376 | * @param Entity $entity |
||
| 377 | * @param bool $useAutoIncrement |
||
| 378 | * @return mixed |
||
| 379 | * @internal |
||
| 380 | */ |
||
| 381 | 11 | public function insert(Entity $entity, $useAutoIncrement = true) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Update $entity in database |
||
| 388 | * |
||
| 389 | * @param Entity $entity |
||
| 390 | * @return bool |
||
| 391 | * @internal |
||
| 392 | */ |
||
| 393 | 6 | public function update(Entity $entity) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Delete $entity from database |
||
| 402 | * |
||
| 403 | * This method does not delete from the map - you can still receive the entity via fetch. |
||
| 404 | * |
||
| 405 | * @param Entity $entity |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | 6 | public function delete(Entity $entity) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Map $entity in the entity map |
||
| 417 | * |
||
| 418 | * Returns the given entity or an entity that previously got mapped. This is useful to work in every function with |
||
| 419 | * the same object. |
||
| 420 | * |
||
| 421 | * ```php?start_inline=true |
||
| 422 | * $user = $enitityManager->map(new User(['id' => 42])); |
||
| 423 | * ``` |
||
| 424 | * |
||
| 425 | * @param Entity $entity |
||
| 426 | * @param bool $update Update the entity map |
||
| 427 | * @return Entity |
||
| 428 | * @throws IncompletePrimaryKey |
||
| 429 | */ |
||
| 430 | 26 | public function map(Entity $entity, $update = false) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Fetch one or more entities |
||
| 444 | * |
||
| 445 | * With $primaryKey it tries to find this primary key in the entity map (carefully: mostly the database returns a |
||
| 446 | * string and we do not convert them). If there is no entity in the entity map it tries to fetch the entity from |
||
| 447 | * the database. The return value is then null (not found) or the entity. |
||
| 448 | * |
||
| 449 | * Without $primaryKey it creates an entityFetcher and returns this. |
||
| 450 | * |
||
| 451 | * @param string|Entity $class The entity class you want to fetch |
||
| 452 | * @param mixed $primaryKey The primary key of the entity you want to fetch |
||
| 453 | * @return Entity|EntityFetcher |
||
| 454 | * @throws IncompletePrimaryKey |
||
| 455 | * @throws InvalidConfiguration |
||
| 456 | * @throws NoConnection |
||
| 457 | * @throws NoEntity |
||
| 458 | */ |
||
| 459 | 61 | public function fetch($class, $primaryKey = null) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Returns $value formatted to use in a sql statement. |
||
| 497 | * |
||
| 498 | * @param mixed $value The variable that should be returned in SQL syntax |
||
| 499 | * @return string |
||
| 500 | * @codeCoverageIgnore This is just a proxy |
||
| 501 | */ |
||
| 502 | public function escapeValue($value) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Returns $identifier quoted for use in a sql statement |
||
| 509 | * |
||
| 510 | * @param string $identifier Identifier to quote |
||
| 511 | * @return string |
||
| 512 | * @codeCoverageIgnore This is just a proxy |
||
| 513 | */ |
||
| 514 | public function escapeIdentifier($identifier) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Returns an array of columns from $table. |
||
| 521 | * |
||
| 522 | * @param string $table |
||
| 523 | * @return Column[]|Table |
||
| 524 | */ |
||
| 525 | 2 | public function describe($table) |
|
| 532 | } |
||
| 533 |
This class constant has been deprecated.