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 |
||
22 | class QueryHandler { |
||
23 | |||
24 | /** |
||
25 | * The global icon. |
||
26 | * @var string |
||
27 | */ |
||
28 | public $icon = ''; |
||
29 | |||
30 | /** |
||
31 | * The global text. |
||
32 | * @var string |
||
33 | */ |
||
34 | public $text = ''; |
||
35 | |||
36 | /** |
||
37 | * The global title. |
||
38 | * @var string |
||
39 | */ |
||
40 | public $title = ''; |
||
41 | |||
42 | private $queryResult; |
||
43 | |||
44 | private $outputMode; |
||
45 | |||
46 | /** |
||
47 | * The template to use for the text, or false if there is none. |
||
48 | * @var string|boolean false |
||
49 | */ |
||
50 | private $template = false; |
||
51 | |||
52 | /** |
||
53 | * Should link targets be made absolute (instead of relative)? |
||
54 | * @var boolean |
||
55 | */ |
||
56 | private $linkAbsolute; |
||
57 | |||
58 | /** |
||
59 | * A separator to use between the subject and properties in the text field. |
||
60 | * @var string |
||
61 | */ |
||
62 | private $subjectSeparator = '<hr />'; |
||
63 | |||
64 | /** |
||
65 | * Show the subject in the text or not? |
||
66 | * @var boolean |
||
67 | */ |
||
68 | private $showSubject = true; |
||
69 | |||
70 | /** |
||
71 | * Hide the namespace or not. |
||
72 | * @var boolean |
||
73 | */ |
||
74 | private $hideNamespace = false; |
||
75 | |||
76 | /** |
||
77 | * Defines which article names in the result are hyperlinked, all normally is the default |
||
78 | * none, subject, all |
||
79 | */ |
||
80 | private $linkStyle = 'all'; |
||
81 | |||
82 | /* |
||
83 | * Show headers (with links), show headers (just text) or hide them. show is default |
||
84 | * show, plain, hide |
||
85 | */ |
||
86 | private $headerStyle = 'show'; |
||
87 | |||
88 | /** |
||
89 | * Marker icon to show when marker equals active page |
||
90 | * @var string|null |
||
91 | */ |
||
92 | private $activeIcon = null; |
||
93 | |||
94 | /** |
||
95 | * @var string |
||
96 | */ |
||
97 | private $userParam = ''; |
||
98 | |||
99 | public function __construct( SMWQueryResult $queryResult, int $outputMode, bool $linkAbsolute = false ) { |
||
104 | |||
105 | public function setTemplate( string $template ) { |
||
108 | |||
109 | public function setUserParam( string $userParam ) { |
||
112 | |||
113 | /** |
||
114 | * Sets the global icon. |
||
115 | */ |
||
116 | public function setIcon( string $icon ) { |
||
119 | |||
120 | /** |
||
121 | * Sets the global title. |
||
122 | */ |
||
123 | public function setTitle( string $title ) { |
||
126 | |||
127 | /** |
||
128 | * Sets the global text. |
||
129 | */ |
||
130 | public function setText( string $text ) { |
||
133 | |||
134 | public function setSubjectSeparator( string $subjectSeparator ) { |
||
137 | |||
138 | public function setShowSubject( bool $showSubject ) { |
||
141 | |||
142 | public function setLinkStyle( string $link ) { |
||
145 | |||
146 | public function setHeaderStyle( string $headers ) { |
||
149 | |||
150 | /** |
||
151 | * @return Location[] |
||
152 | */ |
||
153 | public function getLocations(): iterable { |
||
158 | |||
159 | /** |
||
160 | * @param SMWResultArray[] $row |
||
161 | * @return Location[] |
||
162 | */ |
||
163 | private function handlePageResult( array $row ): array { |
||
181 | |||
182 | private function getTitleAndText( SMWResultArray $resultArray ): array { |
||
201 | |||
202 | /** |
||
203 | * @param SMWResultArray[] $row |
||
204 | * @return array |
||
205 | */ |
||
206 | private function getLocationsAndProperties( array $row ): array { |
||
238 | |||
239 | 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 | private function getResultSubjectText( SMWWikiPageValue $object ): string { |
||
279 | |||
280 | private function showArticleLink() { |
||
283 | |||
284 | /** |
||
285 | * Handles a single property (SMWPrintRequest) to be displayed for a record (SMWDataValue). |
||
286 | */ |
||
287 | private function handleResultProperty( SMWDataValue $object, SMWPrintRequest $printRequest ): string { |
||
299 | |||
300 | private function getPropertyName( SMWPrintRequest $printRequest ): string { |
||
322 | |||
323 | private function getPropertyLinker(): ?Linker { |
||
326 | |||
327 | private function getValueLinker(): ?Linker { |
||
330 | |||
331 | private function getPropertyValue( SMWDataValue $object ): string { |
||
353 | |||
354 | private function hasPage( SMWDataValue $object ): bool { |
||
364 | |||
365 | private function hasTemplate() { |
||
368 | |||
369 | /** |
||
370 | * Get the icon for a row. |
||
371 | * |
||
372 | * @param array $row |
||
373 | * |
||
374 | * @return string |
||
375 | */ |
||
376 | private function getLocationIcon( array $row ) { |
||
414 | |||
415 | private function shouldGetActiveIconUrlFor( Title $title ) { |
||
421 | |||
422 | /** |
||
423 | * Builds a set of locations with the provided title, text and icon. |
||
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 | private function buildLocationsList( array $locations, $text, $icon, array $properties, Title $title = null ): array { |
||
465 | |||
466 | private function getTitleOutput( Title $title = null ) { |
||
473 | |||
474 | /** |
||
475 | * @return \Parser |
||
476 | */ |
||
477 | private function getParser() { |
||
480 | |||
481 | /** |
||
482 | * @return boolean |
||
483 | */ |
||
484 | public function getHideNamespace() { |
||
487 | |||
488 | /** |
||
489 | * @param boolean $hideNamespace |
||
490 | */ |
||
491 | public function setHideNamespace( $hideNamespace ) { |
||
494 | |||
495 | /** |
||
496 | * @return string |
||
497 | */ |
||
498 | public function getActiveIcon() { |
||
501 | |||
502 | /** |
||
503 | * @param string $activeIcon |
||
504 | */ |
||
505 | public function setActiveIcon( $activeIcon ) { |
||
508 | |||
509 | } |
||
510 |
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.