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