| Total Complexity | 45 |
| Total Lines | 359 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like IndividualPage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IndividualPage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 71 | class IndividualPage implements RequestHandlerInterface |
||
| 72 | { |
||
| 73 | use ViewResponseTrait; |
||
| 74 | |||
| 75 | /** @var ClipboardService */ |
||
| 76 | private $clipboard_service; |
||
| 77 | |||
| 78 | /** @var ModuleService */ |
||
| 79 | private $module_service; |
||
| 80 | |||
| 81 | /** @var UserService */ |
||
| 82 | private $user_service; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * IndividualPage constructor. |
||
| 86 | * |
||
| 87 | * @param ClipboardService $clipboard_service |
||
| 88 | * @param ModuleService $module_service |
||
| 89 | * @param UserService $user_service |
||
| 90 | */ |
||
| 91 | public function __construct(ClipboardService $clipboard_service, ModuleService $module_service, UserService $user_service) |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param ServerRequestInterface $request |
||
| 100 | * |
||
| 101 | * @return ResponseInterface |
||
| 102 | */ |
||
| 103 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 104 | { |
||
| 105 | $tree = $request->getAttribute('tree'); |
||
| 106 | assert($tree instanceof Tree); |
||
| 107 | |||
| 108 | $xref = $request->getAttribute('xref'); |
||
| 109 | assert(is_string($xref)); |
||
| 110 | |||
| 111 | $individual = Individual::getInstance($xref, $tree); |
||
| 112 | $individual = Auth::checkIndividualAccess($individual); |
||
| 113 | |||
| 114 | // Redirect to correct xref/slug |
||
| 115 | if ($individual->xref() !== $xref || $request->getAttribute('slug') !== $individual->slug()) { |
||
| 116 | return redirect($individual->url(), StatusCodeInterface::STATUS_MOVED_PERMANENTLY); |
||
| 117 | } |
||
| 118 | |||
| 119 | // What is (was) the age of the individual |
||
| 120 | $bdate = $individual->getBirthDate(); |
||
| 121 | $ddate = $individual->getDeathDate(); |
||
| 122 | |||
| 123 | if ($individual->isDead()) { |
||
| 124 | // If dead, show age at death |
||
| 125 | $age = (new Age($bdate, $ddate))->ageAtEvent(false); |
||
| 126 | } else { |
||
| 127 | // If living, show age today |
||
| 128 | $age = (new Age($bdate, new Date(strtoupper(date('d M Y')))))->ageAtEvent(true); |
||
| 129 | } |
||
| 130 | |||
| 131 | // What images are linked to this individual |
||
| 132 | $individual_media = new Collection(); |
||
| 133 | foreach ($individual->facts(['OBJE']) as $fact) { |
||
| 134 | $media_object = $fact->target(); |
||
| 135 | if ($media_object instanceof Media) { |
||
| 136 | $media_file = $media_object->firstImageFile(); |
||
| 137 | if ($media_file instanceof MediaFile) { |
||
| 138 | $individual_media->add($media_file); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | $name_records = new Collection(); |
||
| 144 | foreach ($individual->facts(['NAME']) as $n => $name_fact) { |
||
| 145 | $name_records->add($this->formatNameRecord($tree, $n, $name_fact)); |
||
| 146 | } |
||
| 147 | |||
| 148 | $sex_records = new Collection(); |
||
| 149 | foreach ($individual->facts(['SEX']) as $n => $sex_fact) { |
||
| 150 | $sex_records->add($this->formatSexRecord($sex_fact)); |
||
| 151 | } |
||
| 152 | |||
| 153 | // If this individual is linked to a user account, show the link |
||
| 154 | $user_link = ''; |
||
| 155 | if (Auth::isAdmin()) { |
||
| 156 | $users = $this->user_service->findByIndividual($individual); |
||
| 157 | foreach ($users as $user) { |
||
| 158 | $user_link = ' — <a href="' . e(route('admin-users', ['filter' => $user->email()])) . '">' . e($user->userName()) . '</a>'; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | return $this->viewResponse('individual-page', [ |
||
| 163 | 'age' => $age, |
||
| 164 | 'clipboard_facts' => $this->clipboard_service->pastableFacts($individual, new Collection()), |
||
| 165 | 'individual' => $individual, |
||
| 166 | 'individual_media' => $individual_media, |
||
| 167 | 'meta_description' => $this->metaDescription($individual), |
||
| 168 | 'meta_robots' => 'index,follow', |
||
| 169 | 'name_records' => $name_records, |
||
| 170 | 'sex_records' => $sex_records, |
||
| 171 | 'sidebars' => $this->getSidebars($individual), |
||
| 172 | 'tabs' => $this->getTabs($individual), |
||
| 173 | 'significant' => $this->significant($individual), |
||
| 174 | 'title' => $individual->fullName() . ' ' . $individual->lifespan(), |
||
| 175 | 'tree' => $tree, |
||
| 176 | 'user_link' => $user_link, |
||
| 177 | ]); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param Individual $individual |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | private function metaDescription(Individual $individual): string |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Format a name record |
||
| 235 | * |
||
| 236 | * @param Tree $tree |
||
| 237 | * @param int $n |
||
| 238 | * @param Fact $fact |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | private function formatNameRecord(Tree $tree, $n, Fact $fact): string |
||
| 243 | { |
||
| 244 | $individual = $fact->record(); |
||
| 245 | |||
| 246 | // Create a dummy record, so we can extract the formatted NAME value from it. |
||
| 247 | $dummy = new Individual( |
||
| 248 | 'xref', |
||
| 249 | "0 @xref@ INDI\n1 DEAT Y\n" . $fact->gedcom(), |
||
| 250 | null, |
||
| 251 | $individual->tree() |
||
| 252 | ); |
||
| 253 | $dummy->setPrimaryName(0); // Make sure we use the name from "1 NAME" |
||
| 254 | |||
| 255 | $container_class = 'card'; |
||
| 256 | $content_class = 'collapse'; |
||
| 257 | $aria = 'false'; |
||
| 258 | |||
| 259 | if ($n === 0) { |
||
| 260 | $content_class = 'collapse show'; |
||
| 261 | $aria = 'true'; |
||
| 262 | } |
||
| 263 | if ($fact->isPendingDeletion()) { |
||
| 264 | $container_class .= ' wt-old'; |
||
| 265 | } elseif ($fact->isPendingAddition()) { |
||
| 266 | $container_class .= ' wt-new'; |
||
| 267 | } |
||
| 268 | |||
| 269 | ob_start(); |
||
| 270 | echo '<dl class="row mb-0"><dt class="col-md-4 col-lg-3">', I18N::translate('Name'), '</dt>'; |
||
| 271 | echo '<dd class="col-md-8 col-lg-9">', $dummy->fullName(), '</dd>'; |
||
| 272 | $ct = preg_match_all('/\n2 (\w+) (.*)/', $fact->gedcom(), $nmatch, PREG_SET_ORDER); |
||
| 273 | for ($i = 0; $i < $ct; $i++) { |
||
| 274 | $tag = $nmatch[$i][1]; |
||
| 275 | if ($tag !== 'SOUR' && $tag !== 'NOTE' && $tag !== 'SPFX') { |
||
| 276 | echo '<dt class="col-md-4 col-lg-3">', GedcomTag::getLabel($tag, $individual), '</dt>'; |
||
| 277 | echo '<dd class="col-md-8 col-lg-9">'; // Before using dir="auto" on this field, note that Gecko treats this as an inline element but WebKit treats it as a block element |
||
| 278 | if (isset($nmatch[$i][2])) { |
||
| 279 | $name = e($nmatch[$i][2]); |
||
| 280 | $name = str_replace('/', '', $name); |
||
| 281 | $name = preg_replace('/(\S*)\*/', '<span class="starredname">\\1</span>', $name); |
||
| 282 | switch ($tag) { |
||
| 283 | case 'TYPE': |
||
| 284 | echo GedcomCodeName::getValue($name, $individual); |
||
| 285 | break; |
||
| 286 | case 'SURN': |
||
| 287 | // The SURN field is not necessarily the surname. |
||
| 288 | // Where it is not a substring of the real surname, show it after the real surname. |
||
| 289 | $surname = e($dummy->getAllNames()[0]['surname']); |
||
| 290 | $surns = preg_replace('/, */', ' ', $nmatch[$i][2]); |
||
| 291 | if (strpos($dummy->getAllNames()[0]['surname'], $surns) !== false) { |
||
| 292 | echo '<span dir="auto">' . $surname . '</span>'; |
||
| 293 | } else { |
||
| 294 | echo I18N::translate('%1$s (%2$s)', '<span dir="auto">' . $surname . '</span>', '<span dir="auto">' . $name . '</span>'); |
||
| 295 | } |
||
| 296 | break; |
||
| 297 | default: |
||
| 298 | echo '<span dir="auto">' . $name . '</span>'; |
||
| 299 | break; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | echo '</dd>'; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | echo '</dl>'; |
||
| 306 | if (strpos($fact->gedcom(), "\n2 SOUR") !== false) { |
||
| 307 | echo '<div id="indi_sour" class="clearfix">', FunctionsPrintFacts::printFactSources($tree, $fact->gedcom(), 2), '</div>'; |
||
| 308 | } |
||
| 309 | if (strpos($fact->gedcom(), "\n2 NOTE") !== false) { |
||
| 310 | echo '<div id="indi_note" class="clearfix">', FunctionsPrint::printFactNotes($tree, $fact->gedcom(), 2), '</div>'; |
||
| 311 | } |
||
| 312 | $content = ob_get_clean(); |
||
| 313 | |||
| 314 | if ($fact->canEdit()) { |
||
| 315 | $edit_links = |
||
| 316 | '<a class="btn btn-link" href="#" data-confirm="' . I18N::translate('Are you sure you want to delete this fact?') . '" data-post-url="' . e(route(DeleteFact::class, ['tree' => $individual->tree()->name(), 'xref' => $individual->xref(), 'fact_id' => $fact->id()])) . '" title="' . I18N::translate('Delete this name') . '">' . view('icons/delete') . '<span class="sr-only">' . I18N::translate('Delete this name') . '</span></a>' . |
||
| 317 | '<a class="btn btn-link" href="' . e(route('edit-name', ['xref' => $individual->xref(), 'fact_id' => $fact->id(), 'tree' => $individual->tree()->name()])) . '" title="' . I18N::translate('Edit the name') . '">' . view('icons/edit') . '<span class="sr-only">' . I18N::translate('Edit the name') . '</span></a>'; |
||
| 318 | } else { |
||
| 319 | $edit_links = ''; |
||
| 320 | } |
||
| 321 | |||
| 322 | return ' |
||
| 323 | <div class="' . $container_class . '"> |
||
| 324 | <div class="card-header" role="tab" id="name-header-' . $n . '"> |
||
| 325 | <a data-toggle="collapse" data-parent="#individual-names" href="#name-content-' . $n . '" aria-expanded="' . $aria . '" aria-controls="name-content-' . $n . '">' . $dummy->fullName() . '</a> |
||
| 326 | ' . $edit_links . ' |
||
| 327 | </div> |
||
| 328 | <div id="name-content-' . $n . '" class="' . $content_class . '" role="tabpanel" aria-labelledby="name-header-' . $n . '"> |
||
| 329 | <div class="card-body">' . $content . '</div> |
||
| 330 | </div> |
||
| 331 | </div>'; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * print information for a sex record |
||
| 336 | * |
||
| 337 | * @param Fact $fact |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | private function formatSexRecord(Fact $fact): string |
||
| 342 | { |
||
| 343 | $individual = $fact->record(); |
||
| 344 | |||
| 345 | switch ($fact->value()) { |
||
| 346 | case 'M': |
||
| 347 | $sex = I18N::translate('Male'); |
||
| 348 | break; |
||
| 349 | case 'F': |
||
| 350 | $sex = I18N::translate('Female'); |
||
| 351 | break; |
||
| 352 | default: |
||
| 353 | $sex = I18N::translateContext('unknown gender', 'Unknown'); |
||
| 354 | break; |
||
| 355 | } |
||
| 356 | |||
| 357 | $container_class = 'card'; |
||
| 358 | if ($fact->isPendingDeletion()) { |
||
| 359 | $container_class .= ' wt-old'; |
||
| 360 | } elseif ($fact->isPendingAddition()) { |
||
| 361 | $container_class .= ' wt-new'; |
||
| 362 | } |
||
| 363 | |||
| 364 | if ($individual->canEdit()) { |
||
| 365 | $edit_links = '<a class="btn btn-link" href="' . e(route(EditFact::class, ['xref' => $individual->xref(), 'fact_id' => $fact->id(), 'tree' => $individual->tree()->name()])) . '" title="' . I18N::translate('Edit the gender') . '">' . view('icons/edit') . '<span class="sr-only">' . I18N::translate('Edit the gender') . '</span></a>'; |
||
| 366 | } else { |
||
| 367 | $edit_links = ''; |
||
| 368 | } |
||
| 369 | |||
| 370 | return ' |
||
| 371 | <div class="' . $container_class . '"> |
||
| 372 | <div class="card-header" role="tab" id="name-header-add"> |
||
| 373 | <div class="card-title mb-0"> |
||
| 374 | <b>' . I18N::translate('Gender') . '</b> ' . $sex . $edit_links . ' |
||
| 375 | </div> |
||
| 376 | </div> |
||
| 377 | </div>'; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Which tabs should we show on this individual's page. |
||
| 382 | * We don't show empty tabs. |
||
| 383 | * |
||
| 384 | * @param Individual $individual |
||
| 385 | * |
||
| 386 | * @return Collection<ModuleSidebarInterface> |
||
| 387 | */ |
||
| 388 | public function getSidebars(Individual $individual): Collection |
||
| 389 | { |
||
| 390 | return $this->module_service->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user()) |
||
| 391 | ->filter(static function (ModuleSidebarInterface $sidebar) use ($individual): bool { |
||
| 392 | return $sidebar->hasSidebarContent($individual); |
||
| 393 | }); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Which tabs should we show on this individual's page. |
||
| 398 | * We don't show empty tabs. |
||
| 399 | * |
||
| 400 | * @param Individual $individual |
||
| 401 | * |
||
| 402 | * @return Collection<ModuleTabInterface> |
||
| 403 | */ |
||
| 404 | public function getTabs(Individual $individual): Collection |
||
| 409 | }); |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * What are the significant elements of this page? |
||
| 414 | * The layout will need them to generate URLs for charts and reports. |
||
| 415 | * |
||
| 416 | * @param Individual $individual |
||
| 417 | * |
||
| 418 | * @return stdClass |
||
| 419 | */ |
||
| 420 | private function significant(Individual $individual): stdClass |
||
| 433 |