| Total Complexity | 45 |
| Total Lines | 203 |
| 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 |
||
| 51 | class SearchGeneralPage implements RequestHandlerInterface |
||
| 52 | { |
||
| 53 | use ViewResponseTrait; |
||
| 54 | |||
| 55 | private SearchService $search_service; |
||
| 56 | |||
| 57 | private TreeService $tree_service; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param SearchService $search_service |
||
| 61 | * @param TreeService $tree_service |
||
| 62 | */ |
||
| 63 | public function __construct(SearchService $search_service, TreeService $tree_service) |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The standard search. |
||
| 71 | * |
||
| 72 | * @param ServerRequestInterface $request |
||
| 73 | * |
||
| 74 | * @return ResponseInterface |
||
| 75 | */ |
||
| 76 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 77 | { |
||
| 78 | $tree = Validator::attributes($request)->tree(); |
||
| 79 | |||
| 80 | $query = Validator::queryParams($request)->string('query', ''); |
||
| 81 | |||
| 82 | // What type of records to search? |
||
| 83 | $search_individuals = Validator::queryParams($request)->boolean('search_individuals', false); |
||
| 84 | $search_families = Validator::queryParams($request)->boolean('search_families', false); |
||
| 85 | $search_locations = Validator::queryParams($request)->boolean('search_locations', false); |
||
| 86 | $search_repositories = Validator::queryParams($request)->boolean('search_repositories', false); |
||
| 87 | $search_sources = Validator::queryParams($request)->boolean('search_sources', false); |
||
| 88 | $search_notes = Validator::queryParams($request)->boolean('search_notes', false); |
||
| 89 | |||
| 90 | // Where to search |
||
| 91 | $search_tree_names = Validator::queryParams($request)->array('search_trees'); |
||
| 92 | |||
| 93 | $exist_notes = DB::table('other') |
||
| 94 | ->where('o_file', '=', $tree->id()) |
||
| 95 | ->where('o_type', '=', Note::RECORD_TYPE) |
||
| 96 | ->exists(); |
||
| 97 | |||
| 98 | $exist_locations = DB::table('other') |
||
| 99 | ->where('o_file', '=', $tree->id()) |
||
| 100 | ->where('o_type', '=', Location::RECORD_TYPE) |
||
| 101 | ->exists(); |
||
| 102 | |||
| 103 | $exist_repositories = DB::table('other') |
||
| 104 | ->where('o_file', '=', $tree->id()) |
||
| 105 | ->where('o_type', '=', Repository::RECORD_TYPE) |
||
| 106 | ->exists(); |
||
| 107 | |||
| 108 | $exist_sources = DB::table('sources') |
||
| 109 | ->where('s_file', '=', $tree->id()) |
||
| 110 | ->exists(); |
||
| 111 | |||
| 112 | // Default to families and individuals only |
||
| 113 | if (!$search_individuals && !$search_families && !$search_repositories && !$search_sources && !$search_notes) { |
||
| 114 | $search_families = true; |
||
| 115 | $search_individuals = true; |
||
| 116 | } |
||
| 117 | |||
| 118 | // What to search for? |
||
| 119 | $search_terms = $this->extractSearchTerms($query); |
||
| 120 | |||
| 121 | // What trees to search? |
||
| 122 | if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') { |
||
| 123 | $all_trees = $this->tree_service->all(); |
||
| 124 | } else { |
||
| 125 | $all_trees = new Collection([$tree]); |
||
| 126 | } |
||
| 127 | |||
| 128 | $search_trees = $all_trees |
||
| 129 | ->filter(static fn (Tree $tree): bool => in_array($tree->name(), $search_tree_names, true)); |
||
| 130 | |||
| 131 | if ($search_trees->isEmpty()) { |
||
| 132 | $search_trees->add($tree); |
||
| 133 | } |
||
| 134 | |||
| 135 | // Do the search |
||
| 136 | $individuals = new Collection(); |
||
| 137 | $families = new Collection(); |
||
| 138 | $locations = new Collection(); |
||
| 139 | $repositories = new Collection(); |
||
| 140 | $sources = new Collection(); |
||
| 141 | $notes = new Collection(); |
||
| 142 | |||
| 143 | if ($search_terms !== []) { |
||
| 144 | // Log search requests for visitors |
||
| 145 | if (Auth::id() === null) { |
||
| 146 | Log::addSearchLog('General: ' . $query, $search_trees->all()); |
||
| 147 | } |
||
| 148 | |||
| 149 | if ($search_individuals) { |
||
| 150 | $individuals = $this->search_service->searchIndividuals($search_trees->all(), $search_terms); |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($search_families) { |
||
| 154 | $tmp1 = $this->search_service->searchFamilies($search_trees->all(), $search_terms); |
||
| 155 | $tmp2 = $this->search_service->searchFamilyNames($search_trees->all(), $search_terms); |
||
| 156 | |||
| 157 | $families = $tmp1->merge($tmp2)->unique(static fn (Family $family): string => $family->xref() . '@' . $family->tree()->id()); |
||
| 158 | } |
||
| 159 | |||
| 160 | if ($search_repositories) { |
||
| 161 | $repositories = $this->search_service->searchRepositories($search_trees->all(), $search_terms); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($search_sources) { |
||
| 165 | $sources = $this->search_service->searchSources($search_trees->all(), $search_terms); |
||
| 166 | } |
||
| 167 | |||
| 168 | if ($search_notes) { |
||
| 169 | $notes = $this->search_service->searchNotes($search_trees->all(), $search_terms); |
||
| 170 | } |
||
| 171 | |||
| 172 | if ($search_locations) { |
||
| 173 | $locations = $this->search_service->searchLocations($search_trees->all(), $search_terms); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | // If only 1 item is returned, automatically forward to that item |
||
| 178 | if ($individuals->count() === 1 && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { |
||
| 179 | return redirect($individuals->first()->url()); |
||
| 180 | } |
||
| 181 | |||
| 182 | if ($individuals->isEmpty() && $families->count() === 1 && $sources->isEmpty() && $notes->isEmpty() && $locations->isEmpty()) { |
||
| 183 | return redirect($families->first()->url()); |
||
| 184 | } |
||
| 185 | |||
| 186 | if ($individuals->isEmpty() && $families->isEmpty() && $sources->count() === 1 && $notes->isEmpty() && $locations->isEmpty()) { |
||
| 187 | return redirect($sources->first()->url()); |
||
| 188 | } |
||
| 189 | |||
| 190 | if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->count() === 1 && $locations->isEmpty()) { |
||
| 191 | return redirect($notes->first()->url()); |
||
| 192 | } |
||
| 193 | |||
| 194 | if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty() && $locations->count() === 1) { |
||
| 195 | return redirect($locations->first()->url()); |
||
| 196 | } |
||
| 197 | |||
| 198 | $title = I18N::translate('General search'); |
||
| 199 | |||
| 200 | return $this->viewResponse('search-general-page', [ |
||
| 201 | 'all_trees' => $all_trees, |
||
| 202 | 'exist_locations' => $exist_locations, |
||
| 203 | 'exist_notes' => $exist_notes, |
||
| 204 | 'exist_repositories' => $exist_repositories, |
||
| 205 | 'exist_sources' => $exist_sources, |
||
| 206 | 'families' => $families, |
||
| 207 | 'individuals' => $individuals, |
||
| 208 | 'locations' => $locations, |
||
| 209 | 'notes' => $notes, |
||
| 210 | 'query' => $query, |
||
| 211 | 'repositories' => $repositories, |
||
| 212 | 'search_families' => $search_families, |
||
| 213 | 'search_individuals' => $search_individuals, |
||
| 214 | 'search_locations' => $search_locations, |
||
| 215 | 'search_notes' => $search_notes, |
||
| 216 | 'search_repositories' => $search_repositories, |
||
| 217 | 'search_sources' => $search_sources, |
||
| 218 | 'search_trees' => $search_trees, |
||
| 219 | 'sources' => $sources, |
||
| 220 | 'title' => $title, |
||
| 221 | 'tree' => $tree, |
||
| 222 | ]); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Convert the query into an array of search terms |
||
| 227 | * |
||
| 228 | * @param string $query |
||
| 229 | * |
||
| 230 | * @return array<string> |
||
| 231 | */ |
||
| 232 | private function extractSearchTerms(string $query): array |
||
| 254 | } |
||
| 255 | } |
||
| 256 |
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