Complex classes like Zend2DbAdapter 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 Zend2DbAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 9 | class Zend2DbAdapter  | 
            ||
| 10 | implements AdapterInterface  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 11 | { | 
            ||
| 12 | private $options;  | 
            ||
| 13 | |||
| 14 | private $dbAdapter;  | 
            ||
| 15 | |||
| 16 | private $defaultDbSelect = null;  | 
            ||
| 17 | |||
| 18 | 38 | public function __construct(Options $options, DbAdapter $dbAdapter)  | 
            |
| 23 | |||
| 24 | /**  | 
            ||
| 25 | * @return Options  | 
            ||
| 26 | */  | 
            ||
| 27 | 37 | private function getOptions()  | 
            |
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * @return DbAdapter  | 
            ||
| 34 | */  | 
            ||
| 35 | 34 | private function getDbAdapter()  | 
            |
| 39 | |||
| 40 | /**  | 
            ||
| 41 | * Data cannot contain keys like idColumnName, levelColumnName, ...  | 
            ||
| 42 | *  | 
            ||
| 43 | * @param array $data  | 
            ||
| 44 | * @return array  | 
            ||
| 45 | */  | 
            ||
| 46 | 2 | private function cleanData(array $data)  | 
            |
| 47 |     { | 
            ||
| 48 | 2 | $options = $this->getOptions();  | 
            |
| 49 | |||
| 50 | $disallowedDataKeys = array(  | 
            ||
| 51 | 2 | $options->getIdColumnName(),  | 
            |
| 52 | 2 | $options->getLeftColumnName(),  | 
            |
| 53 | 2 | $options->getRightColumnName(),  | 
            |
| 54 | 2 | $options->getLevelColumnName(),  | 
            |
| 55 | 2 | $options->getParentIdColumnName(),  | 
            |
| 56 | 2 | $options->getScopeColumnName(),  | 
            |
| 57 | );  | 
            ||
| 58 | |||
| 59 | 2 | return array_diff_key($data, array_flip($disallowedDataKeys));  | 
            |
| 60 | }  | 
            ||
| 61 | |||
| 62 | /**  | 
            ||
| 63 | * @param Db\Sql\Select $dbSelect  | 
            ||
| 64 | * @return void  | 
            ||
| 65 | */  | 
            ||
| 66 | 1 | public function setDefaultDbSelect(Db\Sql\Select $dbSelect)  | 
            |
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * Return clone of default db select  | 
            ||
| 73 | * @return Db\Sql\Select  | 
            ||
| 74 | */  | 
            ||
| 75 | 36 | public function getDefaultDbSelect()  | 
            |
| 76 |     { | 
            ||
| 77 | 36 | $options = $this->getOptions();  | 
            |
| 78 | |||
| 79 | 36 |         if (null === $this->defaultDbSelect) { | 
            |
| 80 | 35 | $this->defaultDbSelect = new Db\Sql\Select($options->getTableName());  | 
            |
| 81 | }  | 
            ||
| 82 | |||
| 83 | 36 | $dbSelect = clone $this->defaultDbSelect;  | 
            |
| 84 | |||
| 85 | 36 | return $dbSelect;  | 
            |
| 86 | }  | 
            ||
| 87 | |||
| 88 | 14 | public function lockTree($scope)  | 
            |
| 89 |     { | 
            ||
| 90 | 14 | $options = $this->getOptions();  | 
            |
| 91 | |||
| 92 | 14 | $dbAdapter = $this->getDbAdapter();  | 
            |
| 93 | |||
| 94 | 14 | $select = $this->getDefaultDbSelect();  | 
            |
| 95 | 14 | $select->columns(array(  | 
            |
| 96 | 14 | 'i' => $options->getIdColumnName(),  | 
            |
| 97 | ));  | 
            ||
| 98 | |||
| 99 | 14 |         if ($options->getScopeColumnName()) { | 
            |
| 100 | 4 | $select->where(array(  | 
            |
| 101 | 4 | $options->getScopeColumnName() => $scope,  | 
            |
| 102 | ));  | 
            ||
| 103 | }  | 
            ||
| 104 | |||
| 105 | 14 | $sql = $select->getSqlString($dbAdapter->getPlatform()) . ' FOR UPDATE';  | 
            |
| 106 | |||
| 107 | 14 | $dbAdapter->query($sql, DbAdapter::QUERY_MODE_EXECUTE);  | 
            |
| 108 | 14 | }  | 
            |
| 109 | |||
| 110 | 15 | public function beginTransaction()  | 
            |
| 115 | |||
| 116 | 14 | public function commitTransaction()  | 
            |
| 121 | |||
| 122 | 1 | public function rollbackTransaction()  | 
            |
| 127 | |||
| 128 | 2 | public function update($nodeId, array $data)  | 
            |
| 129 |     { | 
            ||
| 130 | 2 | $options = $this->getOptions();  | 
            |
| 131 | |||
| 132 | 2 | $dbAdapter = $this->getDbAdapter();  | 
            |
| 133 | |||
| 134 | 2 | $data = $this->cleanData($data);  | 
            |
| 135 | |||
| 136 | 2 | $update = new Db\Sql\Update($options->getTableName());  | 
            |
| 137 | 2 | $update->set($data)  | 
            |
| 138 | 2 | ->where(array(  | 
            |
| 139 | 2 | $options->getIdColumnName() => $nodeId,  | 
            |
| 140 | ));  | 
            ||
| 141 | |||
| 142 | 2 | $dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()),  | 
            |
| 143 | 2 | DbAdapter::QUERY_MODE_EXECUTE);  | 
            |
| 144 | 2 | }  | 
            |
| 145 | |||
| 146 | 9 | public function insert(NodeInfo $nodeInfo, array $data)  | 
            |
| 147 |     { | 
            ||
| 148 | 9 | $options = $this->getOptions();  | 
            |
| 149 | |||
| 150 | 9 | $dbAdapter = $this->getDbAdapter();  | 
            |
| 151 | |||
| 152 | 9 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId();  | 
            |
| 153 | 9 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel();  | 
            |
| 154 | 9 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft();  | 
            |
| 155 | 9 | $data[$options->getRightColumnName()] = $nodeInfo->getRight();  | 
            |
| 156 | |||
| 157 | 9 |         if ($options->getScopeColumnName()) { | 
            |
| 158 | 3 | $data[$options->getScopeColumnName()] = $nodeInfo->getScope();  | 
            |
| 159 | }  | 
            ||
| 160 | |||
| 161 | 9 | $insert = new Db\Sql\Insert($options->getTableName());  | 
            |
| 162 | 9 | $insert->values($data);  | 
            |
| 163 | 9 | $dbAdapter->query($insert->getSqlString($dbAdapter->getPlatform()),  | 
            |
| 164 | 9 | DbAdapter::QUERY_MODE_EXECUTE);  | 
            |
| 165 | |||
| 166 | 9 | $lastGeneratedValue = $dbAdapter->getDriver()  | 
            |
| 167 | 9 | ->getLastGeneratedValue($options->getSequenceName());  | 
            |
| 168 | |||
| 169 | 9 | return $lastGeneratedValue;  | 
            |
| 170 | }  | 
            ||
| 171 | |||
| 172 | 2 | public function delete($leftIndex, $rightIndex, $scope = null)  | 
            |
| 173 |     { | 
            ||
| 174 | 2 | $options = $this->getOptions();  | 
            |
| 175 | |||
| 176 | 2 | $dbAdapter = $this->getDbAdapter();  | 
            |
| 177 | |||
| 178 | 2 | $delete = new Db\Sql\Delete($options->getTableName());  | 
            |
| 179 | 2 | $delete->where  | 
            |
| 180 | 2 | ->greaterThanOrEqualTo($options->getLeftColumnName(), $leftIndex)  | 
            |
| 181 | 2 | ->AND  | 
            |
| 182 | 2 | ->lessThanOrEqualTo($options->getRightColumnName(), $rightIndex);  | 
            |
| 183 | |||
| 184 | 2 |         if ($options->getScopeColumnName()) { | 
            |
| 185 | 1 | $delete->where  | 
            |
| 186 | 1 | ->AND  | 
            |
| 187 | 1 | ->equalTo($options->getScopeColumnName(), $scope);  | 
            |
| 188 | }  | 
            ||
| 189 | |||
| 190 | 2 | $dbAdapter->query($delete->getSqlString($dbAdapter->getPlatform()),  | 
            |
| 191 | 2 | DbAdapter::QUERY_MODE_EXECUTE);  | 
            |
| 192 | 2 | }  | 
            |
