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 ) { |
||
324 | |||
325 | /** |
||
326 | * Handles a SMWWikiPageValue subject value. |
||
327 | * Gets the plain text title and creates the HTML text with headers and the like. |
||
328 | * |
||
329 | * @param SMWWikiPageValue $object |
||
330 | * |
||
331 | * @return string |
||
332 | */ |
||
333 | private function getResultSubjectText( SMWWikiPageValue $object ): string { |
||
376 | |||
377 | private function showArticleLink() { |
||
380 | |||
381 | private function isHeadersHide() { |
||
384 | |||
385 | /** |
||
386 | * Handles a single property (SMWPrintRequest) to be displayed for a record (SMWDataValue). |
||
387 | * |
||
388 | * @param SMWDataValue $object |
||
389 | * @param SMWPrintRequest $printRequest |
||
390 | * |
||
391 | * @return string |
||
392 | */ |
||
393 | private function handleResultProperty( SMWDataValue $object, SMWPrintRequest $printRequest ) { |
||
448 | |||
449 | private function hasTemplate() { |
||
452 | |||
453 | private function isHeadersShow() { |
||
456 | |||
457 | private function isHeadersPlain() { |
||
460 | |||
461 | /** |
||
462 | * Get the icon for a row. |
||
463 | * |
||
464 | * @param array $row |
||
465 | * |
||
466 | * @return string |
||
467 | */ |
||
468 | private function getLocationIcon( array $row ) { |
||
506 | |||
507 | private function shouldGetActiveIconUrlFor( Title $title ) { |
||
513 | |||
514 | /** |
||
515 | * Builds a set of locations with the provided title, text and icon. |
||
516 | * |
||
517 | * @param Location[] $locations |
||
518 | * @param string $text |
||
519 | * @param string $icon |
||
520 | * @param array $properties |
||
521 | * @param Title|null $title |
||
522 | * |
||
523 | * @return Location[] |
||
524 | */ |
||
525 | private function buildLocationsList( array $locations, $text, $icon, array $properties, Title $title = null ) { |
||
557 | |||
558 | private function getTitleOutput( Title $title = null ) { |
||
565 | |||
566 | /** |
||
567 | * @return \Parser |
||
568 | */ |
||
569 | private function getParser() { |
||
572 | |||
573 | /** |
||
574 | * @return boolean |
||
575 | */ |
||
576 | public function getHideNamespace() { |
||
579 | |||
580 | /** |
||
581 | * @param boolean $hideNamespace |
||
582 | */ |
||
583 | public function setHideNamespace( $hideNamespace ) { |
||
586 | |||
587 | /** |
||
588 | * @return string |
||
589 | */ |
||
590 | public function getActiveIcon() { |
||
593 | |||
594 | /** |
||
595 | * @param string $activeIcon |
||
596 | */ |
||
597 | public function setActiveIcon( $activeIcon ) { |
||
600 | |||
601 | } |
||
602 |
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.