| Total Complexity | 96 |
| Total Lines | 486 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FunctionsPrint 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 FunctionsPrint, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 74 | class FunctionsPrint |
||
| 75 | { |
||
| 76 | /** |
||
| 77 | * print a note record |
||
| 78 | * |
||
| 79 | * @param Tree $tree |
||
| 80 | * @param string $text |
||
| 81 | * @param int $nlevel the level of the note record |
||
| 82 | * @param string $nrec the note record to print |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | private static function printNoteRecord(Tree $tree, $text, $nlevel, $nrec): string |
||
| 87 | { |
||
| 88 | $text .= Functions::getCont($nlevel, $nrec); |
||
| 89 | |||
| 90 | // Check if shared note (we have already checked that it exists) |
||
| 91 | if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@ NOTE/', $nrec, $match)) { |
||
| 92 | $note = Note::getInstance($match[1], $tree); |
||
| 93 | $label = 'SHARED_NOTE'; |
||
| 94 | $html = Filter::formatText($note->getNote(), $tree); |
||
| 95 | } else { |
||
| 96 | $note = null; |
||
| 97 | $label = 'NOTE'; |
||
| 98 | $html = Filter::formatText($text, $tree); |
||
| 99 | } |
||
| 100 | |||
| 101 | if (strpos($text, "\n") === false) { |
||
| 102 | // A one-line note? strip the block-level tags, so it displays inline |
||
| 103 | return GedcomTag::getLabelValue($label, strip_tags($html, '<a><strong><em>')); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($tree->getPreference('EXPAND_NOTES')) { |
||
| 107 | // A multi-line note, and we're expanding notes by default |
||
| 108 | return GedcomTag::getLabelValue($label, $html); |
||
| 109 | } |
||
| 110 | |||
| 111 | // A multi-line note, with an expand/collapse option |
||
| 112 | $element_id = Uuid::uuid4()->toString(); |
||
| 113 | // NOTE: class "note-details" is (currently) used only by some third-party themes |
||
| 114 | if ($note) { |
||
| 115 | $first_line = '<a href="' . e($note->url()) . '">' . $note->fullName() . '</a>'; |
||
| 116 | } else { |
||
| 117 | [$text] = explode("\n", strip_tags($html)); |
||
| 118 | $first_line = Str::limit($text, 100, I18N::translate('…')); |
||
| 119 | } |
||
| 120 | |||
| 121 | return |
||
| 122 | '<div class="fact_NOTE"><span class="label">' . |
||
| 123 | '<a href="#" onclick="expand_layer(\'' . $element_id . '\'); return false;"><i id="' . $element_id . '_img" class="icon-plus"></i></a> ' . GedcomTag::getLabel($label) . ':</span> ' . '<span id="' . $element_id . '-alt">' . $first_line . '</span>' . |
||
| 124 | '</div>' . |
||
| 125 | '<div class="note-details" id="' . $element_id . '" style="display:none">' . $html . '</div>'; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Print all of the notes in this fact record |
||
| 130 | * |
||
| 131 | * @param Tree $tree |
||
| 132 | * @param string $factrec The fact to print the notes from |
||
| 133 | * @param int $level The level of the notes |
||
| 134 | * |
||
| 135 | * @return string HTML |
||
| 136 | */ |
||
| 137 | public static function printFactNotes(Tree $tree, $factrec, $level): string |
||
| 138 | { |
||
| 139 | $data = ''; |
||
| 140 | $previous_spos = 0; |
||
| 141 | $nlevel = $level + 1; |
||
| 142 | $ct = preg_match_all("/$level NOTE (.*)/", $factrec, $match, PREG_SET_ORDER); |
||
| 143 | for ($j = 0; $j < $ct; $j++) { |
||
| 144 | $spos1 = strpos($factrec, $match[$j][0], $previous_spos); |
||
| 145 | $spos2 = strpos($factrec . "\n$level", "\n$level", $spos1 + 1); |
||
| 146 | if (!$spos2) { |
||
| 147 | $spos2 = strlen($factrec); |
||
| 148 | } |
||
| 149 | $previous_spos = $spos2; |
||
| 150 | $nrec = substr($factrec, $spos1, $spos2 - $spos1); |
||
| 151 | if (!isset($match[$j][1])) { |
||
| 152 | $match[$j][1] = ''; |
||
| 153 | } |
||
| 154 | if (!preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $match[$j][1], $nmatch)) { |
||
| 155 | $data .= self::printNoteRecord($tree, $match[$j][1], $nlevel, $nrec); |
||
| 156 | } else { |
||
| 157 | $note = Note::getInstance($nmatch[1], $tree); |
||
| 158 | if ($note) { |
||
| 159 | if ($note->canShow()) { |
||
| 160 | $noterec = $note->gedcom(); |
||
| 161 | $nt = preg_match("/0 @$nmatch[1]@ NOTE (.*)/", $noterec, $n1match); |
||
| 162 | $data .= self::printNoteRecord($tree, $nt > 0 ? $n1match[1] : '', 1, $noterec); |
||
| 163 | } |
||
| 164 | } else { |
||
| 165 | $data = '<div class="fact_NOTE"><span class="label">' . I18N::translate('Note') . '</span>: <span class="field error">' . $nmatch[1] . '</span></div>'; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | return $data; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Format age of parents in HTML |
||
| 175 | * |
||
| 176 | * @param Individual $person child |
||
| 177 | * @param Date $birth_date |
||
| 178 | * |
||
| 179 | * @return string HTML |
||
| 180 | */ |
||
| 181 | public static function formatParentsAges(Individual $person, Date $birth_date): string |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Convert a GEDCOM age string to localized text. |
||
| 226 | * |
||
| 227 | * @param string $age_string |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public static function formatGedcomAge(string $age_string): string |
||
| 232 | { |
||
| 233 | switch (strtoupper($age_string)) { |
||
| 234 | case 'CHILD': |
||
| 235 | return I18N::translate('Child'); |
||
| 236 | case 'INFANT': |
||
| 237 | return I18N::translate('Infant'); |
||
| 238 | case 'STILLBORN': |
||
| 239 | return I18N::translate('Stillborn'); |
||
| 240 | default: |
||
| 241 | return preg_replace_callback( |
||
| 242 | [ |
||
| 243 | '/(\d+)([ymwd])/', |
||
| 244 | ], |
||
| 245 | static function (array $match): string { |
||
| 246 | $num = (int) $match[1]; |
||
| 247 | |||
| 248 | switch ($match[2]) { |
||
| 249 | case 'y': |
||
| 250 | return I18N::plural('%s year', '%s years', $num, I18N::number($num)); |
||
| 251 | case 'm': |
||
| 252 | return I18N::plural('%s month', '%s months', $num, I18N::number($num)); |
||
| 253 | case 'w': |
||
| 254 | return I18N::plural('%s week', '%s weeks', $num, I18N::number($num)); |
||
| 255 | case 'd': |
||
| 256 | return I18N::plural('%s day', '%s days', $num, I18N::number($num)); |
||
| 257 | default: |
||
| 258 | throw new LogicException('Should never get here'); |
||
| 259 | } |
||
| 260 | }, |
||
| 261 | $age_string |
||
| 262 | ) ; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Print fact DATE/TIME |
||
| 268 | * |
||
| 269 | * @param Fact $event event containing the date/age |
||
| 270 | * @param GedcomRecord $record the person (or couple) whose ages should be printed |
||
| 271 | * @param bool $anchor option to print a link to calendar |
||
| 272 | * @param bool $time option to print TIME value |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public static function formatFactDate(Fact $event, GedcomRecord $record, $anchor, $time): string |
||
| 277 | { |
||
| 278 | $factrec = $event->gedcom(); |
||
| 279 | $html = ''; |
||
| 280 | // Recorded age |
||
| 281 | if (preg_match('/\n2 AGE (.+)/', $factrec, $match)) { |
||
| 282 | $fact_age = self::formatGedcomAge($match[1]); |
||
| 283 | } else { |
||
| 284 | $fact_age = ''; |
||
| 285 | } |
||
| 286 | if (preg_match('/\n2 HUSB\n3 AGE (.+)/', $factrec, $match)) { |
||
| 287 | $husb_age = self::formatGedcomAge($match[1]); |
||
| 288 | } else { |
||
| 289 | $husb_age = ''; |
||
| 290 | } |
||
| 291 | if (preg_match('/\n2 WIFE\n3 AGE (.+)/', $factrec, $match)) { |
||
| 292 | $wife_age = self::formatGedcomAge($match[1]); |
||
| 293 | } else { |
||
| 294 | $wife_age = ''; |
||
| 295 | } |
||
| 296 | |||
| 297 | // Calculated age |
||
| 298 | $fact = $event->getTag(); |
||
| 299 | if (preg_match('/\n2 DATE (.+)/', $factrec, $match)) { |
||
| 300 | $date = new Date($match[1]); |
||
| 301 | $html .= ' ' . $date->display($anchor); |
||
| 302 | // time |
||
| 303 | if ($time && preg_match('/\n3 TIME (.+)/', $factrec, $match)) { |
||
| 304 | $html .= ' – <span class="date">' . $match[1] . '</span>'; |
||
| 305 | } |
||
| 306 | if ($record instanceof Individual) { |
||
| 307 | if (in_array($fact, Gedcom::BIRTH_EVENTS, true) && $record->tree()->getPreference('SHOW_PARENTS_AGE')) { |
||
| 308 | // age of parents at child birth |
||
| 309 | $html .= self::formatParentsAges($record, $date); |
||
| 310 | } |
||
| 311 | if ($fact !== 'BIRT' && $fact !== 'CHAN' && $fact !== '_TODO') { |
||
| 312 | // age at event |
||
| 313 | $birth_date = $record->getBirthDate(); |
||
| 314 | // Can't use getDeathDate(), as this also gives BURI/CREM events, which |
||
| 315 | // wouldn't give the correct "days after death" result for people with |
||
| 316 | // no DEAT. |
||
| 317 | $death_event = $record->facts(['DEAT'])->first(); |
||
| 318 | if ($death_event instanceof Fact) { |
||
| 319 | $death_date = $death_event->date(); |
||
| 320 | } else { |
||
| 321 | $death_date = new Date(''); |
||
| 322 | } |
||
| 323 | $ageText = ''; |
||
| 324 | if ($fact === 'DEAT' || Date::compare($date, $death_date) <= 0 || !$record->isDead()) { |
||
| 325 | // Before death, print age |
||
| 326 | $age = (new Age($birth_date, $date))->ageAtEvent(false); |
||
| 327 | // Only show calculated age if it differs from recorded age |
||
| 328 | if ($age !== '') { |
||
| 329 | if ($fact_age !== '' && $fact_age !== $age) { |
||
| 330 | $ageText = $age; |
||
| 331 | } elseif ($fact_age === '' && $husb_age === '' && $wife_age === '') { |
||
| 332 | $ageText = $age; |
||
| 333 | } elseif ($husb_age !== '' && $husb_age !== $age && $record->sex() === 'M') { |
||
| 334 | $ageText = $age; |
||
| 335 | } elseif ($wife_age !== '' && $wife_age !== $age && $record->sex() === 'F') { |
||
| 336 | $ageText = $age; |
||
| 337 | } |
||
| 338 | } |
||
| 339 | } |
||
| 340 | if ($fact !== 'DEAT' && Date::compare($death_date, $date) <= 0) { |
||
| 341 | // After death, print time since death |
||
| 342 | $ageText = (new Age($death_date, $date))->timeAfterDeath(); |
||
| 343 | // Family events which occur after death are probably errors |
||
| 344 | if ($event->record() instanceof Family) { |
||
| 345 | $ageText .= view('icons/warning'); |
||
| 346 | } |
||
| 347 | } |
||
| 348 | if ($ageText) { |
||
| 349 | $html .= ' <span class="age">' . $ageText . '</span>'; |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | // print gedcom ages |
||
| 355 | $age_labels = [ |
||
| 356 | I18N::translate('Age') => $fact_age, |
||
| 357 | I18N::translate('Husband') => $husb_age, |
||
| 358 | I18N::translate('Wife') => $wife_age, |
||
| 359 | ]; |
||
| 360 | |||
| 361 | foreach (array_filter($age_labels) as $label => $age) { |
||
| 362 | $html .= ' <span class="label">' . $label . ':</span> <span class="age">' . $age . '</span>'; |
||
| 363 | } |
||
| 364 | |||
| 365 | return $html; |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * print fact PLACe TEMPle STATus |
||
| 370 | * |
||
| 371 | * @param Fact $event gedcom fact record |
||
| 372 | * @param bool $anchor to print a link to placelist |
||
| 373 | * @param bool $sub_records to print place subrecords |
||
| 374 | * @param bool $lds to print LDS TEMPle and STATus |
||
| 375 | * |
||
| 376 | * @return string HTML |
||
| 377 | */ |
||
| 378 | public static function formatFactPlace(Fact $event, $anchor = false, $sub_records = false, $lds = false): string |
||
| 379 | { |
||
| 380 | $tree = $event->record()->tree(); |
||
| 381 | |||
| 382 | if ($anchor) { |
||
| 383 | // Show the full place name, for facts/events tab |
||
| 384 | $html = $event->place()->fullName(true); |
||
| 385 | } else { |
||
| 386 | // Abbreviate the place name, for chart boxes |
||
| 387 | return $event->place()->shortName(); |
||
| 388 | } |
||
| 389 | |||
| 390 | if ($sub_records) { |
||
| 391 | $placerec = Functions::getSubRecord(2, '2 PLAC', $event->gedcom()); |
||
| 392 | if ($placerec !== '') { |
||
| 393 | if (preg_match_all('/\n3 (?:_HEB|ROMN) (.+)/', $placerec, $matches)) { |
||
| 394 | foreach ($matches[1] as $match) { |
||
| 395 | $wt_place = new Place($match, $tree); |
||
| 396 | $html .= ' - ' . $wt_place->fullName(); |
||
| 397 | } |
||
| 398 | } |
||
| 399 | $map_lati = ''; |
||
| 400 | $cts = preg_match('/\d LATI (.*)/', $placerec, $match); |
||
| 401 | if ($cts > 0) { |
||
| 402 | $map_lati = $match[1]; |
||
| 403 | $html .= '<br><span class="label">' . I18N::translate('Latitude') . ': </span>' . $map_lati; |
||
| 404 | } |
||
| 405 | $map_long = ''; |
||
| 406 | $cts = preg_match('/\d LONG (.*)/', $placerec, $match); |
||
| 407 | if ($cts > 0) { |
||
| 408 | $map_long = $match[1]; |
||
| 409 | $html .= ' <span class="label">' . I18N::translate('Longitude') . ': </span>' . $map_long; |
||
| 410 | } |
||
| 411 | if ($map_lati && $map_long) { |
||
| 412 | $map_lati = trim(strtr($map_lati, 'NSEW,�', ' - -. ')); // S5,6789 ==> -5.6789 |
||
| 413 | $map_long = trim(strtr($map_long, 'NSEW,�', ' - -. ')); // E3.456� ==> 3.456 |
||
| 414 | |||
| 415 | $html .= '<a href="https://maps.google.com/maps?q=' . e($map_lati) . ',' . e($map_long) . '" rel="nofollow" title="' . I18N::translate('Google Maps™') . '">' . |
||
| 416 | view('icons/google-maps') . |
||
| 417 | '<span class="sr-only">' . I18N::translate('Google Maps™') . '</span>' . |
||
| 418 | '</a>'; |
||
| 419 | |||
| 420 | $html .= '<a href="https://www.bing.com/maps/?lvl=15&cp=' . e($map_lati) . '~' . e($map_long) . '" rel="nofollow" title="' . I18N::translate('Bing Maps™') . '">' . |
||
| 421 | view('icons/bing-maps') . |
||
| 422 | '<span class="sr-only">' . I18N::translate('Bing Maps™') . '</span>' . |
||
| 423 | '</a>'; |
||
| 424 | |||
| 425 | $html .= '<a href="https://www.openstreetmap.org/#map=15/' . e($map_lati) . '/' . e($map_long) . '" rel="nofollow" title="' . I18N::translate('OpenStreetMap™') . '">' . |
||
| 426 | view('icons/openstreetmap') . |
||
| 427 | '<span class="sr-only">' . I18N::translate('OpenStreetMap™') . '</span>' . |
||
| 428 | '</a>'; |
||
| 429 | } |
||
| 430 | if (preg_match('/\d NOTE (.*)/', $placerec, $match)) { |
||
| 431 | $html .= '<br>' . self::printFactNotes($tree, $placerec, 3); |
||
| 432 | } |
||
| 433 | } |
||
| 434 | } |
||
| 435 | if ($lds) { |
||
| 436 | if (preg_match('/2 TEMP (.*)/', $event->gedcom(), $match)) { |
||
| 437 | $html .= '<br>' . I18N::translate('LDS temple') . ': ' . GedcomCodeTemp::templeName($match[1]); |
||
| 438 | } |
||
| 439 | if (preg_match('/2 STAT (.*)/', $event->gedcom(), $match)) { |
||
| 440 | $html .= '<br>' . I18N::translate('Status') . ': ' . GedcomCodeStat::statusName($match[1]); |
||
| 441 | if (preg_match('/3 DATE (.*)/', $event->gedcom(), $match)) { |
||
| 442 | $date = new Date($match[1]); |
||
| 443 | $html .= ', ' . GedcomTag::getLabel('STAT:DATE') . ': ' . $date->display(); |
||
| 444 | } |
||
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | return $html; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Check for facts that may exist only once for a certain record type. |
||
| 453 | * If the fact already exists in the second array, delete it from the first one. |
||
| 454 | * |
||
| 455 | * @param string[] $uniquefacts |
||
| 456 | * @param Collection $recfacts |
||
| 457 | * |
||
| 458 | * @return string[] |
||
| 459 | */ |
||
| 460 | public static function checkFactUnique(array $uniquefacts, Collection $recfacts): array |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Print a new fact box on details pages |
||
| 476 | * |
||
| 477 | * @param GedcomRecord $record the person, family, source etc the fact will be added to |
||
| 478 | * @param Collection $usedfacts an array of facts already used in this record |
||
| 479 | * @param string $type the type of record INDI, FAM, SOUR etc |
||
| 480 | * |
||
| 481 | * @return void |
||
| 482 | */ |
||
| 483 | public static function printAddNewFact(GedcomRecord $record, Collection $usedfacts, $type): void |
||
| 560 | ]); |
||
| 561 | } |
||
| 562 | } |
||
| 563 |