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 |
||
21 | class NestedSet |
||
22 | implements TreeInterface |
||
|
|||
23 | { |
||
24 | private $adapter; |
||
25 | |||
26 | private $validator; |
||
27 | |||
28 | /** |
||
29 | * @param Options $options |
||
30 | * @param object $dbAdapter |
||
31 | * @return TreeInterface |
||
32 | * @throws InvalidArgumentException |
||
33 | */ |
||
34 | public static function factory(Options $options, $dbAdapter) |
||
35 | { |
||
36 | 4 | if ($dbAdapter instanceof StefanoExtendedDbAdapterInterface) { |
|
37 | $adapter = new Adapter\StefanoDb($options, $dbAdapter); |
||
38 | 4 | } elseif ($dbAdapter instanceof Zend2DbAdapter) { |
|
39 | 1 | $adapter = new Adapter\Zend2($options, $dbAdapter); |
|
40 | } elseif ($dbAdapter instanceof DoctrineConnection) { |
||
41 | 1 | $adapter = new Adapter\Doctrine2DBAL($options, $dbAdapter); |
|
42 | } elseif ($dbAdapter instanceof \Zend_Db_Adapter_Abstract) { |
||
43 | 1 | $adapter = new Adapter\Zend1($options, $dbAdapter); |
|
44 | } else { |
||
45 | 1 | throw new InvalidArgumentException('Db adapter "' . get_class($dbAdapter) |
|
46 | 1 | . '" is not supported'); |
|
47 | } |
||
48 | |||
49 | 3 | return new self($adapter); |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param AdapterInterface $adapter |
||
54 | */ |
||
55 | 105 | public function __construct(AdapterInterface $adapter) |
|
59 | |||
60 | /** |
||
61 | * @return AdapterInterface |
||
62 | */ |
||
63 | 105 | public function getAdapter() |
|
67 | |||
68 | /** |
||
69 | * @return ValidatorInterface |
||
70 | */ |
||
71 | 9 | private function _getValidator() |
|
72 | { |
||
73 | 9 | if (null == $this->validator) { |
|
74 | 9 | $this->validator = new Validator($this->getAdapter()); |
|
75 | } |
||
76 | |||
77 | 9 | return $this->validator; |
|
78 | } |
||
79 | |||
80 | 15 | public function createRootNode($data = array(), $scope = null) |
|
81 | { |
||
82 | 15 | if ($this->getRootNode($scope)) { |
|
83 | 6 | if ($scope) { |
|
84 | 6 | $errorMessage = sprintf('Root node for scope "%s" already exist', $scope); |
|
85 | } else { |
||
86 | $errorMessage = 'Root node already exist'; |
||
87 | } |
||
88 | 12 | ||
89 | throw new RootNodeAlreadyExistException($errorMessage); |
||
90 | 12 | } |
|
91 | |||
92 | $nodeInfo = new NodeInfo(null, null, 0, 1, 2, $scope); |
||
93 | |||
94 | return $this->getAdapter()->insert($nodeInfo, $data); |
||
95 | } |
||
96 | |||
97 | 6 | /** |
|
98 | * @param int $nodeId |
||
99 | 6 | * @param array $data |
|
100 | 6 | */ |
|
101 | 6 | public function updateNode($nodeId, $data) |
|
102 | { |
||
103 | $this->getAdapter() |
||
104 | ->update($nodeId, $data); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param int $targetNodeId |
||
109 | * @param string $placement |
||
110 | 18 | * @param array $data |
|
111 | * @return int|false Id of new created node. False if node has not been created |
||
112 | 18 | * @throws Exception |
|
113 | */ |
||
114 | 18 | protected function addNode($targetNodeId, $placement, $data = array()) |
|
115 | { |
||
116 | 18 | $adapter = $this->getAdapter(); |
|
117 | 18 | ||
118 | 15 | $adapter->beginTransaction(); |
|
119 | 15 | try { |
|
120 | $adapter->lockTree(); |
||
121 | |||
122 | 18 | $targetNode = $adapter->getNodeInfo($targetNodeId); |
|
123 | |||
124 | 18 | if (null == $targetNode) { |
|
125 | 3 | $adapter->commitTransaction(); |
|
126 | |||
127 | 3 | return false; |
|
128 | } |
||
129 | |||
130 | 15 | $addStrategy = $this->getAddStrategy($targetNode, $placement); |
|
131 | |||
132 | 15 | if (false == $addStrategy->canAddNewNode()) { |
|
133 | 6 | $adapter->commitTransaction(); |
|
134 | |||
135 | 6 | return false; |
|
136 | } |
||
137 | |||
138 | //make hole |
||
139 | 15 | $moveFromIndex = $addStrategy->moveIndexesFromIndex(); |
|
140 | 15 | $adapter->moveLeftIndexes($moveFromIndex, 2, $targetNode->getScope()); |
|
141 | 15 | $adapter->moveRightIndexes($moveFromIndex, 2, $targetNode->getScope()); |
|
142 | |||
143 | //insert new node |
||
144 | 15 | $newNodeInfo = new NodeInfo( |
|
145 | 15 | null, |
|
146 | 15 | $addStrategy->newParentId(), |
|
147 | 15 | $addStrategy->newLevel(), |
|
148 | 15 | $addStrategy->newLeftIndex(), |
|
149 | 15 | $addStrategy->newRightIndex(), |
|
150 | 15 | $targetNode->getScope() |
|
151 | ); |
||
152 | 15 | $lastGeneratedValue = $adapter->insert($newNodeInfo, $data); |
|
153 | |||
154 | 15 | $adapter->commitTransaction(); |
|
155 | } catch (Exception $e) { |
||
156 | $adapter->rollbackTransaction(); |
||
157 | |||
158 | throw $e; |
||
159 | } |
||
160 | |||
161 | 15 | return $lastGeneratedValue; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param NodeInfo $targetNode |
||
166 | * @param string $placement |
||
167 | * @return AddStrategyInterface |
||
168 | * @throws InvalidArgumentException |
||
169 | */ |
||
170 | 15 | private function getAddStrategy(NodeInfo $targetNode, $placement) |
|
171 | { |
||
172 | switch ($placement) { |
||
173 | 15 | case self::PLACEMENT_BOTTOM: |
|
174 | 3 | return new AddStrategy\Bottom($targetNode); |
|
175 | 12 | case self::PLACEMENT_TOP: |
|
176 | 3 | return new AddStrategy\Top($targetNode); |
|
177 | 9 | case self::PLACEMENT_CHILD_BOTTOM: |
|
178 | 3 | return new AddStrategy\ChildBottom($targetNode); |
|
179 | 6 | case self::PLACEMENT_CHILD_TOP: |
|
180 | 6 | return new AddStrategy\ChildTop($targetNode); |
|
181 | default: |
||
182 | throw new InvalidArgumentException('Unknown placement "' . $placement . '"'); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | 6 | public function addNodePlacementBottom($targetNodeId, $data = array()) |
|
190 | |||
191 | 3 | public function addNodePlacementTop($targetNodeId, $data = array()) |
|
195 | |||
196 | 3 | public function addNodePlacementChildBottom($targetNodeId, $data = array()) |
|
200 | |||
201 | 6 | public function addNodePlacementChildTop($targetNodeId, $data = array()) |
|
205 | |||
206 | /** |
||
207 | * @param int $sourceNodeId |
||
208 | * @param int $targetNodeId |
||
209 | * @param string $placement |
||
210 | * @return boolean |
||
211 | * @throws Exception |
||
212 | * @throws InvalidArgumentException |
||
213 | */ |
||
214 | 21 | protected function moveNode($sourceNodeId, $targetNodeId, $placement) |
|
291 | |||
292 | public function moveNodePlacementBottom($sourceNodeId, $targetNodeId) |
||
296 | 9 | ||
297 | public function moveNodePlacementTop($sourceNodeId, $targetNodeId) |
||
301 | 3 | ||
302 | public function moveNodePlacementChildBottom($sourceNodeId, $targetNodeId) |
||
306 | 6 | ||
307 | public function moveNodePlacementChildTop($sourceNodeId, $targetNodeId) |
||
311 | 3 | ||
312 | /** |
||
313 | 3 | * @param NodeInfo $sourceNode |
|
314 | * @param NodeInfo $targetNode |
||
315 | * @param string $placement |
||
316 | * @return MoveStrategyInterface |
||
317 | * @throws InvalidArgumentException |
||
318 | */ |
||
319 | private function getMoveStrategy(NodeInfo $sourceNode, NodeInfo $targetNode, $placement) |
||
334 | |||
335 | public function deleteBranch($nodeId) |
||
370 | |||
371 | 6 | public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false) |
|
376 | |||
377 | public function getNode($nodeId) |
||
382 | |||
383 | 6 | public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null) |
|
388 | |||
389 | 6 | public function getChildren($nodeId) |
|
393 | 9 | ||
394 | public function getRootNode($scope = null) |
||
399 | 3 | ||
400 | public function getRoots() |
||
405 | |||
406 | 21 | public function isValid($rootNodeId) |
|
411 | |||
412 | 3 | public function rebuild($rootNodeId) |
|
417 | } |
||
418 |