Complex classes like Search 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 Search, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Search extends Ui |
||
8 | { |
||
9 | protected $query; |
||
10 | protected $parsedQuery; |
||
11 | protected $searchState; |
||
12 | protected $pageLookupResults = array(); |
||
13 | protected $fullTextResults = array(); |
||
14 | protected $highlight = array(); |
||
15 | |||
16 | /** |
||
17 | * Search constructor. |
||
18 | * |
||
19 | * @param array $pageLookupResults pagename lookup results in the form [pagename => pagetitle] |
||
20 | * @param array $fullTextResults fulltext search results in the form [pagename => #hits] |
||
21 | * @param array $highlight array of strings to be highlighted |
||
22 | */ |
||
23 | public function __construct(array $pageLookupResults, array $fullTextResults, $highlight) |
||
24 | { |
||
25 | global $QUERY; |
||
26 | $Indexer = idx_get_indexer(); |
||
27 | |||
28 | $this->query = $QUERY; |
||
29 | $this->parsedQuery = ft_queryParser($Indexer, $QUERY); |
||
30 | $this->searchState = new SearchState($this->parsedQuery); |
||
31 | |||
32 | $this->pageLookupResults = $pageLookupResults; |
||
33 | $this->fullTextResults = $fullTextResults; |
||
34 | $this->highlight = $highlight; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * display the search result |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | public function show() |
||
56 | |||
57 | /** |
||
58 | * Get a form which can be used to adjust/refine the search |
||
59 | * |
||
60 | * @param string $query |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | protected function getSearchFormHTML($query) |
||
93 | |||
94 | /** |
||
95 | * Add elements to adjust how the results are sorted |
||
96 | * |
||
97 | * @param Form $searchForm |
||
98 | */ |
||
99 | protected function addSortTool(Form $searchForm) |
||
148 | |||
149 | /** |
||
150 | * Check if the query is simple enough to modify its namespace limitations without breaking the rest of the query |
||
151 | * |
||
152 | * @param array $parsedQuery |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | protected function isNamespaceAssistanceAvailable(array $parsedQuery) { |
||
163 | |||
164 | /** |
||
165 | * Check if the query is simple enough to modify the fragment search behavior without breaking the rest of the query |
||
166 | * |
||
167 | * @param array $parsedQuery |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | protected function isFragmentAssistanceAvailable(array $parsedQuery) { |
||
182 | |||
183 | /** |
||
184 | * Add the elements to be used for search assistance |
||
185 | * |
||
186 | * @param Form $searchForm |
||
187 | */ |
||
188 | protected function addSearchAssistanceElements(Form $searchForm) |
||
189 | { |
||
190 | $searchForm->addTagOpen('div') |
||
191 | ->addClass('advancedOptions') |
||
192 | ->attr('style', 'display: none;') |
||
193 | ->attr('aria-hidden', 'true'); |
||
194 | |||
195 | $this->addFragmentBehaviorLinks($searchForm); |
||
196 | $this->addNamespaceSelector($searchForm); |
||
197 | $this->addDateSelector($searchForm); |
||
198 | $this->addSortTool($searchForm); |
||
199 | |||
200 | $searchForm->addTagClose('div'); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Add the elements to adjust the fragment search behavior |
||
205 | * |
||
206 | * @param Form $searchForm |
||
207 | */ |
||
208 | protected function addFragmentBehaviorLinks(Form $searchForm) |
||
209 | { |
||
210 | if (!$this->isFragmentAssistanceAvailable($this->parsedQuery)) { |
||
211 | return; |
||
212 | } |
||
213 | global $lang; |
||
214 | |||
215 | $options = [ |
||
216 | 'exact' => [ |
||
217 | 'label' => $lang['search_exact_match'], |
||
218 | 'and' => array_map(function ($term) { |
||
219 | return trim($term, '*'); |
||
220 | }, $this->parsedQuery['and']), |
||
221 | 'not' => array_map(function ($term) { |
||
222 | return trim($term, '*'); |
||
223 | }, $this->parsedQuery['not']), |
||
224 | ], |
||
225 | 'starts' => [ |
||
226 | 'label' => $lang['search_starts_with'], |
||
227 | 'and' => array_map(function ($term) { |
||
228 | return trim($term, '*') . '*'; |
||
229 | }, $this->parsedQuery['and']), |
||
230 | 'not' => array_map(function ($term) { |
||
231 | return trim($term, '*') . '*'; |
||
232 | }, $this->parsedQuery['not']), |
||
233 | ], |
||
234 | 'ends' => [ |
||
235 | 'label' => $lang['search_ends_with'], |
||
236 | 'and' => array_map(function ($term) { |
||
237 | return '*' . trim($term, '*'); |
||
238 | }, $this->parsedQuery['and']), |
||
239 | 'not' => array_map(function ($term) { |
||
240 | return '*' . trim($term, '*'); |
||
241 | }, $this->parsedQuery['not']), |
||
242 | ], |
||
243 | 'contains' => [ |
||
244 | 'label' => $lang['search_contains'], |
||
245 | 'and' => array_map(function ($term) { |
||
246 | return '*' . trim($term, '*') . '*'; |
||
247 | }, $this->parsedQuery['and']), |
||
248 | 'not' => array_map(function ($term) { |
||
249 | return '*' . trim($term, '*') . '*'; |
||
250 | }, $this->parsedQuery['not']), |
||
251 | ] |
||
252 | ]; |
||
253 | |||
254 | // detect current |
||
255 | $activeOption = 'custom'; |
||
256 | foreach ($options as $key => $option) { |
||
257 | if ($this->parsedQuery['and'] === $option['and']) { |
||
258 | $activeOption = $key; |
||
259 | } |
||
260 | } |
||
261 | if ($activeOption === 'custom') { |
||
262 | $options = array_merge(['custom' => [ |
||
263 | 'label' => $lang['search_custom_match'], |
||
264 | ]], $options); |
||
265 | } |
||
266 | |||
267 | $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); |
||
268 | // render current |
||
269 | $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); |
||
270 | if ($activeOption !== 'exact') { |
||
271 | $currentWrapper->addClass('changed'); |
||
272 | } |
||
273 | $searchForm->addHTML($options[$activeOption]['label']); |
||
274 | $searchForm->addTagClose('div'); |
||
275 | |||
276 | // render options list |
||
277 | $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); |
||
278 | |||
279 | foreach ($options as $key => $option) { |
||
280 | $listItem = $searchForm->addTagOpen('li'); |
||
281 | |||
282 | if ($key === $activeOption) { |
||
283 | $listItem->addClass('active'); |
||
284 | $searchForm->addHTML($option['label']); |
||
285 | } else { |
||
286 | $link = $this->searchState |
||
287 | ->withFragments($option['and'], $option['not']) |
||
288 | ->getSearchLink($option['label']) |
||
289 | ; |
||
290 | $searchForm->addHTML($link); |
||
291 | } |
||
292 | $searchForm->addTagClose('li'); |
||
293 | } |
||
294 | $searchForm->addTagClose('ul'); |
||
295 | |||
296 | $searchForm->addTagClose('div'); |
||
297 | |||
298 | // render options list |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Add the elements for the namespace selector |
||
303 | * |
||
304 | * @param Form $searchForm |
||
305 | */ |
||
306 | protected function addNamespaceSelector(Form $searchForm) |
||
307 | { |
||
308 | if (!$this->isNamespaceAssistanceAvailable($this->parsedQuery)) { |
||
309 | return; |
||
310 | } |
||
311 | |||
312 | global $lang; |
||
313 | |||
314 | $baseNS = empty($this->parsedQuery['ns']) ? '' : $this->parsedQuery['ns'][0]; |
||
315 | $extraNS = $this->getAdditionalNamespacesFromResults($baseNS); |
||
316 | |||
317 | $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); |
||
318 | // render current |
||
319 | $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); |
||
320 | if ($baseNS) { |
||
321 | $currentWrapper->addClass('changed'); |
||
322 | $searchForm->addHTML('@' . $baseNS); |
||
323 | } else { |
||
324 | $searchForm->addHTML($lang['search_any_ns']); |
||
325 | } |
||
326 | $searchForm->addTagClose('div'); |
||
327 | |||
328 | // render options list |
||
329 | $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); |
||
330 | |||
331 | $listItem = $searchForm->addTagOpen('li'); |
||
332 | if ($baseNS) { |
||
333 | $listItem->addClass('active'); |
||
334 | $link = $this->searchState->withNamespace('')->getSearchLink($lang['search_any_ns']); |
||
335 | $searchForm->addHTML($link); |
||
336 | } else { |
||
337 | $searchForm->addHTML($lang['search_any_ns']); |
||
338 | } |
||
339 | $searchForm->addTagClose('li'); |
||
340 | |||
341 | foreach ($extraNS as $ns => $count) { |
||
342 | $listItem = $searchForm->addTagOpen('li'); |
||
343 | $label = $ns . ($count ? " <bdi>($count)</bdi>" : ''); |
||
344 | |||
345 | if ($ns === $baseNS) { |
||
346 | $listItem->addClass('active'); |
||
347 | $searchForm->addHTML($label); |
||
348 | } else { |
||
349 | $link = $this->searchState->withNamespace($ns)->getSearchLink($label); |
||
350 | $searchForm->addHTML($link); |
||
351 | } |
||
352 | $searchForm->addTagClose('li'); |
||
353 | } |
||
354 | $searchForm->addTagClose('ul'); |
||
355 | |||
356 | $searchForm->addTagClose('div'); |
||
357 | |||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Parse the full text results for their top namespaces below the given base namespace |
||
362 | * |
||
363 | * @param string $baseNS the namespace within which was searched, empty string for root namespace |
||
364 | * |
||
365 | * @return array an associative array with namespace => #number of found pages, sorted descending |
||
366 | */ |
||
367 | protected function getAdditionalNamespacesFromResults($baseNS) |
||
368 | { |
||
369 | $namespaces = []; |
||
370 | $baseNSLength = strlen($baseNS); |
||
371 | foreach ($this->fullTextResults as $page => $numberOfHits) { |
||
372 | $namespace = getNS($page); |
||
373 | if (!$namespace) { |
||
374 | continue; |
||
375 | } |
||
376 | if ($namespace === $baseNS) { |
||
377 | continue; |
||
378 | } |
||
379 | $firstColon = strpos((string)$namespace, ':', $baseNSLength + 1) ?: strlen($namespace); |
||
380 | $subtopNS = substr($namespace, 0, $firstColon); |
||
381 | if (empty($namespaces[$subtopNS])) { |
||
382 | $namespaces[$subtopNS] = 0; |
||
383 | } |
||
384 | $namespaces[$subtopNS] += 1; |
||
385 | } |
||
386 | ksort($namespaces); |
||
387 | arsort($namespaces); |
||
388 | return $namespaces; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * @ToDo: custom date input |
||
393 | * |
||
394 | * @param Form $searchForm |
||
395 | */ |
||
396 | protected function addDateSelector(Form $searchForm) |
||
397 | { |
||
398 | global $INPUT, $lang; |
||
399 | |||
400 | $options = [ |
||
401 | 'any' => [ |
||
402 | 'before' => false, |
||
403 | 'after' => false, |
||
404 | 'label' => $lang['search_any_time'], |
||
405 | ], |
||
406 | 'week' => [ |
||
407 | 'before' => false, |
||
408 | 'after' => '1 week ago', |
||
409 | 'label' => $lang['search_past_7_days'], |
||
410 | ], |
||
411 | 'month' => [ |
||
412 | 'before' => false, |
||
413 | 'after' => '1 month ago', |
||
414 | 'label' => $lang['search_past_month'], |
||
415 | ], |
||
416 | 'year' => [ |
||
417 | 'before' => false, |
||
418 | 'after' => '1 year ago', |
||
419 | 'label' => $lang['search_past_year'], |
||
420 | ], |
||
421 | ]; |
||
422 | $activeOption = 'any'; |
||
423 | foreach ($options as $key => $option) { |
||
424 | if ($INPUT->str('min') === $option['after']) { |
||
425 | $activeOption = $key; |
||
426 | break; |
||
427 | } |
||
428 | } |
||
429 | |||
430 | $searchForm->addTagOpen('div')->addClass('toggle')->attr('aria-haspopup', 'true'); |
||
431 | // render current |
||
432 | $currentWrapper = $searchForm->addTagOpen('div')->addClass('current'); |
||
433 | if ($INPUT->has('max') || $INPUT->has('min')) { |
||
434 | $currentWrapper->addClass('changed'); |
||
435 | } |
||
436 | $searchForm->addHTML($options[$activeOption]['label']); |
||
437 | $searchForm->addTagClose('div'); |
||
438 | |||
439 | // render options list |
||
440 | $searchForm->addTagOpen('ul')->attr('aria-expanded', 'false'); |
||
441 | |||
442 | foreach ($options as $key => $option) { |
||
443 | $listItem = $searchForm->addTagOpen('li'); |
||
444 | |||
445 | if ($key === $activeOption) { |
||
446 | $listItem->addClass('active'); |
||
447 | $searchForm->addHTML($option['label']); |
||
448 | } else { |
||
449 | $link = $this->searchState |
||
450 | ->withTimeLimitations($option['after'], $option['before']) |
||
451 | ->getSearchLink($option['label']) |
||
452 | ; |
||
453 | $searchForm->addHTML($link); |
||
454 | } |
||
455 | $searchForm->addTagClose('li'); |
||
456 | } |
||
457 | $searchForm->addTagClose('ul'); |
||
458 | |||
459 | $searchForm->addTagClose('div'); |
||
460 | } |
||
461 | |||
462 | |||
463 | /** |
||
464 | * Build the intro text for the search page |
||
465 | * |
||
466 | * @param string $query the search query |
||
467 | * |
||
468 | * @return string |
||
469 | */ |
||
470 | protected function getSearchIntroHTML($query) |
||
471 | { |
||
472 | global $lang; |
||
473 | |||
474 | $intro = p_locale_xhtml('searchpage'); |
||
475 | |||
476 | $queryPagename = $this->createPagenameFromQuery($this->parsedQuery); |
||
477 | $createQueryPageLink = html_wikilink($queryPagename . '?do=edit', $queryPagename); |
||
478 | |||
479 | $pagecreateinfo = ''; |
||
480 | if (auth_quickaclcheck($queryPagename) >= AUTH_CREATE) { |
||
481 | $pagecreateinfo = sprintf($lang['searchcreatepage'], $createQueryPageLink); |
||
482 | } |
||
483 | $intro = str_replace( |
||
484 | array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'), |
||
485 | array(hsc(rawurlencode($query)), hsc($query), $pagecreateinfo), |
||
486 | $intro |
||
487 | ); |
||
488 | |||
489 | return $intro; |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Create a pagename based the parsed search query |
||
494 | * |
||
495 | * @param array $parsedQuery |
||
496 | * |
||
497 | * @return string pagename constructed from the parsed query |
||
498 | */ |
||
499 | protected function createPagenameFromQuery($parsedQuery) |
||
500 | { |
||
501 | $pagename = ''; |
||
502 | if (!empty($parsedQuery['ns'])) { |
||
503 | $pagename .= cleanID($parsedQuery['ns'][0]); |
||
504 | } |
||
505 | $pagename .= ':' . cleanID(implode(' ' , $parsedQuery['highlight'])); |
||
506 | return $pagename; |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Build HTML for a list of pages with matching pagenames |
||
511 | * |
||
512 | * @param array $data search results |
||
513 | * |
||
514 | * @return string |
||
515 | */ |
||
516 | protected function getPageLookupHTML($data) |
||
517 | { |
||
518 | if (empty($data)) { |
||
519 | return ''; |
||
520 | } |
||
521 | |||
522 | global $lang; |
||
523 | |||
524 | $html = '<div class="search_quickresult">'; |
||
525 | $html .= '<h2>' . $lang['quickhits'] . ':</h2>'; |
||
526 | $html .= '<ul class="search_quickhits">'; |
||
527 | foreach ($data as $id => $title) { |
||
528 | $name = null; |
||
529 | if (!useHeading('navigation') && $ns = getNS($id)) { |
||
530 | $name = shorten(noNS($id), ' (' . $ns . ')', 30); |
||
531 | } |
||
532 | $link = html_wikilink(':' . $id, $name); |
||
533 | $eventData = [ |
||
534 | 'listItemContent' => [$link], |
||
535 | 'page' => $id, |
||
536 | ]; |
||
537 | trigger_event('SEARCH_RESULT_PAGELOOKUP', $eventData); |
||
538 | $html .= '<li>' . implode('', $eventData['listItemContent']) . '</li>'; |
||
539 | } |
||
540 | $html .= '</ul> '; |
||
541 | //clear float (see http://www.complexspiral.com/publications/containing-floats/) |
||
542 | $html .= '<div class="clearer"></div>'; |
||
543 | $html .= '</div>'; |
||
544 | |||
545 | return $html; |
||
546 | } |
||
547 | |||
548 | /** |
||
549 | * Build HTML for fulltext search results or "no results" message |
||
550 | * |
||
551 | * @param array $data the results of the fulltext search |
||
552 | * @param array $highlight the terms to be highlighted in the results |
||
553 | * |
||
554 | * @return string |
||
555 | */ |
||
556 | protected function getFulltextResultsHTML($data, $highlight) |
||
557 | { |
||
558 | global $lang; |
||
559 | |||
560 | if (empty($data)) { |
||
561 | return '<div class="nothing">' . $lang['nothingfound'] . '</div>'; |
||
562 | } |
||
563 | |||
564 | $html = '<div class="search_fulltextresult">'; |
||
565 | $html .= '<h2>' . $lang['search_fullresults'] . ':</h2>'; |
||
566 | |||
567 | $html .= '<dl class="search_results">'; |
||
568 | $num = 0; |
||
569 | $position = 0; |
||
570 | |||
571 | foreach ($data as $id => $cnt) { |
||
572 | $position += 1; |
||
573 | $resultLink = html_wikilink(':' . $id, null, $highlight); |
||
574 | |||
575 | $resultHeader = [$resultLink]; |
||
576 | |||
577 | |||
578 | $restrictQueryToNSLink = $this->restrictQueryToNSLink(getNS($id)); |
||
579 | if ($restrictQueryToNSLink) { |
||
580 | $resultHeader[] = $restrictQueryToNSLink; |
||
581 | } |
||
582 | |||
583 | $resultBody = []; |
||
584 | $mtime = filemtime(wikiFN($id)); |
||
585 | $lastMod = '<span class="lastmod">' . $lang['lastmod'] . '</span> '; |
||
586 | $lastMod .= '<time datetime="' . date_iso8601($mtime) . '" title="'.dformat($mtime).'">' . dformat($mtime, '%f') . '</time>'; |
||
587 | $resultBody['meta'] = $lastMod; |
||
588 | if ($cnt !== 0) { |
||
589 | $num++; |
||
590 | $hits = '<span class="hits">' . $cnt . ' ' . $lang['hits'] . '</span>, '; |
||
591 | $resultBody['meta'] = $hits . $resultBody['meta']; |
||
592 | if ($num <= FT_SNIPPET_NUMBER) { // create snippets for the first number of matches only |
||
593 | $resultBody['snippet'] = ft_snippet($id, $highlight); |
||
594 | } |
||
595 | } |
||
596 | |||
597 | $eventData = [ |
||
598 | 'resultHeader' => $resultHeader, |
||
599 | 'resultBody' => $resultBody, |
||
600 | 'page' => $id, |
||
601 | 'position' => $position, |
||
602 | ]; |
||
603 | trigger_event('SEARCH_RESULT_FULLPAGE', $eventData); |
||
604 | $html .= '<div class="search_fullpage_result">'; |
||
605 | $html .= '<dt>' . implode(' ', $eventData['resultHeader']) . '</dt>'; |
||
606 | foreach ($eventData['resultBody'] as $class => $htmlContent) { |
||
607 | $html .= "<dd class=\"$class\">$htmlContent</dd>"; |
||
608 | } |
||
609 | $html .= '</div>'; |
||
610 | } |
||
611 | $html .= '</dl>'; |
||
612 | |||
613 | $html .= '</div>'; |
||
614 | |||
615 | return $html; |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * create a link to restrict the current query to a namespace |
||
620 | * |
||
621 | * @param false|string $ns the namespace to which to restrict the query |
||
622 | * |
||
623 | * @return false|string |
||
624 | */ |
||
625 | protected function restrictQueryToNSLink($ns) |
||
640 | } |
||
641 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: