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 |
||
| 19 | abstract class Dbal |
||
| 20 | { |
||
| 21 | use Escaping; |
||
| 22 | |||
| 23 | /** @var array */ |
||
| 24 | protected static $typeMapping = []; |
||
| 25 | |||
| 26 | protected static $compositeWhereInTemplate = '(%s) IN (VALUES %s)'; |
||
| 27 | |||
| 28 | /** @var EntityManager */ |
||
| 29 | protected $entityManager; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Dbal constructor. |
||
| 33 | * |
||
| 34 | * @param EntityManager $entityManager |
||
| 35 | * @param array $options |
||
| 36 | */ |
||
| 37 | public function __construct(EntityManager $entityManager, array $options = []) |
||
| 45 | 731 | ||
| 46 | /** |
||
| 47 | * Set $option to $value |
||
| 48 | * |
||
| 49 | * @param string $option |
||
| 50 | * @param mixed $value |
||
| 51 | * @return static |
||
| 52 | */ |
||
| 53 | public function setOption($option, $value) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns $identifier quoted for use in a sql statement |
||
| 77 | * |
||
| 78 | * @param string $identifier Identifier to quote |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | public function escapeIdentifier($identifier) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Returns $value formatted to use in a sql statement. |
||
| 90 | * |
||
| 91 | * @param mixed $value The variable that should be returned in SQL syntax |
||
| 92 | * @return string |
||
| 93 | * @throws NotScalar |
||
| 94 | */ |
||
| 95 | public function escapeValue($value) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Describe a table |
||
| 112 | * |
||
| 113 | * @param string $table |
||
| 114 | * @return Table|Column[] |
||
| 115 | 1 | * @throws UnsupportedDriver |
|
| 116 | * @throws Exception |
||
| 117 | 1 | */ |
|
| 118 | public function describe($table) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param Entity[] $entities |
||
| 125 | * @return bool |
||
| 126 | * @throws Exception\InvalidArgument |
||
| 127 | */ |
||
| 128 | 2 | protected static function assertSameType(array $entities) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Insert $entities into database |
||
| 146 | * |
||
| 147 | 6 | * The entities have to be from same type otherwise a InvalidArgument will be thrown. |
|
| 148 | * |
||
| 149 | 6 | * @param Entity ...$entities |
|
| 150 | 6 | * @return bool |
|
| 151 | * @throws Exception\InvalidArgument |
||
| 152 | 6 | */ |
|
| 153 | 6 | public function insert(Entity ...$entities) |
|
| 163 | 6 | ||
| 164 | /** |
||
| 165 | * Insert $entities and update with default values from database |
||
| 166 | 6 | * |
|
| 167 | 6 | * The entities have to be from same type otherwise a InvalidArgument will be thrown. |
|
| 168 | 6 | * |
|
| 169 | 6 | * @param Entity ...$entities |
|
| 170 | * @return bool |
||
| 171 | 3 | * @throws Exception\InvalidArgument |
|
| 172 | */ |
||
| 173 | public function insertAndSync(Entity ...$entities) |
||
| 183 | |||
| 184 | 6 | /** |
|
| 185 | 6 | * Insert $entities and sync with auto increment primary key |
|
| 186 | 6 | * |
|
| 187 | 6 | * The entities have to be from same type otherwise a InvalidArgument will be thrown. |
|
| 188 | 6 | * |
|
| 189 | * @param Entity ...$entities |
||
| 190 | * @return int|bool |
||
| 191 | 6 | * @throws UnsupportedDriver |
|
| 192 | 6 | * @throws Exception\InvalidArgument |
|
| 193 | 6 | */ |
|
| 194 | public function insertAndSyncWithAutoInc(Entity ...$entities) |
||
| 202 | |||
| 203 | /** |
||
| 204 | 12 | * Update $entity in database and returns success |
|
| 205 | * |
||
| 206 | 12 | * @param Entity $entity |
|
| 207 | * @return bool |
||
| 208 | 12 | * @internal |
|
| 209 | 12 | */ |
|
| 210 | 11 | public function update(Entity $entity) |
|
| 236 | 3 | ||
| 237 | 3 | /** |
|
| 238 | * Delete $entity from database |
||
| 239 | 3 | * |
|
| 240 | 3 | * This method does not delete from the map - you can still receive the entity via fetch. |
|
| 241 | 3 | * |
|
| 242 | * @param Entity $entity |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function delete(Entity $entity) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Build the insert statement for $entity |
||
| 263 | * |
||
| 264 | * @param Entity $entity |
||
| 265 | * @param Entity[] $entities |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | 24 | protected function buildInsertStatement(Entity $entity, Entity ...$entities) |
|
| 294 | 51 | ||
| 295 | /** |
||
| 296 | 51 | * Update the autoincrement value |
|
| 297 | * |
||
| 298 | * @param Entity $entity |
||
| 299 | * @param int|string $value |
||
| 300 | */ |
||
| 301 | protected function updateAutoincrement(Entity $entity, $value) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Sync the $entities after insert |
||
| 312 | * |
||
| 313 | * @param Entity ...$entities |
||
| 314 | */ |
||
| 315 | 1 | protected function syncInserted(Entity ...$entities) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Build a where in statement for composite primary keys |
||
| 349 | * |
||
| 350 | * @param array $cols |
||
| 351 | * @param array $entities |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | protected function buildCompositeWhereInStatement(array $cols, array $entities) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Normalize $type |
||
| 370 | * |
||
| 371 | * The type returned by mysql is for example VARCHAR(20) - this function converts it to varchar |
||
| 372 | * |
||
| 373 | * @param string $type |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | protected function normalizeType($type) |
||
| 386 | } |
||
| 387 |