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