Complex classes like Doctrine2DBALAdapter 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 Doctrine2DBALAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Doctrine2DBALAdapter |
||
| 12 | implements AdapterInterface |
||
|
|
|||
| 13 | { |
||
| 14 | private $options; |
||
| 15 | |||
| 16 | private $connection; |
||
| 17 | |||
| 18 | private $defaultDbSelect; |
||
| 19 | |||
| 20 | private $lockSqlBuilder; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param Options $options |
||
| 24 | * @param DbConnection $connection |
||
| 25 | */ |
||
| 26 | 21 | public function __construct(Options $options, DbConnection $connection) |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @return Options |
||
| 34 | */ |
||
| 35 | 20 | private function getOptions() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * @return DbConnection |
||
| 42 | */ |
||
| 43 | 19 | private function getConnection() |
|
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Data cannot contain keys like idColumnName, levelColumnName, ... |
||
| 51 | * |
||
| 52 | * @param array $data |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | 1 | private function cleanData(array $data) |
|
| 56 | { |
||
| 57 | 1 | $options = $this->getOptions(); |
|
| 58 | |||
| 59 | $disallowedDataKeys = array( |
||
| 60 | 1 | $options->getIdColumnName(), |
|
| 61 | 1 | $options->getLeftColumnName(), |
|
| 62 | 1 | $options->getRightColumnName(), |
|
| 63 | 1 | $options->getLevelColumnName(), |
|
| 64 | 1 | $options->getParentIdColumnName(), |
|
| 65 | ); |
||
| 66 | |||
| 67 | 1 | return array_diff_key($data, array_flip($disallowedDataKeys)); |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param QueryBuilder $dbSelect |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | 1 | public function setDefaultDbSelect(QueryBuilder $dbSelect) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Return clone of default db select |
||
| 81 | * @return QueryBuilder |
||
| 82 | */ |
||
| 83 | 18 | public function getDefaultDbSelect() |
|
| 84 | { |
||
| 85 | 18 | $options = $this->getOptions(); |
|
| 86 | |||
| 87 | 18 | if (null == $this->defaultDbSelect) { |
|
| 88 | 17 | $queryBuilder = $this->getConnection() |
|
| 89 | 17 | ->createQueryBuilder(); |
|
| 90 | |||
| 91 | 17 | $queryBuilder->select('*') |
|
| 92 | 17 | ->from($options->getTableName(), null); |
|
| 93 | |||
| 94 | 17 | $this->defaultDbSelect = $queryBuilder; |
|
| 95 | } |
||
| 96 | |||
| 97 | 18 | $dbSelect = clone $this->defaultDbSelect; |
|
| 98 | |||
| 99 | 18 | return $dbSelect; |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return LockSqlBuilderInterface |
||
| 104 | */ |
||
| 105 | 12 | private function getLockSqlBuilder() |
|
| 106 | { |
||
| 107 | 12 | if (null == $this->lockSqlBuilder) { |
|
| 108 | 12 | $vendorName = $this->getConnection() |
|
| 109 | 12 | ->getDatabasePlatform() |
|
| 110 | 12 | ->getName(); |
|
| 111 | |||
| 112 | 12 | $factory = new LockSqlBuilderFactory(); |
|
| 113 | 12 | $this->lockSqlBuilder = $factory->createAdapter($vendorName); |
|
| 114 | } |
||
| 115 | |||
| 116 | 12 | return $this->lockSqlBuilder; |
|
| 117 | } |
||
| 118 | |||
| 119 | 12 | public function lockTable() |
|
| 120 | { |
||
| 121 | 12 | $tableName = $this->getOptions() |
|
| 122 | 12 | ->getTableName(); |
|
| 123 | |||
| 124 | 12 | $sql = $this->getLockSqlBuilder() |
|
| 125 | 12 | ->getLockSqlString($tableName); |
|
| 126 | |||
| 127 | 12 | if (null != $sql) { |
|
| 128 | 12 | $this->getConnection() |
|
| 129 | 12 | ->executeQuery($sql); |
|
| 130 | } |
||
| 131 | 12 | } |
|
| 132 | |||
| 133 | 12 | public function unlockTable() |
|
| 134 | { |
||
| 135 | 12 | $sql = $this->getLockSqlBuilder() |
|
| 136 | 12 | ->getUnlockSqlString(); |
|
| 137 | |||
| 138 | 12 | if (null != $sql) { |
|
| 139 | $this->getConnection() |
||
| 140 | ->executeQuery($sql); |
||
| 141 | } |
||
| 142 | 12 | } |
|
| 143 | |||
| 144 | 12 | public function beginTransaction() |
|
| 149 | |||
| 150 | 12 | public function commitTransaction() |
|
| 155 | |||
| 156 | public function rollbackTransaction() |
||
| 161 | |||
| 162 | 2 | public function update($nodeId, array $data, NodeInfo $nodeInfo = null) |
|
| 163 | { |
||
| 164 | 2 | $options = $this->getOptions(); |
|
| 165 | |||
| 166 | 2 | $connection = $this->getConnection(); |
|
| 167 | |||
| 168 | 2 | if (null == $nodeInfo) { |
|
| 169 | 1 | $data = $this->cleanData($data); |
|
| 170 | } else { |
||
| 171 | 1 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
|
| 172 | 1 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
|
| 173 | 1 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
|
| 174 | 1 | $data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
|
| 175 | } |
||
| 176 | |||
| 177 | 2 | $sql = $connection->createQueryBuilder(); |
|
| 178 | |||
| 179 | 2 | $sql->update($options->getTableName(), null) |
|
| 180 | 2 | ->where($options->getIdColumnName() . ' = :' . $options->getIdColumnName()); |
|
| 181 | |||
| 182 | 2 | foreach ($data as $key => $value) { |
|
| 183 | 2 | $sql->set($connection->quoteIdentifier($key), ':' . $key); |
|
| 184 | } |
||
| 185 | |||
| 186 | 2 | $data[$options->getIdColumnName()] = $nodeId; |
|
| 187 | |||
| 188 | 2 | $connection->executeUpdate($sql, $data); |
|
| 189 | 2 | } |
|
| 190 | |||
| 191 | 4 | public function insert(NodeInfo $nodeInfo, array $data) |
|
| 206 | |||
| 207 | 1 | public function delete($leftIndex, $rightIndex) |
|
| 208 | { |
||
| 209 | 1 | $options = $this->getOptions(); |
|
| 210 | |||
| 211 | 1 | $connection = $this->getConnection(); |
|
| 225 | |||
| 226 | 1 | public function deleteAll($expectNodeId) |
|
| 241 | |||
| 242 | 9 | public function moveLeftIndexes($fromIndex, $shift) |
|
| 264 | |||
| 265 | 9 | public function moveRightIndexes($fromIndex, $shift) |
|
| 287 | |||
| 288 | 4 | public function updateParentId($nodeId, $newParentId) |
|
| 306 | |||
| 307 | 4 | public function updateLevels($leftIndexFrom, $rightIndexTo, $shift) |
|
| 331 | |||
| 332 | 4 | public function moveBranch($leftIndexFrom, $rightIndexTo, $shift) |
|
| 357 | |||
| 358 | 15 | public function getNode($nodeId) |
|
| 382 | |||
| 383 | 14 | public function getNodeInfo($nodeId) |
|
| 402 | |||
| 403 | 1 | public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false) |
|
| 446 | |||
| 447 | 2 | public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null) |
|
| 498 | } |
||
| 499 |