Complex classes like SMQueryHandler 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 SMQueryHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class SMQueryHandler { |
||
| 12 | |||
| 13 | private $queryResult; |
||
| 14 | private $outputmode; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | private $geoShapes = [ |
||
| 20 | 'lines' => [], |
||
| 21 | 'locations' => [], |
||
| 22 | 'polygons' => [] |
||
| 23 | ]; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The template to use for the text, or false if there is none. |
||
| 27 | * |
||
| 28 | * @var string|boolean false |
||
| 29 | */ |
||
| 30 | private $template = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The global icon. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | public $icon = ''; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The global text. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | public $text = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The global title. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | public $title = ''; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Make a separate link to the title or not? |
||
| 55 | * |
||
| 56 | * @var boolean |
||
| 57 | */ |
||
| 58 | public $titleLinkSeparate; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Should link targets be made absolute (instead of relative)? |
||
| 62 | * |
||
| 63 | * @var boolean |
||
| 64 | */ |
||
| 65 | private $linkAbsolute; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The text used for the link to the page (if it's created). $1 will be replaced by the page name. |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $pageLinkText; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * A separator to use between the subject and properties in the text field. |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | private $subjectSeparator = '<hr />'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Make the subject in the text bold or not? |
||
| 83 | * |
||
| 84 | * @var boolean |
||
| 85 | */ |
||
| 86 | private $boldSubject = true; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Show the subject in the text or not? |
||
| 90 | * |
||
| 91 | * @var boolean |
||
| 92 | */ |
||
| 93 | private $showSubject = true; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Hide the namespace or not. |
||
| 97 | * |
||
| 98 | * @var boolean |
||
| 99 | */ |
||
| 100 | private $hideNamespace = false; |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Defines which article names in the result are hyperlinked, all normally is the default |
||
| 105 | * none, subject, all |
||
| 106 | */ |
||
| 107 | private $linkStyle = 'all'; |
||
| 108 | |||
| 109 | /* |
||
| 110 | * Show headers (with links), show headers (just text) or hide them. show is default |
||
| 111 | * show, plain, hide |
||
| 112 | */ |
||
| 113 | private $headerStyle = 'show'; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Marker icon to show when marker equals active page |
||
| 117 | * |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | private $activeIcon; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var string |
||
| 124 | */ |
||
| 125 | private $userParam = ''; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param SMWQueryResult $queryResult |
||
| 129 | * @param integer $outputmode |
||
| 130 | * @param boolean $linkAbsolute |
||
| 131 | * @param string $pageLinkText |
||
| 132 | * @param boolean $titleLinkSeparate |
||
| 133 | * @param string $activeIcon |
||
| 134 | */ |
||
| 135 | public function __construct( SMWQueryResult $queryResult, $outputmode, $linkAbsolute = false, $pageLinkText = '$1', $titleLinkSeparate = false, $hideNamespace = false, $activeIcon = null ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Sets the template. |
||
| 148 | * |
||
| 149 | * @param string $template |
||
| 150 | */ |
||
| 151 | public function setTemplate( $template ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $userParam |
||
| 157 | */ |
||
| 158 | public function setUserParam( $userParam ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Sets the global icon. |
||
| 164 | * |
||
| 165 | * @param string $icon |
||
| 166 | */ |
||
| 167 | public function setIcon( $icon ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Sets the global title. |
||
| 173 | * |
||
| 174 | * @param string $title |
||
| 175 | */ |
||
| 176 | public function setTitle( $title ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Sets the global text. |
||
| 182 | * |
||
| 183 | * @param string $text |
||
| 184 | */ |
||
| 185 | public function setText( $text ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Sets the subject separator. |
||
| 191 | * |
||
| 192 | * @param string $subjectSeparator |
||
| 193 | */ |
||
| 194 | public function setSubjectSeparator( $subjectSeparator ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Sets if the subject should be made bold in the text. |
||
| 200 | * |
||
| 201 | * @param string $boldSubject |
||
| 202 | */ |
||
| 203 | public function setBoldSubject( $boldSubject ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Sets if the subject should shown in the text. |
||
| 209 | * |
||
| 210 | * @param string $showSubject |
||
| 211 | */ |
||
| 212 | public function setShowSubject( $showSubject ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Sets the text for the link to the page when separate from the title. |
||
| 218 | * |
||
| 219 | * @param string $text |
||
| 220 | */ |
||
| 221 | public function setPageLinkText( $text ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * |
||
| 227 | * @param boolean $link |
||
| 228 | */ |
||
| 229 | public function setLinkStyle ( $link ) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * |
||
| 235 | * @param boolean $headers |
||
| 236 | */ |
||
| 237 | public function setHeaderStyle ( $headers ) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | public function getShapes() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @since 2.0 |
||
| 251 | */ |
||
| 252 | private function findShapes() { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns the locations found in the provided result row. |
||
| 260 | * |
||
| 261 | * @param SMWResultArray[] $row |
||
| 262 | */ |
||
| 263 | private function handleResultRow( array $row ) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Handles a SMWWikiPageValue subject value. |
||
| 328 | * Gets the plain text title and creates the HTML text with headers and the like. |
||
| 329 | * |
||
| 330 | * @param SMWWikiPageValue $object |
||
| 331 | * |
||
| 332 | * @return array with title and text |
||
| 333 | */ |
||
| 334 | private function handleResultSubject( SMWWikiPageValue $object ) { |
||
| 376 | |||
| 377 | private function showArticleLink() { |
||
| 380 | |||
| 381 | private function hasTemplate() { |
||
| 382 | return is_string( $this->template ); |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Handles a single property (SMWPrintRequest) to be displayed for a record (SMWDataValue). |
||
| 387 | * |
||
| 388 | * @param SMWDataValue $object |
||
| 389 | * @param SMWPrintRequest $printRequest |
||
| 390 | * |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | private function handleResultProperty( SMWDataValue $object, SMWPrintRequest $printRequest ) { |
||
| 394 | if ( $this->hasTemplate() ) { |
||
| 395 | if ( $object instanceof SMWWikiPageValue ) { |
||
| 396 | return $object->getTitle()->getPrefixedText(); |
||
| 397 | } |
||
| 398 | |||
| 399 | return $object->getLongText( SMW_OUTPUT_WIKI, null ); |
||
| 400 | } |
||
| 401 | |||
| 402 | if ( $this->linkAbsolute ) { |
||
| 403 | $titleText = $printRequest->getText( null ); |
||
| 404 | $t = Title::newFromText($titleText , SMW_NS_PROPERTY ); |
||
| 405 | |||
| 406 | if ($this->isHeadersShow() && $t instanceof Title && $t->exists() ) { |
||
| 407 | $propertyName = $propertyName = Html::element( |
||
| 408 | 'a', |
||
| 409 | [ 'href' => $t->getFullUrl() ], |
||
| 410 | $printRequest->getHTMLText( null ) |
||
| 411 | ); |
||
| 412 | } |
||
| 413 | else { |
||
| 414 | $propertyName = $titleText; |
||
| 415 | } |
||
| 416 | } |
||
| 417 | else { |
||
| 418 | if($this->isHeadersShow()){ |
||
| 419 | $propertyName = $printRequest->getHTMLText( smwfGetLinker() ); |
||
| 420 | }else if($this->isHeadersPlain()){ |
||
| 421 | $propertyName = $printRequest->getText(null); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | if ( $this->linkAbsolute ) { |
||
| 426 | $hasPage = $object->getTypeID() == '_wpg'; |
||
| 427 | |||
| 428 | if ( $hasPage ) { |
||
| 429 | $t = Title::newFromText( $object->getLongText( $this->outputmode, null ), NS_MAIN ); |
||
| 430 | $hasPage = $t !== null && $t->exists(); |
||
| 431 | } |
||
| 432 | |||
| 433 | if ( $hasPage ) { |
||
| 434 | $propertyValue = Html::element( |
||
| 435 | 'a', |
||
| 436 | [ 'href' => $t->getFullUrl() ], |
||
| 437 | $object->getLongText( $this->outputmode, null ) |
||
| 438 | ); |
||
| 439 | } |
||
| 440 | else { |
||
| 441 | $propertyValue = $object->getLongText( $this->outputmode, null ); |
||
| 442 | } |
||
| 443 | } |
||
| 444 | else { |
||
| 445 | $propertyValue = $object->getLongText( $this->outputmode, smwfGetLinker() ); |
||
| 446 | } |
||
| 447 | |||
| 448 | return $propertyName . ( $propertyName === '' ? '' : ': ' ) . $propertyValue; |
||
| 449 | } |
||
| 450 | |||
| 451 | |||
| 452 | private function isHeadersShow() { |
||
| 455 | |||
| 456 | private function isHeadersHide() { |
||
| 459 | |||
| 460 | private function isHeadersPlain() { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Builds a set of locations with the provided title, text and icon. |
||
| 466 | * |
||
| 467 | * @param Location[] $locations |
||
| 468 | * @param string $text |
||
| 469 | * @param string $icon |
||
| 470 | * @param array $properties |
||
| 471 | * @param Title|null $title |
||
| 472 | * |
||
| 473 | * @return Location[] |
||
| 474 | */ |
||
| 475 | private function buildLocationsList( array $locations, $text, $icon, array $properties, Title $title = null ) { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @return \Parser |
||
| 510 | */ |
||
| 511 | private function getParser() { |
||
| 514 | |||
| 515 | private function getTitleOutput( Title $title = null ) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get the icon for a row. |
||
| 525 | * |
||
| 526 | * @param array $row |
||
| 527 | * |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | private function getLocationIcon( array $row ) { |
||
| 566 | |||
| 567 | private function shouldGetActiveIconUrlFor( Title $title ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param boolean $hideNamespace |
||
| 576 | */ |
||
| 577 | public function setHideNamespace( $hideNamespace ) { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @return boolean |
||
| 583 | */ |
||
| 584 | public function getHideNamespace() { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @param string $activeIcon |
||
| 590 | */ |
||
| 591 | public function setActiveIcon( $activeIcon ) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @return string |
||
| 597 | */ |
||
| 598 | public function getActiveIcon() { |
||
| 601 | |||
| 602 | } |
||
| 603 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.