Complex classes like TreeResultPrinter 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 TreeResultPrinter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class TreeResultPrinter extends ListResultPrinter { |
||
28 | |||
29 | private $standardTemplateParameters; |
||
30 | |||
31 | /** |
||
32 | * @var SMWQueryResult | null |
||
33 | */ |
||
34 | private $queryResult = null; |
||
35 | |||
36 | /** |
||
37 | * (non-PHPdoc) |
||
38 | * @see SMWResultPrinter::getName() |
||
39 | */ |
||
40 | public function getName() { |
||
45 | |||
46 | /** |
||
47 | * @return SMWQueryResult |
||
48 | * @throws Exception |
||
49 | */ |
||
50 | 5 | public function getQueryResult() { |
|
58 | |||
59 | /** |
||
60 | * @param SMWQueryResult | null $queryResult |
||
61 | */ |
||
62 | 7 | public function setQueryResult( $queryResult ) { |
|
65 | |||
66 | /** |
||
67 | * @see ResultPrinter::postProcessParameters() |
||
68 | */ |
||
69 | 8 | protected function postProcessParameters() { |
|
98 | |||
99 | /** |
||
100 | * Return serialised results in specified format. |
||
101 | * |
||
102 | * @param SMWQueryResult $queryResult |
||
103 | * @param $outputmode |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | 7 | protected function getResultText( SMWQueryResult $queryResult, $outputmode ) { |
|
156 | |||
157 | /** |
||
158 | * @param string $templateName |
||
159 | * @param string[] $params |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | 5 | public function getTemplateCall( $templateName, $params = [] ) { |
|
171 | |||
172 | /** |
||
173 | * @see SMWResultPrinter::getParamDefinitions |
||
174 | * |
||
175 | * @since 1.8 |
||
176 | * |
||
177 | * @param $definitions array of IParamDefinition |
||
178 | * |
||
179 | * @return array of IParamDefinition|array |
||
180 | */ |
||
181 | 8 | public function getParamDefinitions( array $definitions ) { |
|
212 | |||
213 | /** |
||
214 | * @param string $rootHash |
||
215 | * |
||
216 | * @return TreeNode |
||
217 | */ |
||
218 | 5 | protected function buildTreeFromQueryResult( $rootHash ) { |
|
228 | |||
229 | /** |
||
230 | * @return string | false |
||
231 | */ |
||
232 | 5 | protected function getRootHash() { |
|
248 | |||
249 | /** |
||
250 | * @return TreeNode[] |
||
251 | */ |
||
252 | 5 | protected function getHashOfNodes() { |
|
268 | |||
269 | /** |
||
270 | * Returns a linker object for making hyperlinks |
||
271 | * |
||
272 | * @return \Linker |
||
273 | */ |
||
274 | 1 | public function getLinker( $firstcol = false ) { |
|
277 | |||
278 | /** |
||
279 | * Depending on current linking settings, returns a linker object |
||
280 | * for making hyperlinks or NULL if no links should be created. |
||
281 | * |
||
282 | * @param int $column Column number |
||
283 | * |
||
284 | * @return \Linker|null |
||
285 | */ |
||
286 | 5 | public function getLinkerForColumn( $column ) { |
|
289 | |||
290 | 3 | private function initalizeStandardTemplateParameters() { |
|
303 | |||
304 | /** |
||
305 | * @param string $rootHash |
||
306 | * @param TreeNode[] $nodes |
||
307 | * |
||
308 | * @return TreeNode |
||
309 | */ |
||
310 | 5 | protected function buildTreeFromNodeList( $rootHash, $nodes ) { |
|
354 | |||
355 | /** |
||
356 | * @param TreeNode $tree |
||
357 | * |
||
358 | * @return mixed |
||
359 | */ |
||
360 | 5 | protected function buildLinesFromTree( $tree ) { |
|
373 | |||
374 | /** |
||
375 | * @param string $msgkey |
||
376 | * @param string | string[] $params |
||
377 | */ |
||
378 | 3 | protected function addError( $msgkey, $params = [] ) { |
|
386 | |||
387 | } |
||
388 | |||
389 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.