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 |
||
| 19 | class EntityManager |
||
| 20 | { |
||
| 21 | const OPT_CONNECTION = 'connection'; |
||
| 22 | const OPT_TABLE_NAME_TEMPLATE = 'tableNameTemplate'; |
||
| 23 | const OPT_NAMING_SCHEME_TABLE = 'namingSchemeTable'; |
||
| 24 | const OPT_NAMING_SCHEME_COLUMN = 'namingSchemeColumn'; |
||
| 25 | const OPT_NAMING_SCHEME_METHODS = 'namingSchemeMethods'; |
||
| 26 | |||
| 27 | /** @deprecated */ |
||
| 28 | const OPT_MYSQL_BOOLEAN_TRUE = 'mysqlTrue'; |
||
| 29 | /** @deprecated */ |
||
| 30 | const OPT_MYSQL_BOOLEAN_FALSE = 'mysqlFalse'; |
||
| 31 | /** @deprecated */ |
||
| 32 | const OPT_SQLITE_BOOLEAN_TRUE = 'sqliteTrue'; |
||
| 33 | /** @deprecated */ |
||
| 34 | const OPT_SQLITE_BOOLEAN_FASLE = 'sqliteFalse'; |
||
| 35 | /** @deprecated */ |
||
| 36 | const OPT_PGSQL_BOOLEAN_TRUE = 'pgsqlTrue'; |
||
| 37 | /** @deprecated */ |
||
| 38 | const OPT_PGSQL_BOOLEAN_FALSE = 'pgsqlFalse'; |
||
| 39 | /** @deprecated */ |
||
| 40 | const OPT_QUOTING_CHARACTER = 'quotingChar'; |
||
| 41 | /** @deprecated */ |
||
| 42 | const OPT_IDENTIFIER_DIVIDER = 'identifierDivider'; |
||
| 43 | |||
| 44 | /** Connection to database |
||
| 45 | * @var \PDO|callable|DbConfig */ |
||
| 46 | protected $connection; |
||
| 47 | |||
| 48 | /** The Database Abstraction Layer |
||
| 49 | * @var Dbal */ |
||
| 50 | private $dbal; |
||
| 51 | |||
| 52 | /** The Entity map |
||
| 53 | * @var Entity[][] */ |
||
| 54 | protected $map = []; |
||
| 55 | |||
| 56 | /** The options set for this instance |
||
| 57 | * @var array */ |
||
| 58 | protected $options = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructor |
||
| 62 | * |
||
| 63 | * @param array $options Options for the new EntityManager |
||
| 64 | * @throws InvalidConfiguration |
||
| 65 | */ |
||
| 66 | 20 | public function __construct($options = []) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Set $option to $value |
||
| 75 | * |
||
| 76 | * @param string $option One of OPT_* constants |
||
| 77 | * @param mixed $value |
||
| 78 | * @return self |
||
| 79 | */ |
||
| 80 | 13 | public function setOption($option, $value) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Get $option |
||
| 110 | * |
||
| 111 | * @param $option |
||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | 4 | public function getOption($option) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Add connection after instantiation |
||
| 121 | * |
||
| 122 | * The connection can be an array of parameters for DbConfig::__construct(), a callable function that returns a PDO |
||
| 123 | * instance, an instance of DbConfig or a PDO instance itself. |
||
| 124 | * |
||
| 125 | * When it is not a PDO instance the connection get established on first use. |
||
| 126 | * |
||
| 127 | * @param \PDO|callable|DbConfig|array $connection A configuration for (or a) PDO instance |
||
| 128 | * @throws InvalidConfiguration |
||
| 129 | */ |
||
| 130 | 10 | public function setConnection($connection) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Get the pdo connection for $name. |
||
| 151 | * |
||
| 152 | * @return \PDO |
||
| 153 | * @throws NoConnection |
||
| 154 | */ |
||
| 155 | 9 | public function getConnection() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Synchronizing $entity with database |
||
| 186 | * |
||
| 187 | * If $reset is true it also calls reset() on $entity. |
||
| 188 | * |
||
| 189 | * @param Entity $entity |
||
| 190 | * @param bool $reset Reset entities current data |
||
| 191 | * @return bool |
||
| 192 | * @throws IncompletePrimaryKey |
||
| 193 | * @throws InvalidConfiguration |
||
| 194 | * @throws NoConnection |
||
| 195 | * @throws NoEntity |
||
| 196 | */ |
||
| 197 | 13 | public function sync(Entity $entity, $reset = false) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Insert $entity in database |
||
| 220 | * |
||
| 221 | * Returns boolean if it is not auto incremented or the value of auto incremented column otherwise. |
||
| 222 | * |
||
| 223 | * @param Entity $entity |
||
| 224 | * @param bool $useAutoIncrement |
||
| 225 | * @return mixed |
||
| 226 | * @internal |
||
| 227 | */ |
||
| 228 | 11 | public function insert(Entity $entity, $useAutoIncrement = true) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Update $entity in database |
||
| 235 | * |
||
| 236 | * @param Entity $entity |
||
| 237 | * @return bool |
||
| 238 | * @internal |
||
| 239 | */ |
||
| 240 | 6 | public function update(Entity $entity) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Delete $entity from database |
||
| 249 | * |
||
| 250 | * This method does not delete from the map - you can still receive the entity via fetch. |
||
| 251 | * |
||
| 252 | * @param Entity $entity |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | 6 | public function delete(Entity $entity) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Map $entity in the entity map |
||
| 264 | * |
||
| 265 | * Returns the given entity or an entity that previously got mapped. This is useful to work in every function with |
||
| 266 | * the same object. |
||
| 267 | * |
||
| 268 | * ```php?start_inline=true |
||
| 269 | * $user = $enitityManager->map(new User(['id' => 42])); |
||
| 270 | * ``` |
||
| 271 | * |
||
| 272 | * @param Entity $entity |
||
| 273 | * @param bool $update Update the entity map |
||
| 274 | * @return Entity |
||
| 275 | * @throws IncompletePrimaryKey |
||
| 276 | */ |
||
| 277 | 26 | public function map(Entity $entity, $update = false) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Fetch one or more entities |
||
| 291 | * |
||
| 292 | * With $primaryKey it tries to find this primary key in the entity map (carefully: mostly the database returns a |
||
| 293 | * string and we do not convert them). If there is no entity in the entity map it tries to fetch the entity from |
||
| 294 | * the database. The return value is then null (not found) or the entity. |
||
| 295 | * |
||
| 296 | * Without $primaryKey it creates an entityFetcher and returns this. |
||
| 297 | * |
||
| 298 | * @param string|Entity $class The entity class you want to fetch |
||
| 299 | * @param mixed $primaryKey The primary key of the entity you want to fetch |
||
| 300 | * @return Entity|EntityFetcher |
||
| 301 | * @throws IncompletePrimaryKey |
||
| 302 | * @throws InvalidConfiguration |
||
| 303 | * @throws NoConnection |
||
| 304 | * @throws NoEntity |
||
| 305 | */ |
||
| 306 | 60 | public function fetch($class, $primaryKey = null) |
|
| 341 | |||
| 342 | 175 | public function getDbal() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Returns $value formatted to use in a sql statement. |
||
| 373 | * |
||
| 374 | * @param mixed $value The variable that should be returned in SQL syntax |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | 103 | public function escapeValue($value) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Returns $identifier quoted for use in a sql statement |
||
| 384 | * |
||
| 385 | * @param string $identifier Identifier to quote |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | 74 | public function escapeIdentifier($identifier) |
|
| 392 | } |
||
| 393 |
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: