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 |
||
| 13 | class SMQueryHandler { |
||
| 14 | |||
| 15 | protected $queryResult; |
||
| 16 | protected $outputmode; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @since 2.0 |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $geoShapes = [ |
||
| 24 | 'lines' => [], |
||
| 25 | 'locations' => [], |
||
| 26 | 'polygons' => [] |
||
| 27 | ]; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The template to use for the text, or false if there is none. |
||
| 31 | * |
||
| 32 | * @since 0.7.3 |
||
| 33 | * |
||
| 34 | * @var string|boolean false |
||
| 35 | */ |
||
| 36 | protected $template = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The global icon. |
||
| 40 | * |
||
| 41 | * @since 0.7.3 |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | public $icon = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The global text. |
||
| 49 | * |
||
| 50 | * @since 1.0 |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | public $text = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The global title. |
||
| 58 | * |
||
| 59 | * @since 1.0 |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | public $title = ''; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Make a separate link to the title or not? |
||
| 67 | * |
||
| 68 | * @since 0.7.3 |
||
| 69 | * |
||
| 70 | * @var boolean |
||
| 71 | */ |
||
| 72 | public $titleLinkSeparate; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Should link targets be made absolute (instead of relative)? |
||
| 76 | * |
||
| 77 | * @since 1.0 |
||
| 78 | * |
||
| 79 | * @var boolean |
||
| 80 | */ |
||
| 81 | protected $linkAbsolute; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The text used for the link to the page (if it's created). $1 will be replaced by the page name. |
||
| 85 | * |
||
| 86 | * @since 1.0 |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $pageLinkText; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * A separator to use between the subject and properties in the text field. |
||
| 94 | * |
||
| 95 | * @since 1.0 |
||
| 96 | * |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | protected $subjectSeparator = '<hr />'; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Make the subject in the text bold or not? |
||
| 103 | * |
||
| 104 | * @since 1.0 |
||
| 105 | * |
||
| 106 | * @var boolean |
||
| 107 | */ |
||
| 108 | protected $boldSubject = true; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Show the subject in the text or not? |
||
| 112 | * |
||
| 113 | * @since 1.0 |
||
| 114 | * |
||
| 115 | * @var boolean |
||
| 116 | */ |
||
| 117 | protected $showSubject = true; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Hide the namespace or not. |
||
| 121 | * |
||
| 122 | * @var boolean |
||
| 123 | */ |
||
| 124 | protected $hideNamespace = false; |
||
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Defines which article names in the result are hyperlinked, all normally is the default |
||
| 129 | * none, subject, all |
||
| 130 | */ |
||
| 131 | protected $linkStyle = 'all'; |
||
| 132 | |||
| 133 | /* |
||
| 134 | * Show headers (with links), show headers (just text) or hide them. show is default |
||
| 135 | * show, plain, hide |
||
| 136 | */ |
||
| 137 | protected $headerStyle = 'show'; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Marker icon to show when marker equals active page |
||
| 141 | * |
||
| 142 | * @var string |
||
| 143 | */ |
||
| 144 | protected $activeIcon; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var string |
||
| 148 | */ |
||
| 149 | protected $userParam = ''; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Constructor. |
||
| 153 | * |
||
| 154 | * @since 0.7.3 |
||
| 155 | * |
||
| 156 | * @param SMWQueryResult $queryResult |
||
| 157 | * @param integer $outputmode |
||
| 158 | * @param boolean $linkAbsolute |
||
| 159 | * @param string $pageLinkText |
||
| 160 | * @param boolean $titleLinkSeparate |
||
| 161 | * @param string $activeIcon |
||
| 162 | */ |
||
| 163 | public function __construct( SMWQueryResult $queryResult, $outputmode, $linkAbsolute = false, $pageLinkText = '$1', $titleLinkSeparate = false, $hideNamespace = false, $activeIcon = null ) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Sets the template. |
||
| 176 | * |
||
| 177 | * @since 1.0 |
||
| 178 | * |
||
| 179 | * @param string $template |
||
| 180 | */ |
||
| 181 | public function setTemplate( $template ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @since 3.2 |
||
| 187 | * |
||
| 188 | * @param string $userParam |
||
| 189 | */ |
||
| 190 | public function setUserParam( $userParam ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Sets the global icon. |
||
| 196 | * |
||
| 197 | * @since 1.0 |
||
| 198 | * |
||
| 199 | * @param string $icon |
||
| 200 | */ |
||
| 201 | public function setIcon( $icon ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Sets the global title. |
||
| 207 | * |
||
| 208 | * @since 1.0 |
||
| 209 | * |
||
| 210 | * @param string $title |
||
| 211 | */ |
||
| 212 | public function setTitle( $title ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Sets the global text. |
||
| 218 | * |
||
| 219 | * @since 1.0 |
||
| 220 | * |
||
| 221 | * @param string $text |
||
| 222 | */ |
||
| 223 | public function setText( $text ) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Sets the subject separator. |
||
| 229 | * |
||
| 230 | * @since 1.0 |
||
| 231 | * |
||
| 232 | * @param string $subjectSeparator |
||
| 233 | */ |
||
| 234 | public function setSubjectSeparator( $subjectSeparator ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Sets if the subject should be made bold in the text. |
||
| 240 | * |
||
| 241 | * @since 1.0 |
||
| 242 | * |
||
| 243 | * @param string $boldSubject |
||
| 244 | */ |
||
| 245 | public function setBoldSubject( $boldSubject ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Sets if the subject should shown in the text. |
||
| 251 | * |
||
| 252 | * @since 1.0 |
||
| 253 | * |
||
| 254 | * @param string $showSubject |
||
| 255 | */ |
||
| 256 | public function setShowSubject( $showSubject ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Sets the text for the link to the page when separate from the title. |
||
| 262 | * |
||
| 263 | * @since 1.0 |
||
| 264 | * |
||
| 265 | * @param string $text |
||
| 266 | */ |
||
| 267 | public function setPageLinkText( $text ) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * |
||
| 273 | * @since 2.0.1 |
||
| 274 | * |
||
| 275 | * @param boolean $link |
||
| 276 | */ |
||
| 277 | public function setLinkStyle ( $link ) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * |
||
| 283 | * @since 2.0.1 |
||
| 284 | * |
||
| 285 | * @param boolean $headers |
||
| 286 | */ |
||
| 287 | public function setHeaderStyle ( $headers ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @since 2.0 |
||
| 293 | * |
||
| 294 | * @return array |
||
| 295 | */ |
||
| 296 | public function getShapes() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @since 2.0 |
||
| 303 | */ |
||
| 304 | protected function findShapes() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns the locations found in the provided result row. |
||
| 312 | * |
||
| 313 | * @since 0.7.3 |
||
| 314 | * |
||
| 315 | * @param SMWResultArray[] $row |
||
| 316 | */ |
||
| 317 | protected function handleResultRow( array $row ) { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Handles a SMWWikiPageValue subject value. |
||
| 382 | * Gets the plain text title and creates the HTML text with headers and the like. |
||
| 383 | * |
||
| 384 | * @since 1.0 |
||
| 385 | * |
||
| 386 | * @param SMWWikiPageValue $object |
||
| 387 | * |
||
| 388 | * @return array with title and text |
||
| 389 | */ |
||
| 390 | protected function handleResultSubject( SMWWikiPageValue $object ) { |
||
| 432 | |||
| 433 | protected function showArticleLink(){ |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Handles a single property (SMWPrintRequest) to be displayed for a record (SMWDataValue). |
||
| 439 | * |
||
| 440 | * @since 1.0 |
||
| 441 | * |
||
| 442 | * @param SMWDataValue $object |
||
| 443 | * @param SMWPrintRequest $printRequest |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | protected function handleResultProperty( SMWDataValue $object, SMWPrintRequest $printRequest ) { |
||
| 508 | |||
| 509 | |||
| 510 | protected function isHeadersShow(){ |
||
| 513 | |||
| 514 | protected function isHeadersHide(){ |
||
| 517 | |||
| 518 | protected function isHeadersPlain(){ |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Builds a set of locations with the provided title, text and icon. |
||
| 524 | * |
||
| 525 | * @since 1.0 |
||
| 526 | * |
||
| 527 | * @param Location[] $locations |
||
| 528 | * @param string $text |
||
| 529 | * @param string $icon |
||
| 530 | * @param array $properties |
||
| 531 | * @param Title|null $title |
||
| 532 | * |
||
| 533 | * @return Location[] |
||
| 534 | */ |
||
| 535 | protected function buildLocationsList( array $locations, $text, $icon, array $properties, Title $title = null ) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Get the icon for a row. |
||
| 577 | * |
||
| 578 | * @since 0.7.3 |
||
| 579 | * |
||
| 580 | * @param array $row |
||
| 581 | * |
||
| 582 | * @return string |
||
| 583 | */ |
||
| 584 | protected function getLocationIcon( array $row ) { |
||
| 620 | |||
| 621 | private function shouldGetActiveIconUrlFor( Title $title ) { |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @param boolean $hideNamespace |
||
| 630 | */ |
||
| 631 | public function setHideNamespace( $hideNamespace ) { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @return boolean |
||
| 637 | */ |
||
| 638 | public function getHideNamespace() { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * @param string $activeIcon |
||
| 644 | */ |
||
| 645 | public function setActiveIcon( $activeIcon ){ |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @return string |
||
| 651 | */ |
||
| 652 | public function getActiveIcon( ){ |
||
| 655 | |||
| 656 | } |
||
| 657 |
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.