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 | |||
| 29 | /** @deprecated */ |
||
| 30 | const OPT_MYSQL_BOOLEAN_TRUE = 'mysqlTrue'; |
||
| 31 | /** @deprecated */ |
||
| 32 | const OPT_MYSQL_BOOLEAN_FALSE = 'mysqlFalse'; |
||
| 33 | /** @deprecated */ |
||
| 34 | const OPT_SQLITE_BOOLEAN_TRUE = 'sqliteTrue'; |
||
| 35 | /** @deprecated */ |
||
| 36 | const OPT_SQLITE_BOOLEAN_FASLE = 'sqliteFalse'; |
||
| 37 | /** @deprecated */ |
||
| 38 | const OPT_PGSQL_BOOLEAN_TRUE = 'pgsqlTrue'; |
||
| 39 | /** @deprecated */ |
||
| 40 | const OPT_PGSQL_BOOLEAN_FALSE = 'pgsqlFalse'; |
||
| 41 | /** @deprecated */ |
||
| 42 | const OPT_QUOTING_CHARACTER = 'quotingChar'; |
||
| 43 | /** @deprecated */ |
||
| 44 | const OPT_IDENTIFIER_DIVIDER = 'identifierDivider'; |
||
| 45 | |||
| 46 | /** Connection to database |
||
| 47 | * @var \PDO|callable|DbConfig */ |
||
| 48 | protected $connection; |
||
| 49 | |||
| 50 | /** The Database Abstraction Layer |
||
| 51 | * @var Dbal */ |
||
| 52 | private $dbal; |
||
| 53 | |||
| 54 | /** The Entity map |
||
| 55 | * @var Entity[][] */ |
||
| 56 | protected $map = []; |
||
| 57 | |||
| 58 | /** The options set for this instance |
||
| 59 | * @var array */ |
||
| 60 | protected $options = []; |
||
| 61 | 20 | ||
| 62 | /** Already fetched column descriptions |
||
| 63 | 20 | * @var Column[][] */ |
|
| 64 | protected $descriptions = []; |
||
| 65 | 9 | ||
| 66 | 1 | /** |
|
| 67 | 1 | * Constructor |
|
| 68 | * |
||
| 69 | 8 | * @param array $options Options for the new EntityManager |
|
| 70 | 1 | * @throws InvalidConfiguration |
|
| 71 | 1 | */ |
|
| 72 | public function __construct($options = []) |
||
| 78 | 1 | ||
| 79 | 1 | /** |
|
| 80 | * Set $option to $value |
||
| 81 | 5 | * |
|
| 82 | 1 | * @param string $option One of OPT_* constants |
|
| 83 | 1 | * @param mixed $value |
|
| 84 | * @return self |
||
| 85 | */ |
||
| 86 | 9 | public function setOption($option, $value) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Get $option |
||
| 116 | * |
||
| 117 | * @param $option |
||
| 118 | * @return mixed |
||
| 119 | */ |
||
| 120 | public function getOption($option) |
||
| 124 | |||
| 125 | /** |
||
| 126 | 10 | * Add connection after instantiation |
|
| 127 | * |
||
| 128 | 10 | * The connection can be an array of parameters for DbConfig::__construct(), a callable function that returns a PDO |
|
| 129 | 6 | * instance, an instance of DbConfig or a PDO instance itself. |
|
| 130 | * |
||
| 131 | 4 | * When it is not a PDO instance the connection get established on first use. |
|
| 132 | 1 | * |
|
| 133 | 1 | * @param mixed $connection A configuration for (or a) PDO instance |
|
| 134 | 3 | * @throws InvalidConfiguration |
|
| 135 | 2 | */ |
|
| 136 | 2 | public function setConnection($connection) |
|
| 154 | 1 | ||
| 155 | /** |
||
| 156 | * Get the pdo connection. |
||
| 157 | 8 | * |
|
| 158 | 7 | * @return \PDO |
|
| 159 | * @throws NoConnection |
||
| 160 | 4 | */ |
|
| 161 | 4 | public function getConnection() |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Synchronizing $entity with database |
||
| 192 | * |
||
| 193 | 13 | * If $reset is true it also calls reset() on $entity. |
|
| 194 | * |
||
| 195 | 13 | * @param Entity $entity |
|
| 196 | * @param bool $reset Reset entities current data |
||
| 197 | * @return bool |
||
| 198 | 10 | * @throws IncompletePrimaryKey |
|
| 199 | 10 | * @throws InvalidConfiguration |
|
| 200 | 10 | * @throws NoConnection |
|
| 201 | * @throws NoEntity |
||
| 202 | */ |
||
| 203 | 10 | public function sync(Entity $entity, $reset = false) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Insert $entity in database |
||
| 226 | * |
||
| 227 | * Returns boolean if it is not auto incremented or the value of auto incremented column otherwise. |
||
| 228 | * |
||
| 229 | * @param Entity $entity |
||
| 230 | 8 | * @param bool $useAutoIncrement |
|
| 231 | * @return mixed |
||
| 232 | 8 | * @internal |
|
| 233 | */ |
||
| 234 | public function insert(Entity $entity, $useAutoIncrement = true) |
||
| 238 | 8 | ||
| 239 | 8 | /** |
|
| 240 | 8 | * Update $entity in database |
|
| 241 | * |
||
| 242 | 8 | * @param Entity $entity |
|
| 243 | 8 | * @return bool |
|
| 244 | 8 | * @internal |
|
| 245 | */ |
||
| 246 | 8 | public function update(Entity $entity) |
|
| 252 | 1 | ||
| 253 | /** |
||
| 254 | 3 | * Delete $entity from database |
|
| 255 | 1 | * |
|
| 256 | 1 | * This method does not delete from the map - you can still receive the entity via fetch. |
|
| 257 | 1 | * |
|
| 258 | * @param Entity $entity |
||
| 259 | 2 | * @return bool |
|
| 260 | 1 | */ |
|
| 261 | 1 | public function delete(Entity $entity) |
|
| 267 | |||
| 268 | /** |
||
| 269 | 3 | * Map $entity in the entity map |
|
| 270 | * |
||
| 271 | * Returns the given entity or an entity that previously got mapped. This is useful to work in every function with |
||
| 272 | 2 | * the same object. |
|
| 273 | 2 | * |
|
| 274 | 2 | * ```php?start_inline=true |
|
| 275 | * $user = $enitityManager->map(new User(['id' => 42])); |
||
| 276 | * ``` |
||
| 277 | * |
||
| 278 | * @param Entity $entity |
||
| 279 | * @param bool $update Update the entity map |
||
| 280 | * @return Entity |
||
| 281 | * @throws IncompletePrimaryKey |
||
| 282 | */ |
||
| 283 | public function map(Entity $entity, $update = false) |
||
| 294 | |||
| 295 | 6 | /** |
|
| 296 | 6 | * Fetch one or more entities |
|
| 297 | 6 | * |
|
| 298 | 6 | * With $primaryKey it tries to find this primary key in the entity map (carefully: mostly the database returns a |
|
| 299 | 6 | * string and we do not convert them). If there is no entity in the entity map it tries to fetch the entity from |
|
| 300 | 6 | * the database. The return value is then null (not found) or the entity. |
|
| 301 | * |
||
| 302 | * Without $primaryKey it creates an entityFetcher and returns this. |
||
| 303 | * |
||
| 304 | 6 | * @param string|Entity $class The entity class you want to fetch |
|
| 305 | 6 | * @param mixed $primaryKey The primary key of the entity you want to fetch |
|
| 306 | 6 | * @return Entity|EntityFetcher |
|
| 307 | * @throws IncompletePrimaryKey |
||
| 308 | * @throws InvalidConfiguration |
||
| 309 | 6 | * @throws NoConnection |
|
| 310 | 6 | * @throws NoEntity |
|
| 311 | 6 | */ |
|
| 312 | 6 | public function fetch($class, $primaryKey = null) |
|
| 347 | |||
| 348 | public function getDbal() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Returns $value formatted to use in a sql statement. |
||
| 379 | * |
||
| 380 | * @param mixed $value The variable that should be returned in SQL syntax |
||
| 381 | * @return string |
||
| 382 | * @codeCoverageIgnore This is just a proxy |
||
| 383 | */ |
||
| 384 | public function escapeValue($value) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Returns $identifier quoted for use in a sql statement |
||
| 391 | * |
||
| 392 | 60 | * @param string $identifier Identifier to quote |
|
| 393 | * @return string |
||
| 394 | 60 | * @codeCoverageIgnore This is just a proxy |
|
| 395 | 60 | */ |
|
| 396 | 1 | public function escapeIdentifier($identifier) |
|
| 400 | 51 | ||
| 401 | /** |
||
| 402 | * Returns an array of columns from $table. |
||
| 403 | 9 | * |
|
| 404 | 7 | * @param string $table |
|
| 405 | * @return Dbal\Column[] |
||
| 406 | */ |
||
| 407 | 9 | public function describe($table) |
|
| 414 | } |
||
| 415 |