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 | /** @var EntityManager */ |
||
| 22 | protected $em; |
||
| 23 | |||
| 24 | protected static $quotingCharacter = '"'; |
||
| 25 | protected static $identifierDivider = '.'; |
||
| 26 | protected static $booleanTrue = '1'; |
||
| 27 | protected static $booleanFalse = '0'; |
||
| 28 | |||
| 29 | protected static $registeredTypes = []; |
||
| 30 | protected static $typeMapping = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Dbal constructor. |
||
| 34 | * |
||
| 35 | * @param EntityManager $entityManager |
||
| 36 | */ |
||
| 37 | 604 | public function __construct(EntityManager $entityManager) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Returns $identifier quoted for use in a sql statement |
||
| 44 | * |
||
| 45 | * @param string $identifier Identifier to quote |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | 168 | public function escapeIdentifier($identifier) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Returns $value formatted to use in a sql statement. |
||
| 57 | * |
||
| 58 | * @param mixed $value The variable that should be returned in SQL syntax |
||
| 59 | * @return string |
||
| 60 | * @throws NotScalar |
||
| 61 | */ |
||
| 62 | 162 | public function escapeValue($value) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Describe a table |
||
| 87 | * |
||
| 88 | * @param $table |
||
| 89 | * @return Column[] |
||
| 90 | * @throws UnsupportedDriver |
||
| 91 | */ |
||
| 92 | 1 | public function describe($table) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Inserts $entity and returns the new ID for autoincrement or true |
||
| 99 | * |
||
| 100 | * @param Entity $entity |
||
| 101 | * @param bool $useAutoIncrement |
||
| 102 | * @return bool|int |
||
| 103 | * @throws UnsupportedDriver |
||
| 104 | */ |
||
| 105 | 2 | public function insert($entity, $useAutoIncrement = true) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Update $entity in database |
||
| 120 | * |
||
| 121 | * @param Entity $entity |
||
| 122 | * @return bool |
||
| 123 | * @internal |
||
| 124 | */ |
||
| 125 | 6 | public function update(Entity $entity) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Delete $entity from database |
||
| 154 | * |
||
| 155 | * This method does not delete from the map - you can still receive the entity via fetch. |
||
| 156 | * |
||
| 157 | * @param Entity $entity |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | 6 | public function delete(Entity $entity) |
|
| 175 | |||
| 176 | 8 | public static function registerType($type) |
|
| 182 | |||
| 183 | 17 | public static function setQuotingCharacter($char) |
|
| 187 | |||
| 188 | 17 | public static function setIdentifierDivider($divider) |
|
| 192 | |||
| 193 | 17 | public static function setBooleanTrue($true) |
|
| 197 | |||
| 198 | 17 | public static function setBooleanFalse($false) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | 1 | public static function getQuotingCharacter() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 1 | public static function getIdentifierDivider() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | 1 | public static function getBooleanTrue() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | 1 | public static function getBooleanFalse() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Build the insert statement for $entity |
||
| 237 | * |
||
| 238 | * @param Entity $entity |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | 11 | protected function buildInsertStatement($entity) |
|
| 258 | |||
| 259 | 66 | protected function normalizeType($type) |
|
| 269 | |||
| 270 | 9 | protected function extractParenthesis($type) |
|
| 278 | |||
| 279 | 90 | protected function getType($columnDefinition) |
|
| 296 | } |
||
| 297 |