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 | |||
| 62 | /** Already fetched column descriptions |
||
| 63 | * @var Column[][] */ |
||
| 64 | protected $descriptions = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Constructor |
||
| 68 | * |
||
| 69 | * @param array $options Options for the new EntityManager |
||
| 70 | * @throws InvalidConfiguration |
||
| 71 | */ |
||
| 72 | 20 | public function __construct($options = []) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Set $option to $value |
||
| 81 | * |
||
| 82 | * @param string $option One of OPT_* constants |
||
| 83 | * @param mixed $value |
||
| 84 | * @return self |
||
| 85 | */ |
||
| 86 | 13 | public function setOption($option, $value) |
|
| 87 | { |
||
| 88 | switch ($option) { |
||
| 89 | 13 | case self::OPT_CONNECTION: |
|
| 90 | 1 | $this->setConnection($value); |
|
| 91 | 1 | break; |
|
| 92 | |||
| 93 | 12 | case self::OPT_TABLE_NAME_TEMPLATE: |
|
| 94 | 1 | Entity::setTableNameTemplate($value); |
|
| 95 | 1 | break; |
|
| 96 | |||
| 97 | 11 | case self::OPT_NAMING_SCHEME_TABLE: |
|
| 98 | 1 | Entity::setNamingSchemeTable($value); |
|
| 99 | 1 | break; |
|
| 100 | |||
| 101 | 10 | case self::OPT_NAMING_SCHEME_COLUMN: |
|
| 102 | 1 | Entity::setNamingSchemeColumn($value); |
|
| 103 | 1 | break; |
|
| 104 | |||
| 105 | 9 | case self::OPT_NAMING_SCHEME_METHODS: |
|
| 106 | 1 | Entity::setNamingSchemeMethods($value); |
|
| 107 | 1 | break; |
|
| 108 | } |
||
| 109 | |||
| 110 | 13 | $this->options[$option] = $value; |
|
| 111 | 13 | return $this; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get $option |
||
| 116 | * |
||
| 117 | * @param $option |
||
| 118 | * @return mixed |
||
| 119 | */ |
||
| 120 | 4 | public function getOption($option) |
|
| 121 | { |
||
| 122 | 4 | return isset($this->options[$option]) ? $this->options[$option] : null; |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Add connection after instantiation |
||
| 127 | * |
||
| 128 | * The connection can be an array of parameters for DbConfig::__construct(), a callable function that returns a PDO |
||
| 129 | * instance, an instance of DbConfig or a PDO instance itself. |
||
| 130 | * |
||
| 131 | * When it is not a PDO instance the connection get established on first use. |
||
| 132 | * |
||
| 133 | * @param mixed $connection A configuration for (or a) PDO instance |
||
| 134 | * @throws InvalidConfiguration |
||
| 135 | */ |
||
| 136 | 10 | public function setConnection($connection) |
|
| 137 | { |
||
| 138 | 10 | if (is_callable($connection) || $connection instanceof DbConfig) { |
|
| 139 | 6 | $this->connection = $connection; |
|
| 140 | } else { |
||
| 141 | 4 | if ($connection instanceof \PDO) { |
|
| 142 | 1 | $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
| 143 | 1 | $this->connection = $connection; |
|
| 144 | 3 | } elseif (is_array($connection)) { |
|
| 145 | 2 | $dbConfigReflection = new \ReflectionClass(DbConfig::class); |
|
| 146 | 2 | $this->connection = $dbConfigReflection->newInstanceArgs($connection); |
|
| 147 | } else { |
||
| 148 | 1 | throw new InvalidConfiguration( |
|
| 149 | 1 | 'Connection must be callable, DbConfig, PDO or an array of parameters for DbConfig::__constructor' |
|
| 150 | ); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | 9 | } |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Get the pdo connection. |
||
| 157 | * |
||
| 158 | * @return \PDO |
||
| 159 | * @throws NoConnection |
||
| 160 | */ |
||
| 161 | 9 | public function getConnection() |
|
| 162 | { |
||
| 163 | 9 | if (!$this->connection) { |
|
| 164 | 1 | throw new NoConnection('No database connection'); |
|
| 165 | } |
||
| 166 | |||
| 167 | 8 | if (!$this->connection instanceof \PDO) { |
|
| 168 | 7 | if ($this->connection instanceof DbConfig) { |
|
| 169 | /** @var DbConfig $dbConfig */ |
||
| 170 | 4 | $dbConfig = $this->connection; |
|
| 171 | 4 | $this->connection = new \PDO( |
|
| 172 | 4 | $dbConfig->getDsn(), |
|
| 173 | 4 | $dbConfig->user, |
|
| 174 | 4 | $dbConfig->pass, |
|
| 175 | 4 | $dbConfig->attributes |
|
| 176 | ); |
||
| 177 | } else { |
||
| 178 | 3 | $pdo = call_user_func($this->connection); |
|
| 179 | 3 | if (!$pdo instanceof \PDO) { |
|
| 180 | 1 | throw new NoConnection('Getter does not return PDO instance'); |
|
| 181 | } |
||
| 182 | 2 | $this->connection = $pdo; |
|
| 183 | } |
||
| 184 | 6 | $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
| 185 | } |
||
| 186 | |||
| 187 | 7 | return $this->connection; |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Synchronizing $entity with database |
||
| 192 | * |
||
| 193 | * If $reset is true it also calls reset() on $entity. |
||
| 194 | * |
||
| 195 | * @param Entity $entity |
||
| 196 | * @param bool $reset Reset entities current data |
||
| 197 | * @return bool |
||
| 198 | * @throws IncompletePrimaryKey |
||
| 199 | * @throws InvalidConfiguration |
||
| 200 | * @throws NoConnection |
||
| 201 | * @throws NoEntity |
||
| 202 | */ |
||
| 203 | 13 | public function sync(Entity $entity, $reset = false) |
|
| 204 | { |
||
| 205 | 13 | $this->map($entity, true); |
|
| 206 | |||
| 207 | /** @var EntityFetcher $fetcher */ |
||
| 208 | 10 | $fetcher = $this->fetch(get_class($entity)); |
|
| 209 | 10 | foreach ($entity->getPrimaryKey() as $var => $value) { |
|
| 210 | 10 | $fetcher->where($var, $value); |
|
| 211 | } |
||
| 212 | |||
| 213 | 10 | $result = $this->getConnection()->query($fetcher->getQuery()); |
|
| 214 | 7 | if ($originalData = $result->fetch(\PDO::FETCH_ASSOC)) { |
|
| 215 | 5 | $entity->setOriginalData($originalData); |
|
| 216 | 5 | if ($reset) { |
|
| 217 | 2 | $entity->reset(); |
|
| 218 | } |
||
| 219 | 5 | return true; |
|
| 220 | } |
||
| 221 | 2 | return false; |
|
| 222 | } |
||
| 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 | * @param bool $useAutoIncrement |
||
| 231 | * @return mixed |
||
| 232 | * @internal |
||
| 233 | */ |
||
| 234 | 11 | public function insert(Entity $entity, $useAutoIncrement = true) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Update $entity in database |
||
| 241 | * |
||
| 242 | * @param Entity $entity |
||
| 243 | * @return bool |
||
| 244 | * @internal |
||
| 245 | */ |
||
| 246 | 6 | public function update(Entity $entity) |
|
| 247 | { |
||
| 248 | 6 | $this->getDbal()->update($entity); |
|
| 249 | 3 | $this->sync($entity, true); |
|
| 250 | 3 | return true; |
|
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Delete $entity from database |
||
| 255 | * |
||
| 256 | * This method does not delete from the map - you can still receive the entity via fetch. |
||
| 257 | * |
||
| 258 | * @param Entity $entity |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | 6 | public function delete(Entity $entity) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * 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 | * the same object. |
||
| 273 | * |
||
| 274 | * ```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 | 26 | public function map(Entity $entity, $update = false) |
|
| 284 | { |
||
| 285 | 26 | $class = get_class($entity); |
|
| 286 | 26 | $key = md5(serialize($entity->getPrimaryKey())); |
|
| 287 | |||
| 288 | 22 | if ($update || !isset($this->map[$class][$key])) { |
|
| 289 | 22 | $this->map[$class][$key] = $entity; |
|
| 290 | } |
||
| 291 | |||
| 292 | 22 | return $this->map[$class][$key]; |
|
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Fetch one or more entities |
||
| 297 | * |
||
| 298 | * With $primaryKey it tries to find this primary key in the entity map (carefully: mostly the database returns a |
||
| 299 | * string and we do not convert them). If there is no entity in the entity map it tries to fetch the entity from |
||
| 300 | * 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 | * @param string|Entity $class The entity class you want to fetch |
||
| 305 | * @param mixed $primaryKey The primary key of the entity you want to fetch |
||
| 306 | * @return Entity|EntityFetcher |
||
| 307 | * @throws IncompletePrimaryKey |
||
| 308 | * @throws InvalidConfiguration |
||
| 309 | * @throws NoConnection |
||
| 310 | * @throws NoEntity |
||
| 311 | */ |
||
| 312 | 60 | public function fetch($class, $primaryKey = null) |
|
| 313 | { |
||
| 314 | 60 | $reflection = new \ReflectionClass($class); |
|
| 315 | 60 | if (!$reflection->isSubclassOf(Entity::class)) { |
|
| 316 | 1 | throw new NoEntity($class . ' is not a subclass of Entity'); |
|
| 317 | } |
||
| 318 | |||
| 319 | 59 | if ($primaryKey === null) { |
|
| 320 | 51 | return new EntityFetcher($this, $class); |
|
| 321 | } |
||
| 322 | |||
| 323 | 9 | if (!is_array($primaryKey)) { |
|
| 324 | 7 | $primaryKey = [$primaryKey]; |
|
| 325 | } |
||
| 326 | |||
| 327 | 9 | $primaryKeyVars = $class::getPrimaryKeyVars(); |
|
| 328 | 9 | if (count($primaryKeyVars) !== count($primaryKey)) { |
|
| 329 | 1 | throw new IncompletePrimaryKey( |
|
| 330 | 1 | 'Primary key consist of [' . implode(',', $primaryKeyVars) . '] only ' . count($primaryKey) . ' given' |
|
| 331 | ); |
||
| 332 | } |
||
| 333 | |||
| 334 | 8 | $primaryKey = array_combine($primaryKeyVars, $primaryKey); |
|
| 335 | |||
| 336 | 8 | if (isset($this->map[$class][md5(serialize($primaryKey))])) { |
|
| 337 | 7 | return $this->map[$class][md5(serialize($primaryKey))]; |
|
| 338 | } |
||
| 339 | |||
| 340 | 1 | $fetcher = new EntityFetcher($this, $class); |
|
| 341 | 1 | foreach ($primaryKey as $var => $value) { |
|
| 342 | 1 | $fetcher->where($var, $value); |
|
| 343 | } |
||
| 344 | |||
| 345 | 1 | return $fetcher->one(); |
|
| 346 | } |
||
| 347 | |||
| 348 | 11 | public function getDbal() |
|
| 349 | { |
||
| 350 | 11 | if (!$this->dbal) { |
|
| 351 | 11 | $connectionType = $this->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME); |
|
| 352 | 11 | $dbalClass = __NAMESPACE__ . '\\Dbal\\' . ucfirst($connectionType); |
|
| 353 | 11 | if (!class_exists($dbalClass)) { |
|
| 354 | 2 | $this->dbal = new Other($this); |
|
| 355 | } else { |
||
| 356 | 9 | $this->dbal = new $dbalClass($this); |
|
| 357 | } |
||
| 358 | |||
| 359 | // backward compatibility - deprecated |
||
| 360 | 11 | if (isset($this->options[$connectionType . 'True'])) { |
|
| 361 | 1 | $this->dbal->setBooleanTrue($this->options[$connectionType . 'True']); |
|
| 362 | } |
||
| 363 | 11 | if (isset($this->options[$connectionType . 'False'])) { |
|
| 364 | 1 | $this->dbal->setBooleanFalse($this->options[$connectionType . 'False']); |
|
| 365 | } |
||
| 366 | 11 | if (isset($this->options[self::OPT_QUOTING_CHARACTER])) { |
|
| 367 | 1 | $this->dbal->setQuotingCharacter($this->options[self::OPT_QUOTING_CHARACTER]); |
|
| 368 | } |
||
| 369 | 11 | if (isset($this->options[self::OPT_IDENTIFIER_DIVIDER])) { |
|
| 370 | 1 | $this->dbal->setIdentifierDivider($this->options[self::OPT_IDENTIFIER_DIVIDER]); |
|
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | 11 | return $this->dbal; |
|
| 375 | } |
||
| 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 | * @param string $identifier Identifier to quote |
||
| 393 | * @return string |
||
| 394 | * @codeCoverageIgnore This is just a proxy |
||
| 395 | */ |
||
| 396 | public function escapeIdentifier($identifier) |
||
| 397 | { |
||
| 398 | return $this->getDbal()->escapeIdentifier($identifier); |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Returns an array of columns from $table. |
||
| 403 | * |
||
| 404 | * @param string $table |
||
| 405 | * @return Dbal\Column[] |
||
| 406 | */ |
||
| 407 | 2 | public function describe($table) |
|
| 414 | } |
||
| 415 |