Conditions | 63 |
Paths | > 20000 |
Total Lines | 348 |
Code Lines | 266 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
155 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
156 | { |
||
157 | $tree = Validator::attributes($request)->tree(); |
||
158 | $user = Validator::attributes($request)->user(); |
||
159 | |||
160 | Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user); |
||
161 | |||
162 | // All individuals with this surname |
||
163 | $surname_param = Validator::queryParams($request)->string('surname', ''); |
||
164 | $surname = I18N::strtoupper(I18N::language()->normalize($surname_param)); |
||
165 | |||
166 | // All surnames beginning with this letter, where "@" is unknown and "," is none |
||
167 | $alpha = Validator::queryParams($request)->string('alpha', ''); |
||
168 | |||
169 | // All first names beginning with this letter where "@" is unknown |
||
170 | $falpha = Validator::queryParams($request)->string('falpha', ''); |
||
171 | |||
172 | // What type of list to display, if any |
||
173 | $show = Validator::queryParams($request)->string('show', 'surn'); |
||
174 | |||
175 | // All individuals |
||
176 | $show_all = Validator::queryParams($request)->string('show_all', ''); |
||
177 | |||
178 | // Include/exclude married names |
||
179 | $show_marnm = Validator::queryParams($request)->string('show_marnm', ''); |
||
180 | |||
181 | // Break long lists down by given name |
||
182 | $show_all_firstnames = Validator::queryParams($request)->string('show_all_firstnames', ''); |
||
183 | |||
184 | $params = [ |
||
185 | 'alpha' => $alpha, |
||
186 | 'falpha' => $falpha, |
||
187 | 'show' => $show, |
||
188 | 'show_all' => $show_all, |
||
189 | 'show_all_firstnames' => $show_all_firstnames, |
||
190 | 'show_marnm' => $show_marnm, |
||
191 | 'surname' => $surname, |
||
192 | ]; |
||
193 | |||
194 | if ($surname_param !== $surname) { |
||
195 | return Registry::responseFactory() |
||
196 | ->redirectUrl($this->listUrl($tree, $params), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); |
||
197 | } |
||
198 | |||
199 | // Make sure parameters are consistent with each other. |
||
200 | if ($show_all_firstnames === 'yes') { |
||
201 | $falpha = ''; |
||
202 | } |
||
203 | |||
204 | if ($show_all === 'yes') { |
||
205 | $alpha = ''; |
||
206 | $surname = ''; |
||
207 | } |
||
208 | |||
209 | if ($surname !== '') { |
||
210 | $alpha = I18N::language()->initialLetter($surname); |
||
211 | } |
||
212 | |||
213 | $surname_data = $this->surnameData($tree, $show_marnm === 'yes', $this->families); |
||
214 | $all_surns = $this->allSurns($surname_data); |
||
215 | $all_surnames = $this->allSurnames($surname_data); |
||
216 | $surname_initials = $this->surnameInitials($surname_data); |
||
217 | |||
218 | // We've requested a surname that doesn't currently exist. |
||
219 | if ($surname !== '' && !array_key_exists($surname, $all_surns)) { |
||
220 | $message = I18N::translate('There are no individuals with the surname “%s”', e($surname)); |
||
221 | FlashMessages::addMessage($message); |
||
222 | |||
223 | return Registry::responseFactory() |
||
224 | ->redirectUrl($this->listUrl($tree)); |
||
225 | } |
||
226 | |||
227 | // Make sure selections are consistent. |
||
228 | // i.e. can’t specify show_all and surname at the same time. |
||
229 | if ($show_all === 'yes') { |
||
230 | if ($show_all_firstnames === 'yes') { |
||
231 | $legend = I18N::translate('All'); |
||
232 | $params = ['tree' => $tree->name(), 'show_all' => 'yes', 'show_marnm' => $show_marnm]; |
||
233 | $show = 'indi'; |
||
234 | } elseif ($falpha !== '') { |
||
235 | $legend = I18N::translate('All') . ', ' . e($falpha) . '…'; |
||
236 | $params = ['tree' => $tree->name(), 'show_all' => 'yes', 'show_marnm' => $show_marnm]; |
||
237 | $show = 'indi'; |
||
238 | } else { |
||
239 | $legend = I18N::translate('All'); |
||
240 | $params = ['tree' => $tree->name(), 'show_all' => 'yes', 'show_marnm' => $show_marnm]; |
||
241 | } |
||
242 | } elseif ($surname !== '') { |
||
243 | $show_all = 'no'; |
||
244 | if ($surname === Individual::NOMEN_NESCIO) { |
||
245 | $legend = I18N::translateContext('Unknown surname', '…'); |
||
246 | $show = 'indi'; // The surname list makes no sense with only one surname. |
||
247 | } else { |
||
248 | // The surname parameter is a root/canonical form. Display the actual surnames found. |
||
249 | $variants = array_keys($all_surnames[$surname] ?? [$surname => $surname]); |
||
250 | usort($variants, I18N::comparator()); |
||
251 | $variants = array_map(static fn (string $x): string => $x === '' ? I18N::translate('No surname') : $x, $variants); |
||
252 | $legend = implode('/', $variants); |
||
253 | $show = 'indi'; // The surname list makes no sense with only one surname. |
||
254 | } |
||
255 | $params = ['tree' => $tree->name(), 'surname' => $surname, 'falpha' => $falpha, 'show_marnm' => $show_marnm]; |
||
256 | switch ($falpha) { |
||
257 | case '': |
||
258 | break; |
||
259 | case '@': |
||
260 | $legend .= ', ' . I18N::translateContext('Unknown given name', '…'); |
||
261 | break; |
||
262 | default: |
||
263 | $legend .= ', ' . e($falpha) . '…'; |
||
264 | break; |
||
265 | } |
||
266 | } elseif ($alpha === '@') { |
||
267 | $show_all = 'no'; |
||
268 | $legend = I18N::translateContext('Unknown surname', '…'); |
||
269 | $params = ['alpha' => $alpha, 'tree' => $tree->name(), 'show_marnm' => $show_marnm]; |
||
270 | $surname = Individual::NOMEN_NESCIO; |
||
271 | $show = 'indi'; // SURN list makes no sense here |
||
272 | } elseif ($alpha === ',') { |
||
273 | $show_all = 'no'; |
||
274 | $legend = I18N::translate('No surname'); |
||
275 | $params = ['alpha' => $alpha, 'tree' => $tree->name(), 'show_marnm' => $show_marnm]; |
||
276 | $show = 'indi'; // SURN list makes no sense here |
||
277 | } elseif ($alpha !== '') { |
||
278 | $show_all = 'no'; |
||
279 | $legend = e($alpha) . '…'; |
||
280 | $params = ['alpha' => $alpha, 'tree' => $tree->name(), 'show_marnm' => $show_marnm]; |
||
281 | } else { |
||
282 | $show_all = 'no'; |
||
283 | $legend = '…'; |
||
284 | $params = ['tree' => $tree->name(), 'show_marnm' => $show_marnm]; |
||
285 | $show = 'none'; // Don't show lists until something is chosen |
||
286 | } |
||
287 | $legend = '<bdi>' . $legend . '</bdi>'; |
||
288 | |||
289 | if ($this->families) { |
||
290 | $title = I18N::translate('Families') . ' — ' . $legend; |
||
291 | } else { |
||
292 | $title = I18N::translate('Individuals') . ' — ' . $legend; |
||
293 | } |
||
294 | |||
295 | ob_start(); ?> |
||
296 | <div class="d-flex flex-column wt-page-options wt-page-options-individual-list d-print-none"> |
||
297 | <ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-surname"> |
||
298 | |||
299 | <?php foreach ($surname_initials as $letter => $count) : ?> |
||
300 | <li class="wt-initials-list-item d-flex"> |
||
301 | <?php if ($count > 0) : ?> |
||
302 | <a href="<?= e($this->listUrl($tree, ['alpha' => $letter, 'show_marnm' => $show_marnm, 'tree' => $tree->name()])) ?>" class="wt-initial px-1<?= $letter === $alpha ? ' active' : '' ?> '" title="<?= I18N::number($count) ?>"><?= $this->displaySurnameInitial((string) $letter) ?></a> |
||
303 | <?php else : ?> |
||
304 | <span class="wt-initial px-1 text-muted"><?= $this->displaySurnameInitial((string) $letter) ?></span> |
||
305 | |||
306 | <?php endif ?> |
||
307 | </li> |
||
308 | <?php endforeach ?> |
||
309 | |||
310 | <?php if (Session::has('initiated')) : ?> |
||
311 | <!-- Search spiders don't get the "show all" option as the other links give them everything. --> |
||
312 | <li class="wt-initials-list-item d-flex"> |
||
313 | <a class="wt-initial px-1<?= $show_all === 'yes' ? ' active' : '' ?>" href="<?= e($this->listUrl($tree, ['show_all' => 'yes'] + $params)) ?>"><?= I18N::translate('All') ?></a> |
||
314 | </li> |
||
315 | <?php endif ?> |
||
316 | </ul> |
||
317 | |||
318 | <!-- Search spiders don't get an option to show/hide the surname sublists, nor does it make sense on the all/unknown/surname views --> |
||
319 | <?php if ($show !== 'none' && Session::has('initiated')) : ?> |
||
320 | <?php if ($show_marnm === 'yes') : ?> |
||
321 | <p> |
||
322 | <a href="<?= e($this->listUrl($tree, ['show' => $show, 'show_marnm' => 'no'] + $params)) ?>"> |
||
323 | <?= I18N::translate('Exclude individuals with “%s” as a married name', $legend) ?> |
||
324 | </a> |
||
325 | </p> |
||
326 | <?php else : ?> |
||
327 | <p> |
||
328 | <a href="<?= e($this->listUrl($tree, ['show' => $show, 'show_marnm' => 'yes'] + $params)) ?>"> |
||
329 | <?= I18N::translate('Include individuals with “%s” as a married name', $legend) ?> |
||
330 | </a> |
||
331 | </p> |
||
332 | <?php endif ?> |
||
333 | |||
334 | <?php if ($alpha !== '@' && $alpha !== ',' && $surname === '') : ?> |
||
335 | <?php if ($show === 'surn') : ?> |
||
336 | <p> |
||
337 | <a href="<?= e($this->listUrl($tree, ['show' => 'indi'] + $params)) ?>"> |
||
338 | <?= I18N::translate('Show the list of individuals') ?> |
||
339 | </a> |
||
340 | </p> |
||
341 | <?php else : ?> |
||
342 | <p> |
||
343 | <a href="<?= e($this->listUrl($tree, ['show' => 'surn'] + $params)) ?>"> |
||
344 | <?= I18N::translate('Show the list of surnames') ?> |
||
345 | </a> |
||
346 | </p> |
||
347 | <?php endif ?> |
||
348 | <?php endif ?> |
||
349 | <?php endif ?> |
||
350 | </div> |
||
351 | |||
352 | <div class="wt-page-content"> |
||
353 | <?php |
||
354 | if ($show === 'indi' || $show === 'surn') { |
||
355 | switch ($alpha) { |
||
356 | case '@': |
||
357 | $filter = static fn (string $x): bool => $x === Individual::NOMEN_NESCIO; |
||
358 | break; |
||
359 | case ',': |
||
360 | $filter = static fn (string $x): bool => $x === ''; |
||
361 | break; |
||
362 | case '': |
||
363 | if ($show_all === 'yes') { |
||
364 | $filter = static fn (string $x): bool => $x !== '' && $x !== Individual::NOMEN_NESCIO; |
||
365 | } else { |
||
366 | $filter = static fn (string $x): bool => $x === $surname; |
||
367 | } |
||
368 | break; |
||
369 | default: |
||
370 | if ($surname === '') { |
||
371 | $filter = static fn (string $x): bool => I18N::language()->initialLetter($x) === $alpha; |
||
372 | } else { |
||
373 | $filter = static fn (string $x): bool => $x === $surname; |
||
374 | } |
||
375 | break; |
||
376 | } |
||
377 | |||
378 | $all_surnames = array_filter($all_surnames, $filter, ARRAY_FILTER_USE_KEY); |
||
379 | |||
380 | if ($show === 'surn') { |
||
381 | // Show the surname list |
||
382 | switch ($tree->getPreference('SURNAME_LIST_STYLE')) { |
||
383 | case 'style1': |
||
384 | echo view('lists/surnames-column-list', [ |
||
385 | 'module' => $this, |
||
386 | 'params' => ['show' => 'indi', 'show_all' => null] + $params, |
||
387 | 'surnames' => $all_surnames, |
||
388 | 'totals' => true, |
||
389 | 'tree' => $tree, |
||
390 | ]); |
||
391 | break; |
||
392 | case 'style3': |
||
393 | echo view('lists/surnames-tag-cloud', [ |
||
394 | 'module' => $this, |
||
395 | 'params' => ['show' => 'indi', 'show_all' => null] + $params, |
||
396 | 'surnames' => $all_surnames, |
||
397 | 'totals' => true, |
||
398 | 'tree' => $tree, |
||
399 | ]); |
||
400 | break; |
||
401 | case 'style2': |
||
402 | default: |
||
403 | echo view('lists/surnames-table', [ |
||
404 | 'families' => $this->families, |
||
405 | 'module' => $this, |
||
406 | 'order' => [[0, 'asc']], |
||
407 | 'params' => ['show' => 'indi', 'show_all' => null] + $params, |
||
408 | 'surnames' => $all_surnames, |
||
409 | 'tree' => $tree, |
||
410 | ]); |
||
411 | break; |
||
412 | } |
||
413 | } else { |
||
414 | // Show the list |
||
415 | $count = array_sum(array_map(static fn (array $x): int => array_sum($x), $all_surnames)); |
||
416 | |||
417 | // Don't sublist short lists. |
||
418 | $sublist_threshold = (int) $tree->getPreference('SUBLIST_TRIGGER_I'); |
||
419 | if ($sublist_threshold === 0 || $count < $sublist_threshold) { |
||
420 | $falpha = ''; |
||
421 | } else { |
||
422 | // Break long lists by initial letter of given name |
||
423 | $all_surnames = array_values(array_map(static fn ($x): array => array_keys($x), $all_surnames)); |
||
424 | $all_surnames = array_merge(...$all_surnames); |
||
425 | $givn_initials = $this->givenNameInitials($tree, $all_surnames, $show_marnm === 'yes', $this->families); |
||
426 | |||
427 | if ($surname !== '' || $show_all === 'yes') { |
||
428 | if ($show_all !== 'yes') { |
||
429 | echo '<h2 class="wt-page-title">', I18N::translate('Individuals with surname %s', $legend), '</h2>'; |
||
430 | } |
||
431 | // Don't show the list until we have some filter criteria |
||
432 | $show = $falpha !== '' || $show_all_firstnames === 'yes' ? 'indi' : 'none'; |
||
433 | echo '<ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-given-names">'; |
||
434 | foreach ($givn_initials as $givn_initial => $given_count) { |
||
435 | echo '<li class="wt-initials-list-item d-flex">'; |
||
436 | if ($given_count > 0) { |
||
437 | if ($show === 'indi' && $givn_initial === $falpha && $show_all_firstnames !== 'yes') { |
||
438 | echo '<a class="wt-initial px-1 active" href="' . e($this->listUrl($tree, ['falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->displayGivenNameInitial((string) $givn_initial) . '</a>'; |
||
439 | } else { |
||
440 | echo '<a class="wt-initial px-1" href="' . e($this->listUrl($tree, ['falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->displayGivenNameInitial((string) $givn_initial) . '</a>'; |
||
441 | } |
||
442 | } else { |
||
443 | echo '<span class="wt-initial px-1 text-muted">' . $this->displayGivenNameInitial((string) $givn_initial) . '</span>'; |
||
444 | } |
||
445 | echo '</li>'; |
||
446 | } |
||
447 | // Search spiders don't get the "show all" option as the other links give them everything. |
||
448 | if (Session::has('initiated')) { |
||
449 | echo '<li class="wt-initials-list-item d-flex">'; |
||
450 | if ($show_all_firstnames === 'yes') { |
||
451 | echo '<span class="wt-initial px-1 active">' . I18N::translate('All') . '</span>'; |
||
452 | } else { |
||
453 | echo '<a class="wt-initial px-1" href="' . e($this->listUrl($tree, ['show_all_firstnames' => 'yes'] + $params)) . '" title="' . I18N::number($count) . '">' . I18N::translate('All') . '</a>'; |
||
454 | } |
||
455 | echo '</li>'; |
||
456 | } |
||
457 | echo '</ul>'; |
||
458 | } |
||
459 | } |
||
460 | if ($show === 'indi') { |
||
461 | if ($alpha === '@') { |
||
462 | $surns_to_show = ['@N.N.']; |
||
463 | } elseif ($alpha === ',') { |
||
464 | $surns_to_show = ['']; |
||
465 | } elseif ($surname !== '') { |
||
466 | $surns_to_show = $all_surns[$surname]; |
||
467 | } elseif ($alpha !== '') { |
||
468 | $tmp = array_filter( |
||
469 | $all_surns, |
||
470 | static fn (string $x): bool => I18N::language()->initialLetter($x) === $alpha, |
||
471 | ARRAY_FILTER_USE_KEY |
||
472 | ); |
||
473 | |||
474 | $surns_to_show = array_merge(...array_values($tmp)); |
||
475 | } else { |
||
476 | $surns_to_show = []; |
||
477 | } |
||
478 | |||
479 | if ($this->families) { |
||
480 | echo view('lists/families-table', [ |
||
481 | 'families' => $this->families($tree, $surns_to_show, $falpha, $show_marnm === 'yes'), |
||
482 | 'tree' => $tree, |
||
483 | ]); |
||
484 | } else { |
||
485 | echo view('lists/individuals-table', [ |
||
486 | 'individuals' => $this->individuals($tree, $surns_to_show, $falpha, $show_marnm === 'yes', false), |
||
487 | 'sosa' => false, |
||
488 | 'tree' => $tree, |
||
489 | ]); |
||
490 | } |
||
491 | } |
||
492 | } |
||
493 | } ?> |
||
494 | </div> |
||
495 | <?php |
||
496 | |||
497 | $html = ob_get_clean(); |
||
498 | |||
499 | return $this->viewResponse('modules/individual-list/page', [ |
||
500 | 'content' => $html, |
||
501 | 'title' => $title, |
||
502 | 'tree' => $tree, |
||
503 | ]); |
||
826 |