| 193 | |||
| 194 | 12 | public function moveLeftIndexes($fromIndex, $shift, $scope = null)  | 
            |
| 195 |     { | 
            ||
| 196 | 12 | $options = $this->getOptions();  | 
            |
| 197 | |||
| 198 | 12 |         if (0 == $shift) { | 
            |
| 199 | return null;  | 
            ||
| 200 | }  | 
            ||
| 201 | |||
| 202 | 12 | $dbAdapter = $this->getDbAdapter();  | 
            |
| 203 | 12 | $dbPlatform = $dbAdapter->getPlatform();  | 
            |
| 204 | |||
| 205 | 12 | $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName())  | 
            |
| 206 | 12 | . ' SET '  | 
            |
| 207 | 12 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' = '  | 
            |
| 208 | 12 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' + :shift'  | 
            |
| 209 | 12 | . ' WHERE '  | 
            |
| 210 | 12 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' > :fromIndex';  | 
            |
| 211 | |||
| 212 | 12 |         if ($options->getScopeColumnName()) { | 
            |
| 213 | 3 | $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope);  | 
            |
| 214 | }  | 
            ||
| 215 | |||
| 216 | $binds = array(  | 
            ||
| 217 | 12 | ':shift' => $shift,  | 
            |
| 218 | 12 | ':fromIndex' => $fromIndex,  | 
            |
| 219 | );  | 
            ||
| 220 | |||
| 221 | 12 | $dbAdapter->query($sql)  | 
            |
| 222 | 12 | ->execute($binds);  | 
            |
| 223 | 12 | }  | 
            |
| 224 | |||
| 225 | 12 | public function moveRightIndexes($fromIndex, $shift, $scope = null)  | 
            |
| 226 |     { | 
            ||
| 227 | 12 | $options = $this->getOptions();  | 
            |
| 228 | |||
| 229 | 12 |         if (0 == $shift) { | 
            |
| 230 | return null;  | 
            ||
| 231 | }  | 
            ||
| 232 | |||
| 233 | 12 | $dbAdapter = $this->getDbAdapter();  | 
            |
| 234 | 12 | $dbPlatform = $dbAdapter->getPlatform();  | 
            |
| 235 | |||
| 236 | 12 | $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName())  | 
            |
| 237 | 12 | . ' SET '  | 
            |
| 238 | 12 | . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' = '  | 
            |
| 239 | 12 | . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' + :shift'  | 
            |
| 240 | 12 | . ' WHERE '  | 
            |
| 241 | 12 | . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' > :fromIndex';  | 
            |
| 242 | |||
| 243 | 12 |         if ($options->getScopeColumnName()) { | 
            |
| 244 | 3 | $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope);  | 
            |
| 245 | }  | 
            ||
| 246 | |||
| 247 | $binds = array(  | 
            ||
| 248 | 12 | ':shift' => $shift,  | 
            |
| 249 | 12 | ':fromIndex' => $fromIndex,  | 
            |
| 250 | );  | 
            ||
| 251 | |||
| 252 | 12 | $dbAdapter->query($sql)  | 
            |
| 253 | 12 | ->execute($binds);  | 
            |
| 254 | 12 | }  | 
            |
| 255 | |||
| 256 | 4 | public function updateParentId($nodeId, $newParentId)  | 
            |
| 257 |     { | 
            ||
| 258 | 4 | $options = $this->getOptions();  | 
            |
| 259 | |||
| 260 | 4 | $dbAdapter = $this->getDbAdapter();  | 
            |
| 261 | |||
| 262 | 4 | $update = new Db\Sql\Update($options->getTableName());  | 
            |
| 263 | 4 | $update->set(array(  | 
            |
| 264 | 4 | $options->getParentIdColumnName() => $newParentId,  | 
            |
| 265 | ))  | 
            ||
| 266 | 4 | ->where(array(  | 
            |
| 267 | 4 | $options->getIdColumnName() => $nodeId,  | 
            |
| 268 | ));  | 
            ||
| 269 | |||
| 270 | 4 | $dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()),  | 
            |
| 271 | 4 | DbAdapter::QUERY_MODE_EXECUTE);  | 
            |
| 272 | 4 | }  | 
            |
| 273 | |||
| 274 | 5 | public function updateLevels($leftIndexFrom, $rightIndexTo, $shift, $scope = null)  | 
            |
| 275 |     { | 
            ||
| 276 | 5 | $options = $this->getOptions();  | 
            |
| 277 | |||
| 278 | 5 |         if (0 == $shift) { | 
            |
| 279 | 1 | return null;  | 
            |
| 280 | }  | 
            ||
| 281 | |||
| 282 | 4 | $dbAdapter = $this->getDbAdapter();  | 
            |
| 283 | 4 | $dbPlatform = $dbAdapter->getPlatform();  | 
            |
| 284 | |||
| 285 | 4 | $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName())  | 
            |
| 286 | 4 | . ' SET '  | 
            |
| 287 | 4 | . $dbPlatform->quoteIdentifier($options->getLevelColumnName()) . ' = '  | 
            |
| 288 | 4 | . $dbPlatform->quoteIdentifier($options->getLevelColumnName()) . ' + :shift'  | 
            |
| 289 | 4 | . ' WHERE '  | 
            |
| 290 | 4 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom'  | 
            |
| 291 | 4 | . ' AND ' . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo';  | 
            |
| 292 | |||
| 293 | 4 |         if ($options->getScopeColumnName()) { | 
            |
| 294 | $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope);  | 
            ||
| 295 | }  | 
            ||
| 296 | |||
| 297 | $binds = array(  | 
            ||
| 298 | 4 | ':shift' => $shift,  | 
            |
| 299 | 4 | ':leftFrom' => $leftIndexFrom,  | 
            |
| 300 | 4 | ':rightTo' => $rightIndexTo,  | 
            |
| 301 | );  | 
            ||
| 302 | |||
| 303 | 4 | $dbAdapter->query($sql)  | 
            |
| 304 | 4 | ->execute($binds);  | 
            |
| 305 | 4 | }  | 
            |
| 306 | |||
| 307 | 5 | public function moveBranch($leftIndexFrom, $rightIndexTo, $shift, $scope = null)  | 
            |
| 308 |     { | 
            ||
| 309 | 5 |         if (0 == $shift) { | 
            |
| 310 | return null;  | 
            ||
| 311 | }  | 
            ||
| 312 | |||
| 313 | 5 | $options = $this->getOptions();  | 
            |
| 314 | |||
| 315 | 5 | $dbAdapter = $this->getDbAdapter();  | 
            |
| 316 | 5 | $dbPlatform = $dbAdapter->getPlatform();  | 
            |
| 317 | |||
| 318 | 5 | $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName())  | 
            |
| 319 | 5 | . ' SET '  | 
            |
| 320 | 5 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' = '  | 
            |
| 321 | 5 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' + :shift, '  | 
            |
| 322 | 5 | . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' = '  | 
            |
| 323 | 5 | . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' + :shift'  | 
            |
| 324 | 5 | . ' WHERE '  | 
            |
| 325 | 5 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom'  | 
            |
| 326 | 5 | . ' AND ' . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo';  | 
            |
| 327 | |||
| 328 | 5 |         if ($options->getScopeColumnName()) { | 
            |
| 329 | 1 | $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope);  | 
            |
| 330 | }  | 
            ||
| 331 | |||
| 332 | $binds = array(  | 
            ||
| 333 | 5 | ':shift' => $shift,  | 
            |
| 334 | 5 | ':leftFrom' => $leftIndexFrom,  | 
            |
| 335 | 5 | ':rightTo' => $rightIndexTo,  | 
            |
| 336 | );  | 
            ||
