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