Complex classes like NestedSet 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 NestedSet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class NestedSet |
||
| 21 | implements TreeInterface |
||
|
|
|||
| 22 | { |
||
| 23 | private $adapter; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param Options $options |
||
| 27 | * @param object $dbAdapter |
||
| 28 | * @return TreeInterface |
||
| 29 | * @throws InvalidArgumentException |
||
| 30 | */ |
||
| 31 | 4 | public static function factory(Options $options, $dbAdapter) |
|
| 32 | { |
||
| 33 | 4 | if ($dbAdapter instanceof ExtendedAdapterInterface) { |
|
| 34 | 1 | $adapter = new Zend2DbAdapter($options, $dbAdapter); |
|
| 35 | } elseif ($dbAdapter instanceof DoctrineConnection) { |
||
| 36 | 1 | $adapter = new Doctrine2DBALAdapter($options, $dbAdapter); |
|
| 37 | } elseif ($dbAdapter instanceof Zend_Db_Adapter_Abstract) { |
||
| 38 | 1 | $adapter = new Zend1DbAdapter($options, $dbAdapter); |
|
| 39 | } else { |
||
| 40 | 1 | throw new InvalidArgumentException('Db adapter "' . get_class($dbAdapter) |
|
| 41 | 1 | . '" is not supported'); |
|
| 42 | } |
||
| 43 | |||
| 44 | 3 | return new self($adapter); |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param AdapterInterface $adapter |
||
| 49 | */ |
||
| 50 | 54 | public function __construct(AdapterInterface $adapter) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * @return AdapterInterface |
||
| 57 | */ |
||
| 58 | 54 | public function getAdapter() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @return int |
||
| 65 | */ |
||
| 66 | 33 | private function getRootNodeId() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Test if node is root node |
||
| 73 | * |
||
| 74 | * @param int $nodeId |
||
| 75 | * @return boolean |
||
| 76 | */ |
||
| 77 | 3 | private function isRoot($nodeId) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param int $nodeId |
||
| 88 | * @param array $data |
||
| 89 | */ |
||
| 90 | 3 | public function updateNode($nodeId, $data) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param int $targetNodeId |
||
| 98 | * @param string $placement |
||
| 99 | * @param array $data |
||
| 100 | * @return int|false Id of new created node. False if node has not been created |
||
| 101 | * @throws Exception |
||
| 102 | */ |
||
| 103 | 15 | protected function addNode($targetNodeId, $placement, $data = array()) |
|
| 104 | { |
||
| 105 | 15 | $adapter = $this->getAdapter(); |
|
| 106 | |||
| 107 | 15 | $adapter->beginTransaction(); |
|
| 108 | try { |
||
| 109 | 15 | $adapter->lockTable(); |
|
| 110 | |||
| 111 | 15 | $targetNode = $adapter->getNodeInfo($targetNodeId); |
|
| 112 | |||
| 113 | 15 | if (null == $targetNode) { |
|
| 114 | 3 | $adapter->commitTransaction(); |
|
| 115 | 3 | $adapter->unlockTable(); |
|
| 116 | |||
| 117 | 3 | return false; |
|
| 118 | } |
||
| 119 | |||
| 120 | 12 | $addStrategy = $this->getAddStrategy($targetNode, $placement); |
|
| 121 | |||
| 122 | 12 | if (false == $addStrategy->canAddNewNode($this->getRootNodeId())) { |
|
| 123 | 6 | $adapter->commitTransaction(); |
|
| 124 | 6 | $adapter->unlockTable(); |
|
| 125 | |||
| 126 | 6 | return false; |
|
| 127 | } |
||
| 128 | |||
| 129 | //make hole |
||
| 130 | 12 | $moveFromIndex = $addStrategy->moveIndexesFromIndex($targetNode); |
|
| 131 | 12 | $adapter->moveLeftIndexes($moveFromIndex, 2); |
|
| 132 | 12 | $adapter->moveRightIndexes($moveFromIndex, 2); |
|
| 133 | |||
| 134 | //insert new node |
||
| 135 | 12 | $newNodeInfo = new NodeInfo( |
|
| 136 | 12 | null, |
|
| 137 | 12 | $addStrategy->newParentId(), |
|
| 138 | 12 | $addStrategy->newLevel(), |
|
| 139 | 12 | $addStrategy->newLeftIndex(), |
|
| 140 | 12 | $addStrategy->newRightIndex() |
|
| 141 | ); |
||
| 142 | 12 | $lastGeneratedValue = $adapter->insert($newNodeInfo, $data); |
|
| 143 | |||
| 144 | 12 | $adapter->commitTransaction(); |
|
| 145 | 12 | $adapter->unlockTable(); |
|
| 146 | } catch (Exception $e) { |
||
| 147 | $adapter->rollbackTransaction(); |
||
| 148 | $adapter->unlockTable(); |
||
| 149 | |||
| 150 | throw $e; |
||
| 151 | } |
||
| 152 | |||
| 153 | 12 | return $lastGeneratedValue; |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param NodeInfo $targetNode |
||
| 158 | * @param string $placement |
||
| 159 | * @return AddStrategyInterface |
||
| 160 | * @throws InvalidArgumentException |
||
| 161 | */ |
||
| 162 | 12 | private function getAddStrategy(NodeInfo $targetNode, $placement) |
|
| 179 | |||
| 180 | 6 | public function addNodePlacementBottom($targetNodeId, $data = array()) |
|
| 184 | |||
| 185 | 3 | public function addNodePlacementTop($targetNodeId, $data = array()) |
|
| 189 | |||
| 190 | 3 | public function addNodePlacementChildBottom($targetNodeId, $data = array()) |
|
| 194 | |||
| 195 | 3 | public function addNodePlacementChildTop($targetNodeId, $data = array()) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param int $sourceNodeId |
||
| 202 | * @param int $targetNodeId |
||
| 203 | * @param string $placement |
||
| 204 | * @return boolean |
||
| 205 | * @throws Exception |
||
| 206 | * @throws InvalidArgumentException |
||
| 207 | */ |
||
| 208 | 15 | protected function moveNode($sourceNodeId, $targetNodeId, $placement) |
|
| 285 | |||
| 286 | 6 | public function moveNodePlacementBottom($sourceNodeId, $targetNodeId) |
|
| 290 | |||
| 291 | 3 | public function moveNodePlacementTop($sourceNodeId, $targetNodeId) |
|
| 295 | |||
| 296 | 3 | public function moveNodePlacementChildBottom($sourceNodeId, $targetNodeId) |
|
| 300 | |||
| 301 | 3 | public function moveNodePlacementChildTop($sourceNodeId, $targetNodeId) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param NodeInfo $sourceNode |
||
| 308 | * @param NodeInfo $targetNode |
||
| 309 | * @param string $placement |
||
| 310 | * @return MoveStrategyInterface |
||
| 311 | * @throws InvalidArgumentException |
||
| 312 | */ |
||
| 313 | 15 | private function getMoveStrategy(NodeInfo $sourceNode, NodeInfo $targetNode, $placement) |
|
| 330 | |||
| 331 | 3 | public function deleteBranch($nodeId) |
|
| 373 | |||
| 374 | 3 | public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false) |
|
| 379 | |||
| 380 | 3 | public function clear(array $data = array()) |
|
| 404 | |||
| 405 | 3 | public function getNode($nodeId) |
|
| 410 | |||
| 411 | 6 | public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null) |
|
| 416 | |||
| 417 | 3 | public function getChildren($nodeId) |
|
| 421 | } |
||
| 422 |