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 |
||
| 21 | class EntityManager |
||
| 22 | { |
||
| 23 | const OPT_CONNECTION = 'connection'; |
||
| 24 | const OPT_TABLE_NAME_TEMPLATE = 'tableNameTemplate'; |
||
| 25 | const OPT_NAMING_SCHEME_TABLE = 'namingSchemeTable'; |
||
| 26 | const OPT_NAMING_SCHEME_COLUMN = 'namingSchemeColumn'; |
||
| 27 | const OPT_NAMING_SCHEME_METHODS = 'namingSchemeMethods'; |
||
| 28 | const OPT_QUOTING_CHARACTER = 'quotingChar'; |
||
| 29 | const OPT_IDENTIFIER_DIVIDER = 'identifierDivider'; |
||
| 30 | const OPT_BOOLEAN_TRUE = 'true'; |
||
| 31 | const OPT_BOOLEAN_FALSE = 'false'; |
||
| 32 | |||
| 33 | /** @deprecated */ |
||
| 34 | const OPT_MYSQL_BOOLEAN_TRUE = 'mysqlTrue'; |
||
| 35 | /** @deprecated */ |
||
| 36 | const OPT_MYSQL_BOOLEAN_FALSE = 'mysqlFalse'; |
||
| 37 | /** @deprecated */ |
||
| 38 | const OPT_SQLITE_BOOLEAN_TRUE = 'sqliteTrue'; |
||
| 39 | /** @deprecated */ |
||
| 40 | const OPT_SQLITE_BOOLEAN_FASLE = 'sqliteFalse'; |
||
| 41 | /** @deprecated */ |
||
| 42 | const OPT_PGSQL_BOOLEAN_TRUE = 'pgsqlTrue'; |
||
| 43 | /** @deprecated */ |
||
| 44 | const OPT_PGSQL_BOOLEAN_FALSE = 'pgsqlFalse'; |
||
| 45 | |||
| 46 | /** Connection to database |
||
| 47 | * @var \PDO|callable|DbConfig */ |
||
| 48 | protected $connection; |
||
| 49 | |||
| 50 | /** The Database Abstraction Layer |
||
| 51 | * @var Dbal */ |
||
| 52 | protected $dbal; |
||
| 53 | |||
| 54 | /** The Namer instance |
||
| 55 | * @var Namer */ |
||
| 56 | protected $namer; |
||
| 57 | |||
| 58 | /** The Entity map |
||
| 59 | * @var Entity[][] */ |
||
| 60 | protected $map = []; |
||
| 61 | |||
| 62 | /** The options set for this instance |
||
| 63 | * @var array */ |
||
| 64 | protected $options = []; |
||
| 65 | |||
| 66 | /** Already fetched column descriptions |
||
| 67 | * @var Column[][] */ |
||
| 68 | protected $descriptions = []; |
||
| 69 | |||
| 70 | /** Mapping for EntityManager instances |
||
| 71 | * @var EntityManager[string]|EntityManager[string][string] */ |
||
| 72 | protected static $emMapping = [ |
||
| 73 | 'byClass' => [], |
||
| 74 | 'byNameSpace' => [], |
||
| 75 | 'byParent' => [], |
||
| 76 | 'last' => null, |
||
| 77 | ]; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructor |
||
| 81 | * |
||
| 82 | * @param array $options Options for the new EntityManager |
||
| 83 | * @throws InvalidConfiguration |
||
| 84 | */ |
||
| 85 | 729 | public function __construct($options = []) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Get an instance of the EntityManager. |
||
| 96 | * |
||
| 97 | * If no class is given it gets $class from backtrace. |
||
| 98 | * |
||
| 99 | * It first gets tries the EntityManager for the Namespace of $class, then for the parents of $class. If no |
||
| 100 | * EntityManager is found it returns the last created EntityManager (null if no EntityManager got created). |
||
| 101 | * |
||
| 102 | * @param string $class |
||
| 103 | * @return EntityManager |
||
| 104 | */ |
||
| 105 | 260 | public static function getInstance($class = null) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get the instance by NameSpace mapping |
||
| 128 | * |
||
| 129 | * @param $class |
||
| 130 | * @return EntityManager |
||
| 131 | */ |
||
| 132 | 17 | private static function getInstanceByNameSpace($class) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Get the instance by Parent class mapping |
||
| 145 | * |
||
| 146 | * @param $class |
||
| 147 | * @return EntityManager |
||
| 148 | */ |
||
| 149 | 18 | private static function getInstanceByParent($class) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Define $this EntityManager as the default EntityManager for $nameSpace |
||
| 168 | * |
||
| 169 | * @param $nameSpace |
||
| 170 | * @return self |
||
| 171 | */ |
||
| 172 | 2 | public function defineForNamespace($nameSpace) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Define $this EntityManager as the default EntityManager for subClasses of $class |
||
| 180 | * |
||
| 181 | * @param $class |
||
| 182 | * @return self |
||
| 183 | */ |
||
| 184 | 2 | public function defineForParent($class) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Set $option to $value |
||
| 192 | * |
||
| 193 | * @param string $option One of OPT_* constants |
||
| 194 | * @param mixed $value |
||
| 195 | * @return self |
||
| 196 | */ |
||
| 197 | 9 | public function setOption($option, $value) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get $option |
||
| 211 | * |
||
| 212 | * @param $option |
||
| 213 | * @return mixed |
||
| 214 | */ |
||
| 215 | 4 | public function getOption($option) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Add connection after instantiation |
||
| 222 | * |
||
| 223 | * The connection can be an array of parameters for DbConfig::__construct(), a callable function that returns a PDO |
||
| 224 | * instance, an instance of DbConfig or a PDO instance itself. |
||
| 225 | * |
||
| 226 | * When it is not a PDO instance the connection get established on first use. |
||
| 227 | * |
||
| 228 | * @param mixed $connection A configuration for (or a) PDO instance |
||
| 229 | * @throws InvalidConfiguration |
||
| 230 | */ |
||
| 231 | 10 | public function setConnection($connection) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Get the pdo connection. |
||
| 252 | * |
||
| 253 | * @return \PDO |
||
| 254 | * @throws NoConnection |
||
| 255 | */ |
||
| 256 | 9 | public function getConnection() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Get the Datbase Abstraction Layer |
||
| 287 | * |
||
| 288 | * @return Dbal |
||
| 289 | */ |
||
| 290 | 11 | public function getDbal() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Get the Namer instance |
||
| 317 | * |
||
| 318 | * @return Namer |
||
| 319 | * @codeCoverageIgnore trivial code... |
||
| 320 | */ |
||
| 321 | public function getNamer() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Synchronizing $entity with database |
||
| 332 | * |
||
| 333 | * If $reset is true it also calls reset() on $entity. |
||
| 334 | * |
||
| 335 | * @param Entity $entity |
||
| 336 | * @param bool $reset Reset entities current data |
||
| 337 | * @return bool |
||
| 338 | * @throws IncompletePrimaryKey |
||
| 339 | * @throws InvalidConfiguration |
||
| 340 | * @throws NoConnection |
||
| 341 | * @throws NoEntity |
||
| 342 | */ |
||
| 343 | 13 | public function sync(Entity $entity, $reset = false) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Insert $entity in database |
||
| 366 | * |
||
| 367 | * Returns boolean if it is not auto incremented or the value of auto incremented column otherwise. |
||
| 368 | * |
||
| 369 | * @param Entity $entity |
||
| 370 | * @param bool $useAutoIncrement |
||
| 371 | * @return mixed |
||
| 372 | * @internal |
||
| 373 | */ |
||
| 374 | 11 | public function insert(Entity $entity, $useAutoIncrement = true) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Update $entity in database |
||
| 381 | * |
||
| 382 | * @param Entity $entity |
||
| 383 | * @return bool |
||
| 384 | * @internal |
||
| 385 | */ |
||
| 386 | 6 | public function update(Entity $entity) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Delete $entity from database |
||
| 395 | * |
||
| 396 | * This method does not delete from the map - you can still receive the entity via fetch. |
||
| 397 | * |
||
| 398 | * @param Entity $entity |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | 6 | public function delete(Entity $entity) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Map $entity in the entity map |
||
| 410 | * |
||
| 411 | * Returns the given entity or an entity that previously got mapped. This is useful to work in every function with |
||
| 412 | * the same object. |
||
| 413 | * |
||
| 414 | * ```php?start_inline=true |
||
| 415 | * $user = $enitityManager->map(new User(['id' => 42])); |
||
| 416 | * ``` |
||
| 417 | * |
||
| 418 | * @param Entity $entity |
||
| 419 | * @param bool $update Update the entity map |
||
| 420 | * @return Entity |
||
| 421 | * @throws IncompletePrimaryKey |
||
| 422 | */ |
||
| 423 | 26 | public function map(Entity $entity, $update = false) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Fetch one or more entities |
||
| 437 | * |
||
| 438 | * With $primaryKey it tries to find this primary key in the entity map (carefully: mostly the database returns a |
||
| 439 | * string and we do not convert them). If there is no entity in the entity map it tries to fetch the entity from |
||
| 440 | * the database. The return value is then null (not found) or the entity. |
||
| 441 | * |
||
| 442 | * Without $primaryKey it creates an entityFetcher and returns this. |
||
| 443 | * |
||
| 444 | * @param string|Entity $class The entity class you want to fetch |
||
| 445 | * @param mixed $primaryKey The primary key of the entity you want to fetch |
||
| 446 | * @return Entity|EntityFetcher |
||
| 447 | * @throws IncompletePrimaryKey |
||
| 448 | * @throws InvalidConfiguration |
||
| 449 | * @throws NoConnection |
||
| 450 | * @throws NoEntity |
||
| 451 | */ |
||
| 452 | 60 | public function fetch($class, $primaryKey = null) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Returns $value formatted to use in a sql statement. |
||
| 490 | * |
||
| 491 | * @param mixed $value The variable that should be returned in SQL syntax |
||
| 492 | * @return string |
||
| 493 | * @codeCoverageIgnore This is just a proxy |
||
| 494 | */ |
||
| 495 | public function escapeValue($value) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Returns $identifier quoted for use in a sql statement |
||
| 502 | * |
||
| 503 | * @param string $identifier Identifier to quote |
||
| 504 | * @return string |
||
| 505 | * @codeCoverageIgnore This is just a proxy |
||
| 506 | */ |
||
| 507 | public function escapeIdentifier($identifier) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Returns an array of columns from $table. |
||
| 514 | * |
||
| 515 | * @param string $table |
||
| 516 | * @return Column[] |
||
| 517 | */ |
||
| 518 | 2 | public function describe($table) |
|
| 525 | } |
||
| 526 |