Conditions | 93 |
Paths | > 20000 |
Total Lines | 401 |
Code Lines | 289 |
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 |
||
412 | public function searchIndividualsAdvanced(array $trees, array $fields, array $modifiers): Collection |
||
413 | { |
||
414 | $fields = array_filter($fields); |
||
415 | |||
416 | $query = DB::table('individuals') |
||
417 | ->select(['individuals.*']) |
||
418 | ->distinct(); |
||
419 | |||
420 | $this->whereTrees($query, 'i_file', $trees); |
||
421 | |||
422 | // Join the following tables |
||
423 | $father_name = false; |
||
424 | $mother_name = false; |
||
425 | $spouse_family = false; |
||
426 | $indi_name = false; |
||
427 | $indi_date = false; |
||
428 | $fam_date = false; |
||
429 | $indi_plac = false; |
||
430 | $fam_plac = false; |
||
431 | |||
432 | foreach ($fields as $field_name => $field_value) { |
||
433 | if ($field_value !== '') { |
||
434 | if (substr($field_name, 0, 14) === 'FAMC:HUSB:NAME') { |
||
435 | $father_name = true; |
||
436 | } elseif (substr($field_name, 0, 14) === 'FAMC:WIFE:NAME') { |
||
437 | $mother_name = true; |
||
438 | } elseif (substr($field_name, 0, 4) === 'NAME') { |
||
439 | $indi_name = true; |
||
440 | } elseif (strpos($field_name, ':DATE') !== false) { |
||
441 | if (substr($field_name, 0, 4) === 'FAMS') { |
||
442 | $fam_date = true; |
||
443 | $spouse_family = true; |
||
444 | } else { |
||
445 | $indi_date = true; |
||
446 | } |
||
447 | } elseif (strpos($field_name, ':PLAC') !== false) { |
||
448 | if (substr($field_name, 0, 4) === 'FAMS') { |
||
449 | $fam_plac = true; |
||
450 | $spouse_family = true; |
||
451 | } else { |
||
452 | $indi_plac = true; |
||
453 | } |
||
454 | } elseif ($field_name === 'FAMS:NOTE') { |
||
455 | $spouse_family = true; |
||
456 | } |
||
457 | } |
||
458 | } |
||
459 | |||
460 | if ($father_name || $mother_name) { |
||
461 | $query->join('link AS l1', static function (JoinClause $join): void { |
||
462 | $join |
||
463 | ->on('l1.l_file', '=', 'individuals.i_file') |
||
464 | ->on('l1.l_from', '=', 'individuals.i_id') |
||
465 | ->where('l1.l_type', '=', 'FAMC'); |
||
466 | }); |
||
467 | |||
468 | if ($father_name) { |
||
469 | $query->join('link AS l2', static function (JoinClause $join): void { |
||
470 | $join |
||
471 | ->on('l2.l_file', '=', 'l1.l_file') |
||
472 | ->on('l2.l_from', '=', 'l1.l_to') |
||
473 | ->where('l2.l_type', '=', 'HUSB'); |
||
474 | }); |
||
475 | $query->join('name AS father_name', static function (JoinClause $join): void { |
||
476 | $join |
||
477 | ->on('father_name.n_file', '=', 'l2.l_file') |
||
478 | ->on('father_name.n_id', '=', 'l2.l_to'); |
||
479 | }); |
||
480 | } |
||
481 | |||
482 | if ($mother_name) { |
||
483 | $query->join('link AS l3', static function (JoinClause $join): void { |
||
484 | $join |
||
485 | ->on('l3.l_file', '=', 'l1.l_file') |
||
486 | ->on('l3.l_from', '=', 'l1.l_to') |
||
487 | ->where('l3.l_type', '=', 'WIFE'); |
||
488 | }); |
||
489 | $query->join('name AS mother_name', static function (JoinClause $join): void { |
||
490 | $join |
||
491 | ->on('mother_name.n_file', '=', 'l3.l_file') |
||
492 | ->on('mother_name.n_id', '=', 'l3.l_to'); |
||
493 | }); |
||
494 | } |
||
495 | } |
||
496 | |||
497 | if ($spouse_family) { |
||
498 | $query->join('link AS l4', static function (JoinClause $join): void { |
||
499 | $join |
||
500 | ->on('l4.l_file', '=', 'individuals.i_file') |
||
501 | ->on('l4.l_from', '=', 'individuals.i_id') |
||
502 | ->where('l4.l_type', '=', 'FAMS'); |
||
503 | }); |
||
504 | $query->join('families AS spouse_families', static function (JoinClause $join): void { |
||
505 | $join |
||
506 | ->on('spouse_families.f_file', '=', 'l4.l_file') |
||
507 | ->on('spouse_families.f_id', '=', 'l4.l_to'); |
||
508 | }); |
||
509 | } |
||
510 | |||
511 | if ($indi_name) { |
||
512 | $query->join('name AS individual_name', static function (JoinClause $join): void { |
||
513 | $join |
||
514 | ->on('individual_name.n_file', '=', 'individuals.i_file') |
||
515 | ->on('individual_name.n_id', '=', 'individuals.i_id'); |
||
516 | }); |
||
517 | } |
||
518 | |||
519 | if ($indi_date) { |
||
520 | $query->join('dates AS individual_dates', static function (JoinClause $join): void { |
||
521 | $join |
||
522 | ->on('individual_dates.d_file', '=', 'individuals.i_file') |
||
523 | ->on('individual_dates.d_gid', '=', 'individuals.i_id'); |
||
524 | }); |
||
525 | } |
||
526 | |||
527 | if ($fam_date) { |
||
528 | $query->join('dates AS family_dates', static function (JoinClause $join): void { |
||
529 | $join |
||
530 | ->on('family_dates.d_file', '=', 'spouse_families.f_file') |
||
531 | ->on('family_dates.d_gid', '=', 'spouse_families.f_id'); |
||
532 | }); |
||
533 | } |
||
534 | |||
535 | if ($indi_plac) { |
||
536 | $query->join('placelinks AS individual_placelinks', static function (JoinClause $join): void { |
||
537 | $join |
||
538 | ->on('individual_placelinks.pl_file', '=', 'individuals.i_file') |
||
539 | ->on('individual_placelinks.pl_gid', '=', 'individuals.i_id'); |
||
540 | }); |
||
541 | $query->join('places AS individual_places', static function (JoinClause $join): void { |
||
542 | $join |
||
543 | ->on('individual_places.p_file', '=', 'individual_placelinks.pl_file') |
||
544 | ->on('individual_places.p_id', '=', 'individual_placelinks.pl_p_id'); |
||
545 | }); |
||
546 | } |
||
547 | |||
548 | if ($fam_plac) { |
||
549 | $query->join('placelinks AS familyl_placelinks', static function (JoinClause $join): void { |
||
550 | $join |
||
551 | ->on('familyl_placelinks.pl_file', '=', 'individuals.i_file') |
||
552 | ->on('familyl_placelinks.pl_gid', '=', 'individuals.i_id'); |
||
553 | }); |
||
554 | $query->join('places AS family_places', static function (JoinClause $join): void { |
||
555 | $join |
||
556 | ->on('family_places.p_file', '=', 'familyl_placelinks.pl_file') |
||
557 | ->on('family_places.p_id', '=', 'familyl_placelinks.pl_p_id'); |
||
558 | }); |
||
559 | } |
||
560 | |||
561 | foreach ($fields as $field_name => $field_value) { |
||
562 | $parts = explode(':', $field_name . '::::'); |
||
563 | if ($parts[0] === 'NAME') { |
||
564 | // NAME:* |
||
565 | switch ($parts[1]) { |
||
566 | case 'GIVN': |
||
567 | switch ($modifiers[$field_name]) { |
||
568 | case 'EXACT': |
||
569 | $query->where('individual_name.n_givn', '=', $field_value); |
||
570 | break; |
||
571 | case 'BEGINS': |
||
572 | $query->where('individual_name.n_givn', 'LIKE', $field_value . '%'); |
||
573 | break; |
||
574 | case 'CONTAINS': |
||
575 | $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); |
||
576 | break; |
||
577 | case 'SDX_STD': |
||
578 | $sdx = Soundex::russell($field_value); |
||
579 | if ($sdx !== '') { |
||
580 | $this->wherePhonetic($query, 'individual_name.n_soundex_givn_std', $sdx); |
||
581 | } else { |
||
582 | // No phonetic content? Use a substring match |
||
583 | $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); |
||
584 | } |
||
585 | break; |
||
586 | case 'SDX': // SDX uses DM by default. |
||
587 | case 'SDX_DM': |
||
588 | $sdx = Soundex::daitchMokotoff($field_value); |
||
589 | if ($sdx !== '') { |
||
590 | $this->wherePhonetic($query, 'individual_name.n_soundex_givn_dm', $sdx); |
||
591 | } else { |
||
592 | // No phonetic content? Use a substring match |
||
593 | $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%'); |
||
594 | } |
||
595 | break; |
||
596 | } |
||
597 | break; |
||
598 | case 'SURN': |
||
599 | switch ($modifiers[$field_name]) { |
||
600 | case 'EXACT': |
||
601 | $query->where('individual_name.n_surn', '=', $field_value); |
||
602 | break; |
||
603 | case 'BEGINS': |
||
604 | $query->where('individual_name.n_surn', 'LIKE', $field_value . '%'); |
||
605 | break; |
||
606 | case 'CONTAINS': |
||
607 | $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%'); |
||
608 | break; |
||
609 | case 'SDX_STD': |
||
610 | $sdx = Soundex::russell($field_value); |
||
611 | if ($sdx !== '') { |
||
612 | $this->wherePhonetic($query, 'individual_name.n_soundex_surn_std', $sdx); |
||
613 | } else { |
||
614 | // No phonetic content? Use a substring match |
||
615 | $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%'); |
||
616 | } |
||
617 | break; |
||
618 | case 'SDX': // SDX uses DM by default. |
||
619 | case 'SDX_DM': |
||
620 | $sdx = Soundex::daitchMokotoff($field_value); |
||
621 | if ($sdx !== '') { |
||
622 | $this->wherePhonetic($query, 'individual_name.n_soundex_surn_dm', $sdx); |
||
623 | } else { |
||
624 | // No phonetic content? Use a substring match |
||
625 | $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%'); |
||
626 | } |
||
627 | break; |
||
628 | } |
||
629 | break; |
||
630 | case 'NICK': |
||
631 | case '_MARNM': |
||
632 | case '_HEB': |
||
633 | case '_AKA': |
||
634 | $query |
||
635 | ->where('individual_name', '=', $parts[1]) |
||
636 | ->where('individual_name', 'LIKE', '%' . $field_value . '%'); |
||
637 | break; |
||
638 | } |
||
639 | unset($fields[$field_name]); |
||
640 | } elseif ($parts[1] === 'DATE') { |
||
641 | // *:DATE |
||
642 | $date = new Date($field_value); |
||
643 | if ($date->isOK()) { |
||
644 | $delta = 365 * ($modifiers[$field_name] ?? 0); |
||
645 | $query |
||
646 | ->where('individual_dates.d_fact', '=', $parts[0]) |
||
647 | ->where('individual_dates.d_julianday1', '>=', $date->minimumJulianDay() - $delta) |
||
648 | ->where('individual_dates.d_julianday2', '<=', $date->minimumJulianDay() + $delta); |
||
649 | } |
||
650 | unset($fields[$field_name]); |
||
651 | } elseif ($parts[0] === 'FAMS' && $parts[2] === 'DATE') { |
||
652 | // FAMS:*:DATE |
||
653 | $date = new Date($field_value); |
||
654 | if ($date->isOK()) { |
||
655 | $delta = 365 * $modifiers[$field_name]; |
||
656 | $query |
||
657 | ->where('family_dates.d_fact', '=', $parts[1]) |
||
658 | ->where('family_dates.d_julianday1', '>=', $date->minimumJulianDay() - $delta) |
||
659 | ->where('family_dates.d_julianday2', '<=', $date->minimumJulianDay() + $delta); |
||
660 | } |
||
661 | unset($fields[$field_name]); |
||
662 | } elseif ($parts[1] === 'PLAC') { |
||
663 | // *:PLAC |
||
664 | // SQL can only link a place to a person/family, not to an event. |
||
665 | $query->where('individual_places.p_place', 'LIKE', '%' . $field_value . '%'); |
||
666 | } elseif ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') { |
||
667 | // FAMS:*:PLAC |
||
668 | // SQL can only link a place to a person/family, not to an event. |
||
669 | $query->where('family_places.p_place', 'LIKE', '%' . $field_value . '%'); |
||
670 | } elseif ($parts[0] === 'FAMC' && $parts[2] === 'NAME') { |
||
671 | $table = $parts[1] === 'HUSB' ? 'father_name' : 'mother_name'; |
||
672 | // NAME:* |
||
673 | switch ($parts[3]) { |
||
674 | case 'GIVN': |
||
675 | switch ($modifiers[$field_name]) { |
||
676 | case 'EXACT': |
||
677 | $query->where($table . '.n_givn', '=', $field_value); |
||
678 | break; |
||
679 | case 'BEGINS': |
||
680 | $query->where($table . '.n_givn', 'LIKE', $field_value . '%'); |
||
681 | break; |
||
682 | case 'CONTAINS': |
||
683 | $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); |
||
684 | break; |
||
685 | case 'SDX_STD': |
||
686 | $sdx = Soundex::russell($field_value); |
||
687 | if ($sdx !== '') { |
||
688 | $this->wherePhonetic($query, $table . '.n_soundex_givn_std', $sdx); |
||
689 | } else { |
||
690 | // No phonetic content? Use a substring match |
||
691 | $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); |
||
692 | } |
||
693 | break; |
||
694 | case 'SDX': // SDX uses DM by default. |
||
695 | case 'SDX_DM': |
||
696 | $sdx = Soundex::daitchMokotoff($field_value); |
||
697 | if ($sdx !== '') { |
||
698 | $this->wherePhonetic($query, $table . '.n_soundex_givn_dm', $sdx); |
||
699 | } else { |
||
700 | // No phonetic content? Use a substring match |
||
701 | $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%'); |
||
702 | } |
||
703 | break; |
||
704 | } |
||
705 | break; |
||
706 | case 'SURN': |
||
707 | switch ($modifiers[$field_name]) { |
||
708 | case 'EXACT': |
||
709 | $query->where($table . '.n_surn', '=', $field_value); |
||
710 | break; |
||
711 | case 'BEGINS': |
||
712 | $query->where($table . '.n_surn', 'LIKE', $field_value . '%'); |
||
713 | break; |
||
714 | case 'CONTAINS': |
||
715 | $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); |
||
716 | break; |
||
717 | case 'SDX_STD': |
||
718 | $sdx = Soundex::russell($field_value); |
||
719 | if ($sdx !== '') { |
||
720 | $this->wherePhonetic($query, $table . '.n_soundex_surn_std', $sdx); |
||
721 | } else { |
||
722 | // No phonetic content? Use a substring match |
||
723 | $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); |
||
724 | } |
||
725 | break; |
||
726 | case 'SDX': // SDX uses DM by default. |
||
727 | case 'SDX_DM': |
||
728 | $sdx = Soundex::daitchMokotoff($field_value); |
||
729 | if ($sdx !== '') { |
||
730 | $this->wherePhonetic($query, $table . '.n_soundex_surn_dm', $sdx); |
||
731 | } else { |
||
732 | // No phonetic content? Use a substring match |
||
733 | $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%'); |
||
734 | } |
||
735 | break; |
||
736 | } |
||
737 | break; |
||
738 | } |
||
739 | unset($fields[$field_name]); |
||
740 | } elseif ($parts[0] === 'FAMS') { |
||
741 | // e.g. searches for occupation, religion, note, etc. |
||
742 | // Initial matching only. Need PHP to apply filter. |
||
743 | $query->where('spouse_families.f_gedcom', 'LIKE', "%\n1 " . $parts[1] . ' %' . $field_value . '%'); |
||
744 | } elseif ($parts[1] === 'TYPE') { |
||
745 | // e.g. FACT:TYPE or EVEN:TYPE |
||
746 | // Initial matching only. Need PHP to apply filter. |
||
747 | $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . '%\n2 TYPE %' . $field_value . '%'); |
||
748 | } else { |
||
749 | // e.g. searches for occupation, religion, note, etc. |
||
750 | // Initial matching only. Need PHP to apply filter. |
||
751 | $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . '%' . $parts[1] . '%' . $field_value . '%'); |
||
752 | } |
||
753 | } |
||
754 | return $query |
||
755 | ->get() |
||
756 | ->each($this->rowLimiter()) |
||
757 | ->map($this->individualRowMapper()) |
||
758 | ->filter(GedcomRecord::accessFilter()) |
||
759 | ->filter(static function (Individual $individual) use ($fields): bool { |
||
760 | // Check for searches which were only partially matched by SQL |
||
761 | foreach ($fields as $field_name => $field_value) { |
||
762 | $regex = '/' . preg_quote($field_value, '/') . '/i'; |
||
763 | |||
764 | $parts = explode(':', $field_name . '::::'); |
||
765 | |||
766 | // *:PLAC |
||
767 | if ($parts[1] === 'PLAC') { |
||
768 | foreach ($individual->facts([$parts[0]]) as $fact) { |
||
769 | if (preg_match($regex, $fact->place()->gedcomName())) { |
||
770 | continue 2; |
||
771 | } |
||
772 | } |
||
773 | return false; |
||
774 | } |
||
775 | |||
776 | // FAMS:*:PLAC |
||
777 | if ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') { |
||
778 | foreach ($individual->spouseFamilies() as $family) { |
||
779 | foreach ($family->facts([$parts[1]]) as $fact) { |
||
780 | if (preg_match($regex, $fact->place()->gedcomName())) { |
||
781 | continue 2; |
||
782 | } |
||
783 | } |
||
784 | } |
||
785 | return false; |
||
786 | } |
||
787 | |||
788 | // e.g. searches for occupation, religion, note, etc. |
||
789 | if ($parts[0] === 'FAMS') { |
||
790 | foreach ($individual->spouseFamilies() as $family) { |
||
791 | foreach ($family->facts([$parts[1]]) as $fact) { |
||
792 | if (preg_match($regex, $fact->value())) { |
||
793 | continue 3; |
||
794 | } |
||
795 | } |
||
796 | } |
||
797 | return false; |
||
798 | } |
||
799 | |||
800 | // e.g. FACT:TYPE or EVEN:TYPE |
||
801 | if ($parts[1] === 'TYPE' || $parts[1] === '_WT_USER') { |
||
802 | foreach ($individual->facts([$parts[0]]) as $fact) { |
||
803 | if (preg_match($regex, $fact->attribute($parts[1]))) { |
||
804 | continue 2; |
||
805 | } |
||
806 | } |
||
807 | |||
808 | return false; |
||
809 | } |
||
810 | } |
||
811 | |||
812 | return true; |
||
813 | }); |
||
1149 |