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