| 337 | |||
| 338 | 5 | $dbAdapter->query($sql)  | 
            |
| 339 | 5 | ->execute($binds);  | 
            |
| 340 | 5 | }  | 
            |
| 341 | |||
| 342 | 8 | public function getRoots($scope = null)  | 
            |
| 343 |     { | 
            ||
| 344 | 8 | $options = $this->getOptions();  | 
            |
| 345 | |||
| 346 | 8 | $dbAdapter = $this->getDbAdapter();  | 
            |
| 347 | |||
| 348 | 8 | $select = $this->getDefaultDbSelect();  | 
            |
| 349 | 8 | $select->where  | 
            |
| 350 | 8 | ->equalTo($options->getParentIdColumnName(), 0);  | 
            |
| 351 | |||
| 352 | 8 |         if (null != $scope && $options->getScopeColumnName()) { | 
            |
| 353 | 2 | $select->where  | 
            |
| 354 | 2 | ->equalTo($options->getScopeColumnName(), $scope);  | 
            |
| 355 | }  | 
            ||
| 356 | |||
| 357 | 8 | $result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()),  | 
            |
| 358 | 8 | DbAdapter::QUERY_MODE_EXECUTE);  | 
            |
| 359 | |||
| 360 | 8 | return $result->toArray();  | 
            |
| 361 | }  | 
            ||
| 362 | |||
| 363 | 7 | public function getRoot($scope = null)  | 
            |
| 369 | |||
| 370 | 25 | public function getNode($nodeId)  | 
            |
| 388 | |||
| 389 | /**  | 
            ||
| 390 | * @param array $data  | 
            ||
| 391 | * @return NodeInfo  | 
            ||
| 392 | */  | 
            ||
| 393 | 22 | private function _buildNodeInfoObject(array $data)  | 
            |
| 394 |     { | 
            ||
| 395 | 22 | $options = $this->getOptions();  | 
            |
| 396 | |||
| 397 | 22 | $id = $data[$options->getIdColumnName()];  | 
            |
| 398 | 22 | $parentId = $data[$options->getParentIdColumnName()];  | 
            |
| 399 | 22 | $level = $data[$options->getLevelColumnName()];  | 
            |
| 400 | 22 | $left = $data[$options->getLeftColumnName()];  | 
            |
| 401 | 22 | $right = $data[$options->getRightColumnName()];  | 
            |
| 402 | |||
| 403 | 22 |         if (isset($data[$options->getScopeColumnName()])) { | 
            |
| 404 | 9 | $scope = $data[$options->getScopeColumnName()];  | 
            |
| 405 |         } else { | 
            ||
| 406 | 13 | $scope = null;  | 
            |
| 407 | }  | 
            ||
| 408 | |||
| 409 | 22 | return new NodeInfo($id, $parentId, $level, $left, $right, $scope);  | 
            |
| 410 | }  | 
            ||
| 411 | |||
| 412 | 23 | public function getNodeInfo($nodeId)  | 
            |
| 420 | |||
| 421 | 3 | public function getChildrenNodeInfo($parentNodeId)  | 
            |
| 422 |     { | 
            ||
| 423 | 3 | $dbAdapter = $this->getDbAdapter();  | 
            |
| 424 | 3 | $options = $this->getOptions();  | 
            |
| 425 | |||
| 426 | $columns = array(  | 
            ||
| 427 | 3 | $options->getIdColumnName(),  | 
            |
| 428 | 3 | $options->getLeftColumnName(),  | 
            |
| 429 | 3 | $options->getRightColumnName(),  | 
            |
| 430 | 3 | $options->getParentIdColumnName(),  | 
            |
| 431 | 3 | $options->getLevelColumnName(),  | 
            |
| 432 | );  | 
            ||
| 433 | |||
| 456 | |||
| 457 | 1 | public function updateNodeMetadata(NodeInfo $nodeInfo)  | 
            |
| 477 | |||
| 478 | |||
| 479 | 2 | public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false)  | 
            |
| 521 | |||
| 522 | 3 | public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null)  | 
            |
| 582 | }  | 
            ||
| 583 |