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 |
||
| 24 | class QueryHandler { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The global icon. |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public $icon = ''; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The global text. |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | public $text = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The global title. |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | public $title = ''; |
||
| 43 | |||
| 44 | private $queryResult; |
||
| 45 | |||
| 46 | private $outputMode; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The template to use for the text, or false if there is none. |
||
| 50 | * @var string|boolean false |
||
| 51 | */ |
||
| 52 | private $template = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Should link targets be made absolute (instead of relative)? |
||
| 56 | * @var boolean |
||
| 57 | */ |
||
| 58 | private $linkAbsolute; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A separator to use between the subject and properties in the text field. |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | private $subjectSeparator = '<hr />'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Show the subject in the text or not? |
||
| 68 | * @var boolean |
||
| 69 | */ |
||
| 70 | private $showSubject = true; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Hide the namespace or not. |
||
| 74 | * @var boolean |
||
| 75 | */ |
||
| 76 | private $hideNamespace = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Defines which article names in the result are hyperlinked, all normally is the default |
||
| 80 | * none, subject, all |
||
| 81 | */ |
||
| 82 | private $linkStyle = 'all'; |
||
| 83 | |||
| 84 | /* |
||
| 85 | * Show headers (with links), show headers (just text) or hide them. show is default |
||
| 86 | * show, plain, hide |
||
| 87 | */ |
||
| 88 | private $headerStyle = 'show'; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Marker icon to show when marker equals active page |
||
| 92 | * @var string|null |
||
| 93 | */ |
||
| 94 | private $activeIcon = null; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | private $userParam = ''; |
||
| 100 | |||
| 101 | 3 | public function __construct( SMWQueryResult $queryResult, int $outputMode, bool $linkAbsolute = false ) { |
|
| 106 | |||
| 107 | 3 | public function setTemplate( string $template ) { |
|
| 110 | |||
| 111 | 3 | public function setUserParam( string $userParam ) { |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Sets the global icon. |
||
| 117 | */ |
||
| 118 | public function setIcon( string $icon ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Sets the global title. |
||
| 124 | */ |
||
| 125 | public function setTitle( string $title ) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Sets the global text. |
||
| 131 | */ |
||
| 132 | public function setText( string $text ) { |
||
| 135 | |||
| 136 | public function setSubjectSeparator( string $subjectSeparator ) { |
||
| 139 | |||
| 140 | 3 | public function setShowSubject( bool $showSubject ) { |
|
| 143 | |||
| 144 | 3 | public function setLinkStyle( string $link ) { |
|
| 147 | |||
| 148 | 3 | public function setHeaderStyle( string $headers ) { |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @return Location[] |
||
| 154 | */ |
||
| 155 | 3 | public function getLocations(): iterable { |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param SMWResultArray[] $row |
||
| 163 | * @return Location[] |
||
| 164 | */ |
||
| 165 | 3 | private function handlePageResult( array $row ): array { |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param SMWResultArray $resultArray |
||
| 180 | * @return array|string[] [string $title, string $text] |
||
| 181 | */ |
||
| 182 | 3 | private function getTitleAndText( SMWResultArray $resultArray ): array { |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param SMWResultArray[] $row |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | 3 | private function getLocationsAndProperties( array $row ): array { |
|
| 238 | |||
| 239 | 3 | private function locationFromDataItem( SMWDIGeoCoord $dataItem ): Location { |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Handles a SMWWikiPageValue subject value. |
||
| 248 | * Gets the plain text title and creates the HTML text with headers and the like. |
||
| 249 | * |
||
| 250 | * @param SMWWikiPageValue $object |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | 3 | private function getResultSubjectText( SMWWikiPageValue $object ): string { |
|
| 281 | |||
| 282 | 3 | private function showArticleLink() { |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Handles a single property (SMWPrintRequest) to be displayed for a record (SMWDataValue). |
||
| 288 | */ |
||
| 289 | 1 | private function handleResultProperty( SMWDataValue $object, SMWPrintRequest $printRequest ): string { |
|
| 301 | |||
| 302 | 1 | private function getPropertyName( SMWPrintRequest $printRequest ): string { |
|
| 324 | |||
| 325 | 1 | private function getPropertyLinker(): ?Linker { |
|
| 328 | |||
| 329 | 1 | private function getValueLinker(): ?Linker { |
|
| 332 | |||
| 333 | 1 | private function getPropertyValue( SMWDataValue $object ): string { |
|
| 355 | |||
| 356 | private function hasPage( SMWDataValue $object ): bool { |
||
| 366 | |||
| 367 | 3 | private function hasTemplate() { |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Get the icon for a row. |
||
| 373 | * |
||
| 374 | * @param array $row |
||
| 375 | * |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | 3 | private function getLocationIcon( array $row ) { |
|
| 416 | |||
| 417 | 3 | private function shouldGetActiveIconUrlFor( Title $title ) { |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param Location[] $locations |
||
| 426 | * @param string $text |
||
| 427 | * @param string $icon |
||
| 428 | * @param array $properties |
||
| 429 | * @param Title|null $title |
||
| 430 | * |
||
| 431 | * @return Location[] |
||
| 432 | */ |
||
| 433 | 3 | private function buildLocationsForPage( array $locations, $text, $icon, array $properties, Title $title = null ): array { |
|
| 448 | |||
| 449 | 3 | private function buildPopupText( array $properties, string $titleOutput, Location $location ): string { |
|
| 458 | |||
| 459 | 1 | private function newTemplatedPopup(): TemplatedPopup { |
|
| 465 | |||
| 466 | 3 | private function getTitleOutput( Title $title = null ) { |
|
| 473 | |||
| 474 | 1 | private function getParser(): \Parser { |
|
| 477 | |||
| 478 | /** |
||
| 479 | * @return boolean |
||
| 480 | */ |
||
| 481 | public function getHideNamespace() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param boolean $hideNamespace |
||
| 487 | */ |
||
| 488 | 3 | public function setHideNamespace( $hideNamespace ) { |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | public function getActiveIcon() { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @param string $activeIcon |
||
| 501 | */ |
||
| 502 | 3 | public function setActiveIcon( $activeIcon ) { |
|
| 505 | |||
| 506 | } |
||
| 507 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.