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 |
||
| 18 | abstract class Dbal |
||
| 19 | { |
||
| 20 | /** @var EntityManager */ |
||
| 21 | protected $em; |
||
| 22 | |||
| 23 | protected static $quotingCharacter = '"'; |
||
| 24 | protected static $identifierDivider = '.'; |
||
| 25 | protected static $booleanTrue = '1'; |
||
| 26 | protected static $booleanFalse = '0'; |
||
| 27 | |||
| 28 | protected static $registeredTypes = []; |
||
| 29 | protected static $typeMapping = []; |
||
| 30 | |||
| 31 | 17 | public static function setQuotingCharacter($char) |
|
| 35 | |||
| 36 | 17 | public static function setIdentifierDivider($divider) |
|
| 40 | |||
| 41 | 17 | public static function setBooleanTrue($true) |
|
| 45 | |||
| 46 | 17 | public static function setBooleanFalse($false) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | 1 | public static function getQuotingCharacter() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | 1 | public static function getIdentifierDivider() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | 1 | public static function getBooleanTrue() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | 1 | public static function getBooleanFalse() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Dbal constructor. |
||
| 85 | * |
||
| 86 | * @param EntityManager $entityManager |
||
| 87 | */ |
||
| 88 | 278 | public function __construct(EntityManager $entityManager) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Returns $identifier quoted for use in a sql statement |
||
| 95 | * |
||
| 96 | * @param string $identifier Identifier to quote |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | 163 | public function escapeIdentifier($identifier) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Returns $value formatted to use in a sql statement. |
||
| 108 | * |
||
| 109 | * @param mixed $value The variable that should be returned in SQL syntax |
||
| 110 | * @return string |
||
| 111 | * @throws NotScalar |
||
| 112 | */ |
||
| 113 | 162 | public function escapeValue($value) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Describe a table |
||
| 138 | * |
||
| 139 | * @param $table |
||
| 140 | * @return Column[] |
||
| 141 | * @throws UnsupportedDriver |
||
| 142 | */ |
||
| 143 | 1 | public function describe($table) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Inserts $entity and returns the new ID for autoincrement or true |
||
| 150 | * |
||
| 151 | * @param Entity $entity |
||
| 152 | * @param bool $useAutoIncrement |
||
| 153 | * @return bool|int |
||
| 154 | * @throws UnsupportedDriver |
||
| 155 | */ |
||
| 156 | 3 | public function insert($entity, $useAutoIncrement = true) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Update $entity in database |
||
| 171 | * |
||
| 172 | * @param Entity $entity |
||
| 173 | * @return bool |
||
| 174 | * @internal |
||
| 175 | */ |
||
| 176 | 6 | public function update(Entity $entity) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Delete $entity from database |
||
| 205 | * |
||
| 206 | * This method does not delete from the map - you can still receive the entity via fetch. |
||
| 207 | * |
||
| 208 | * @param Entity $entity |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | 6 | public function delete(Entity $entity) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Build the insert statement for $entity |
||
| 229 | * |
||
| 230 | * @param Entity $entity |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | 11 | protected function buildInsertStatement($entity) |
|
| 250 | |||
| 251 | 61 | protected function normalizeType($type) |
|
| 261 | |||
| 262 | 9 | protected function extractParenthesis($type) |
|
| 270 | |||
| 271 | 85 | protected function getType($columnDefinition) |
|
| 296 | |||
| 297 | 85 | protected function columnFactory($columnDefinition, $type) |
|
| 306 | } |
||
| 307 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.