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