Complex classes like TreeGraph 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 TreeGraph, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class TreeGraph |
||
50 | { |
||
51 | /** |
||
52 | * An array of nodes on the previous level. |
||
53 | * |
||
54 | * @var array |
||
55 | * |
||
56 | * @since 1.0 |
||
57 | */ |
||
58 | private $previousLevelNodes = array(); |
||
59 | |||
60 | /** |
||
61 | * An array of nodes in this graph. |
||
62 | * |
||
63 | * @var array |
||
64 | * |
||
65 | * @since 1.0 |
||
66 | */ |
||
67 | private $nodes = array(); |
||
68 | |||
69 | /** |
||
70 | * The root node of the graph. |
||
71 | * |
||
72 | * @var \Alpha\Util\Graph\GraphNode |
||
73 | * |
||
74 | * @since 1.0 |
||
75 | */ |
||
76 | private $root; |
||
77 | |||
78 | /** |
||
79 | * The amount of space between graph rows. |
||
80 | * |
||
81 | * @var int |
||
82 | * |
||
83 | * @since 1.0 |
||
84 | */ |
||
85 | private $rowSpace; |
||
86 | |||
87 | /** |
||
88 | * The amount of space between graph columns. |
||
89 | * |
||
90 | * @var int |
||
91 | * |
||
92 | * @since 1.0 |
||
93 | */ |
||
94 | private $colSpace; |
||
95 | |||
96 | /** |
||
97 | * The amount of space between graph branches. |
||
98 | * |
||
99 | * @var int |
||
100 | * |
||
101 | * @since 1.0 |
||
102 | */ |
||
103 | private $branchSpace; |
||
104 | |||
105 | /** |
||
106 | * Flag to track whether the chart is rendered or not. |
||
107 | * |
||
108 | * @var bool |
||
109 | * |
||
110 | * @since 1.0 |
||
111 | */ |
||
112 | private $isRendered = false; |
||
113 | |||
114 | /** |
||
115 | * The index of the current node in the graph we are inspecting. |
||
116 | * |
||
117 | * @var int |
||
118 | * |
||
119 | * @since 1.0 |
||
120 | */ |
||
121 | private $position = 0; |
||
122 | |||
123 | /** |
||
124 | * The height of the graph. |
||
125 | * |
||
126 | * @var int |
||
127 | * |
||
128 | * @since 1.0 |
||
129 | */ |
||
130 | private $height = 0; |
||
131 | |||
132 | /** |
||
133 | * The width of the graph. |
||
134 | * |
||
135 | * @var int |
||
136 | * |
||
137 | * @since 1.0 |
||
138 | */ |
||
139 | private $width = 0; |
||
140 | |||
141 | /** |
||
142 | * Trace logger. |
||
143 | * |
||
144 | * @var \Alpha\Util\Logging\Logger |
||
145 | * |
||
146 | * @since 1.0 |
||
147 | */ |
||
148 | private static $logger = null; |
||
149 | |||
150 | /** |
||
151 | * Constructor. |
||
152 | * |
||
153 | * @param int $rowSpace |
||
154 | * @param int $colSpace |
||
155 | * @param int $branchSpace |
||
156 | * |
||
157 | * @since 1.0 |
||
158 | */ |
||
159 | public function __construct($rowSpace = 40, $colSpace = 40, $branchSpace = 80) |
||
169 | |||
170 | /** |
||
171 | * Add a new node to the graph. |
||
172 | * |
||
173 | * @param int $id |
||
174 | * @param int $pid |
||
175 | * @param string $message |
||
176 | * @param int $w |
||
177 | * @param int $h |
||
178 | * @param array $nodeColour |
||
179 | * @param string $URL |
||
180 | * |
||
181 | * @since 1.0 |
||
182 | */ |
||
183 | public function add($id, $pid, $message = '', $w = 0, $h = 0, $nodeColour, $URL) |
||
199 | |||
200 | /** |
||
201 | * Get the specified node from the graph. |
||
202 | * |
||
203 | * @param int $id |
||
204 | * |
||
205 | * @return \Alpha\Util\Graph\GraphNode |
||
206 | * |
||
207 | * @since 2.0.1 |
||
208 | */ |
||
209 | public function get($id) |
||
217 | |||
218 | /** |
||
219 | * The first pass of the graph. |
||
220 | * |
||
221 | * @param \Alpha\Util\Graph\GraphNode $node |
||
222 | * @param int $level |
||
223 | * |
||
224 | * @since 1.0 |
||
225 | */ |
||
226 | private function firstPass($node, $level) |
||
261 | |||
262 | /** |
||
263 | * The second pass of the graph. |
||
264 | * |
||
265 | * @param \Alpha\Util\Graph\GraphNode $node |
||
266 | * @param int $level |
||
267 | * @param int $x |
||
268 | * @param int $y |
||
269 | * |
||
270 | * @since 1.0 |
||
271 | */ |
||
272 | private function secondPass($node, $level, $x = 0, $y = 0) |
||
295 | |||
296 | /** |
||
297 | * Handles the laying out of multi-branch trees. |
||
298 | * |
||
299 | * @param \Alpha\Util\Graph\GraphNode $node |
||
300 | * @param int $level |
||
301 | * |
||
302 | * @since 1.0 |
||
303 | */ |
||
304 | private function layout($node, $level) |
||
358 | |||
359 | /** |
||
360 | * Setup neighbour nodes. |
||
361 | * |
||
362 | * @param \Alpha\Util\Graph\GraphNode $node |
||
363 | * @param int $level |
||
364 | * |
||
365 | * @since 1.0 |
||
366 | */ |
||
367 | private function setNeighbours($node, $level) |
||
378 | |||
379 | /** |
||
380 | * Get left most node in the branch. |
||
381 | * |
||
382 | * @param \Alpha\Util\Graph\GraphNode $node |
||
383 | * @param int $level |
||
384 | * @param int $maxlevel |
||
385 | * |
||
386 | * @return \Alpha\Util\Graph\GraphNode |
||
387 | * |
||
388 | * @since 1.0 |
||
389 | */ |
||
390 | private function getLeftmost($node, $level, $maxlevel) |
||
414 | |||
415 | /** |
||
416 | * Render the chart in memory. |
||
417 | * |
||
418 | * @since 1.0 |
||
419 | */ |
||
420 | protected function render() |
||
431 | |||
432 | /** |
||
433 | * Get the width of the graph, will invoke render() if not already rendered. |
||
434 | * |
||
435 | * @since 1.0 |
||
436 | */ |
||
437 | public function getWidth() |
||
445 | |||
446 | /** |
||
447 | * Get the heith of the graph, will invoke render() if not already rendered. |
||
448 | * |
||
449 | * @since 1.0 |
||
450 | */ |
||
451 | public function getHeight() |
||
459 | |||
460 | /** |
||
461 | * Get the next GraphNode instance in the graph, will invoke render() if not already rendered. |
||
462 | * |
||
463 | * @return \Alpha\Util\Graph\GraphNode |
||
464 | * |
||
465 | * @since 1.0 |
||
466 | */ |
||
467 | public function next() |
||
481 | |||
482 | /** |
||
483 | * Check to see if another GraphNode instance in the graph is available. |
||
484 | * |
||
485 | * @return bool |
||
486 | * |
||
487 | * @since 1.0 |
||
488 | */ |
||
489 | public function hasNext() |
||
497 | } |
||
498 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: