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