Complex classes like Doctrine2DBAL 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 Doctrine2DBAL, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Doctrine2DBAL |
||
| 10 | implements AdapterInterface |
||
|
|
|||
| 11 | { |
||
| 12 | private $options; |
||
| 13 | |||
| 14 | private $connection; |
||
| 15 | |||
| 16 | private $defaultDbSelect; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param Options $options |
||
| 20 | * @param DbConnection $connection |
||
| 21 | */ |
||
| 22 | 49 | public function __construct(Options $options, DbConnection $connection) |
|
| 27 | |||
| 28 | /** |
||
| 29 | * @return Options |
||
| 30 | */ |
||
| 31 | 46 | private function getOptions() |
|
| 35 | |||
| 36 | /** |
||
| 37 | * @return DbConnection |
||
| 38 | */ |
||
| 39 | 47 | private function getConnection() |
|
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * Data cannot contain keys like idColumnName, levelColumnName, ... |
||
| 47 | * |
||
| 48 | * @param array $data |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | 3 | private function cleanData(array $data) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Return base db select without any join, etc. |
||
| 69 | * @return QueryBuilder |
||
| 70 | */ |
||
| 71 | 24 | public function getBlankDbSelect() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @param QueryBuilder $dbSelect |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | 1 | public function setDefaultDbSelect(QueryBuilder $dbSelect) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Return clone of default db select |
||
| 93 | * @return QueryBuilder |
||
| 94 | */ |
||
| 95 | 14 | public function getDefaultDbSelect() |
|
| 105 | |||
| 106 | 1 | public function lockTree() |
|
| 119 | |||
| 120 | 1 | public function beginTransaction() |
|
| 125 | |||
| 126 | 1 | public function commitTransaction() |
|
| 131 | |||
| 132 | 1 | public function rollbackTransaction() |
|
| 137 | |||
| 138 | 3 | public function update($nodeId, array $data) |
|
| 139 | { |
||
| 140 | 3 | $options = $this->getOptions(); |
|
| 141 | |||
| 142 | 3 | $connection = $this->getConnection(); |
|
| 143 | |||
| 144 | 3 | $data = $this->cleanData($data); |
|
| 145 | |||
| 146 | 3 | $sql = $connection->createQueryBuilder(); |
|
| 147 | |||
| 148 | 3 | $sql->update($options->getTableName(), null) |
|
| 149 | 3 | ->where($options->getIdColumnName() . ' = :' . $options->getIdColumnName()); |
|
| 150 | |||
| 151 | 3 | foreach ($data as $key => $value) { |
|
| 152 | 3 | $sql->set($connection->quoteIdentifier($key), ':' . $key); |
|
| 153 | } |
||
| 154 | |||
| 155 | 3 | $data[$options->getIdColumnName()] = $nodeId; |
|
| 156 | |||
| 157 | 3 | $connection->executeUpdate($sql->getSQL(), $data); |
|
| 158 | 3 | } |
|
| 159 | |||
| 160 | 3 | public function insert(NodeInfo $nodeInfo, array $data) |
|
| 161 | { |
||
| 162 | 3 | $options = $this->getOptions(); |
|
| 163 | |||
| 164 | 3 | $connection = $this->getConnection(); |
|
| 165 | |||
| 166 | 3 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
|
| 167 | 3 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
|
| 168 | 3 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
|
| 169 | 3 | $data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
|
| 170 | |||
| 171 | 3 | if ($options->getScopeColumnName()) { |
|
| 172 | 1 | $data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
|
| 173 | } |
||
| 174 | |||
| 175 | 3 | $connection->insert($options->getTableName(), $data); |
|
| 176 | |||
| 177 | 3 | return $connection->lastInsertId($options->getSequenceName()); |
|
| 178 | } |
||
| 179 | |||
| 180 | 2 | public function delete($nodeId) |
|
| 181 | { |
||
| 182 | 2 | $options = $this->getOptions(); |
|
| 183 | |||
| 184 | 2 | $connection = $this->getConnection(); |
|
| 185 | |||
| 186 | 2 | $sql = $connection->createQueryBuilder(); |
|
| 187 | 2 | $sql->delete($options->getTableName()) |
|
| 188 | 2 | ->where($options->getIdColumnName() . ' = :id'); |
|
| 189 | |||
| 190 | $params = array( |
||
| 191 | 2 | ':id' => $nodeId, |
|
| 192 | ); |
||
| 193 | |||
| 194 | 2 | $connection->executeQuery($sql->getSQL(), $params); |
|
| 195 | 2 | } |
|
| 196 | |||
| 197 | 2 | public function moveLeftIndexes($fromIndex, $shift, $scope = null) |
|
| 198 | { |
||
| 199 | 2 | $options = $this->getOptions(); |
|
| 200 | |||
| 201 | 2 | if (0 == $shift) { |
|
| 202 | return; |
||
| 203 | } |
||
| 204 | |||
| 205 | 2 | $connection = $this->getConnection(); |
|
| 206 | |||
| 207 | 2 | $sql = $connection->createQueryBuilder(); |
|
| 208 | 2 | $sql->update($options->getTableName()) |
|
| 209 | 2 | ->set($options->getLeftColumnName(), $options->getLeftColumnName() . ' + :shift') |
|
| 210 | 2 | ->where($options->getLeftColumnName() . ' > :fromIndex'); |
|
| 211 | |||
| 212 | $params = array( |
||
| 213 | 2 | ':shift' => $shift, |
|
| 214 | 2 | ':fromIndex' => $fromIndex, |
|
| 215 | ); |
||
| 216 | |||
| 217 | 2 | if ($options->getScopeColumnName()) { |
|
| 218 | 1 | $sql->andWhere($options->getScopeColumnName() . ' = :scope'); |
|
| 219 | 1 | $params[':scope'] = $scope; |
|
| 220 | } |
||
| 221 | |||
| 222 | 2 | $connection->executeUpdate($sql->getSQL(), $params); |
|
| 223 | 2 | } |
|
| 224 | |||
| 225 | 2 | public function moveRightIndexes($fromIndex, $shift, $scope = null) |
|
| 226 | { |
||
| 227 | 2 | $options = $this->getOptions(); |
|
| 228 | |||
| 229 | 2 | if (0 == $shift) { |
|
| 230 | return; |
||
| 231 | } |
||
| 232 | |||
| 233 | 2 | $connection = $this->getConnection(); |
|
| 234 | |||
| 235 | 2 | $sql = $connection->createQueryBuilder(); |
|
| 236 | 2 | $sql->update($options->getTableName()) |
|
| 237 | 2 | ->set($options->getRightColumnName(), $options->getRightColumnName() . ' + :shift') |
|
| 238 | 2 | ->where($options->getRightColumnName() . ' > :fromIndex'); |
|
| 239 | |||
| 240 | $params = array( |
||
| 241 | 2 | ':shift' => $shift, |
|
| 242 | 2 | ':fromIndex' => $fromIndex, |
|
| 243 | ); |
||
| 244 | |||
| 245 | 2 | if ($options->getScopeColumnName()) { |
|
| 246 | 1 | $sql->andWhere($options->getScopeColumnName() . ' = :scope'); |
|
| 247 | 1 | $params[':scope'] = $scope; |
|
| 248 | } |
||
| 249 | |||
| 250 | 2 | $connection->executeUpdate($sql->getSQL(), $params); |
|
| 251 | 2 | } |
|
| 252 | |||
| 253 | 1 | public function updateParentId($nodeId, $newParentId) |
|
| 254 | { |
||
| 255 | 1 | $options = $this->getOptions(); |
|
| 256 | |||
| 257 | 1 | $connection = $this->getConnection(); |
|
| 258 | |||
| 259 | 1 | $sql = $connection->createQueryBuilder(); |
|
| 260 | 1 | $sql->update($options->getTableName()) |
|
| 261 | 1 | ->set($options->getParentIdColumnName(), ':parentId') |
|
| 262 | 1 | ->where($options->getIdColumnName() . ' = :nodeId'); |
|
| 263 | |||
| 264 | $params = array( |
||
| 265 | 1 | ':parentId' => $newParentId, |
|
| 266 | 1 | ':nodeId' => $nodeId, |
|
| 267 | ); |
||
| 268 | |||
| 269 | 1 | $connection->executeUpdate($sql->getSQL(), $params); |
|
| 270 | 1 | } |
|
| 271 | |||
| 272 | 2 | public function updateLevels($leftIndexFrom, $rightIndexTo, $shift, $scope = null) |
|
| 273 | { |
||
| 274 | 2 | $options = $this->getOptions(); |
|
| 275 | |||
| 276 | 2 | if (0 == $shift) { |
|
| 277 | return; |
||
| 278 | } |
||
| 279 | |||
| 280 | 2 | $connection = $this->getConnection(); |
|
| 281 | |||
| 282 | 2 | $sql = $connection->createQueryBuilder(); |
|
| 283 | 2 | $sql->update($options->getTableName()) |
|
| 284 | 2 | ->set($options->getLevelColumnName(), $options->getLevelColumnName() . ' + :shift') |
|
| 285 | 2 | ->where($options->getLeftColumnName() . ' >= :leftFrom' |
|
| 286 | 2 | . ' AND ' . $options->getRightColumnName() . ' <= :rightTo'); |
|
| 287 | |||
| 288 | $params = array( |
||
| 289 | 2 | ':shift' => $shift, |
|
| 290 | 2 | ':leftFrom' => $leftIndexFrom, |
|
| 291 | 2 | ':rightTo' => $rightIndexTo, |
|
| 292 | ); |
||
| 293 | |||
| 294 | 2 | if ($options->getScopeColumnName()) { |
|
| 295 | 1 | $sql->andWhere($options->getScopeColumnName() . ' = :scope'); |
|
| 296 | 1 | $params[':scope'] = $scope; |
|
| 297 | } |
||
| 298 | |||
| 299 | 2 | $connection->executeUpdate($sql->getSQL(), $params); |
|
| 300 | 2 | } |
|
| 301 | |||
| 302 | 2 | public function moveBranch($leftIndexFrom, $rightIndexTo, $shift, $scope = null) |
|
| 303 | { |
||
| 304 | 2 | if (0 == $shift) { |
|
| 305 | return; |
||
| 306 | } |
||
| 307 | |||
| 308 | 2 | $options = $this->getOptions(); |
|
| 309 | |||
| 310 | 2 | $connection = $this->getConnection(); |
|
| 311 | |||
| 312 | 2 | $sql = $connection->createQueryBuilder(); |
|
| 313 | 2 | $sql->update($options->getTableName()) |
|
| 314 | 2 | ->set($options->getLeftColumnName(), $options->getLeftColumnName() . ' + :shift') |
|
| 315 | 2 | ->set($options->getRightColumnName(), $options->getRightColumnName() . ' + :shift') |
|
| 316 | 2 | ->where($options->getLeftColumnName() . ' >= :leftFrom' |
|
| 317 | 2 | . ' AND ' . $options->getRightColumnName() . ' <= :rightTo'); |
|
| 318 | |||
| 319 | $params = array( |
||
| 320 | 2 | ':shift' => $shift, |
|
| 321 | 2 | ':leftFrom' => $leftIndexFrom, |
|
| 322 | 2 | ':rightTo' => $rightIndexTo, |
|
| 323 | ); |
||
| 324 | |||
| 325 | 2 | if ($options->getScopeColumnName()) { |
|
| 326 | 1 | $sql->andWhere($options->getScopeColumnName() . ' = :scope'); |
|
| 327 | 1 | $params[':scope'] = $scope; |
|
| 328 | } |
||
| 329 | |||
| 330 | 2 | $connection->executeUpdate($sql->getSQL(), $params); |
|
| 331 | 2 | } |
|
| 332 | |||
| 333 | 4 | public function getRoots($scope = null) |
|
| 334 | { |
||
| 335 | 4 | $options = $this->getOptions(); |
|
| 336 | |||
| 337 | 4 | $connection = $this->getConnection(); |
|
| 338 | |||
| 339 | 4 | $sql = $this->getBlankDbSelect(); |
|
| 340 | 4 | $sql->where($options->getParentIdColumnName() . ' IS NULL'); |
|
| 341 | 4 | $sql->orderBy($options->getIdColumnName()); |
|
| 342 | |||
| 343 | 4 | $params = array(); |
|
| 344 | |||
| 345 | 4 | if (null != $scope && $options->getScopeColumnName()) { |
|
| 346 | 1 | $sql->where($options->getScopeColumnName() . ' = :scope'); |
|
| 347 | 1 | $params[':scope'] = $scope; |
|
| 348 | } |
||
| 349 | |||
| 350 | 4 | $stmt = $connection->executeQuery($sql->getSQL(), $params); |
|
| 351 | |||
| 352 | 4 | $node = $stmt->fetchAll(); |
|
| 353 | |||
| 354 | 4 | return $node; |
|
| 355 | } |
||
| 356 | |||
| 357 | 2 | public function getRoot($scope = null) |
|
| 362 | |||
| 363 | 2 | public function getNode($nodeId) |
|
| 364 | { |
||
| 365 | 2 | $options = $this->getOptions(); |
|
| 366 | |||
| 367 | 2 | $nodeId = (int) $nodeId; |
|
| 368 | |||
| 369 | 2 | $connection = $this->getConnection(); |
|
| 370 | |||
| 371 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * @param array $data |
||
| 388 | * @return NodeInfo |
||
| 389 | */ |
||
| 390 | 14 | private function _buildNodeInfoObject(array $data) |
|
| 408 | |||
| 409 | 15 | public function getNodeInfo($nodeId) |
|
| 435 | |||
| 436 | 3 | public function getChildrenNodeInfo($parentNodeId) |
|
| 472 | |||
| 473 | 2 | public function updateNodeMetadata(NodeInfo $nodeInfo) |
|
| 492 | |||
| 493 | 5 | public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false) |
|
| 540 | |||
| 541 | 7 | public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null) |
|
| 595 | } |
||
| 596 |