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 | /** |
||
45 | * Make a separate link to the title or not? |
||
46 | * @var boolean |
||
47 | */ |
||
48 | public $titleLinkSeparate = false; |
||
49 | |||
50 | private $queryResult; |
||
51 | |||
52 | private $outputMode; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private $geoShapes = [ |
||
58 | 'lines' => [], |
||
59 | 'locations' => [], |
||
60 | 'polygons' => [] |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * The template to use for the text, or false if there is none. |
||
65 | * @var string|boolean false |
||
66 | */ |
||
67 | private $template = false; |
||
68 | |||
69 | /** |
||
70 | * Should link targets be made absolute (instead of relative)? |
||
71 | * @var boolean |
||
72 | */ |
||
73 | private $linkAbsolute; |
||
74 | |||
75 | /** |
||
76 | * The text used for the link to the page (if it's created). $1 will be replaced by the page name. |
||
77 | * @var string |
||
78 | */ |
||
79 | private $pageLinkText = '$1'; |
||
80 | |||
81 | /** |
||
82 | * A separator to use between the subject and properties in the text field. |
||
83 | * @var string |
||
84 | */ |
||
85 | private $subjectSeparator = '<hr />'; |
||
86 | |||
87 | /** |
||
88 | * Show the subject in the text or not? |
||
89 | * @var boolean |
||
90 | */ |
||
91 | private $showSubject = true; |
||
92 | |||
93 | /** |
||
94 | * Hide the namespace or not. |
||
95 | * @var boolean |
||
96 | */ |
||
97 | private $hideNamespace = false; |
||
98 | |||
99 | /** |
||
100 | * Defines which article names in the result are hyperlinked, all normally is the default |
||
101 | * none, subject, all |
||
102 | */ |
||
103 | private $linkStyle = 'all'; |
||
104 | |||
105 | /* |
||
106 | * Show headers (with links), show headers (just text) or hide them. show is default |
||
107 | * show, plain, hide |
||
108 | */ |
||
109 | private $headerStyle = 'show'; |
||
110 | |||
111 | /** |
||
112 | * Marker icon to show when marker equals active page |
||
113 | * @var string|null |
||
114 | */ |
||
115 | private $activeIcon = null; |
||
116 | |||
117 | /** |
||
118 | * @var string |
||
119 | */ |
||
120 | private $userParam = ''; |
||
121 | |||
122 | public function __construct( SMWQueryResult $queryResult, int $outputMode, bool $linkAbsolute = false ) { |
||
127 | |||
128 | public function setTemplate( string $template ) { |
||
131 | |||
132 | public function setUserParam( string $userParam ) { |
||
135 | |||
136 | /** |
||
137 | * Sets the global icon. |
||
138 | */ |
||
139 | public function setIcon( string $icon ) { |
||
142 | |||
143 | /** |
||
144 | * Sets the global title. |
||
145 | */ |
||
146 | public function setTitle( string $title ) { |
||
149 | |||
150 | /** |
||
151 | * Sets the global text. |
||
152 | */ |
||
153 | public function setText( string $text ) { |
||
156 | |||
157 | public function setSubjectSeparator( string $subjectSeparator ) { |
||
160 | |||
161 | public function setShowSubject( bool $showSubject ) { |
||
164 | |||
165 | /** |
||
166 | * Sets the text for the link to the page when separate from the title. |
||
167 | */ |
||
168 | public function setPageLinkText( string $text ) { |
||
171 | |||
172 | public function setLinkStyle( string $link ) { |
||
175 | |||
176 | public function setHeaderStyle( string $headers ) { |
||
179 | |||
180 | /** |
||
181 | * @return array |
||
182 | */ |
||
183 | public function getShapes() { |
||
187 | |||
188 | /** |
||
189 | * @since 2.0 |
||
190 | */ |
||
191 | private function findShapes() { |
||
196 | |||
197 | private function getTitleAndText( SMWResultArray $resultArray ): array { |
||
216 | |||
217 | /** |
||
218 | * Returns the locations found in the provided result row. |
||
219 | * |
||
220 | * @param SMWResultArray[] $row |
||
221 | */ |
||
222 | private function handleResultRow( array $row ) { |
||
271 | |||
272 | private function locationFromDataItem( SMWDIGeoCoord $dataItem ): Location { |
||
278 | |||
279 | /** |
||
280 | * Handles a SMWWikiPageValue subject value. |
||
281 | * Gets the plain text title and creates the HTML text with headers and the like. |
||
282 | * |
||
283 | * @param SMWWikiPageValue $object |
||
284 | * |
||
285 | * @return string |
||
286 | */ |
||
287 | private function getResultSubjectText( SMWWikiPageValue $object ): string { |
||
328 | |||
329 | private function showArticleLink() { |
||
332 | |||
333 | private function isHeadersHide() { |
||
336 | |||
337 | /** |
||
338 | * Handles a single property (SMWPrintRequest) to be displayed for a record (SMWDataValue). |
||
339 | */ |
||
340 | private function handleResultProperty( SMWDataValue $object, SMWPrintRequest $printRequest ): string { |
||
352 | |||
353 | private function getPropertyName( SMWPrintRequest $printRequest ): string { |
||
375 | |||
376 | private function getPropertyLinker(): ?Linker { |
||
379 | |||
380 | private function getValueLinker(): ?Linker { |
||
383 | |||
384 | private function getPropertyValue( SMWDataValue $object ): string { |
||
406 | |||
407 | private function hasPage( SMWDataValue $object ): bool { |
||
417 | |||
418 | private function hasTemplate() { |
||
421 | |||
422 | /** |
||
423 | * Get the icon for a row. |
||
424 | * |
||
425 | * @param array $row |
||
426 | * |
||
427 | * @return string |
||
428 | */ |
||
429 | private function getLocationIcon( array $row ) { |
||
467 | |||
468 | private function shouldGetActiveIconUrlFor( Title $title ) { |
||
474 | |||
475 | /** |
||
476 | * Builds a set of locations with the provided title, text and icon. |
||
477 | * |
||
478 | * @param Location[] $locations |
||
479 | * @param string $text |
||
480 | * @param string $icon |
||
481 | * @param array $properties |
||
482 | * @param Title|null $title |
||
483 | * |
||
484 | * @return Location[] |
||
485 | */ |
||
486 | private function buildLocationsList( array $locations, $text, $icon, array $properties, Title $title = null ) { |
||
518 | |||
519 | private function getTitleOutput( Title $title = null ) { |
||
526 | |||
527 | /** |
||
528 | * @return \Parser |
||
529 | */ |
||
530 | private function getParser() { |
||
533 | |||
534 | /** |
||
535 | * @return boolean |
||
536 | */ |
||
537 | public function getHideNamespace() { |
||
540 | |||
541 | /** |
||
542 | * @param boolean $hideNamespace |
||
543 | */ |
||
544 | public function setHideNamespace( $hideNamespace ) { |
||
547 | |||
548 | /** |
||
549 | * @return string |
||
550 | */ |
||
551 | public function getActiveIcon() { |
||
554 | |||
555 | /** |
||
556 | * @param string $activeIcon |
||
557 | */ |
||
558 | public function setActiveIcon( $activeIcon ) { |
||
561 | |||
562 | } |
||
563 |