Complex classes like Zend1 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 Zend1, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Zend1 |
||
| 9 | implements AdapterInterface |
||
|
|
|||
| 10 | { |
||
| 11 | protected $options; |
||
| 12 | |||
| 13 | protected $dbAdapter; |
||
| 14 | |||
| 15 | protected $defaultDbSelect; |
||
| 16 | |||
| 17 | 49 | public function __construct(Options $options, ZendDbAdapter $dbAdapter) |
|
| 22 | |||
| 23 | /** |
||
| 24 | * @return Options |
||
| 25 | */ |
||
| 26 | 46 | private function getOptions() |
|
| 30 | |||
| 31 | /** |
||
| 32 | * @return ZendDbAdapter |
||
| 33 | */ |
||
| 34 | 45 | public function getDbAdapter() |
|
| 38 | |||
| 39 | 3 | private function cleanData(array $data) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Return base db select without any join, etc. |
||
| 57 | * @return \Zend_Db_Select |
||
| 58 | */ |
||
| 59 | 27 | public function getBlankDbSelect() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param \Zend_Db_Select $dbSelect |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | 1 | public function setDefaultDbSelect(\Zend_Db_Select $dbSelect) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Return clone of default select |
||
| 75 | * |
||
| 76 | * @return \Zend_Db_Select |
||
| 77 | */ |
||
| 78 | 14 | public function getDefaultDbSelect() |
|
| 88 | |||
| 89 | 1 | public function lockTree() |
|
| 102 | |||
| 103 | 1 | public function beginTransaction() |
|
| 107 | |||
| 108 | 1 | public function commitTransaction() |
|
| 112 | |||
| 113 | 1 | public function rollbackTransaction() |
|
| 117 | |||
| 118 | 3 | public function update($nodeId, array $data) |
|
| 119 | { |
||
| 120 | 3 | $options = $this->getOptions(); |
|
| 121 | |||
| 122 | 3 | $dbAdapter = $this->getDbAdapter(); |
|
| 123 | |||
| 124 | 3 | $data = $this->cleanData($data); |
|
| 125 | |||
| 126 | $where = array( |
||
| 127 | 3 | $dbAdapter->quoteIdentifier($options->getIdColumnName()) . ' = ?' => $nodeId, |
|
| 128 | ); |
||
| 129 | 3 | $dbAdapter->update($options->getTableName(), $data, $where); |
|
| 130 | 3 | } |
|
| 131 | |||
| 132 | 3 | public function insert(NodeInfo $nodeInfo, array $data) |
|
| 133 | { |
||
| 134 | 3 | $options = $this->getOptions(); |
|
| 135 | 3 | $dbAdapter = $this->getDbAdapter(); |
|
| 136 | |||
| 137 | 3 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
|
| 138 | 3 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
|
| 139 | 3 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
|
| 140 | 3 | $data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
|
| 141 | |||
| 142 | 3 | if ($options->getScopeColumnName()) { |
|
| 143 | 1 | $data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
|
| 144 | } |
||
| 145 | |||
| 146 | 3 | $dbAdapter->insert($options->getTableName(), $data); |
|
| 147 | 3 | if ('' != $options->getSequenceName()) { |
|
| 148 | 3 | $lastGeneratedValue = $dbAdapter->lastSequenceId($options->getSequenceName()); |
|
| 149 | } else { |
||
| 150 | $lastGeneratedValue = $dbAdapter->lastInsertId(); |
||
| 151 | } |
||
| 152 | |||
| 153 | 3 | return $lastGeneratedValue; |
|
| 154 | } |
||
| 155 | |||
| 156 | 2 | public function delete($nodeId) |
|
| 157 | { |
||
| 158 | 2 | $options = $this->getOptions(); |
|
| 159 | |||
| 160 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 161 | |||
| 162 | $where = array( |
||
| 163 | 2 | $dbAdapter->quoteIdentifier($options->getIdColumnName()) . ' = ?' => $nodeId, |
|
| 164 | ); |
||
| 165 | |||
| 166 | 2 | $dbAdapter->delete($options->getTableName(), $where); |
|
| 167 | 2 | } |
|
| 168 | |||
| 169 | 2 | public function moveLeftIndexes($fromIndex, $shift, $scope = null) |
|
| 170 | { |
||
| 171 | 2 | $options = $this->getOptions(); |
|
| 172 | |||
| 173 | 2 | if (0 == $shift) { |
|
| 174 | return; |
||
| 175 | } |
||
| 176 | |||
| 177 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 178 | 2 | $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName()) |
|
| 179 | 2 | . ' SET ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
|
| 180 | 2 | . ' = ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' + :shift' |
|
| 181 | 2 | . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' > :fromIndex'; |
|
| 182 | |||
| 183 | 2 | if ($options->getScopeColumnName()) { |
|
| 184 | 1 | $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope); |
|
| 185 | } |
||
| 186 | |||
| 187 | $binds = array( |
||
| 188 | 2 | ':shift' => $shift, |
|
| 189 | 2 | ':fromIndex' => $fromIndex, |
|
| 190 | ); |
||
| 191 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
| 192 | 2 | } |
|
| 193 | |||
| 194 | 2 | public function moveRightIndexes($fromIndex, $shift, $scope = null) |
|
| 195 | { |
||
| 196 | 2 | $options = $this->getOptions(); |
|
| 197 | |||
| 198 | 2 | if (0 == $shift) { |
|
| 199 | return; |
||
| 200 | } |
||
| 201 | |||
| 202 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 203 | |||
| 204 | 2 | $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName()) |
|
| 205 | 2 | . ' SET ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) |
|
| 206 | 2 | . ' = ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' + :shift' |
|
| 207 | 2 | . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' > :fromIndex'; |
|
| 208 | |||
| 209 | 2 | if ($options->getScopeColumnName()) { |
|
| 210 | 1 | $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope); |
|
| 211 | } |
||
| 212 | |||
| 213 | $binds = array( |
||
| 214 | 2 | ':shift' => $shift, |
|
| 215 | 2 | ':fromIndex' => $fromIndex, |
|
| 216 | ); |
||
| 217 | |||
| 218 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
| 219 | 2 | } |
|
| 220 | |||
| 221 | 1 | public function updateParentId($nodeId, $newParentId) |
|
| 222 | { |
||
| 223 | 1 | $options = $this->getOptions(); |
|
| 224 | |||
| 225 | 1 | $dbAdapter = $this->getDbAdapter(); |
|
| 226 | |||
| 227 | $bind = array( |
||
| 228 | 1 | $options->getParentIdColumnName() => $newParentId, |
|
| 229 | ); |
||
| 230 | |||
| 231 | $where = array( |
||
| 232 | 1 | $dbAdapter->quoteIdentifier($options->getIdColumnName()) . ' = ?' => $nodeId, |
|
| 233 | ); |
||
| 234 | 1 | $dbAdapter->update($options->getTableName(), $bind, $where); |
|
| 235 | 1 | } |
|
| 236 | |||
| 237 | 2 | public function updateLevels($leftIndexFrom, $rightIndexTo, $shift, $scope = null) |
|
| 238 | { |
||
| 239 | 2 | $options = $this->getOptions(); |
|
| 240 | |||
| 241 | 2 | if (0 == $shift) { |
|
| 242 | return null; |
||
| 243 | } |
||
| 244 | |||
| 245 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 246 | |||
| 247 | 2 | $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName()) |
|
| 248 | 2 | . ' SET ' . $dbAdapter->quoteIdentifier($options->getLevelColumnName()) |
|
| 249 | 2 | . ' = ' . $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' + :shift' |
|
| 250 | 2 | . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
|
| 251 | 2 | . ' >= :leftFrom' . ' AND ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) |
|
| 252 | 2 | . ' <= :rightTo'; |
|
| 253 | |||
| 254 | 2 | if ($options->getScopeColumnName()) { |
|
| 255 | 1 | $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope); |
|
| 256 | } |
||
| 257 | |||
| 258 | $binds = array( |
||
| 259 | 2 | ':shift' => $shift, |
|
| 260 | 2 | ':leftFrom' => $leftIndexFrom, |
|
| 261 | 2 | ':rightTo' => $rightIndexTo, |
|
| 262 | ); |
||
| 263 | |||
| 264 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
| 265 | 2 | } |
|
| 266 | |||
| 267 | 2 | public function moveBranch($leftIndexFrom, $rightIndexTo, $shift, $scope = null) |
|
| 268 | { |
||
| 269 | 2 | if (0 == $shift) { |
|
| 270 | return null; |
||
| 271 | } |
||
| 272 | |||
| 273 | 2 | $options = $this->getOptions(); |
|
| 274 | |||
| 275 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 276 | |||
| 277 | 2 | $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName()) |
|
| 278 | 2 | . ' SET ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
|
| 279 | 2 | . ' = ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' + :shift, ' |
|
| 280 | 2 | . $dbAdapter->quoteIdentifier($options->getRightColumnName()) |
|
| 281 | 2 | . ' = ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' + :shift' |
|
| 282 | 2 | . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom' |
|
| 283 | 2 | . ' AND ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo'; |
|
| 284 | |||
| 285 | 2 | if ($options->getScopeColumnName()) { |
|
| 286 | 1 | $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope); |
|
| 287 | } |
||
| 288 | |||
| 289 | $binds = array( |
||
| 290 | 2 | ':shift' => $shift, |
|
| 291 | 2 | ':leftFrom' => $leftIndexFrom, |
|
| 292 | 2 | ':rightTo' => $rightIndexTo, |
|
| 293 | ); |
||
| 294 | |||
| 295 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
| 296 | 2 | } |
|
| 297 | |||
| 298 | 4 | public function getRoots($scope = null) |
|
| 299 | { |
||
| 300 | 4 | $options = $this->getOptions(); |
|
| 301 | |||
| 302 | 4 | $dbAdapter = $this->getDbAdapter(); |
|
| 303 | |||
| 304 | 4 | $select = $this->getBlankDbSelect() |
|
| 305 | 4 | ->where($options->getParentIdColumnName() . ' IS NULL') |
|
| 306 | 4 | ->order($options->getIdColumnName()); |
|
| 307 | |||
| 308 | 4 | if (null != $scope && $options->getScopeColumnName()) { |
|
| 309 | 1 | $select->where($options->getScopeColumnName() . ' = ?', $scope); |
|
| 310 | } |
||
| 311 | |||
| 312 | 4 | return $dbAdapter->fetchAll($select); |
|
| 313 | } |
||
| 314 | |||
| 315 | 2 | public function getRoot($scope = null) |
|
| 321 | |||
| 322 | 2 | public function getNode($nodeId) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @param array $data |
||
| 339 | * @return NodeInfo |
||
| 340 | */ |
||
| 341 | 14 | private function _buildNodeInfoObject(array $data) |
|
| 342 | { |
||
| 343 | 14 | $options = $this->getOptions(); |
|
| 344 | |||
| 345 | 14 | $id = $data[$options->getIdColumnName()]; |
|
| 346 | 14 | $parentId = $data[$options->getParentIdColumnName()]; |
|
| 347 | 14 | $level = $data[$options->getLevelColumnName()]; |
|
| 348 | 14 | $left = $data[$options->getLeftColumnName()]; |
|
| 349 | 14 | $right = $data[$options->getRightColumnName()]; |
|
| 350 | |||
| 351 | 14 | if (isset($data[$options->getScopeColumnName()])) { |
|
| 352 | 4 | $scope = $data[$options->getScopeColumnName()]; |
|
| 353 | } else { |
||
| 354 | 10 | $scope = null; |
|
| 355 | } |
||
| 356 | |||
| 357 | 14 | return new NodeInfo($id, $parentId, $level, $left, $right, $scope); |
|
| 358 | } |
||
| 359 | |||
| 360 | 15 | public function getNodeInfo($nodeId) |
|
| 379 | |||
| 380 | 3 | public function getChildrenNodeInfo($parentNodeId) |
|
| 381 | { |
||
| 382 | 3 | $dbAdapter = $this->getDbAdapter(); |
|
| 383 | 3 | $options = $this->getOptions(); |
|
| 384 | |||
| 385 | $columns = array( |
||
| 386 | 3 | $options->getIdColumnName(), |
|
| 387 | 3 | $options->getLeftColumnName(), |
|
| 388 | 3 | $options->getRightColumnName(), |
|
| 389 | 3 | $options->getParentIdColumnName(), |
|
| 390 | 3 | $options->getLevelColumnName(), |
|
| 391 | ); |
||
| 392 | |||
| 393 | 3 | if ($options->getScopeColumnName()) { |
|
| 394 | 1 | $columns[] = $options->getScopeColumnName(); |
|
| 395 | } |
||
| 396 | |||
| 397 | 3 | $select = $this->getBlankDbSelect(); |
|
| 398 | 3 | $select->reset(\Zend_Db_Select::COLUMNS); |
|
| 399 | 3 | $select->columns($columns); |
|
| 400 | 3 | $select->order($options->getLeftColumnName()); |
|
| 401 | 3 | $select->where($options->getParentIdColumnName() . ' = ?', $parentNodeId); |
|
| 402 | |||
| 403 | 3 | $data = $dbAdapter->fetchAll($select); |
|
| 404 | |||
| 405 | 3 | $result = array(); |
|
| 406 | |||
| 407 | 3 | foreach ($data as $nodeData) { |
|
| 408 | 2 | $result[] = $this->_buildNodeInfoObject($nodeData); |
|
| 409 | } |
||
| 410 | |||
| 411 | 3 | return $result; |
|
| 412 | } |
||
| 413 | |||
| 414 | 2 | public function updateNodeMetadata(NodeInfo $nodeInfo) |
|
| 415 | { |
||
| 416 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 417 | 2 | $options = $this->getOptions(); |
|
| 418 | |||
| 419 | $bind = array( |
||
| 420 | 2 | $options->getRightColumnName() => $nodeInfo->getRight(), |
|
| 421 | 2 | $options->getLeftColumnName() => $nodeInfo->getLeft(), |
|
| 422 | 2 | $options->getLevelColumnName() => $nodeInfo->getLevel(), |
|
| 423 | ); |
||
| 424 | |||
| 425 | $where = array( |
||
| 426 | 2 | $dbAdapter->quoteIdentifier($options->getIdColumnName()) . ' = ?' => $nodeInfo->getId(), |
|
| 427 | ); |
||
| 428 | |||
| 429 | 2 | $dbAdapter->update($options->getTableName(), $bind, $where); |
|
| 430 | 2 | } |
|
| 431 | |||
| 432 | 5 | public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false) |
|
| 433 | { |
||
| 434 | 5 | $options = $this->getOptions(); |
|
| 435 | |||
| 436 | 5 | $startLevel = (int) $startLevel; |
|
| 437 | |||
| 438 | // node does not exist |
||
| 439 | 5 | if (!$nodeInfo = $this->getNodeInfo($nodeId)) { |
|
| 440 | 1 | return array(); |
|
| 441 | } |
||
| 442 | |||
| 443 | 4 | $dbAdapter = $this->getDbAdapter(); |
|
| 444 | |||
| 445 | 4 | $select = $this->getDefaultDbSelect(); |
|
| 446 | |||
| 447 | 4 | if ($options->getScopeColumnName()) { |
|
| 448 | 1 | $select->where($options->getScopeColumnName() .' = ?', $nodeInfo->getScope()); |
|
| 449 | } |
||
| 450 | |||
| 451 | 4 | $select->where( |
|
| 452 | 4 | $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' <= ?', $nodeInfo->getLeft() |
|
| 453 | 4 | )->where( |
|
| 454 | 4 | $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' >= ?', $nodeInfo->getRight() |
|
| 455 | 4 | )->order($options->getLeftColumnName() . ' ASC'); |
|
| 456 | |||
| 457 | 4 | if (0 < $startLevel) { |
|
| 458 | 1 | $select->where( |
|
| 459 | 1 | $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' >= ?', $startLevel |
|
| 460 | ); |
||
| 461 | } |
||
| 462 | |||
| 463 | 4 | if (true == $excludeLastNode) { |
|
| 464 | 1 | $select->where( |
|
| 465 | 1 | $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' < ?', $nodeInfo->getLevel() |
|
| 466 | ); |
||
| 467 | } |
||
| 468 | |||
| 469 | 4 | $result = $dbAdapter->fetchAll($select); |
|
| 470 | |||
| 471 | 4 | return $result; |
|
| 472 | } |
||
| 473 | |||
| 474 | 7 | public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null) |
|
| 475 | { |
||
| 476 | 7 | $options = $this->getOptions(); |
|
| 477 | |||
| 478 | 7 | if (!$nodeInfo = $this->getNodeInfo($nodeId)) { |
|
| 479 | 1 | return array(); |
|
| 480 | } |
||
| 481 | |||
| 482 | 6 | $dbAdapter = $this->getDbAdapter(); |
|
| 483 | 6 | $select = $this->getDefaultDbSelect(); |
|
| 484 | 6 | $select->order($options->getLeftColumnName() . ' ASC'); |
|
| 485 | |||
| 486 | 6 | if ($options->getScopeColumnName()) { |
|
| 487 | 1 | $select->where($options->getScopeColumnName() . ' = ?', $nodeInfo->getScope()); |
|
| 488 | } |
||
| 489 | |||
| 490 | 6 | if (0 != $startLevel) { |
|
| 491 | 2 | $level = $nodeInfo->getLevel() + (int) $startLevel; |
|
| 492 | 2 | $select->where( |
|
| 493 | 2 | $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' >= ?', $level |
|
| 494 | ); |
||
| 495 | } |
||
| 496 | |||
| 497 | 6 | if (null != $levels) { |
|
| 498 | 1 | $endLevel = $nodeInfo->getLevel() + (int) $startLevel + abs($levels); |
|
| 499 | 1 | $select->where( |
|
| 500 | 1 | $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' < ?', $endLevel |
|
| 501 | ); |
||
| 502 | } |
||
| 503 | |||
| 504 | 6 | if (null != $excludeBranch && null != ($excludeNodeInfo = $this->getNodeInfo($excludeBranch))) { |
|
| 505 | 1 | $where = sprintf( |
|
| 506 | 1 | "(%s OR %s) AND (%s OR %s)", |
|
| 507 | 1 | $this->getWhereBetween($options->getLeftColumnName(), $nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1), |
|
| 508 | 1 | $this->getWhereBetween($options->getLeftColumnName(), |
|
| 509 | 1 | $excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()), |
|
| 510 | 1 | $this->getWhereBetween($options->getRightColumnName(), |
|
| 511 | 1 | $excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()), |
|
| 512 | 1 | $this->getWhereBetween($options->getRightColumnName(), |
|
| 513 | 1 | $nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1) |
|
| 514 | ); |
||
| 515 | 1 | $select->where($where); |
|
| 516 | } else { |
||
| 517 | 5 | $select->where( |
|
| 518 | 5 | $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' >= ?', $nodeInfo->getLeft() |
|
| 519 | ); |
||
| 520 | 5 | $select->where( |
|
| 521 | 5 | $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' <= ?', $nodeInfo->getRight() |
|
| 522 | ); |
||
| 523 | } |
||
| 524 | |||
| 525 | 6 | $resultArray = $dbAdapter->fetchAll($select); |
|
| 526 | |||
| 527 | 6 | return (0 < count($resultArray)) ? $resultArray : array(); |
|
| 528 | } |
||
| 529 | |||
| 530 | 1 | protected function getWhereBetween($column, $first, $second) |
|
| 538 | } |
||
| 539 |