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 | 56 | public function __construct(Options $options, ZendDbAdapter $dbAdapter) |
|
| 17 | { |
||
| 18 | 56 | $this->setOptions($options); |
|
| 19 | 56 | $this->setDbAdapter($dbAdapter); |
|
| 20 | 56 | } |
|
| 21 | |||
| 22 | /** |
||
| 23 | * @param ZendDbAdapter $dbAdapter |
||
| 24 | */ |
||
| 25 | 56 | protected function setDbAdapter(ZendDbAdapter $dbAdapter): void |
|
| 26 | { |
||
| 27 | 56 | $this->dbAdapter = $dbAdapter; |
|
| 28 | 56 | } |
|
| 29 | |||
| 30 | /** |
||
| 31 | * @return ZendDbAdapter |
||
| 32 | */ |
||
| 33 | 53 | public function getDbAdapter(): ZendDbAdapter |
|
| 34 | { |
||
| 35 | 53 | return $this->dbAdapter; |
|
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritdoc} |
||
| 40 | * |
||
| 41 | * @return ZendDbSelect |
||
| 42 | */ |
||
| 43 | 32 | public function getBlankDbSelect(): ZendDbSelect |
|
| 44 | { |
||
| 45 | 32 | return $this->getDbAdapter() |
|
| 46 | 32 | ->select() |
|
| 47 | 32 | ->from($this->getOptions()->getTableName()); |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Return default db select. Always new instance. |
||
| 52 | * |
||
| 53 | * @return ZendDbSelect |
||
| 54 | */ |
||
| 55 | 17 | public function getDefaultDbSelect(): ZendDbSelect |
|
| 56 | { |
||
| 57 | 17 | return $this->getDbSelectBuilder()(); |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | 1 | public function lockTree(): void |
|
| 64 | { |
||
| 65 | 1 | $options = $this->getOptions(); |
|
| 66 | |||
| 67 | 1 | $dbAdapter = $this->getDbAdapter(); |
|
| 68 | |||
| 69 | 1 | $select = $this->getBlankDbSelect() |
|
| 70 | 1 | ->reset(\Zend_Db_Select::COLUMNS) |
|
| 71 | 1 | ->columns(array('i' => $options->getIdColumnName(true))) |
|
| 72 | 1 | ->forUpdate(true); |
|
| 73 | |||
| 74 | 1 | $dbAdapter->fetchAll($select); |
|
| 75 | 1 | } |
|
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritdoc} |
||
| 79 | */ |
||
| 80 | 1 | public function beginTransaction(): void |
|
| 81 | { |
||
| 82 | 1 | $this->getDbAdapter()->beginTransaction(); |
|
| 83 | 1 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | */ |
||
| 88 | 1 | public function commitTransaction(): void |
|
| 89 | { |
||
| 90 | 1 | $this->getDbAdapter()->commit(); |
|
| 91 | 1 | } |
|
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | 1 | public function rollbackTransaction(): void |
|
| 97 | { |
||
| 98 | 1 | $this->getDbAdapter()->rollBack(); |
|
| 99 | 1 | } |
|
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | 3 | public function update($nodeId, array $data): void |
|
| 105 | { |
||
| 106 | 3 | $options = $this->getOptions(); |
|
| 107 | |||
| 108 | 3 | $dbAdapter = $this->getDbAdapter(); |
|
| 109 | |||
| 110 | 3 | $data = $this->cleanData($data); |
|
| 111 | |||
| 112 | $where = array( |
||
| 113 | 3 | $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
|
| 114 | ); |
||
| 115 | 3 | $dbAdapter->update($options->getTableName(), $data, $where); |
|
| 116 | 3 | } |
|
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | 4 | public function insert(NodeInfo $nodeInfo, array $data) |
|
| 122 | { |
||
| 123 | 4 | $options = $this->getOptions(); |
|
| 124 | 4 | $dbAdapter = $this->getDbAdapter(); |
|
| 125 | |||
| 126 | 4 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
|
| 127 | 4 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
|
| 128 | 4 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
|
| 129 | 4 | $data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
|
| 130 | |||
| 131 | 4 | if ($options->getScopeColumnName()) { |
|
|
|
|||
| 132 | 1 | $data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
|
| 133 | } |
||
| 134 | |||
| 135 | 4 | $dbAdapter->insert($options->getTableName(), $data); |
|
| 136 | |||
| 137 | 4 | if (array_key_exists($options->getIdColumnName(), $data)) { |
|
| 138 | 1 | return $data[$options->getIdColumnName()]; |
|
| 139 | } else { |
||
| 140 | 3 | if ('' != $options->getSequenceName()) { |
|
| 141 | 3 | $lastGeneratedValue = $dbAdapter->lastSequenceId($options->getSequenceName()); |
|
| 142 | } else { |
||
| 143 | $lastGeneratedValue = $dbAdapter->lastInsertId(); |
||
| 144 | } |
||
| 145 | |||
| 146 | 3 | return $lastGeneratedValue; |
|
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | 2 | public function delete($nodeId): void |
|
| 154 | { |
||
| 155 | 2 | $options = $this->getOptions(); |
|
| 156 | |||
| 157 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 158 | |||
| 159 | $where = array( |
||
| 160 | 2 | $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
|
| 161 | ); |
||
| 162 | |||
| 163 | 2 | $dbAdapter->delete($options->getTableName(), $where); |
|
| 164 | 2 | } |
|
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | 2 | public function moveLeftIndexes($fromIndex, $shift, $scope = null): void |
|
| 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 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | 2 | public function moveRightIndexes($fromIndex, $shift, $scope = null): void |
|
| 198 | { |
||
| 199 | 2 | $options = $this->getOptions(); |
|
| 200 | |||
| 201 | 2 | if (0 == $shift) { |
|
| 202 | return; |
||
| 203 | } |
||
| 204 | |||
| 205 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 206 | |||
| 207 | 2 | $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
|
| 208 | 2 | .' SET '.$dbAdapter->quoteIdentifier($options->getRightColumnName()) |
|
| 209 | 2 | .' = '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' + :shift' |
|
| 210 | 2 | .' WHERE '.$dbAdapter->quoteIdentifier($options->getRightColumnName()).' > :fromIndex'; |
|
| 211 | |||
| 212 | 2 | if ($options->getScopeColumnName()) { |
|
| 213 | 1 | $sql .= ' AND '.$dbAdapter->quoteIdentifier($options->getScopeColumnName()).' = '.$dbAdapter->quote($scope); |
|
| 214 | } |
||
| 215 | |||
| 216 | $binds = array( |
||
| 217 | 2 | ':shift' => $shift, |
|
| 218 | 2 | ':fromIndex' => $fromIndex, |
|
| 219 | ); |
||
| 220 | |||
| 221 | 2 | $dbAdapter->prepare($sql)->execute($binds); |
|
| 222 | 2 | } |
|
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | 1 | public function updateParentId($nodeId, $newParentId): void |
|
| 228 | { |
||
| 229 | 1 | $options = $this->getOptions(); |
|
| 230 | |||
| 231 | 1 | $dbAdapter = $this->getDbAdapter(); |
|
| 232 | |||
| 233 | $bind = array( |
||
| 234 | 1 | $options->getParentIdColumnName() => $newParentId, |
|
| 235 | ); |
||
| 236 | |||
| 237 | $where = array( |
||
| 238 | 1 | $dbAdapter->quoteIdentifier($options->getIdColumnName()).' = ?' => $nodeId, |
|
| 239 | ); |
||
| 240 | 1 | $dbAdapter->update($options->getTableName(), $bind, $where); |
|
| 241 | 1 | } |
|
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | 2 | public function updateLevels(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | */ |
||
| 279 | 2 | public function moveBranch(int $leftIndexFrom, int $rightIndexTo, int $shift, $scope = null): void |
|
| 280 | { |
||
| 281 | 2 | if (0 == $shift) { |
|
| 282 | return; |
||
| 283 | } |
||
| 284 | |||
| 285 | 2 | $options = $this->getOptions(); |
|
| 286 | |||
| 287 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 288 | |||
| 289 | 2 | $sql = 'UPDATE '.$dbAdapter->quoteIdentifier($options->getTableName()) |
|
| 290 | 2 | .' SET '.$dbAdapter->quoteIdentifier($options->getLeftColumnName()) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * {@inheritdoc} |
||
| 312 | */ |
||
| 313 | 4 | public function getRoots($scope = null): array |
|
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritdoc} |
||
| 332 | */ |
||
| 333 | 2 | public function getRoot($scope = null): array |
|
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritdoc} |
||
| 342 | */ |
||
| 343 | 2 | public function getNode($nodeId): ?array |
|
| 358 | |||
| 359 | /** |
||
| 360 | * {@inheritdoc} |
||
| 361 | */ |
||
| 362 | 19 | public function getNodeInfo($nodeId): ?NodeInfo |
|
| 379 | |||
| 380 | /** |
||
| 381 | * {@inheritdoc} |
||
| 382 | */ |
||
| 383 | 4 | public function getChildrenNodeInfo($parentNodeId): array |
|
| 416 | |||
| 417 | /** |
||
| 418 | * {@inheritdoc} |
||
| 419 | */ |
||
| 420 | 2 | public function updateNodeMetadata(NodeInfo $nodeInfo): void |
|
| 437 | |||
| 438 | /** |
||
| 439 | * {@inheritdoc} |
||
| 440 | */ |
||
| 441 | 6 | public function getAncestors($nodeId, int $startLevel = 0, int $excludeLastNLevels = 0): array |
|
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | 9 | public function getDescendants($nodeId, int $startLevel = 0, ?int $levels = null, $excludeBranch = null): array |
|
| 539 | |||
| 540 | 2 | protected function getWhereBetween($column, $first, $second) |
|
| 549 | } |
||
| 550 |
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: