Complex classes like Dbal 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 Dbal, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class Dbal |
||
| 18 | { |
||
| 19 | /** @var array */ |
||
| 20 | protected static $typeMapping = []; |
||
| 21 | |||
| 22 | /** @var EntityManager */ |
||
| 23 | protected $entityManager; |
||
| 24 | /** @var string */ |
||
| 25 | protected $quotingCharacter = '"'; |
||
| 26 | /** @var string */ |
||
| 27 | protected $identifierDivider = '.'; |
||
| 28 | /** @var string */ |
||
| 29 | protected $booleanTrue = '1'; |
||
| 30 | /** @var string */ |
||
| 31 | protected $booleanFalse = '0'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Dbal constructor. |
||
| 35 | * |
||
| 36 | * @param EntityManager $entityManager |
||
| 37 | * @param array $options |
||
| 38 | */ |
||
| 39 | 746 | public function __construct(EntityManager $entityManager, array $options = []) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Set $option to $value |
||
| 50 | * |
||
| 51 | * @param string $option |
||
| 52 | * @param mixed $value |
||
| 53 | * @return self |
||
| 54 | */ |
||
| 55 | 22 | public function setOption($option, $value) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Returns $identifier quoted for use in a sql statement |
||
| 79 | * |
||
| 80 | * @param string $identifier Identifier to quote |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | 190 | public function escapeIdentifier($identifier) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Returns $value formatted to use in a sql statement. |
||
| 92 | * |
||
| 93 | * @param mixed $value The variable that should be returned in SQL syntax |
||
| 94 | * @return string |
||
| 95 | * @throws NotScalar |
||
| 96 | */ |
||
| 97 | 183 | public function escapeValue($value) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Describe a table |
||
| 111 | * |
||
| 112 | * @param string $table |
||
| 113 | * @return Table|Column[] |
||
| 114 | * @throws UnsupportedDriver |
||
| 115 | * @throws Exception |
||
| 116 | */ |
||
| 117 | 1 | public function describe($table) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @param Entity[] $entities |
||
| 124 | * @return bool |
||
| 125 | * @throws Exception\InvalidArgument |
||
| 126 | */ |
||
| 127 | 12 | protected static function assertSameType(array $entities) |
|
| 142 | |||
| 143 | 5 | public function insert(Entity ...$entities) |
|
| 153 | |||
| 154 | 5 | public function insertAndSync(Entity ...$entities) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @param Entity ...$entities |
||
| 167 | * @return int|bool |
||
| 168 | * @throws UnsupportedDriver |
||
| 169 | */ |
||
| 170 | 1 | public function insertAndSyncWithAutoInc(Entity ...$entities) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Update $entity in database and returns success |
||
| 181 | * |
||
| 182 | * @param Entity $entity |
||
| 183 | * @return bool |
||
| 184 | * @internal |
||
| 185 | */ |
||
| 186 | 6 | public function update(Entity $entity) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Delete $entity from database |
||
| 215 | * |
||
| 216 | * This method does not delete from the map - you can still receive the entity via fetch. |
||
| 217 | * |
||
| 218 | * @param Entity $entity |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | 6 | public function delete(Entity $entity) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Build the insert statement for $entity |
||
| 239 | * |
||
| 240 | * @param Entity $entity |
||
| 241 | * @param Entity[] $entities |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | 10 | protected function buildInsertStatement(Entity $entity, Entity ...$entities) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Update the autoincrement value |
||
| 273 | * |
||
| 274 | * @param Entity $entity |
||
| 275 | * @param int|string $value |
||
| 276 | */ |
||
| 277 | 1 | protected function updateAutoincrement(Entity $entity, $value) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Sync the $entities after insert |
||
| 288 | * |
||
| 289 | * @param Entity ...$entities |
||
| 290 | */ |
||
| 291 | 6 | protected function syncInserted(Entity ...$entities) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Normalize $type |
||
| 331 | * |
||
| 332 | * The type returned by mysql is for example VARCHAR(20) - this function converts it to varchar |
||
| 333 | * |
||
| 334 | * @param string $type |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | 79 | protected function normalizeType($type) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Extract content from parenthesis in $type |
||
| 350 | * |
||
| 351 | * @param string $type |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | 24 | protected function extractParenthesis($type) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Escape a string for query |
||
| 365 | * |
||
| 366 | * @param string $value |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | 120 | protected function escapeString($value) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Escape an integer for query |
||
| 376 | * |
||
| 377 | * @param int $value |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | 52 | protected function escapeInteger($value) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Escape a double for Query |
||
| 387 | * |
||
| 388 | * @param double $value |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 4 | protected function escapeDouble($value) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Escape NULL for query |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | 1 | protected function escapeNULL() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Escape a boolean for query |
||
| 408 | * |
||
| 409 | * @param bool $value |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | 20 | protected function escapeBoolean($value) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Escape a date time object for query |
||
| 419 | * |
||
| 420 | * @param \DateTime $value |
||
| 421 | * @return mixed |
||
| 422 | */ |
||
| 423 | 1 | protected function escapeDateTime(\DateTime $value) |
|
| 428 | } |
||
| 429 |