| Total Complexity | 77 |
| Total Lines | 514 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like LifespanController 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 LifespanController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class LifespanController extends PageController |
||
| 41 | { |
||
| 42 | // Base color parameters |
||
| 43 | const RANGE = 120; // degrees |
||
| 44 | const SATURATION = 100; // percent |
||
| 45 | const LIGHTNESS = 30; // percent |
||
| 46 | const ALPHA = 0.25; |
||
| 47 | const CHART_TOP = 10; // pixels |
||
| 48 | const BAR_SPACING = 22; // pixels |
||
| 49 | const YEAR_SPAN = 10; // Number of years per scale section |
||
| 50 | const PIXELS_PER_YEAR = 7; // Number of pixels to shift per year |
||
| 51 | const SESSION_DATA = 'lifespan_data'; |
||
| 52 | |||
| 53 | /** @var string|null Chart parameter */ |
||
| 54 | public $place = null; |
||
| 55 | |||
| 56 | /** @var int|null Chart parameter */ |
||
| 57 | public $beginYear = null; |
||
| 58 | |||
| 59 | /** @var int|null Chart parameter */ |
||
| 60 | public $endYear = null; |
||
| 61 | |||
| 62 | /** @var string Chart parameter */ |
||
| 63 | public $subtitle = ' '; |
||
| 64 | |||
| 65 | /** @var Individual[] A list of individuals to display. */ |
||
| 66 | private $people = array(); |
||
| 67 | |||
| 68 | /** @var string The default calendar to use. */ |
||
| 69 | private $defaultCalendar; |
||
| 70 | |||
| 71 | /** @var string Which calendar to use. */ |
||
| 72 | private $calendar; |
||
| 73 | |||
| 74 | /** @var string Which calendar escape to use. */ |
||
| 75 | private $calendarEscape; |
||
| 76 | |||
| 77 | /** @var int The earliest year to show. */ |
||
| 78 | private $timelineMinYear; |
||
| 79 | |||
| 80 | /** @var int That latest year to show. */ |
||
| 81 | private $timelineMaxYear; |
||
| 82 | |||
| 83 | /** @var int The current year. */ |
||
| 84 | private $currentYear; |
||
| 85 | |||
| 86 | /** @var string[] A list of colors to use. */ |
||
| 87 | private $colors = array(); |
||
| 88 | |||
| 89 | /** @todo This attribute is public to support the PHP5.3 closure workaround. */ |
||
| 90 | /** @var Place|null A place to serarh. */ |
||
| 91 | public $place_obj = null; |
||
| 92 | |||
| 93 | /** @todo This attribute is public to support the PHP5.3 closure workaround. */ |
||
| 94 | /** @var Date|null Start of the date range. */ |
||
| 95 | public $startDate = null; |
||
| 96 | |||
| 97 | /** @todo This attribute is public to support the PHP5.3 closure workaround. */ |
||
| 98 | /** @var Date|null End of the date range. */ |
||
| 99 | public $endDate = null; |
||
| 100 | |||
| 101 | /** @var bool Only match dates in the chosen calendar. */ |
||
| 102 | private $strictDate; |
||
| 103 | |||
| 104 | /** @todo This attribute is public to support the PHP5.3 closure workaround. */ |
||
| 105 | /** @var string[] List of facts/events to include. */ |
||
| 106 | public $facts; |
||
| 107 | |||
| 108 | /** @var string[] Facts and events to exclude from the chart */ |
||
| 109 | private $nonfacts = array( |
||
| 110 | 'FAMS', 'FAMC', 'MAY', 'BLOB', 'OBJE', 'SEX', 'NAME', 'SOUR', 'NOTE', 'BAPL', 'ENDL', |
||
| 111 | 'SLGC', 'SLGS', '_TODO', '_WT_OBJE_SORT', 'CHAN', 'HUSB', 'WIFE', 'CHIL', 'OCCU', 'ASSO', |
||
| 112 | ); |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Startup activity |
||
| 116 | */ |
||
| 117 | public function __construct() |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Add a person (and optionally their immediate family members) to the pids array |
||
| 301 | * |
||
| 302 | * @param Individual $person |
||
| 303 | * @param bool $add_family |
||
| 304 | * |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | private function addFamily(Individual $person, $add_family) |
||
| 308 | { |
||
| 309 | $xrefs = array(); |
||
| 310 | $xrefs[] = $person->getXref(); |
||
| 311 | if ($add_family) { |
||
| 312 | foreach ($person->getSpouseFamilies() as $family) { |
||
| 313 | $spouse = $family->getSpouse($person); |
||
| 314 | if ($spouse) { |
||
| 315 | $xrefs[] = $spouse->getXref(); |
||
| 316 | foreach ($family->getChildren() as $child) { |
||
| 317 | $xrefs[] = $child->getXref(); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 | foreach ($person->getChildFamilies() as $family) { |
||
| 322 | foreach ($family->getSpouses() as $parent) { |
||
| 323 | $xrefs[] = $parent->getXref(); |
||
| 324 | } |
||
| 325 | foreach ($family->getChildren() as $sibling) { |
||
| 326 | if ($person !== $sibling) { |
||
| 327 | $xrefs[] = $sibling->getXref(); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | return $xrefs; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Prints the time line scale |
||
| 338 | */ |
||
| 339 | public function printTimeline() |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Populate the timeline |
||
| 351 | * |
||
| 352 | * @return int |
||
| 353 | */ |
||
| 354 | public function fillTimeline() |
||
| 355 | { |
||
| 356 | $rows = array(); |
||
| 357 | $maxY = self::CHART_TOP; |
||
| 358 | //base case |
||
| 359 | if (!$this->people) { |
||
| 360 | return $maxY; |
||
| 361 | } |
||
| 362 | |||
| 363 | foreach ($this->people as $person) { |
||
| 364 | $bdate = $this->getCalendarDate($person->getEstimatedBirthDate()->minimumJulianDay()); |
||
| 365 | $ddate = $this->getCalendarDate($person->getEstimatedDeathDate()->maximumJulianDay()); |
||
| 366 | $birthYear = $bdate->y; |
||
| 367 | $age = min($ddate->y, $this->currentYear) - $birthYear; // truncate the bar at the current year |
||
| 368 | $width = max(9, $age * self::PIXELS_PER_YEAR); // min width is width of sex icon |
||
| 369 | $startPos = ($birthYear - $this->timelineMinYear) * self::PIXELS_PER_YEAR; |
||
| 370 | |||
| 371 | //-- calculate a good Y top value |
||
| 372 | $Y = self::CHART_TOP; |
||
| 373 | $ready = false; |
||
| 374 | while (!$ready) { |
||
| 375 | if (!isset($rows[$Y])) { |
||
| 376 | $ready = true; |
||
| 377 | $rows[$Y]['x1'] = $startPos; |
||
| 378 | $rows[$Y]['x2'] = $startPos + $width; |
||
| 379 | } else { |
||
| 380 | if ($rows[$Y]['x1'] > $startPos + $width) { |
||
| 381 | $ready = true; |
||
| 382 | $rows[$Y]['x1'] = $startPos; |
||
| 383 | } elseif ($rows[$Y]['x2'] < $startPos) { |
||
| 384 | $ready = true; |
||
| 385 | $rows[$Y]['x2'] = $startPos + $width; |
||
| 386 | } else { |
||
| 387 | //move down a line |
||
| 388 | $Y += self::BAR_SPACING; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | $facts = $person->getFacts(); |
||
| 394 | foreach ($person->getSpouseFamilies() as $family) { |
||
| 395 | foreach ($family->getFacts() as $fact) { |
||
| 396 | $facts[] = $fact; |
||
| 397 | } |
||
| 398 | } |
||
| 399 | Functions::sortFacts($facts); |
||
| 400 | |||
| 401 | $that = $this; // PHP5.3 cannot access $this inside a closure |
||
| 402 | $acceptedFacts = array_filter($facts, function (Fact $fact) use ($that) { |
||
| 403 | return |
||
| 404 | (in_array($fact->getTag(), $that->facts) && $fact->getDate()->isOK()) || |
||
| 405 | (($that->place_obj || $that->startDate) && $that->checkFact($fact)); |
||
| 406 | }); |
||
| 407 | |||
| 408 | $eventList = array(); |
||
| 409 | foreach ($acceptedFacts as $fact) { |
||
| 410 | $tag = $fact->getTag(); |
||
| 411 | //-- if the fact is a generic EVENt then get the qualifying TYPE |
||
| 412 | if ($tag == "EVEN") { |
||
| 413 | $tag = $fact->getAttribute('TYPE'); |
||
| 414 | } |
||
| 415 | $eventList[] = array( |
||
| 416 | 'label' => GedcomTag::getLabel($tag), |
||
| 417 | 'date' => $fact->getDate()->display(), |
||
| 418 | 'place' => $fact->getPlace()->getFullName(), |
||
| 419 | ); |
||
| 420 | } |
||
| 421 | $direction = I18N::direction() === 'ltr' ? 'left' : 'right'; |
||
| 422 | $lifespan = ' ' . $person->getLifeSpan(); // put the space here so its included in the length calcs |
||
| 423 | $sex = $person->getSex(); |
||
| 424 | $popupClass = strtr($sex, array('M' => '', 'U' => 'NN')); |
||
| 425 | $color = $sex === 'U' ? '' : sprintf("background-color: %s", $this->colors[$sex]->getNextColor()); |
||
| 426 | |||
| 427 | // following lines are a nasty method of approximating |
||
| 428 | // the width of a string in pixels from the character count |
||
| 429 | $name_length = mb_strlen(strip_tags($person->getFullName())) * 6.5; |
||
| 430 | $short_name_length = mb_strlen(strip_tags($person->getShortName())) * 6.5; |
||
| 431 | $lifespan_length = mb_strlen(strip_tags($lifespan)) * 6.5; |
||
| 432 | |||
| 433 | if ($width > $name_length + $lifespan_length) { |
||
| 434 | $printName = $person->getFullName(); |
||
| 435 | $abbrLifespan = $lifespan; |
||
| 436 | } elseif ($width > $name_length) { |
||
| 437 | $printName = $person->getFullName(); |
||
| 438 | $abbrLifespan = '…'; |
||
| 439 | } elseif ($width > $short_name_length) { |
||
| 440 | $printName = $person->getShortName(); |
||
| 441 | $abbrLifespan = ''; |
||
| 442 | } else { |
||
| 443 | $printName = ''; |
||
| 444 | $abbrLifespan = ''; |
||
| 445 | } |
||
| 446 | |||
| 447 | // Bar framework |
||
| 448 | printf(' |
||
| 449 | <div class="person_box%s" style="top:%spx; %s:%spx; width:%spx; %s"> |
||
| 450 | <div class="itr">%s %s %s |
||
| 451 | <div class="popup person_box%s"> |
||
| 452 | <div> |
||
| 453 | <a href="%s">%s%s</a> |
||
| 454 | </div>', |
||
| 455 | $popupClass, $Y, $direction, $startPos, $width, $color, |
||
| 456 | $person->getSexImage(), $printName, $abbrLifespan, |
||
| 457 | $popupClass, |
||
| 458 | $person->getHtmlUrl(), $person->getFullName(), $lifespan |
||
| 459 | ); |
||
| 460 | |||
| 461 | // Add events to popup |
||
| 462 | foreach ($eventList as $event) { |
||
| 463 | printf("<div>%s: %s %s</div>", $event['label'], $event['date'], $event['place']); |
||
| 464 | } |
||
| 465 | echo |
||
| 466 | '</div>' . // class="popup" |
||
| 467 | '</div>' . // class="itr" |
||
| 468 | '</div>'; // class=$popupclass |
||
| 469 | |||
| 470 | $maxY = max($maxY, $Y); |
||
| 471 | } |
||
| 472 | |||
| 473 | return $maxY; |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Function checkFact |
||
| 478 | * |
||
| 479 | * Does this fact meet the search criteria? |
||
| 480 | * |
||
| 481 | * @todo This function is public to support the PHP5.3 closure workaround. |
||
| 482 | * |
||
| 483 | * @param Fact $fact |
||
| 484 | * |
||
| 485 | * @return bool |
||
| 486 | */ |
||
| 487 | public function checkFact(Fact $fact) |
||
| 488 | { |
||
| 489 | $valid = !in_array($fact->getTag(), $this->nonfacts); |
||
| 490 | if ($valid && $this->place_obj) { |
||
| 491 | $valid = stripos($fact->getPlace()->getGedcomName(), $this->place_obj->getGedcomName()) !== false; |
||
| 492 | } |
||
| 493 | if ($valid && $this->startDate) { |
||
| 494 | if ($this->strictDate && $this->calendar !== $this->defaultCalendar) { |
||
| 495 | $valid = stripos($fact->getAttribute('DATE'), $this->calendar) !== false; |
||
| 496 | } |
||
| 497 | if ($valid) { |
||
| 498 | $date = $fact->getDate(); |
||
| 499 | $valid = $date->isOK() && Date::compare($date, $this->startDate) >= 0 && Date::compare($date, $this->endDate) <= 0; |
||
| 500 | } |
||
| 501 | } |
||
| 502 | |||
| 503 | return $valid; |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Function getCalendarDate |
||
| 508 | * |
||
| 509 | * @todo This function is public to support the PHP5.3 closure workaround. |
||
| 510 | * |
||
| 511 | * @param int $date |
||
| 512 | * |
||
| 513 | * @return object |
||
| 514 | */ |
||
| 515 | public function getCalendarDate($date) |
||
| 516 | { |
||
| 517 | switch ($this->calendar) { |
||
| 518 | case 'julian': |
||
| 519 | $caldate = new JulianDate($date); |
||
| 520 | break; |
||
| 521 | case 'french': |
||
| 522 | $caldate = new FrenchDate($date); |
||
| 523 | break; |
||
| 524 | case 'jewish': |
||
| 525 | $caldate = new JewishDate($date); |
||
| 526 | break; |
||
| 527 | case 'hijri': |
||
| 528 | $caldate = new HijriDate($date); |
||
| 529 | break; |
||
| 530 | case 'jalali': |
||
| 531 | $caldate = new JalaliDate($date); |
||
| 532 | break; |
||
| 533 | default: |
||
| 534 | $caldate = new GregorianDate($date); |
||
| 535 | } |
||
| 536 | |||
| 537 | return $caldate; |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Function getCalendarOptionList |
||
| 542 | * |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | public function getCalendarOptionList() |
||
| 554 | } |
||
| 555 | } |
||
| 556 |