| Total Complexity | 45 |
| Total Lines | 188 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like SearchGeneralPage 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.
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 SearchGeneralPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | final class SearchGeneralPage implements RequestHandlerInterface |
||
| 49 | { |
||
| 50 | use ViewResponseTrait; |
||
| 51 | |||
| 52 | public function __construct( |
||
| 56 | } |
||
| 57 | |||
| 58 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 59 | { |
||
| 60 | $tree = Validator::attributes($request)->tree(); |
||
| 61 | |||
| 62 | $query = Validator::queryParams($request)->string('query', ''); |
||
| 63 | |||
| 64 | // What type of records to search? |
||
| 65 | $search_individuals = Validator::queryParams($request)->boolean('search_individuals', false); |
||
| 66 | $search_families = Validator::queryParams($request)->boolean('search_families', false); |
||
| 67 | $search_locations = Validator::queryParams($request)->boolean('search_locations', false); |
||
| 68 | $search_repositories = Validator::queryParams($request)->boolean('search_repositories', false); |
||
| 69 | $search_sources = Validator::queryParams($request)->boolean('search_sources', false); |
||
| 70 | $search_notes = Validator::queryParams($request)->boolean('search_notes', false); |
||
| 71 | |||
| 72 | // Where to search |
||
| 73 | $search_tree_names = Validator::queryParams($request)->array('search_trees'); |
||
| 74 | |||
| 75 | $exist_notes = DB::table('other') |
||
| 76 | ->where('o_file', '=', $tree->id()) |
||
| 77 | ->where('o_type', '=', Note::RECORD_TYPE) |
||
| 78 | ->exists(); |
||
| 79 | |||
| 80 | $exist_locations = DB::table('other') |
||
| 81 | ->where('o_file', '=', $tree->id()) |
||
| 82 | ->where('o_type', '=', Location::RECORD_TYPE) |
||
| 83 | ->exists(); |
||
| 84 | |||
| 85 | $exist_repositories = DB::table('other') |
||
| 86 | ->where('o_file', '=', $tree->id()) |
||
| 87 | ->where('o_type', '=', Repository::RECORD_TYPE) |
||
| 88 | ->exists(); |
||
| 89 | |||
| 90 | $exist_sources = DB::table('sources') |
||
| 91 | ->where('s_file', '=', $tree->id()) |
||
| 92 | ->exists(); |
||
| 93 | |||
| 94 | // Default to families and individuals only |
||
| 95 | if (!$search_individuals && !$search_families && !$search_repositories && !$search_sources && !$search_notes) { |
||
| 96 | $search_families = true; |
||
| 97 | $search_individuals = true; |
||
| 98 | } |
||
| 99 | |||
| 100 | // What to search for? |
||
| 101 | $search_terms = $this->extractSearchTerms($query); |
||
| 102 | |||
| 103 | // What trees to search? |
||
| 104 | if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') { |
||
| 105 | $all_trees = $this->tree_service->all(); |
||
| 106 | } else { |
||
| 107 | $all_trees = new Collection([$tree]); |
||
| 108 | } |
||
| 109 | |||
| 110 | $search_trees = $all_trees |
||
| 111 | ->filter(static fn (Tree $tree): bool => in_array($tree->name(), $search_tree_names, true)); |
||
| 112 | |||
| 113 | if ($search_trees->isEmpty()) { |
||
| 114 | $search_trees->add($tree); |
||
| 115 | } |
||
| 116 | |||
| 117 | // Do the search |
||
| 118 | $individuals = new Collection(); |
||
| 119 | $families = new Collection(); |
||
| 120 | $locations = new Collection(); |
||
| 121 | $repositories = new Collection(); |
||
| 122 | $sources = new Collection(); |
||
| 123 | $notes = new Collection(); |
||
| 124 | |||
| 125 | if ($search_terms !== []) { |
||
| 126 | // Log search requests for visitors |
||
| 127 | if (Auth::id() === null) { |
||
| 128 | Log::addSearchLog('General: ' . $query, $search_trees->all()); |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($search_individuals) { |
||
| 132 | $individuals = $this->search_service->searchIndividuals($search_trees->all(), $search_terms); |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($search_families) { |
||
| 136 | $tmp1 = $this->search_service->searchFamilies($search_trees->all(), $search_terms); |
||
| 137 | $tmp2 = $this->search_service->searchFamilyNames($search_trees->all(), $search_terms); |
||
| 138 | |||
| 139 | $families = $tmp1->merge($tmp2)->unique(static fn (Family $family): string => $family->xref() . '@' . $family->tree()->id()); |
||
| 140 | } |
||
| 141 | |||
| 142 | if ($search_repositories) { |
||
| 143 | $repositories = $this->search_service->searchRepositories($search_trees->all(), $search_terms); |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($search_sources) { |
||
| 147 | $sources = $this->search_service->searchSources($search_trees->all(), $search_terms); |
||
| 148 | } |
||
| 149 | |||
| 150 | if ($search_notes) { |
||
| 151 | $notes = $this->search_service->searchNotes($search_trees->all(), $search_terms); |
||
| 152 | } |
||
| 153 | |||
| 154 | if ($search_locations) { |
||
| 155 | $locations = $this->search_service->searchLocations($search_trees->all(), $search_terms); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | // If only 1 item is returned, automatically forward to that item |
||
| 160 | if ($individuals->count() === 1 && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { |
||
| 161 | return redirect($individuals->first()->url()); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($individuals->isEmpty() && $families->count() === 1 && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { |
||
| 165 | return redirect($families->first()->url()); |
||
| 166 | } |
||
| 167 | |||
| 168 | if ($individuals->isEmpty() && $families->isEmpty() && $sources->count() === 1 && $notes->isEmpty() && $locations->isEmpty()) { |
||
| 169 | return redirect($sources->first()->url()); |
||
| 170 | } |
||
| 171 | |||
| 172 | if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->count() === 1 && $locations->isEmpty()) { |
||
| 173 | return redirect($notes->first()->url()); |
||
| 174 | } |
||
| 175 | |||
| 176 | if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->count() === 1) { |
||
| 177 | return redirect($locations->first()->url()); |
||
| 178 | } |
||
| 179 | |||
| 180 | $title = I18N::translate('General search'); |
||
| 181 | |||
| 182 | return $this->viewResponse('search-general-page', [ |
||
| 183 | 'all_trees' => $all_trees, |
||
| 184 | 'exist_locations' => $exist_locations, |
||
| 185 | 'exist_notes' => $exist_notes, |
||
| 186 | 'exist_repositories' => $exist_repositories, |
||
| 187 | 'exist_sources' => $exist_sources, |
||
| 188 | 'families' => $families, |
||
| 189 | 'individuals' => $individuals, |
||
| 190 | 'locations' => $locations, |
||
| 191 | 'notes' => $notes, |
||
| 192 | 'query' => $query, |
||
| 193 | 'repositories' => $repositories, |
||
| 194 | 'search_families' => $search_families, |
||
| 195 | 'search_individuals' => $search_individuals, |
||
| 196 | 'search_locations' => $search_locations, |
||
| 197 | 'search_notes' => $search_notes, |
||
| 198 | 'search_repositories' => $search_repositories, |
||
| 199 | 'search_sources' => $search_sources, |
||
| 200 | 'search_trees' => $search_trees, |
||
| 201 | 'sources' => $sources, |
||
| 202 | 'title' => $title, |
||
| 203 | 'tree' => $tree, |
||
| 204 | ]); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Convert the query into an array of search terms |
||
| 209 | * |
||
| 210 | * @param string $query |
||
| 211 | * |
||
| 212 | * @return array<string> |
||
| 213 | */ |
||
| 214 | private function extractSearchTerms(string $query): array |
||
| 236 | } |
||
| 237 | } |
||
| 238 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths