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 |
||
21 | class QueryHandler { |
||
22 | |||
23 | /** |
||
24 | * The global icon. |
||
25 | * @var string |
||
26 | */ |
||
27 | public $icon = ''; |
||
28 | |||
29 | /** |
||
30 | * The global text. |
||
31 | * @var string |
||
32 | */ |
||
33 | public $text = ''; |
||
34 | |||
35 | /** |
||
36 | * The global title. |
||
37 | * @var string |
||
38 | */ |
||
39 | public $title = ''; |
||
40 | |||
41 | /** |
||
42 | * Make a separate link to the title or not? |
||
43 | * @var boolean |
||
44 | */ |
||
45 | public $titleLinkSeparate = false; |
||
46 | |||
47 | private $queryResult; |
||
48 | |||
49 | private $outputMode; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private $geoShapes = [ |
||
55 | 'lines' => [], |
||
56 | 'locations' => [], |
||
57 | 'polygons' => [] |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * The template to use for the text, or false if there is none. |
||
62 | * @var string|boolean false |
||
63 | */ |
||
64 | private $template = false; |
||
65 | |||
66 | /** |
||
67 | * Should link targets be made absolute (instead of relative)? |
||
68 | * @var boolean |
||
69 | */ |
||
70 | private $linkAbsolute; |
||
71 | |||
72 | /** |
||
73 | * The text used for the link to the page (if it's created). $1 will be replaced by the page name. |
||
74 | * @var string |
||
75 | */ |
||
76 | private $pageLinkText = '$1'; |
||
77 | |||
78 | /** |
||
79 | * A separator to use between the subject and properties in the text field. |
||
80 | * @var string |
||
81 | */ |
||
82 | private $subjectSeparator = '<hr />'; |
||
83 | |||
84 | /** |
||
85 | * Show the subject in the text or not? |
||
86 | * @var boolean |
||
87 | */ |
||
88 | private $showSubject = true; |
||
89 | |||
90 | /** |
||
91 | * Hide the namespace or not. |
||
92 | * @var boolean |
||
93 | */ |
||
94 | private $hideNamespace = false; |
||
95 | |||
96 | /** |
||
97 | * Defines which article names in the result are hyperlinked, all normally is the default |
||
98 | * none, subject, all |
||
99 | */ |
||
100 | private $linkStyle = 'all'; |
||
101 | |||
102 | /* |
||
103 | * Show headers (with links), show headers (just text) or hide them. show is default |
||
104 | * show, plain, hide |
||
105 | */ |
||
106 | private $headerStyle = 'show'; |
||
107 | |||
108 | /** |
||
109 | * Marker icon to show when marker equals active page |
||
110 | * @var string|null |
||
111 | */ |
||
112 | private $activeIcon = null; |
||
113 | |||
114 | /** |
||
115 | * @var string |
||
116 | */ |
||
117 | private $userParam = ''; |
||
118 | |||
119 | public function __construct( SMWQueryResult $queryResult, int $outputMode, bool $linkAbsolute = false ) { |
||
124 | |||
125 | public function setTemplate( string $template ) { |
||
128 | |||
129 | public function setUserParam( string $userParam ) { |
||
132 | |||
133 | /** |
||
134 | * Sets the global icon. |
||
135 | */ |
||
136 | public function setIcon( string $icon ) { |
||
139 | |||
140 | /** |
||
141 | * Sets the global title. |
||
142 | */ |
||
143 | public function setTitle( string $title ) { |
||
146 | |||
147 | /** |
||
148 | * Sets the global text. |
||
149 | */ |
||
150 | public function setText( string $text ) { |
||
153 | |||
154 | public function setSubjectSeparator( string $subjectSeparator ) { |
||
157 | |||
158 | public function setShowSubject( bool $showSubject ) { |
||
161 | |||
162 | /** |
||
163 | * Sets the text for the link to the page when separate from the title. |
||
164 | */ |
||
165 | public function setPageLinkText( string $text ) { |
||
168 | |||
169 | public function setLinkStyle( string $link ) { |
||
172 | |||
173 | public function setHeaderStyle( string $headers ) { |
||
176 | |||
177 | /** |
||
178 | * @return array |
||
179 | */ |
||
180 | public function getShapes() { |
||
184 | |||
185 | /** |
||
186 | * @since 2.0 |
||
187 | */ |
||
188 | private function findShapes() { |
||
193 | |||
194 | /** |
||
195 | * Returns the locations found in the provided result row. |
||
196 | * |
||
197 | * @param SMWResultArray[] $row |
||
198 | */ |
||
199 | private function handleResultRow( array $row ) { |
||
267 | |||
268 | /** |
||
269 | * Handles a SMWWikiPageValue subject value. |
||
270 | * Gets the plain text title and creates the HTML text with headers and the like. |
||
271 | * |
||
272 | * @param SMWWikiPageValue $object |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | private function getResultSubjectText( SMWWikiPageValue $object ): string { |
||
317 | |||
318 | private function showArticleLink() { |
||
321 | |||
322 | private function isHeadersHide() { |
||
325 | |||
326 | /** |
||
327 | * Handles a single property (SMWPrintRequest) to be displayed for a record (SMWDataValue). |
||
328 | */ |
||
329 | private function handleResultProperty( SMWDataValue $object, SMWPrintRequest $printRequest ): string { |
||
341 | |||
342 | private function getPropertyName( SMWPrintRequest $printRequest ): string { |
||
368 | |||
369 | private function getPropertyValue( SMWDataValue $object ): string { |
||
389 | |||
390 | private function hasPage( SMWDataValue $object ): bool { |
||
400 | |||
401 | private function hasTemplate() { |
||
404 | |||
405 | private function isHeadersShow() { |
||
408 | |||
409 | private function isHeadersPlain() { |
||
412 | |||
413 | /** |
||
414 | * Get the icon for a row. |
||
415 | * |
||
416 | * @param array $row |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | private function getLocationIcon( array $row ) { |
||
458 | |||
459 | private function shouldGetActiveIconUrlFor( Title $title ) { |
||
465 | |||
466 | /** |
||
467 | * Builds a set of locations with the provided title, text and icon. |
||
468 | * |
||
469 | * @param Location[] $locations |
||
470 | * @param string $text |
||
471 | * @param string $icon |
||
472 | * @param array $properties |
||
473 | * @param Title|null $title |
||
474 | * |
||
475 | * @return Location[] |
||
476 | */ |
||
477 | private function buildLocationsList( array $locations, $text, $icon, array $properties, Title $title = null ) { |
||
509 | |||
510 | private function getTitleOutput( Title $title = null ) { |
||
517 | |||
518 | /** |
||
519 | * @return \Parser |
||
520 | */ |
||
521 | private function getParser() { |
||
524 | |||
525 | /** |
||
526 | * @return boolean |
||
527 | */ |
||
528 | public function getHideNamespace() { |
||
531 | |||
532 | /** |
||
533 | * @param boolean $hideNamespace |
||
534 | */ |
||
535 | public function setHideNamespace( $hideNamespace ) { |
||
538 | |||
539 | /** |
||
540 | * @return string |
||
541 | */ |
||
542 | public function getActiveIcon() { |
||
545 | |||
546 | /** |
||
547 | * @param string $activeIcon |
||
548 | */ |
||
549 | public function setActiveIcon( $activeIcon ) { |
||
552 | |||
553 | } |
||
554 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.