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 | 191 | 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 | 184 | 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) |
|
| 118 | { |
||
| 119 | 1 | throw new UnsupportedDriver('Not supported for this driver'); |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Inserts $entity in database and synchronizes the entity |
||
| 124 | * |
||
| 125 | * Returns whether the insert was successful or not. |
||
| 126 | * |
||
| 127 | * @param Entity $entity |
||
| 128 | * @param bool $useAutoIncrement |
||
| 129 | * @return bool |
||
| 130 | * @throws UnsupportedDriver |
||
| 131 | */ |
||
| 132 | 2 | public function insert(Entity $entity, $useAutoIncrement = true) |
|
| 133 | { |
||
| 134 | 2 | $statement = $this->buildInsertStatement($entity); |
|
| 135 | |||
| 136 | 2 | if ($useAutoIncrement && $entity::isAutoIncremented()) { |
|
| 137 | 1 | throw new UnsupportedDriver('Auto incremented column for this driver is not supported'); |
|
| 138 | } |
||
| 139 | |||
| 140 | 1 | $this->entityManager->getConnection()->query($statement); |
|
| 141 | 1 | return $this->entityManager->sync($entity, true); |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Inserts $entities in one query |
||
| 146 | * |
||
| 147 | * If update is false the entities will not be synchronized after insert. |
||
| 148 | * |
||
| 149 | * @param Entity[] $entities |
||
| 150 | * @param bool $update |
||
| 151 | * @param bool $useAutoIncrement |
||
| 152 | * @return bool |
||
| 153 | * @throws UnsupportedDriver |
||
| 154 | */ |
||
| 155 | public function bulkInsert(array $entities, $update = true, $useAutoIncrement = true) |
||
| 156 | { |
||
| 157 | if (count($entities) === 0) { |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | |||
| 161 | $statement = $this->buildInsertStatement(...$entities); |
||
| 162 | |||
| 163 | $entity = reset($entities); |
||
| 164 | if ($update && $useAutoIncrement && $entity::isAutoIncremented()) { |
||
| 165 | throw new UnsupportedDriver('Auto incremented column for this driver is not supported'); |
||
| 166 | } |
||
| 167 | |||
| 168 | $this->entityManager->getConnection()->query($statement); |
||
| 169 | if ($update) { |
||
| 170 | $this->syncInserted(...$entities); |
||
| 171 | } |
||
| 172 | return true; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Update $entity in database and returns success |
||
| 177 | * |
||
| 178 | * @param Entity $entity |
||
| 179 | * @return bool |
||
| 180 | * @internal |
||
| 181 | */ |
||
| 182 | 6 | public function update(Entity $entity) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Delete $entity from database |
||
| 211 | * |
||
| 212 | * This method does not delete from the map - you can still receive the entity via fetch. |
||
| 213 | * |
||
| 214 | * @param Entity $entity |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | 6 | public function delete(Entity $entity) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Build the insert statement for $entity |
||
| 235 | * |
||
| 236 | * @param Entity $entity |
||
| 237 | * @param Entity[] $entities |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | 12 | protected function buildInsertStatement(Entity $entity, Entity ...$entities) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Update the autoincrement value |
||
| 269 | * |
||
| 270 | * @param Entity $entity |
||
| 271 | * @param int|string $value |
||
| 272 | */ |
||
| 273 | 3 | protected function updateAutoincrement(Entity $entity, $value) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Sync the $entities after insert |
||
| 284 | * |
||
| 285 | * @param Entity ...$entities |
||
| 286 | */ |
||
| 287 | protected function syncInserted(Entity ...$entities) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Normalize $type |
||
| 325 | * |
||
| 326 | * The type returned by mysql is for example VARCHAR(20) - this function converts it to varchar |
||
| 327 | * |
||
| 328 | * @param string $type |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | 79 | protected function normalizeType($type) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Extract content from parenthesis in $type |
||
| 344 | * |
||
| 345 | * @param string $type |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | 24 | protected function extractParenthesis($type) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Escape a string for query |
||
| 359 | * |
||
| 360 | * @param string $value |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | 121 | protected function escapeString($value) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Escape an integer for query |
||
| 370 | * |
||
| 371 | * @param int $value |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | 51 | protected function escapeInteger($value) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Escape a double for Query |
||
| 381 | * |
||
| 382 | * @param double $value |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | 4 | protected function escapeDouble($value) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Escape NULL for query |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | 1 | protected function escapeNULL() |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Escape a boolean for query |
||
| 402 | * |
||
| 403 | * @param bool $value |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | 20 | protected function escapeBoolean($value) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Escape a date time object for query |
||
| 413 | * |
||
| 414 | * @param \DateTime $value |
||
| 415 | * @return mixed |
||
| 416 | */ |
||
| 417 | 1 | protected function escapeDateTime(\DateTime $value) |
|
| 422 | } |
||
| 423 |