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 | public function __construct( SMWQueryResult $queryResult, int $outputMode, bool $linkAbsolute = false ) { |
||
| 106 | |||
| 107 | public function setTemplate( string $template ) { |
||
| 110 | |||
| 111 | 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 | public function setShowSubject( bool $showSubject ) { |
||
| 143 | |||
| 144 | public function setLinkStyle( string $link ) { |
||
| 147 | |||
| 148 | public function setHeaderStyle( string $headers ) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return Location[] |
||
| 154 | */ |
||
| 155 | public function getLocations(): iterable { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param SMWResultArray[] $row |
||
| 163 | * @return Location[] |
||
| 164 | */ |
||
| 165 | private function handlePageResult( array $row ): array { |
||
| 183 | |||
| 184 | private function getTitleAndText( SMWResultArray $resultArray ): array { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param SMWResultArray[] $row |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | private function getLocationsAndProperties( array $row ): array { |
||
| 240 | |||
| 241 | private function locationFromDataItem( SMWDIGeoCoord $dataItem ): Location { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Handles a SMWWikiPageValue subject value. |
||
| 250 | * Gets the plain text title and creates the HTML text with headers and the like. |
||
| 251 | * |
||
| 252 | * @param SMWWikiPageValue $object |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | private function getResultSubjectText( SMWWikiPageValue $object ): string { |
||
| 283 | |||
| 284 | private function showArticleLink() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Handles a single property (SMWPrintRequest) to be displayed for a record (SMWDataValue). |
||
| 290 | */ |
||
| 291 | private function handleResultProperty( SMWDataValue $object, SMWPrintRequest $printRequest ): string { |
||
| 303 | |||
| 304 | private function getPropertyName( SMWPrintRequest $printRequest ): string { |
||
| 326 | |||
| 327 | private function getPropertyLinker(): ?Linker { |
||
| 330 | |||
| 331 | private function getValueLinker(): ?Linker { |
||
| 334 | |||
| 335 | private function getPropertyValue( SMWDataValue $object ): string { |
||
| 357 | |||
| 358 | private function hasPage( SMWDataValue $object ): bool { |
||
| 368 | |||
| 369 | private function hasTemplate() { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get the icon for a row. |
||
| 375 | * |
||
| 376 | * @param array $row |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | private function getLocationIcon( array $row ) { |
||
| 418 | |||
| 419 | private function shouldGetActiveIconUrlFor( Title $title ) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Builds a set of locations with the provided title, text and icon. |
||
| 428 | * |
||
| 429 | * @param Location[] $locations |
||
| 430 | * @param string $text |
||
| 431 | * @param string $icon |
||
| 432 | * @param array $properties |
||
| 433 | * @param Title|null $title |
||
| 434 | * |
||
| 435 | * @return Location[] |
||
| 436 | */ |
||
| 437 | private function buildLocationsList( array $locations, $text, $icon, array $properties, Title $title = null ): array { |
||
| 469 | |||
| 470 | private function getTitleOutput( Title $title = null ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @return \Parser |
||
| 480 | */ |
||
| 481 | private function getParser() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @return boolean |
||
| 487 | */ |
||
| 488 | public function getHideNamespace() { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param boolean $hideNamespace |
||
| 494 | */ |
||
| 495 | public function setHideNamespace( $hideNamespace ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | public function getActiveIcon() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param string $activeIcon |
||
| 508 | */ |
||
| 509 | public function setActiveIcon( $activeIcon ) { |
||
| 512 | |||
| 513 | } |
||
| 514 |
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.