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 ) { |
|
| 94 | |||
| 95 | 7 | $this->setQueryResult( $queryResult ); |
|
| 96 | |||
| 97 | 7 | if ( $this->params['parent'] === '' ) { |
|
| 98 | 2 | $this->addError( 'srf-tree-noparentprop' ); |
|
| 99 | 2 | return ''; |
|
| 100 | } |
||
| 101 | |||
| 102 | 5 | $rootHash = $this->getRootHash(); |
|
| 103 | |||
| 104 | 5 | if ( $rootHash === false ) { |
|
| 105 | $this->addError( 'srf-tree-rootinvalid', $this->params['root'] ); |
||
| 106 | return ''; |
||
| 107 | } |
||
| 108 | |||
| 109 | $this->hasTemplates = |
||
| 110 | 5 | $this->params['introtemplate'] !== '' || |
|
| 111 | 5 | $this->params['outrotemplate'] !== '' || |
|
| 112 | 5 | $this->params['template'] !== ''; |
|
| 113 | |||
| 114 | 5 | if ( $this->hasTemplates ) { |
|
| 115 | 3 | $this->initalizeStandardTemplateParameters(); |
|
| 116 | } |
||
| 117 | |||
| 118 | 5 | $tree = $this->buildTreeFromQueryResult( $rootHash ); |
|
| 119 | 5 | $lines = $this->buildLinesFromTree( $tree ); |
|
| 120 | |||
| 121 | // Display default if the result is empty |
||
| 122 | 5 | if ( count( $lines ) === 0 ) { |
|
| 123 | return $this->params['default']; |
||
| 124 | } |
||
| 125 | |||
| 126 | // FIXME: Linking to further events ($this->linkFurtherResults()) |
||
| 127 | // does not make sense for tree format. But maybe display a warning? |
||
| 128 | |||
| 129 | 5 | $resultText = join( |
|
| 130 | 5 | "\n", |
|
| 131 | 5 | array_merge( |
|
| 132 | 5 | [ $this->getTemplateCall( $this->params['introtemplate'] ) ], |
|
| 133 | $lines, |
||
| 134 | 5 | [ $this->getTemplateCall( $this->params['outrotemplate'] ) ] |
|
| 135 | ) |
||
| 136 | ); |
||
| 137 | |||
| 138 | 5 | $this->setQueryResult( null ); |
|
| 139 | |||
| 140 | 5 | if ( trim( $this->params[ 'class' ] ) === '' ) { |
|
| 141 | 5 | return $resultText; |
|
| 142 | } |
||
| 143 | |||
| 144 | 1 | return Html::rawElement( 'div', [ 'class' => $this->params[ 'class' ] ], $resultText ); |
|
| 145 | } |
||
| 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() { |
|
| 287 | |||
| 288 | 3 | $query = $this->getQueryResult()->getQuery(); |
|
| 289 | 3 | $userparam = trim( $this->params[ 'userparam' ] ); |
|
| 290 | |||
| 291 | $this->standardTemplateParameters = |
||
| 292 | 3 | ( $userparam !== '' ? ( '|userparam=' . $userparam ) : '' ) . |
|
| 293 | 3 | '|smw-resultquerycondition=' . $query->getQueryString() . |
|
| 294 | 3 | '|smw-resultquerylimit=' . $query->getLimit() . |
|
| 295 | 3 | '|smw-resultqueryoffset=' . $query->getOffset(); |
|
| 296 | |||
| 297 | 3 | } |
|
| 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 ) { |
|
| 307 | |||
| 308 | 5 | $isRootSpecified = $rootHash !== ''; |
|
| 309 | |||
| 310 | 5 | $root = new TreeNode(); |
|
| 311 | 5 | if ( $isRootSpecified ) { |
|
| 312 | 1 | $root->addChild( $nodes[$rootHash] ); |
|
| 313 | } |
||
| 314 | |||
| 315 | 5 | $store = $this->getQueryResult()->getStore(); |
|
| 316 | 5 | $parentPointerProperty = DIProperty::newFromUserLabel( $this->params['parent'] ); |
|
| 317 | |||
| 318 | 5 | foreach ( $nodes as $hash => $node ) { |
|
| 319 | |||
| 320 | 5 | $parents = $store->getPropertyValues( |
|
| 321 | 5 | $node->getResultSubject(), |
|
| 322 | $parentPointerProperty |
||
| 323 | ); |
||
| 324 | |||
| 325 | 5 | if ( empty( $parents ) && !$isRootSpecified ) { |
|
| 326 | |||
| 327 | 5 | $root->addChild( $node ); |
|
| 328 | |||
| 329 | } else { |
||
| 330 | |||
| 331 | 4 | foreach ( $parents as $parent ) { |
|
| 332 | |||
| 333 | 4 | $parentHash = $parent->getSerialization(); |
|
| 334 | |||
| 335 | try { |
||
| 336 | 4 | if ( array_key_exists( $parentHash, $nodes ) ) { |
|
| 337 | 4 | $nodes[$parentHash]->addChild( $node ); |
|
| 338 | } elseif ( !$isRootSpecified ) { |
||
| 339 | 4 | $root->addChild( $node ); |
|
| 340 | } |
||
| 341 | } |
||
| 342 | 1 | catch ( Exception $e ) { |
|
| 343 | 1 | $this->addError( $e->getMessage(), $node->getResultSubject()->getTitle()->getPrefixedText() ); |
|
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | 5 | return $root; |
|
| 349 | } |
||
| 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.