Conditions | 41 |
Paths | 6192 |
Total Lines | 146 |
Code Lines | 92 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | ]); |
||
256 |