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() { |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Return serialised results in specified format. |
||
| 87 | * |
||
| 88 | * @param SMWQueryResult $queryResult |
||
| 89 | * @param $outputmode |
||
| 90 | * |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | 7 | protected function getResultText( SMWQueryResult $queryResult, $outputmode ) { |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $templateName |
||
| 149 | * @param string[] $params |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | 5 | public function getTemplateCall( $templateName, $params = [] ) { |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @see SMWResultPrinter::getParamDefinitions |
||
| 164 | * |
||
| 165 | * @since 1.8 |
||
| 166 | * |
||
| 167 | * @param $definitions array of IParamDefinition |
||
| 168 | * |
||
| 169 | * @return array of IParamDefinition|array |
||
| 170 | * @throws Exception |
||
| 171 | */ |
||
| 172 | 8 | public function getParamDefinitions( array $definitions ) { |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $rootHash |
||
| 211 | * |
||
| 212 | * @return TreeNode |
||
| 213 | */ |
||
| 214 | 5 | protected function buildTreeFromQueryResult( $rootHash ) { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @return string | false |
||
| 227 | */ |
||
| 228 | 5 | protected function getRootHash() { |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @return TreeNode[] |
||
| 247 | */ |
||
| 248 | 5 | protected function getHashOfNodes() { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Returns a linker object for making hyperlinks |
||
| 267 | * |
||
| 268 | * @return \Linker |
||
| 269 | */ |
||
| 270 | 1 | public function getLinker( $firstcol = false ) { |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Depending on current linking settings, returns a linker object |
||
| 276 | * for making hyperlinks or NULL if no links should be created. |
||
| 277 | * |
||
| 278 | * @param int $column Column number |
||
| 279 | * |
||
| 280 | * @return \Linker|null |
||
| 281 | */ |
||
| 282 | 5 | public function getLinkerForColumn( $column ) { |
|
| 285 | |||
| 286 | 3 | private function initalizeStandardTemplateParameters() { |
|
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $rootHash |
||
| 301 | * @param TreeNode[] $nodes |
||
| 302 | * |
||
| 303 | * @return TreeNode |
||
| 304 | * @throws \Exception |
||
| 305 | */ |
||
| 306 | 5 | protected function buildTreeFromNodeList( $rootHash, $nodes ) { |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @param TreeNode $tree |
||
| 353 | * |
||
| 354 | * @return mixed |
||
| 355 | */ |
||
| 356 | 5 | protected function buildLinesFromTree( $tree ) { |
|
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $msgkey |
||
| 372 | * @param string | string[] $params |
||
| 373 | */ |
||
| 374 | 3 | protected function addError( $msgkey, $params = [] ) { |
|
| 382 | |||
| 383 | } |
||
| 384 | |||
| 385 |
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 theSoncalls the wrong method in the parent class.