Complex classes like Tree 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 Tree, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Tree |
||
15 | { |
||
16 | /** |
||
17 | * Set the build mode simple and add a depth field for branches |
||
18 | * @const int |
||
19 | */ |
||
20 | const BUILD_MODE_DEPTH = 0; |
||
21 | |||
22 | /** |
||
23 | * Builds simple mode and add left->right fields |
||
24 | * @const int |
||
25 | */ |
||
26 | const BUILD_MODE_LEFT_RIGHT = 1; |
||
27 | |||
28 | /** |
||
29 | * Builds the full type off tree, containing all off the above |
||
30 | * @const int |
||
31 | */ |
||
32 | const BUILD_MODE_COMPLETE = 2; |
||
33 | |||
34 | /** |
||
35 | * Default parent id from where to start referencing |
||
36 | * @const int |
||
37 | */ |
||
38 | const ROOT_PARENT_ID = 0; |
||
39 | |||
40 | /** |
||
41 | * Wheather to log debug details |
||
42 | * @var boolean |
||
43 | */ |
||
44 | private $debug = false; |
||
45 | |||
46 | /** |
||
47 | * Collected debug data if enabled |
||
48 | * @var string |
||
49 | */ |
||
50 | private $debugData; |
||
51 | |||
52 | /** |
||
53 | * "Dirty" flag - weather to build the tree if it wasn't compiled allready |
||
54 | * @var boolean |
||
55 | */ |
||
56 | private $compiled = false; |
||
57 | |||
58 | /** |
||
59 | * Store all tree items |
||
60 | * @var array - array([ID] => TreeBuilder\Branch, [ID] => TreeBuilder\Branch) |
||
61 | */ |
||
62 | private $branches = array(); |
||
63 | |||
64 | /** |
||
65 | * Stores the compiled tree |
||
66 | * @var array |
||
67 | */ |
||
68 | private $compiledTree = array(); |
||
69 | |||
70 | /** |
||
71 | * A delagated adapter for the output |
||
72 | * @var Adapter |
||
73 | */ |
||
74 | private $delegatedAdapter; |
||
75 | |||
76 | /** |
||
77 | * Defines the build mode off the tree |
||
78 | * @var int |
||
79 | */ |
||
80 | private $buildMode; |
||
81 | |||
82 | /** |
||
83 | * Enables debug mode |
||
84 | */ |
||
85 | public function enableDebug() |
||
89 | |||
90 | /** |
||
91 | * Show debug state |
||
92 | * @return boolean |
||
93 | */ |
||
94 | public function isDebugEnabled() |
||
98 | |||
99 | /** |
||
100 | * Get debug data |
||
101 | * @return string |
||
102 | * @throws \Exception |
||
103 | */ |
||
104 | public function getDebugData() |
||
111 | |||
112 | /** |
||
113 | * Delegate an adapter for the wanted output |
||
114 | * @param Adapter $adapter |
||
115 | * @return \TreeBuilder\Tree |
||
116 | */ |
||
117 | public function registerAdapter(Adapter $adapter) |
||
122 | |||
123 | /** |
||
124 | * Set the build mode identified by local constants |
||
125 | * @param int $value |
||
126 | * @return \TreeBuilder\Tree |
||
127 | */ |
||
128 | public function setBuildMode($value) |
||
133 | |||
134 | /** |
||
135 | * Add a new TreeItem |
||
136 | * @param Branch $item |
||
137 | */ |
||
138 | public function addBranch(Branch $item) |
||
143 | |||
144 | /** |
||
145 | * Returns the builded tree |
||
146 | * @return array |
||
147 | */ |
||
148 | public function getTree() |
||
160 | |||
161 | /** |
||
162 | * Return a branch by it's id |
||
163 | * @param int $branchId |
||
164 | * @return \TreeBuilder\Base\Branch |
||
165 | * @throws \Exception |
||
166 | */ |
||
167 | public function getBranchById($branchId) |
||
180 | |||
181 | /** |
||
182 | * Return all leaf branches |
||
183 | * @return array |
||
184 | */ |
||
185 | public function getLeafs() |
||
195 | |||
196 | /** |
||
197 | * Build the tree |
||
198 | */ |
||
199 | public function buildTree() |
||
237 | |||
238 | /** |
||
239 | * Build the tree parent->child references |
||
240 | * @param int $branchId |
||
241 | * @param Branch $item |
||
242 | */ |
||
243 | private function buildBranchReference($branchId, Branch $item) |
||
259 | |||
260 | /** |
||
261 | * Set the depth filed for items |
||
262 | * @param array $items |
||
263 | * @param int $depth |
||
264 | */ |
||
265 | private function addDepth(array $items, $depth = 0) |
||
274 | |||
275 | /** |
||
276 | * Add left->right fields to tree |
||
277 | * @param array $items |
||
278 | * @param int $left |
||
279 | * @return int |
||
280 | */ |
||
281 | private function addLeftRight(array $items, $left = 1) |
||
297 | |||
298 | /** |
||
299 | * Mark leaf branches |
||
300 | * @param array $items |
||
301 | */ |
||
302 | private function markLeafs($items) |
||
312 | |||
313 | /** |
||
314 | * Add to debug data |
||
315 | * @param string $string |
||
316 | */ |
||
317 | private function logDebug($string) |
||
321 | } |
||
322 |