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