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 |
||
| 20 | class EntityManager |
||
| 21 | { |
||
| 22 | const OPT_CONNECTION = 'connection'; |
||
| 23 | const OPT_TABLE_NAME_TEMPLATE = 'tableNameTemplate'; |
||
| 24 | const OPT_NAMING_SCHEME_TABLE = 'namingSchemeTable'; |
||
| 25 | const OPT_NAMING_SCHEME_COLUMN = 'namingSchemeColumn'; |
||
| 26 | const OPT_NAMING_SCHEME_METHODS = 'namingSchemeMethods'; |
||
| 27 | const OPT_QUOTING_CHARACTER = 'quotingChar'; |
||
| 28 | const OPT_IDENTIFIER_DIVIDER = 'identifierDivider'; |
||
| 29 | const OPT_BOOLEAN_TRUE = 'true'; |
||
| 30 | const OPT_BOOLEAN_FALSE = 'false'; |
||
| 31 | const OPT_DBAL_CLASS = 'dbalClass'; |
||
| 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_FALSE = '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 Table[]|Column[][] */ |
||
| 68 | protected $descriptions = []; |
||
| 69 | |||
| 70 | /** Classes forcing bulk insert |
||
| 71 | * @var BulkInsert[] */ |
||
| 72 | protected $bulkInserts = []; |
||
| 73 | |||
| 74 | /** Mapping for EntityManager instances |
||
| 75 | * @var EntityManager[string]|EntityManager[string][string] */ |
||
| 76 | protected static $emMapping = [ |
||
| 77 | 'byClass' => [], |
||
| 78 | 'byNameSpace' => [], |
||
| 79 | 'byParent' => [], |
||
| 80 | 'last' => null, |
||
| 81 | ]; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Constructor |
||
| 85 | * |
||
| 86 | * @param array $options Options for the new EntityManager |
||
| 87 | */ |
||
| 88 | 770 | public function __construct($options = []) |
|
| 89 | { |
||
| 90 | 770 | foreach ($options as $option => $value) { |
|
| 91 | 11 | $this->setOption($option, $value); |
|
| 92 | } |
||
| 93 | |||
| 94 | 770 | self::$emMapping['last'] = $this; |
|
| 95 | 770 | } |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Get an instance of the EntityManager. |
||
| 99 | * |
||
| 100 | * If no class is given it gets $class from backtrace. |
||
| 101 | * |
||
| 102 | * It first gets tries the EntityManager for the Namespace of $class, then for the parents of $class. If no |
||
| 103 | * EntityManager is found it returns the last created EntityManager (null if no EntityManager got created). |
||
| 104 | * |
||
| 105 | * @param string $class |
||
| 106 | * @return EntityManager |
||
| 107 | */ |
||
| 108 | 322 | public static function getInstance($class = null) |
|
| 109 | { |
||
| 110 | 322 | if (empty($class)) { |
|
| 111 | 42 | $trace = debug_backtrace(); |
|
| 112 | 42 | if (empty($trace[1]['class'])) { |
|
| 113 | 1 | return self::$emMapping['last']; |
|
| 114 | } |
||
| 115 | 41 | $class = $trace[1]['class']; |
|
| 116 | } |
||
| 117 | |||
| 118 | 321 | if (!isset(self::$emMapping['byClass'][$class])) { |
|
| 119 | 321 | if (!($em = self::getInstanceByParent($class)) && !($em = self::getInstanceByNameSpace($class))) { |
|
| 120 | 319 | return self::$emMapping['last']; |
|
| 121 | } |
||
| 122 | |||
| 123 | 2 | self::$emMapping['byClass'][$class] = $em; |
|
| 124 | } |
||
| 125 | |||
| 126 | 2 | return self::$emMapping['byClass'][$class]; |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get the instance by NameSpace mapping |
||
| 131 | * |
||
| 132 | * @param $class |
||
| 133 | * @return EntityManager |
||
| 134 | */ |
||
| 135 | 320 | private static function getInstanceByNameSpace($class) |
|
| 136 | { |
||
| 137 | 320 | foreach (self::$emMapping['byNameSpace'] as $nameSpace => $em) { |
|
| 138 | 2 | if (strpos($class, $nameSpace) === 0) { |
|
| 139 | 2 | return $em; |
|
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | 319 | return null; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the instance by Parent class mapping |
||
| 148 | * |
||
| 149 | * @param $class |
||
| 150 | * @return EntityManager |
||
| 151 | */ |
||
| 152 | 321 | private static function getInstanceByParent($class) |
|
| 153 | { |
||
| 154 | // we don't need a reflection when we don't have mapping byParent |
||
| 155 | 321 | if (empty(self::$emMapping['byParent'])) { |
|
| 156 | 319 | return null; |
|
| 157 | } |
||
| 158 | |||
| 159 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
| 160 | 2 | $reflection = new \ReflectionClass($class); |
|
| 161 | 2 | foreach (self::$emMapping['byParent'] as $parentClass => $em) { |
|
| 162 | 2 | if ($reflection->isSubclassOf($parentClass)) { |
|
| 163 | 2 | return $em; |
|
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | 1 | return null; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Define $this EntityManager as the default EntityManager for $nameSpace |
||
| 172 | * |
||
| 173 | * @param $nameSpace |
||
| 174 | * @return self |
||
| 175 | */ |
||
| 176 | 2 | public function defineForNamespace($nameSpace) |
|
| 177 | { |
||
| 178 | 2 | self::$emMapping['byNameSpace'][$nameSpace] = $this; |
|
| 179 | 2 | return $this; |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Define $this EntityManager as the default EntityManager for subClasses of $class |
||
| 184 | * |
||
| 185 | * @param $class |
||
| 186 | * @return self |
||
| 187 | */ |
||
| 188 | 2 | public function defineForParent($class) |
|
| 189 | { |
||
| 190 | 2 | self::$emMapping['byParent'][$class] = $this; |
|
| 191 | 2 | return $this; |
|
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set $option to $value |
||
| 196 | * |
||
| 197 | * @param string $option One of OPT_* constants |
||
| 198 | * @param mixed $value |
||
| 199 | * @return self |
||
| 200 | */ |
||
| 201 | 15 | public function setOption($option, $value) |
|
| 202 | { |
||
| 203 | switch ($option) { |
||
| 204 | 15 | case self::OPT_CONNECTION: |
|
| 205 | 1 | $this->setConnection($value); |
|
| 206 | 1 | break; |
|
| 207 | |||
| 208 | 14 | case self::OPT_SQLITE_BOOLEAN_TRUE: |
|
| 209 | 13 | case self::OPT_MYSQL_BOOLEAN_TRUE: |
|
| 210 | 12 | case self::OPT_PGSQL_BOOLEAN_TRUE: |
|
| 211 | 3 | $option = self::OPT_BOOLEAN_TRUE; |
|
| 212 | 3 | break; |
|
| 213 | |||
| 214 | 11 | case self::OPT_SQLITE_BOOLEAN_FALSE: |
|
| 215 | 10 | case self::OPT_MYSQL_BOOLEAN_FALSE: |
|
| 216 | 9 | case self::OPT_PGSQL_BOOLEAN_FALSE: |
|
| 217 | 3 | $option = self::OPT_BOOLEAN_FALSE; |
|
| 218 | 3 | break; |
|
| 219 | } |
||
| 220 | |||
| 221 | 15 | $this->options[$option] = $value; |
|
| 222 | 15 | return $this; |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get $option |
||
| 227 | * |
||
| 228 | * @param $option |
||
| 229 | * @return mixed |
||
| 230 | */ |
||
| 231 | 10 | public function getOption($option) |
|
| 232 | { |
||
| 233 | 10 | return isset($this->options[$option]) ? $this->options[$option] : null; |
|
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Add connection after instantiation |
||
| 238 | * |
||
| 239 | * The connection can be an array of parameters for DbConfig::__construct(), a callable function that returns a PDO |
||
| 240 | * instance, an instance of DbConfig or a PDO instance itself. |
||
| 241 | * |
||
| 242 | * When it is not a PDO instance the connection get established on first use. |
||
| 243 | * |
||
| 244 | * @param mixed $connection A configuration for (or a) PDO instance |
||
| 245 | * @throws InvalidConfiguration |
||
| 246 | */ |
||
| 247 | 36 | public function setConnection($connection) |
|
| 248 | { |
||
| 249 | 36 | if (is_callable($connection) || $connection instanceof DbConfig) { |
|
| 250 | 6 | $this->connection = $connection; |
|
| 251 | } else { |
||
| 252 | 30 | if ($connection instanceof \PDO) { |
|
| 253 | 27 | $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
| 254 | 27 | $this->connection = $connection; |
|
| 255 | 3 | } elseif (is_array($connection)) { |
|
| 256 | 2 | $this->connection = new DbConfig(...$connection); |
|
| 257 | } else { |
||
| 258 | 1 | throw new InvalidConfiguration( |
|
| 259 | 1 | 'Connection must be callable, DbConfig, PDO or an array of parameters for DbConfig::__constructor' |
|
| 260 | ); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | 35 | } |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Get the pdo connection. |
||
| 267 | * |
||
| 268 | * @return \PDO |
||
| 269 | * @throws NoConnection |
||
| 270 | * @throws NoConnection |
||
| 271 | */ |
||
| 272 | 20 | public function getConnection() |
|
| 273 | { |
||
| 274 | 20 | if (!$this->connection) { |
|
| 275 | 1 | throw new NoConnection('No database connection'); |
|
| 276 | } |
||
| 277 | |||
| 278 | 19 | if (!$this->connection instanceof \PDO) { |
|
| 279 | 7 | if ($this->connection instanceof DbConfig) { |
|
| 280 | /** @var DbConfig $dbConfig */ |
||
| 281 | 4 | $dbConfig = $this->connection; |
|
| 282 | 4 | $this->connection = new \PDO( |
|
| 283 | 4 | $dbConfig->getDsn(), |
|
| 284 | 4 | $dbConfig->user, |
|
| 285 | 4 | $dbConfig->pass, |
|
| 286 | 4 | $dbConfig->attributes |
|
| 287 | ); |
||
| 288 | } else { |
||
| 289 | 3 | $pdo = call_user_func($this->connection); |
|
| 290 | 3 | if (!$pdo instanceof \PDO) { |
|
| 291 | 1 | throw new NoConnection('Getter does not return PDO instance'); |
|
| 292 | } |
||
| 293 | 2 | $this->connection = $pdo; |
|
| 294 | } |
||
| 295 | 6 | $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
| 296 | } |
||
| 297 | |||
| 298 | 18 | return $this->connection; |
|
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get the Datbase Abstraction Layer |
||
| 303 | * |
||
| 304 | * @return Dbal |
||
| 305 | */ |
||
| 306 | 18 | public function getDbal() |
|
| 307 | { |
||
| 308 | 18 | if (!$this->dbal) { |
|
| 309 | 18 | $connectionType = $this->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
| 310 | 18 | $options = &$this->options; |
|
| 311 | 18 | $dbalClass = isset($options[self::OPT_DBAL_CLASS]) ? |
|
| 312 | 18 | $options[self::OPT_DBAL_CLASS] : __NAMESPACE__ . '\\Dbal\\' . ucfirst($connectionType); |
|
| 313 | |||
| 314 | 18 | if (!class_exists($dbalClass)) { |
|
| 315 | 2 | $dbalClass = Other::class; |
|
| 316 | } |
||
| 317 | |||
| 318 | 18 | $this->dbal = new $dbalClass($this, $options); |
|
| 319 | } |
||
| 320 | |||
| 321 | 18 | return $this->dbal; |
|
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the Namer instance |
||
| 326 | * |
||
| 327 | * @return Namer |
||
| 328 | * @codeCoverageIgnore trivial code... |
||
| 329 | */ |
||
| 330 | public function getNamer() |
||
| 331 | { |
||
| 332 | if (!$this->namer) { |
||
| 333 | $this->namer = new Namer($this->options); |
||
| 334 | } |
||
| 335 | |||
| 336 | return $this->namer; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Synchronizing $entity with database |
||
| 341 | * |
||
| 342 | * If $reset is true it also calls reset() on $entity. |
||
| 343 | * |
||
| 344 | * @param Entity $entity |
||
| 345 | * @param bool $reset Reset entities current data |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | 14 | public function sync(Entity $entity, $reset = false) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Insert $entity in database |
||
| 371 | * |
||
| 372 | * Returns boolean if it is not auto incremented or the value of auto incremented column otherwise. |
||
| 373 | * |
||
| 374 | * @param Entity $entity |
||
| 375 | * @param bool $useAutoIncrement |
||
| 376 | * @return bool |
||
| 377 | * @internal |
||
| 378 | */ |
||
| 379 | 15 | public function insert(Entity $entity, $useAutoIncrement = true) |
|
| 380 | { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Force $class to use bulk insert. |
||
| 391 | * |
||
| 392 | * At the end you should call finish bulk insert otherwise you may loose data. |
||
| 393 | * |
||
| 394 | * @param string $class |
||
| 395 | * @param int $limit Maximum number of rows per insert |
||
| 396 | * @return BulkInsert |
||
| 397 | */ |
||
| 398 | 2 | public function useBulkInserts($class, $limit = 20) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Finish the bulk insert for $class. |
||
| 408 | * |
||
| 409 | * Returns an array of entities added. |
||
| 410 | * |
||
| 411 | * @param $class |
||
| 412 | * @return Entity[] |
||
| 413 | */ |
||
| 414 | 2 | public function finishBulkInserts($class) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Update $entity in database |
||
| 423 | * |
||
| 424 | * @param Entity $entity |
||
| 425 | * @return bool |
||
| 426 | * @internal |
||
| 427 | */ |
||
| 428 | 6 | public function update(Entity $entity) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Delete $entity from database |
||
| 435 | * |
||
| 436 | * This method does not delete from the map - you can still receive the entity via fetch. |
||
| 437 | * |
||
| 438 | * @param Entity $entity |
||
| 439 | * @return bool |
||
| 440 | */ |
||
| 441 | 6 | public function delete(Entity $entity) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Map $entity in the entity map |
||
| 450 | * |
||
| 451 | * Returns the given entity or an entity that previously got mapped. This is useful to work in every function with |
||
| 452 | * the same object. |
||
| 453 | * |
||
| 454 | * ```php?start_inline=true |
||
| 455 | * $user = $enitityManager->map(new User(['id' => 42])); |
||
| 456 | * ``` |
||
| 457 | * |
||
| 458 | * @param Entity $entity |
||
| 459 | * @param bool $update Update the entity map |
||
| 460 | * @param string $class Overwrite the class |
||
| 461 | * @return Entity |
||
| 462 | */ |
||
| 463 | 41 | public function map(Entity $entity, $update = false, $class = null) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Fetch one or more entities |
||
| 477 | * |
||
| 478 | * With $primaryKey it tries to find this primary key in the entity map (carefully: mostly the database returns a |
||
| 479 | * string and we do not convert them). If there is no entity in the entity map it tries to fetch the entity from |
||
| 480 | * the database. The return value is then null (not found) or the entity. |
||
| 481 | * |
||
| 482 | * Without $primaryKey it creates an entityFetcher and returns this. |
||
| 483 | * |
||
| 484 | * @param string $class The entity class you want to fetch |
||
| 485 | * @param mixed $primaryKey The primary key of the entity you want to fetch |
||
| 486 | * @return Entity|EntityFetcher |
||
| 487 | * @throws IncompletePrimaryKey |
||
| 488 | * @throws NoEntity |
||
| 489 | */ |
||
| 490 | 67 | public function fetch($class, $primaryKey = null) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Returns $value formatted to use in a sql statement. |
||
| 530 | * |
||
| 531 | * @param mixed $value The variable that should be returned in SQL syntax |
||
| 532 | * @return string |
||
| 533 | * @codeCoverageIgnore This is just a proxy |
||
| 534 | */ |
||
| 535 | public function escapeValue($value) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Returns $identifier quoted for use in a sql statement |
||
| 542 | * |
||
| 543 | * @param string $identifier Identifier to quote |
||
| 544 | * @return string |
||
| 545 | * @codeCoverageIgnore This is just a proxy |
||
| 546 | */ |
||
| 547 | public function escapeIdentifier($identifier) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Returns an array of columns from $table. |
||
| 554 | * |
||
| 555 | * @param string $table |
||
| 556 | * @return Column[]|Table |
||
| 557 | */ |
||
| 558 | 2 | public function describe($table) |
|
| 565 | } |
||
| 566 |
This check looks for function calls that miss required arguments.