Conditions | 30 |
Paths | 1320 |
Total Lines | 107 |
Code Lines | 64 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
71 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
72 | { |
||
73 | $tree = $request->getAttribute('tree'); |
||
74 | assert($tree instanceof Tree, new InvalidArgumentException()); |
||
75 | |||
76 | $params = $request->getQueryParams(); |
||
77 | $query = $params['query'] ?? ''; |
||
78 | |||
79 | // What type of records to search? |
||
80 | $search_individuals = (bool) ($params['search_individuals'] ?? false); |
||
81 | $search_families = (bool) ($params['search_families'] ?? false); |
||
82 | $search_repositories = (bool) ($params['search_repositories'] ?? false); |
||
83 | $search_sources = (bool) ($params['search_sources'] ?? false); |
||
84 | $search_notes = (bool) ($params['search_notes'] ?? false); |
||
85 | |||
86 | // Default to individuals only |
||
87 | if (!$search_individuals && !$search_families && !$search_repositories && !$search_sources && !$search_notes) { |
||
88 | $search_individuals = true; |
||
89 | } |
||
90 | |||
91 | // What to search for? |
||
92 | $search_terms = $this->extractSearchTerms($query); |
||
93 | |||
94 | // What trees to seach? |
||
95 | if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') { |
||
96 | $all_trees = $this->tree_service->all()->all(); |
||
97 | } else { |
||
98 | $all_trees = [$tree]; |
||
99 | } |
||
100 | |||
101 | $search_tree_names = $params['search_trees'] ?? []; |
||
102 | |||
103 | $search_trees = array_filter($all_trees, static function (Tree $tree) use ($search_tree_names): bool { |
||
104 | return in_array($tree->name(), $search_tree_names, true); |
||
105 | }); |
||
106 | |||
107 | if ($search_trees === []) { |
||
108 | $search_trees = [$tree]; |
||
109 | } |
||
110 | |||
111 | // Do the search |
||
112 | $individuals = new Collection(); |
||
113 | $families = new Collection(); |
||
114 | $repositories = new Collection(); |
||
115 | $sources = new Collection(); |
||
116 | $notes = new Collection(); |
||
117 | |||
118 | if ($search_terms !== []) { |
||
119 | if ($search_individuals) { |
||
120 | $individuals = $this->search_service->searchIndividuals($search_trees, $search_terms); |
||
121 | } |
||
122 | |||
123 | if ($search_families) { |
||
124 | $tmp1 = $this->search_service->searchFamilies($search_trees, $search_terms); |
||
125 | $tmp2 = $this->search_service->searchFamilyNames($search_trees, $search_terms); |
||
126 | |||
127 | $families = $tmp1->merge($tmp2)->unique(); |
||
128 | } |
||
129 | |||
130 | if ($search_repositories) { |
||
131 | $repositories = $this->search_service->searchRepositories($search_trees, $search_terms); |
||
132 | } |
||
133 | |||
134 | if ($search_sources) { |
||
135 | $sources = $this->search_service->searchSources($search_trees, $search_terms); |
||
136 | } |
||
137 | |||
138 | if ($search_notes) { |
||
139 | $notes = $this->search_service->searchNotes($search_trees, $search_terms); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | // If only 1 item is returned, automatically forward to that item |
||
144 | if ($individuals->count() === 1 && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty()) { |
||
145 | return redirect($individuals->first()->url()); |
||
146 | } |
||
147 | |||
148 | if ($individuals->isEmpty() && $families->count() === 1 && $sources->isEmpty() && $notes->isEmpty()) { |
||
149 | return redirect($families->first()->url()); |
||
150 | } |
||
151 | |||
152 | if ($individuals->isEmpty() && $families->isEmpty() && $sources->count() === 1 && $notes->isEmpty()) { |
||
153 | return redirect($sources->first()->url()); |
||
154 | } |
||
155 | |||
156 | if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->count() === 1) { |
||
157 | return redirect($notes->first()->url()); |
||
158 | } |
||
159 | |||
160 | $title = I18N::translate('General search'); |
||
161 | |||
162 | return $this->viewResponse('search-general-page', [ |
||
163 | 'all_trees' => $all_trees, |
||
164 | 'families' => $families, |
||
165 | 'individuals' => $individuals, |
||
166 | 'notes' => $notes, |
||
167 | 'query' => $query, |
||
168 | 'repositories' => $repositories, |
||
169 | 'search_families' => $search_families, |
||
170 | 'search_individuals' => $search_individuals, |
||
171 | 'search_notes' => $search_notes, |
||
172 | 'search_repositories' => $search_repositories, |
||
173 | 'search_sources' => $search_sources, |
||
174 | 'search_trees' => $search_trees, |
||
175 | 'sources' => $sources, |
||
176 | 'title' => $title, |
||
177 | 'tree' => $tree, |
||
178 | ]); |
||
207 |