Complex classes like QueryHandler 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 QueryHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class QueryHandler { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The global icon. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | public $icon = ''; |
||
| 30 | /** |
||
| 31 | * The global text. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | public $text = ''; |
||
| 36 | /** |
||
| 37 | * The global title. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public $title = ''; |
||
| 42 | /** |
||
| 43 | * Make a separate link to the title or not? |
||
| 44 | * |
||
| 45 | * @var boolean |
||
| 46 | */ |
||
| 47 | public $titleLinkSeparate = false; |
||
| 48 | private $queryResult; |
||
| 49 | private $outputMode; |
||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $geoShapes = [ |
||
| 54 | 'lines' => [], |
||
| 55 | 'locations' => [], |
||
| 56 | 'polygons' => [] |
||
| 57 | ]; |
||
| 58 | /** |
||
| 59 | * The template to use for the text, or false if there is none. |
||
| 60 | * |
||
| 61 | * @var string|boolean false |
||
| 62 | */ |
||
| 63 | private $template = false; |
||
| 64 | /** |
||
| 65 | * Should link targets be made absolute (instead of relative)? |
||
| 66 | * |
||
| 67 | * @var boolean |
||
| 68 | */ |
||
| 69 | private $linkAbsolute; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The text used for the link to the page (if it's created). $1 will be replaced by the page name. |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $pageLinkText = '$1'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * A separator to use between the subject and properties in the text field. |
||
| 80 | * |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | private $subjectSeparator = '<hr />'; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Make the subject in the text bold or not? |
||
| 87 | * |
||
| 88 | * @var boolean |
||
| 89 | */ |
||
| 90 | private $boldSubject = true; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Show the subject in the text or not? |
||
| 94 | * |
||
| 95 | * @var boolean |
||
| 96 | */ |
||
| 97 | private $showSubject = true; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Hide the namespace or not. |
||
| 101 | * |
||
| 102 | * @var boolean |
||
| 103 | */ |
||
| 104 | private $hideNamespace = false; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Defines which article names in the result are hyperlinked, all normally is the default |
||
| 108 | * none, subject, all |
||
| 109 | */ |
||
| 110 | private $linkStyle = 'all'; |
||
| 111 | |||
| 112 | /* |
||
| 113 | * Show headers (with links), show headers (just text) or hide them. show is default |
||
| 114 | * show, plain, hide |
||
| 115 | */ |
||
| 116 | private $headerStyle = 'show'; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Marker icon to show when marker equals active page |
||
| 120 | * |
||
| 121 | * @var string|null |
||
| 122 | */ |
||
| 123 | private $activeIcon = null; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var string |
||
| 127 | */ |
||
| 128 | private $userParam = ''; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param SMWQueryResult $queryResult |
||
| 132 | * @param integer $outputMode |
||
| 133 | * @param boolean $linkAbsolute |
||
| 134 | */ |
||
| 135 | public function __construct( SMWQueryResult $queryResult, $outputMode, $linkAbsolute = false ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Sets the template. |
||
| 143 | * |
||
| 144 | * @param string $template |
||
| 145 | */ |
||
| 146 | public function setTemplate( $template ) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $userParam |
||
| 152 | */ |
||
| 153 | public function setUserParam( $userParam ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Sets the global icon. |
||
| 159 | * |
||
| 160 | * @param string $icon |
||
| 161 | */ |
||
| 162 | public function setIcon( $icon ) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Sets the global title. |
||
| 168 | * |
||
| 169 | * @param string $title |
||
| 170 | */ |
||
| 171 | public function setTitle( $title ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Sets the global text. |
||
| 177 | * |
||
| 178 | * @param string $text |
||
| 179 | */ |
||
| 180 | public function setText( $text ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Sets the subject separator. |
||
| 186 | * |
||
| 187 | * @param string $subjectSeparator |
||
| 188 | */ |
||
| 189 | public function setSubjectSeparator( $subjectSeparator ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Sets if the subject should be made bold in the text. |
||
| 195 | * |
||
| 196 | * @param string $boldSubject |
||
| 197 | */ |
||
| 198 | public function setBoldSubject( $boldSubject ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Sets if the subject should shown in the text. |
||
| 204 | * |
||
| 205 | * @param string $showSubject |
||
| 206 | */ |
||
| 207 | public function setShowSubject( $showSubject ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Sets the text for the link to the page when separate from the title. |
||
| 213 | * |
||
| 214 | * @param string $text |
||
| 215 | */ |
||
| 216 | public function setPageLinkText( $text ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * |
||
| 222 | * @param boolean $link |
||
| 223 | */ |
||
| 224 | public function setLinkStyle( $link ) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * |
||
| 230 | * @param boolean $headers |
||
| 231 | */ |
||
| 232 | public function setHeaderStyle( $headers ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | public function getShapes() { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @since 2.0 |
||
| 246 | */ |
||
| 247 | private function findShapes() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Returns the locations found in the provided result row. |
||
| 255 | * |
||
| 256 | * @param SMWResultArray[] $row |
||
| 257 | */ |
||
| 258 | private function handleResultRow( array $row ) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Handles a SMWWikiPageValue subject value. |
||
| 335 | * Gets the plain text title and creates the HTML text with headers and the like. |
||
| 336 | * |
||
| 337 | * @param SMWWikiPageValue $object |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | private function getResultSubjectText( SMWWikiPageValue $object ): string { |
||
| 384 | |||
| 385 | private function showArticleLink() { |
||
| 388 | |||
| 389 | private function isHeadersHide() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Handles a single property (SMWPrintRequest) to be displayed for a record (SMWDataValue). |
||
| 395 | * |
||
| 396 | * @param SMWDataValue $object |
||
| 397 | * @param SMWPrintRequest $printRequest |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | private function handleResultProperty( SMWDataValue $object, SMWPrintRequest $printRequest ) { |
||
| 456 | |||
| 457 | private function hasTemplate() { |
||
| 460 | |||
| 461 | private function isHeadersShow() { |
||
| 464 | |||
| 465 | private function isHeadersPlain() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get the icon for a row. |
||
| 471 | * |
||
| 472 | * @param array $row |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | private function getLocationIcon( array $row ) { |
||
| 514 | |||
| 515 | private function shouldGetActiveIconUrlFor( Title $title ) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Builds a set of locations with the provided title, text and icon. |
||
| 524 | * |
||
| 525 | * @param Location[] $locations |
||
| 526 | * @param string $text |
||
| 527 | * @param string $icon |
||
| 528 | * @param array $properties |
||
| 529 | * @param Title|null $title |
||
| 530 | * |
||
| 531 | * @return Location[] |
||
| 532 | */ |
||
| 533 | private function buildLocationsList( array $locations, $text, $icon, array $properties, Title $title = null ) { |
||
| 565 | |||
| 566 | private function getTitleOutput( Title $title = null ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @return \Parser |
||
| 576 | */ |
||
| 577 | private function getParser() { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @return boolean |
||
| 583 | */ |
||
| 584 | public function getHideNamespace() { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @param boolean $hideNamespace |
||
| 590 | */ |
||
| 591 | public function setHideNamespace( $hideNamespace ) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @return string |
||
| 597 | */ |
||
| 598 | public function getActiveIcon() { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @param string $activeIcon |
||
| 604 | */ |
||
| 605 | public function setActiveIcon( $activeIcon ) { |
||
| 608 | |||
| 609 | } |
||
| 610 |
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.