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 | /** |
||
| 14 | * The global icon. |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | public $icon = ''; |
||
| 19 | /** |
||
| 20 | * The global text. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | public $text = ''; |
||
| 25 | /** |
||
| 26 | * The global title. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public $title = ''; |
||
| 31 | /** |
||
| 32 | * Make a separate link to the title or not? |
||
| 33 | * |
||
| 34 | * @var boolean |
||
| 35 | */ |
||
| 36 | public $titleLinkSeparate = false; |
||
| 37 | private $queryResult; |
||
| 38 | private $outputMode; |
||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $geoShapes = [ |
||
| 43 | 'lines' => [], |
||
| 44 | 'locations' => [], |
||
| 45 | 'polygons' => [] |
||
| 46 | ]; |
||
| 47 | /** |
||
| 48 | * The template to use for the text, or false if there is none. |
||
| 49 | * |
||
| 50 | * @var string|boolean false |
||
| 51 | */ |
||
| 52 | private $template = false; |
||
| 53 | /** |
||
| 54 | * Should link targets be made absolute (instead of relative)? |
||
| 55 | * |
||
| 56 | * @var boolean |
||
| 57 | */ |
||
| 58 | private $linkAbsolute; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The text used for the link to the page (if it's created). $1 will be replaced by the page name. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $pageLinkText = '$1'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * A separator to use between the subject and properties in the text field. |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $subjectSeparator = '<hr />'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Make the subject in the text bold or not? |
||
| 76 | * |
||
| 77 | * @var boolean |
||
| 78 | */ |
||
| 79 | private $boldSubject = true; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Show the subject in the text or not? |
||
| 83 | * |
||
| 84 | * @var boolean |
||
| 85 | */ |
||
| 86 | private $showSubject = true; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Hide the namespace or not. |
||
| 90 | * |
||
| 91 | * @var boolean |
||
| 92 | */ |
||
| 93 | private $hideNamespace = false; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Defines which article names in the result are hyperlinked, all normally is the default |
||
| 97 | * none, subject, all |
||
| 98 | */ |
||
| 99 | private $linkStyle = 'all'; |
||
| 100 | |||
| 101 | /* |
||
| 102 | * Show headers (with links), show headers (just text) or hide them. show is default |
||
| 103 | * show, plain, hide |
||
| 104 | */ |
||
| 105 | private $headerStyle = 'show'; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Marker icon to show when marker equals active page |
||
| 109 | * |
||
| 110 | * @var string|null |
||
| 111 | */ |
||
| 112 | private $activeIcon = null; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | private $userParam = ''; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param SMWQueryResult $queryResult |
||
| 121 | * @param integer $outputMode |
||
| 122 | * @param boolean $linkAbsolute |
||
| 123 | */ |
||
| 124 | public function __construct( SMWQueryResult $queryResult, $outputMode, $linkAbsolute = false ) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Sets the template. |
||
| 132 | * |
||
| 133 | * @param string $template |
||
| 134 | */ |
||
| 135 | public function setTemplate( $template ) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $userParam |
||
| 141 | */ |
||
| 142 | public function setUserParam( $userParam ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Sets the global icon. |
||
| 148 | * |
||
| 149 | * @param string $icon |
||
| 150 | */ |
||
| 151 | public function setIcon( $icon ) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Sets the global title. |
||
| 157 | * |
||
| 158 | * @param string $title |
||
| 159 | */ |
||
| 160 | public function setTitle( $title ) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Sets the global text. |
||
| 166 | * |
||
| 167 | * @param string $text |
||
| 168 | */ |
||
| 169 | public function setText( $text ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Sets the subject separator. |
||
| 175 | * |
||
| 176 | * @param string $subjectSeparator |
||
| 177 | */ |
||
| 178 | public function setSubjectSeparator( $subjectSeparator ) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Sets if the subject should be made bold in the text. |
||
| 184 | * |
||
| 185 | * @param string $boldSubject |
||
| 186 | */ |
||
| 187 | public function setBoldSubject( $boldSubject ) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Sets if the subject should shown in the text. |
||
| 193 | * |
||
| 194 | * @param string $showSubject |
||
| 195 | */ |
||
| 196 | public function setShowSubject( $showSubject ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Sets the text for the link to the page when separate from the title. |
||
| 202 | * |
||
| 203 | * @param string $text |
||
| 204 | */ |
||
| 205 | public function setPageLinkText( $text ) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * |
||
| 211 | * @param boolean $link |
||
| 212 | */ |
||
| 213 | public function setLinkStyle( $link ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * |
||
| 219 | * @param boolean $headers |
||
| 220 | */ |
||
| 221 | public function setHeaderStyle( $headers ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function getShapes() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @since 2.0 |
||
| 235 | */ |
||
| 236 | private function findShapes() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Returns the locations found in the provided result row. |
||
| 244 | * |
||
| 245 | * @param SMWResultArray[] $row |
||
| 246 | */ |
||
| 247 | private function handleResultRow( array $row ) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Handles a SMWWikiPageValue subject value. |
||
| 331 | * Gets the plain text title and creates the HTML text with headers and the like. |
||
| 332 | * |
||
| 333 | * @param SMWWikiPageValue $object |
||
| 334 | * |
||
| 335 | * @return array with title and text |
||
| 336 | */ |
||
| 337 | private function handleResultSubject( SMWWikiPageValue $object ) { |
||
| 383 | |||
| 384 | private function showArticleLink() { |
||
| 387 | |||
| 388 | private function isHeadersHide() { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Handles a single property (SMWPrintRequest) to be displayed for a record (SMWDataValue). |
||
| 394 | * |
||
| 395 | * @param SMWDataValue $object |
||
| 396 | * @param SMWPrintRequest $printRequest |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | private function handleResultProperty( SMWDataValue $object, SMWPrintRequest $printRequest ) { |
||
| 460 | |||
| 461 | private function hasTemplate() { |
||
| 464 | |||
| 465 | private function isHeadersShow() { |
||
| 468 | |||
| 469 | private function isHeadersPlain() { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Get the icon for a row. |
||
| 475 | * |
||
| 476 | * @param array $row |
||
| 477 | * |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | private function getLocationIcon( array $row ) { |
||
| 518 | |||
| 519 | private function shouldGetActiveIconUrlFor( Title $title ) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Builds a set of locations with the provided title, text and icon. |
||
| 528 | * |
||
| 529 | * @param Location[] $locations |
||
| 530 | * @param string $text |
||
| 531 | * @param string $icon |
||
| 532 | * @param array $properties |
||
| 533 | * @param Title|null $title |
||
| 534 | * |
||
| 535 | * @return Location[] |
||
| 536 | */ |
||
| 537 | private function buildLocationsList( array $locations, $text, $icon, array $properties, Title $title = null ) { |
||
| 569 | |||
| 570 | private function getTitleOutput( Title $title = null ) { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * @return \Parser |
||
| 580 | */ |
||
| 581 | private function getParser() { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @return boolean |
||
| 587 | */ |
||
| 588 | public function getHideNamespace() { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @param boolean $hideNamespace |
||
| 594 | */ |
||
| 595 | public function setHideNamespace( $hideNamespace ) { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @return string |
||
| 601 | */ |
||
| 602 | public function getActiveIcon() { |
||
| 605 | |||
| 606 | /** |
||
| 607 | * @param string $activeIcon |
||
| 608 | */ |
||
| 609 | public function setActiveIcon( $activeIcon ) { |
||
| 612 | |||
| 613 | } |
||
| 614 |
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.