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 |
||
| 12 | class Zend1 extends AdapterAbstract implements AdapterInterface |
||
| 13 | { |
||
| 14 | protected $dbAdapter; |
||
| 15 | |||
| 16 | protected $defaultDbSelect; |
||
| 17 | |||
| 18 | 50 | public function __construct(Options $options, ZendDbAdapter $dbAdapter) |
|
| 19 | { |
||
| 20 | 50 | $this->setOptions($options); |
|
| 21 | 50 | $this->setDbAdapter($dbAdapter); |
|
| 22 | 50 | } |
|
| 23 | |||
| 24 | /** |
||
| 25 | * @param ZendDbAdapter $dbAdapter |
||
| 26 | */ |
||
| 27 | 50 | protected function setDbAdapter(ZendDbAdapter $dbAdapter): void |
|
| 28 | { |
||
| 29 | 50 | $this->dbAdapter = $dbAdapter; |
|
| 30 | 50 | } |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @return ZendDbAdapter |
||
| 34 | */ |
||
| 35 | 47 | public function getDbAdapter(): ZendDbAdapter |
|
| 36 | { |
||
| 37 | 47 | return $this->dbAdapter; |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Return base db select without any join, etc. |
||
| 42 | * |
||
| 43 | * @return ZendDbSelect |
||
| 44 | */ |
||
| 45 | 27 | public function getBlankDbSelect(): ZendDbSelect |
|
| 46 | { |
||
| 47 | 27 | return $this->getDbAdapter()->select()->from($this->getOptions()->getTableName()); |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param ZendDbSelect $dbSelect |
||
| 52 | */ |
||
| 53 | 1 | public function setDefaultDbSelect(ZendDbSelect $dbSelect): void |
|
| 54 | { |
||
| 55 | 1 | $this->defaultDbSelect = $dbSelect; |
|
| 56 | 1 | } |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Return clone of default select. |
||
| 60 | * |
||
| 61 | * @return ZendDbSelect |
||
| 62 | */ |
||
| 63 | 14 | public function getDefaultDbSelect(): ZendDbSelect |
|
| 64 | { |
||
| 65 | 14 | if (null == $this->defaultDbSelect) { |
|
| 66 | 13 | $this->defaultDbSelect = $this->getBlankDbSelect(); |
|
| 67 | } |
||
| 68 | |||
| 69 | 14 | $dbSelect = clone $this->defaultDbSelect; |
|
| 70 | |||
| 71 | 14 | return $dbSelect; |
|
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | 1 | public function lockTree(): void |
|
| 78 | { |
||
| 79 | 1 | $options = $this->getOptions(); |
|
| 80 | |||
| 81 | 1 | $dbAdapter = $this->getDbAdapter(); |
|
| 82 | |||
| 83 | 1 | $select = $this->getBlankDbSelect() |
|
| 84 | 1 | ->reset(\Zend_Db_Select::COLUMNS) |
|
| 85 | 1 | ->columns(array('i' => $options->getIdColumnName())) |
|
| 86 | 1 | ->forUpdate(true); |
|
| 87 | |||
| 88 | 1 | $dbAdapter->fetchAll($select); |
|
| 89 | 1 | } |
|
| 90 | |||
| 91 | /** |
||
| 92 | * {@inheritdoc} |
||
| 93 | */ |
||
| 94 | 1 | public function beginTransaction(): void |
|
| 95 | { |
||
| 96 | 1 | $this->getDbAdapter()->beginTransaction(); |
|
| 97 | 1 | } |
|
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritdoc} |
||
| 101 | */ |
||
| 102 | 1 | public function commitTransaction(): void |
|
| 103 | { |
||
| 104 | 1 | $this->getDbAdapter()->commit(); |
|
| 105 | 1 | } |
|
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | 1 | public function rollbackTransaction(): void |
|
| 111 | { |
||
| 112 | 1 | $this->getDbAdapter()->rollBack(); |
|
| 113 | 1 | } |
|
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | 3 | public function update($nodeId, array $data): void |
|
| 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 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | 3 | public function insert(NodeInfo $nodeInfo, array $data) |
|
| 136 | { |
||
| 137 | 3 | $options = $this->getOptions(); |
|
| 138 | 3 | $dbAdapter = $this->getDbAdapter(); |
|
| 139 | |||
| 140 | 3 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
|
| 141 | 3 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
|
| 142 | 3 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
|
| 143 | 3 | $data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
|
| 144 | |||
| 145 | 3 | if ($options->getScopeColumnName()) { |
|
|
|
|||
| 146 | 1 | $data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
|
| 147 | } |
||
| 148 | |||
| 149 | 3 | $dbAdapter->insert($options->getTableName(), $data); |
|
| 150 | 3 | if ('' != $options->getSequenceName()) { |
|
| 151 | $lastGeneratedValue = $dbAdapter->lastSequenceId($options->getSequenceName()); |
||
| 152 | } else { |
||
| 153 | 3 | $lastGeneratedValue = $dbAdapter->lastInsertId(); |
|
| 154 | } |
||
| 155 | |||
| 156 | 3 | return $lastGeneratedValue; |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | 2 | public function delete($nodeId): void |
|
| 163 | { |
||
| 164 | 2 | $options = $this->getOptions(); |
|
| 165 | |||
| 166 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 167 | |||
| 168 | $where = array( |
||
| 169 | 2 | $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
|
| 170 | ); |
||
| 171 | |||
| 172 | 2 | $dbAdapter->delete($options->getTableName(), $where); |
|
| 173 | 2 | } |
|
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | 2 | public function moveLeftIndexes($fromIndex, $shift, $scope = null): void |
|
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | 2 | public function moveRightIndexes($fromIndex, $shift, $scope = null): void |
|
| 207 | { |
||
| 208 | 2 | $options = $this->getOptions(); |
|
| 209 | |||
| 210 | 2 | if (0 == $shift) { |
|
| 211 | return; |
||
| 212 | } |
||
| 213 | |||
| 214 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 215 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | 1 | public function updateParentId($nodeId, $newParentId): void |
|
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | 2 | public function updateLevels(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | 2 | public function moveBranch(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
| 318 | |||
| 319 | /** |
||
| 320 | * {@inheritdoc} |
||
| 321 | */ |
||
| 322 | 4 | public function getRoots($scope = null): array |
|
| 338 | |||
| 339 | /** |
||
| 340 | * {@inheritdoc} |
||
| 341 | */ |
||
| 342 | 2 | public function getRoot($scope = null): array |
|
| 348 | |||
| 349 | /** |
||
| 350 | * {@inheritdoc} |
||
| 351 | */ |
||
| 352 | 2 | public function getNode($nodeId): ?array |
|
| 367 | |||
| 368 | /** |
||
| 369 | * {@inheritdoc} |
||
| 370 | */ |
||
| 371 | 15 | public function getNodeInfo($nodeId): ?NodeInfo |
|
| 390 | |||
| 391 | /** |
||
| 392 | * {@inheritdoc} |
||
| 393 | */ |
||
| 394 | 3 | public function getChildrenNodeInfo($parentNodeId): array |
|
| 427 | |||
| 428 | /** |
||
| 429 | * {@inheritdoc} |
||
| 430 | */ |
||
| 431 | 2 | public function updateNodeMetadata(NodeInfo $nodeInfo): void |
|
| 448 | |||
| 449 | /** |
||
| 450 | * {@inheritdoc} |
||
| 451 | */ |
||
| 452 | 5 | public function getPath($nodeId, int $startLevel = 0, bool $excludeLastNode = false): array |
|
| 493 | |||
| 494 | /** |
||
| 495 | * {@inheritdoc} |
||
| 496 | */ |
||
| 497 | 7 | public function getDescendants($nodeId, int $startLevel = 0, ?int $levels = null, ?int $excludeBranch = null): array |
|
| 552 | |||
| 553 | /** |
||
| 554 | * {@inheritdoc} |
||
| 555 | */ |
||
| 556 | 1 | protected function getWhereBetween($column, $first, $second) |
|
| 565 | } |
||
| 566 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: