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 |
||
7 | class Tree |
||
8 | { |
||
9 | /** |
||
10 | * @var array $dependenciesInfos : List of dependency with there infos; |
||
11 | */ |
||
12 | protected $dependenciesInfos = []; |
||
13 | |||
14 | /** |
||
15 | * @var array $listDepends : List the depends of each dependancy |
||
16 | */ |
||
17 | protected $listDepends = []; |
||
18 | |||
19 | /** |
||
20 | * @var array $dependenciesPositions : List of position in the tree for |
||
21 | * each dependancy |
||
22 | */ |
||
23 | protected $dependenciesPositions = []; |
||
24 | |||
25 | /** |
||
26 | * @var array $tree : The generate tree |
||
27 | */ |
||
28 | protected $tree = []; |
||
29 | |||
30 | /** |
||
31 | * @var \stdClass $genOrderSecurityLoop : Informations about dependency |
||
32 | * during the generateOrder. |
||
33 | * It's a security against infinite loop. |
||
34 | */ |
||
35 | protected $genOrderSecurityLoop; |
||
36 | |||
37 | /** |
||
38 | * Add a dependency to system |
||
39 | * |
||
40 | * @param string $name : Dependency's name |
||
41 | * @param int $order : Order to load dependency |
||
42 | * @param array $dependencies : All dependencies for this dependency |
||
43 | * |
||
44 | * @throws Exception : If dependency already declared |
||
45 | * |
||
46 | * @return Tree : Current instance |
||
47 | */ |
||
48 | public function addDependency($name, $order = 0, $dependencies = []) |
||
83 | |||
84 | /** |
||
85 | * Generate the tree |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | public function generateTree() |
||
119 | |||
120 | /** |
||
121 | * Check if all depends is correctly spoted in the tree |
||
122 | * |
||
123 | * @param string $dependencyName : The name of the dependency for which |
||
124 | * check depends |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | protected function checkDepend($dependencyName) |
||
150 | |||
151 | /** |
||
152 | * Move a depend to a new position in the tree |
||
153 | * |
||
154 | * @param string $dependencyName : The dependency name |
||
155 | * @param int $newOrder : The new position in the tree |
||
156 | * |
||
157 | * @return void |
||
158 | */ |
||
159 | protected function moveDepend($dependencyName, $newOrder) |
||
181 | |||
182 | /** |
||
183 | * Generate the order position from the depends of each dependencies |
||
184 | * |
||
185 | * First we generate a reversed tree from depends |
||
186 | * Reverse the tree for have a tree in the correct depends order |
||
187 | * And update the order value of each dependency with the tree of depends |
||
188 | * |
||
189 | * @return void |
||
190 | */ |
||
191 | public function generateOrderFromDependencies() |
||
249 | |||
250 | /** |
||
251 | * Define the order of all the dependencies of dependency |
||
252 | * |
||
253 | * @param string $dependencyName : The name of dependency for which we |
||
254 | * read the dependencies |
||
255 | * @param int $currentOrder : The current order of the dependency |
||
256 | * |
||
257 | * @return void |
||
258 | */ |
||
259 | protected function generateOrderForADependency($dependencyName, $currentOrder) |
||
289 | |||
290 | /** |
||
291 | * Add a dependency into list used by security infinite depend loop. |
||
292 | * |
||
293 | * @param string $dependencyName Dependency name |
||
294 | * @param int $order The current order of the dependency |
||
295 | * |
||
296 | * @return void |
||
297 | */ |
||
298 | protected function genOrderSecurityLoopAddDepend($dependencyName, $order) |
||
308 | |||
309 | /** |
||
310 | * Check if we reinitialize the list used against dependency infinite loop |
||
311 | * |
||
312 | * @param int $order The current order of the dependency |
||
313 | * |
||
314 | * @return void |
||
315 | */ |
||
316 | protected function checkReinitGenOrderSecurityLoop($order) |
||
322 | |||
323 | /** |
||
324 | * (re)Initialize the list used against dependency infinite loop |
||
325 | * |
||
326 | * @param int $order The current order of the dependency |
||
327 | * |
||
328 | * @return void |
||
329 | */ |
||
330 | protected function initGenOrderSecurityLoop($order) |
||
337 | |||
338 | /** |
||
339 | * Check if we allow to moved a dependency in the tree to protect |
||
340 | * against infine loop. |
||
341 | * It's for the case where a dependency is moved to be loaded before an |
||
342 | * another, but this another dependency depend on the first package |
||
343 | * who asked to be moved. |
||
344 | * So the system try to moved packages at the infine. We protect this. |
||
345 | * |
||
346 | * @see Issue #4 on the github repo. |
||
347 | * |
||
348 | * @param string $checkDependName : The name of dependency |
||
349 | * |
||
350 | * @return void |
||
351 | * |
||
352 | * @throws Exception The infinite loop security. |
||
353 | */ |
||
354 | protected function genOrderCheckInfiniteLoop($checkDependName) |
||
395 | } |
||
396 |
It seems like you are relying on a variable being defined by an iteration: