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) |
|
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()) |
|
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 | 4 | 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 | 16 | protected function moveNode($sourceNodeId, $targetNodeId, $placement) |
|
209 | { |
||
210 | 15 | $adapter = $this->getAdapter(); |
|
211 | |||
212 | //source node and target node are equal |
||
213 | 15 | if ($sourceNodeId == $targetNodeId) { |
|
214 | 3 | return false; |
|
215 | } |
||
216 | |||
217 | 15 | $adapter->beginTransaction(); |
|
218 | try { |
||
219 | 16 | $adapter->lockTable(); |
|
220 | |||
221 | 16 | $sourceNodeInfo = $adapter->getNodeInfo($sourceNodeId); |
|
222 | 15 | $targetNodeInfo = $adapter->getNodeInfo($targetNodeId); |
|
223 | |||
224 | //source node or target node does not exist |
||
225 | 15 | if (!$sourceNodeInfo || !$targetNodeInfo) { |
|
226 | 3 | $adapter->commitTransaction(); |
|
227 | 3 | $adapter->unlockTable(); |
|
228 | |||
229 | 3 | return false; |
|
230 | } |
||
231 | |||
232 | 15 | $moveStrategy = $this->getMoveStrategy($sourceNodeInfo, $targetNodeInfo, $placement); |
|
233 | |||
234 | 15 | if (!$moveStrategy->canMoveBranch($this->getRootNodeId())) { |
|
235 | 9 | $adapter->commitTransaction(); |
|
236 | 9 | $adapter->unlockTable(); |
|
237 | |||
238 | 9 | return false; |
|
239 | } |
||
240 | |||
241 | 12 | if ($moveStrategy->isSourceNodeAtRequiredPosition()) { |
|
242 | 12 | $adapter->commitTransaction(); |
|
243 | 12 | $adapter->unlockTable(); |
|
244 | |||
245 | 12 | return true; |
|
246 | } |
||
247 | |||
248 | //update parent id |
||
249 | 12 | $newParentId = $moveStrategy->getNewParentId(); |
|
250 | 12 | if ($sourceNodeInfo->getParentId() != $newParentId) { |
|
251 | 12 | $adapter->updateParentId($sourceNodeId, $newParentId); |
|
252 | 12 | } |
|
253 | |||
254 | //update levels |
||
255 | 12 | $adapter->updateLevels($sourceNodeInfo->getLeft(), $sourceNodeInfo->getRight(), |
|
256 | 12 | $moveStrategy->getLevelShift()); |
|
257 | |||
258 | //make hole |
||
259 | 12 | $adapter->moveLeftIndexes($moveStrategy->makeHoleFromIndex(), |
|
260 | 12 | $moveStrategy->getIndexShift()); |
|
261 | 12 | $adapter->moveRightIndexes($moveStrategy->makeHoleFromIndex(), |
|
262 | 12 | $moveStrategy->getIndexShift()); |
|
263 | |||
264 | //move branch to the hole |
||
265 | 12 | $adapter->moveBranch($moveStrategy->getHoleLeftIndex(), |
|
266 | 12 | $moveStrategy->getHoleRightIndex(), $moveStrategy->getSourceNodeIndexShift()); |
|
267 | |||
268 | //patch hole |
||
269 | 12 | $adapter->moveLeftIndexes($moveStrategy->fixHoleFromIndex(), |
|
270 | 12 | ($moveStrategy->getIndexShift() * -1)); |
|
271 | 12 | $adapter->moveRightIndexes($moveStrategy->fixHoleFromIndex(), |
|
272 | 12 | ($moveStrategy->getIndexShift() * -1)); |
|
273 | |||
274 | 12 | $adapter->commitTransaction(); |
|
275 | 12 | $adapter->unlockTable(); |
|
276 | 12 | } catch (Exception $e) { |
|
277 | $adapter->rollbackTransaction(); |
||
278 | $adapter->unlockTable(); |
||
279 | |||
280 | throw $e; |
||
281 | } |
||
282 | |||
283 | 12 | return true; |
|
284 | } |
||
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) |
|
332 | { |
||
333 | 3 | if ($this->isRoot($nodeId)) { |
|
334 | 3 | return false; |
|
335 | } |
||
336 | |||
337 | 3 | $adapter = $this->getAdapter(); |
|
338 | |||
339 | 3 | $adapter->beginTransaction(); |
|
340 | try { |
||
341 | 3 | $adapter->lockTable(); |
|
342 | |||
343 | // node does not exist |
||
344 | 3 | if (!$nodeInfo = $adapter->getNodeInfo($nodeId)) { |
|
345 | 3 | $adapter->commitTransaction(); |
|
346 | 3 | $adapter->unlockTable(); |
|
347 | |||
348 | 3 | return false; |
|
349 | } |
||
350 | |||
351 | // delete branch |
||
352 | 3 | $leftIndex = $nodeInfo->getLeft(); |
|
353 | 3 | $rightIndex = $nodeInfo->getRight(); |
|
354 | 3 | $adapter->delete($leftIndex, $rightIndex); |
|
355 | |||
356 | //patch hole |
||
357 | 3 | $moveFromIndex = $nodeInfo->getLeft(); |
|
358 | 3 | $shift = $nodeInfo->getLeft() - $nodeInfo->getRight() - 1; |
|
359 | 3 | $adapter->moveLeftIndexes($moveFromIndex, $shift); |
|
360 | 3 | $adapter->moveRightIndexes($moveFromIndex, $shift); |
|
361 | |||
362 | 3 | $adapter->commitTransaction(); |
|
363 | 3 | $adapter->unlockTable(); |
|
364 | 3 | } catch (Exception $e) { |
|
365 | $adapter->rollbackTransaction(); |
||
366 | $adapter->unlockTable(); |
||
367 | |||
368 | throw $e; |
||
369 | } |
||
370 | |||
371 | 3 | return true; |
|
372 | } |
||
373 | |||
374 | 3 | public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false) |
|
379 | |||
380 | 3 | public function clear(array $data = array()) |
|
381 | { |
||
382 | 3 | $adapter = $this->getAdapter(); |
|
383 | |||
384 | 3 | $adapter->beginTransaction(); |
|
385 | try { |
||
386 | 3 | $adapter->lockTable(); |
|
387 | |||
388 | 3 | $adapter->deleteAll($this->getRootNodeId()); |
|
389 | |||
390 | 3 | $nodeInfo = new NodeInfo(null, 0, 0, 1, 2); |
|
391 | 3 | $adapter->update($this->getRootNodeId(), $data, $nodeInfo); |
|
392 | |||
393 | 3 | $adapter->commitTransaction(); |
|
394 | 3 | $adapter->unlockTable(); |
|
395 | 3 | } catch (Exception $e) { |
|
396 | $adapter->rollbackTransaction(); |
||
397 | $adapter->unlockTable(); |
||
398 | |||
399 | throw $e; |
||
400 | } |
||
401 | |||
402 | 3 | return $this; |
|
403 | } |
||
404 | |||
405 | 3 | public function getNode($nodeId) |
|
410 | |||
411 | 7 | public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null) |
|
416 | |||
417 | 3 | public function getChildren($nodeId) |
|
421 | } |
||
422 |