Conditions | 109 |
Paths | > 20000 |
Total Lines | 431 |
Code Lines | 314 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
513 | public function searchIndividualsAdvanced(Tree $tree, array $fields, array $modifiers): Collection |
||
514 | { |
||
515 | $fields = array_filter($fields, static fn (string $x): bool => $x !== ''); |
||
516 | |||
517 | $query = DB::table('individuals') |
||
518 | ->where('i_file', '=', $tree->id()) |
||
519 | ->select(['individuals.*']) |
||
520 | ->distinct(); |
||
521 | |||
522 | // Join the following tables |
||
523 | $father_name = false; |
||
524 | $mother_name = false; |
||
525 | $spouse_family = false; |
||
526 | $indi_name = false; |
||
527 | $indi_dates = []; |
||
528 | $fam_dates = []; |
||
529 | $indi_plac = false; |
||
530 | $fam_plac = false; |
||
531 | |||
532 | foreach ($fields as $field_name => $field_value) { |
||
533 | if (str_starts_with($field_name, 'FATHER:NAME')) { |
||
534 | $father_name = true; |
||
535 | } elseif (str_starts_with($field_name, 'MOTHER:NAME')) { |
||
536 | $mother_name = true; |
||
537 | } elseif (str_starts_with($field_name, 'INDI:NAME:GIVN')) { |
||
538 | $indi_name = true; |
||
539 | } elseif (str_starts_with($field_name, 'INDI:NAME:SURN')) { |
||
540 | $indi_name = true; |
||
541 | } elseif (str_starts_with($field_name, 'FAM:')) { |
||
542 | $spouse_family = true; |
||
543 | if (str_ends_with($field_name, ':DATE')) { |
||
544 | $fam_dates[] = explode(':', $field_name)[1]; |
||
545 | } elseif (str_ends_with($field_name, ':PLAC')) { |
||
546 | $fam_plac = true; |
||
547 | } |
||
548 | } elseif (str_starts_with($field_name, 'INDI:')) { |
||
549 | if (str_ends_with($field_name, ':DATE')) { |
||
550 | $indi_dates[] = explode(':', $field_name)[1]; |
||
551 | } elseif (str_ends_with($field_name, ':PLAC')) { |
||
552 | $indi_plac = true; |
||
553 | } |
||
554 | } |
||
555 | } |
||
556 | |||
557 | if ($father_name || $mother_name) { |
||
558 | $query->join('link AS l1', static function (JoinClause $join): void { |
||
559 | $join |
||
560 | ->on('l1.l_file', '=', 'individuals.i_file') |
||
561 | ->on('l1.l_from', '=', 'individuals.i_id') |
||
562 | ->where('l1.l_type', '=', 'FAMC'); |
||
563 | }); |
||
564 | |||
565 | if ($father_name) { |
||
566 | $query->join('link AS l2', static function (JoinClause $join): void { |
||
567 | $join |
||
568 | ->on('l2.l_file', '=', 'l1.l_file') |
||
569 | ->on('l2.l_from', '=', 'l1.l_to') |
||
570 | ->where('l2.l_type', '=', 'HUSB'); |
||
571 | }); |
||
572 | $query->join('name AS father_name', static function (JoinClause $join): void { |
||
573 | $join |
||
574 | ->on('father_name.n_file', '=', 'l2.l_file') |
||
575 | ->on('father_name.n_id', '=', 'l2.l_to'); |
||
576 | }); |
||
577 | } |
||
578 | |||
579 | if ($mother_name) { |
||
580 | $query->join('link AS l3', static function (JoinClause $join): void { |
||
581 | $join |
||
582 | ->on('l3.l_file', '=', 'l1.l_file') |
||
583 | ->on('l3.l_from', '=', 'l1.l_to') |
||
584 | ->where('l3.l_type', '=', 'WIFE'); |
||
585 | }); |
||
586 | $query->join('name AS mother_name', static function (JoinClause $join): void { |
||
587 | $join |
||
588 | ->on('mother_name.n_file', '=', 'l3.l_file') |
||
589 | ->on('mother_name.n_id', '=', 'l3.l_to'); |
||
590 | }); |
||
591 | } |
||
592 | } |
||
593 | |||
594 | if ($spouse_family) { |
||
595 | $query->join('link AS l4', static function (JoinClause $join): void { |
||
596 | $join |
||
597 | ->on('l4.l_file', '=', 'individuals.i_file') |
||
598 | ->on('l4.l_from', '=', 'individuals.i_id') |
||
599 | ->where('l4.l_type', '=', 'FAMS'); |
||
600 | }); |
||
601 | $query->join('families AS spouse_families', static function (JoinClause $join): void { |
||
602 | $join |
||
603 | ->on('spouse_families.f_file', '=', 'l4.l_file') |
||
604 | ->on('spouse_families.f_id', '=', 'l4.l_to'); |
||
605 | }); |
||
606 | } |
||
607 | |||
608 | if ($indi_name) { |
||
609 | $query->join('name AS individual_name', static function (JoinClause $join): void { |
||
610 | $join |
||
611 | ->on('individual_name.n_file', '=', 'individuals.i_file') |
||
612 | ->on('individual_name.n_id', '=', 'individuals.i_id'); |
||
613 | }); |
||
614 | } |
||
615 | |||
616 | foreach (array_unique($indi_dates) as $indi_date) { |
||
617 | $query->join('dates AS date_' . $indi_date, static function (JoinClause $join) use ($indi_date): void { |
||
618 | $join |
||
619 | ->on('date_' . $indi_date . '.d_file', '=', 'individuals.i_file') |
||
620 | ->on('date_' . $indi_date . '.d_gid', '=', 'individuals.i_id'); |
||
621 | }); |
||
622 | } |
||
623 | |||
624 | foreach (array_unique($fam_dates) as $fam_date) { |
||
625 | $query->join('dates AS date_' . $fam_date, static function (JoinClause $join) use ($fam_date): void { |
||
626 | $join |
||
627 | ->on('date_' . $fam_date . '.d_file', '=', 'spouse_families.f_file') |
||
628 | ->on('date_' . $fam_date . '.d_gid', '=', 'spouse_families.f_id'); |
||
629 | }); |
||
630 | } |
||
631 | |||
632 | if ($indi_plac) { |
||
633 | $query->join('placelinks AS individual_placelinks', static function (JoinClause $join): void { |
||
634 | $join |
||
635 | ->on('individual_placelinks.pl_file', '=', 'individuals.i_file') |
||
636 | ->on('individual_placelinks.pl_gid', '=', 'individuals.i_id'); |
||
637 | }); |
||
638 | $query->join('places AS individual_places', static function (JoinClause $join): void { |
||
639 | $join |
||
640 | ->on('individual_places.p_file', '=', 'individual_placelinks.pl_file') |
||
641 | ->on('individual_places.p_id', '=', 'individual_placelinks.pl_p_id'); |
||
642 | }); |
||
643 | } |
||
644 | |||
645 | if ($fam_plac) { |
||
646 | $query->join('placelinks AS family_placelinks', static function (JoinClause $join): void { |
||
647 | $join |
||
648 | ->on('family_placelinks.pl_file', '=', 'spouse_families.f_file') |
||
649 | ->on('family_placelinks.pl_gid', '=', 'spouse_families.f_id'); |
||
650 | }); |
||
651 | $query->join('places AS family_places', static function (JoinClause $join): void { |
||
652 | $join |
||
653 | ->on('family_places.p_file', '=', 'family_placelinks.pl_file') |
||
654 | ->on('family_places.p_id', '=', 'family_placelinks.pl_p_id'); |
||
655 | }); |
||
656 | } |
||
657 | |||
658 | foreach ($fields as $field_name => $field_value) { |
||
659 | $parts = explode(':', $field_name . ':::'); |
||
660 | if (str_starts_with($field_name, 'INDI:NAME:')) { |
||
661 | switch ($field_name) { |
||
662 | case 'INDI:NAME:GIVN': |
||
663 | switch ($modifiers[$field_name]) { |
||
664 | case 'EXACT': |
||
665 | $query->where('individual_name.n_givn', '=', $field_value); |
||
666 | break; |
||
667 | case 'BEGINS': |
||
668 | $query->where('individual_name.n_givn', DB::iLike(), $field_value . '%'); |
||
669 | break; |
||
670 | case 'CONTAINS': |
||
671 | $query->where('individual_name.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
672 | break; |
||
673 | case 'SDX_STD': |
||
674 | $sdx = Soundex::russell($field_value); |
||
675 | if ($sdx !== '') { |
||
676 | $this->wherePhonetic($query, 'individual_name.n_soundex_givn_std', $sdx); |
||
677 | } else { |
||
678 | // No phonetic content? Use a substring match |
||
679 | $query->where('individual_name.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
680 | } |
||
681 | break; |
||
682 | case 'SDX': // SDX uses DM by default. |
||
683 | case 'SDX_DM': |
||
684 | $sdx = Soundex::daitchMokotoff($field_value); |
||
685 | if ($sdx !== '') { |
||
686 | $this->wherePhonetic($query, 'individual_name.n_soundex_givn_dm', $sdx); |
||
687 | } else { |
||
688 | // No phonetic content? Use a substring match |
||
689 | $query->where('individual_name.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
690 | } |
||
691 | break; |
||
692 | } |
||
693 | unset($fields[$field_name]); |
||
694 | break; |
||
695 | case 'INDI:NAME:SURN': |
||
696 | switch ($modifiers[$field_name]) { |
||
697 | case 'EXACT': |
||
698 | $query->where(function (Builder $query) use ($field_value): void { |
||
699 | $query |
||
700 | ->where('individual_name.n_surn', '=', $field_value) |
||
701 | ->orWhere('individual_name.n_surname', '=', $field_value); |
||
702 | }); |
||
703 | break; |
||
704 | case 'BEGINS': |
||
705 | $query->where(function (Builder $query) use ($field_value): void { |
||
706 | $query |
||
707 | ->where('individual_name.n_surn', DB::iLike(), $field_value . '%') |
||
708 | ->orWhere('individual_name.n_surname', DB::iLike(), $field_value . '%'); |
||
709 | }); |
||
710 | break; |
||
711 | case 'CONTAINS': |
||
712 | $query->where(function (Builder $query) use ($field_value): void { |
||
713 | $query |
||
714 | ->where('individual_name.n_surn', DB::iLike(), '%' . $field_value . '%') |
||
715 | ->orWhere('individual_name.n_surname', DB::iLike(), '%' . $field_value . '%'); |
||
716 | }); |
||
717 | break; |
||
718 | case 'SDX_STD': |
||
719 | $sdx = Soundex::russell($field_value); |
||
720 | if ($sdx !== '') { |
||
721 | $this->wherePhonetic($query, 'individual_name.n_soundex_surn_std', $sdx); |
||
722 | } else { |
||
723 | // No phonetic content? Use a substring match |
||
724 | $query->where(function (Builder $query) use ($field_value): void { |
||
725 | $query |
||
726 | ->where('individual_name.n_surn', DB::iLike(), '%' . $field_value . '%') |
||
727 | ->orWhere('individual_name.n_surname', DB::iLike(), '%' . $field_value . '%'); |
||
728 | }); |
||
729 | } |
||
730 | break; |
||
731 | case 'SDX': // SDX uses DM by default. |
||
732 | case 'SDX_DM': |
||
733 | $sdx = Soundex::daitchMokotoff($field_value); |
||
734 | if ($sdx !== '') { |
||
735 | $this->wherePhonetic($query, 'individual_name.n_soundex_surn_dm', $sdx); |
||
736 | } else { |
||
737 | // No phonetic content? Use a substring match |
||
738 | $query->where(function (Builder $query) use ($field_value): void { |
||
739 | $query |
||
740 | ->where('individual_name.n_surn', DB::iLike(), '%' . $field_value . '%') |
||
741 | ->orWhere('individual_name.n_surname', DB::iLike(), '%' . $field_value . '%'); |
||
742 | }); |
||
743 | } |
||
744 | break; |
||
745 | } |
||
746 | unset($fields[$field_name]); |
||
747 | break; |
||
748 | case 'INDI:NAME:NICK': |
||
749 | case 'INDI:NAME:_MARNM': |
||
750 | case 'INDI:NAME:_HEB': |
||
751 | case 'INDI:NAME:_AKA': |
||
752 | $like = "%\n1 NAME%\n2 " . $parts[2] . ' %' . preg_quote($field_value, '/') . '%'; |
||
753 | $query->where('individuals.i_gedcom', DB::iLike(), $like); |
||
754 | break; |
||
755 | } |
||
756 | } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':DATE')) { |
||
757 | $date = new Date($field_value); |
||
758 | if ($date->isOK()) { |
||
759 | $delta = 365 * (int) ($modifiers[$field_name] ?? 0); |
||
760 | $query |
||
761 | ->where('date_' . $parts[1] . '.d_fact', '=', $parts[1]) |
||
762 | ->where('date_' . $parts[1] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta) |
||
763 | ->where('date_' . $parts[1] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta); |
||
764 | } |
||
765 | unset($fields[$field_name]); |
||
766 | } elseif (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':DATE')) { |
||
767 | $date = new Date($field_value); |
||
768 | if ($date->isOK()) { |
||
769 | $delta = 365 * (int) ($modifiers[$field_name] ?? 0); |
||
770 | $query |
||
771 | ->where('date_' . $parts[1] . '.d_fact', '=', $parts[1]) |
||
772 | ->where('date_' . $parts[1] . '.d_julianday1', '>=', $date->minimumJulianDay() - $delta) |
||
773 | ->where('date_' . $parts[1] . '.d_julianday2', '<=', $date->maximumJulianDay() + $delta); |
||
774 | } |
||
775 | unset($fields[$field_name]); |
||
776 | } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':PLAC')) { |
||
777 | // SQL can only link a place to a person/family, not to an event. |
||
778 | $query->where('individual_places.p_id', '=', $field_value); |
||
779 | } elseif (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':PLAC')) { |
||
780 | // SQL can only link a place to a person/family, not to an event. |
||
781 | $query->where('family_places.p_id', '=', $field_value); |
||
782 | } elseif (str_starts_with($field_name, 'MOTHER:NAME:') || str_starts_with($field_name, 'FATHER:NAME:')) { |
||
783 | $table = str_starts_with($field_name, 'FATHER:NAME:') ? 'father_name' : 'mother_name'; |
||
784 | switch ($parts[2]) { |
||
785 | case 'GIVN': |
||
786 | switch ($modifiers[$field_name]) { |
||
787 | case 'EXACT': |
||
788 | $query->where($table . '.n_givn', '=', $field_value); |
||
789 | break; |
||
790 | case 'BEGINS': |
||
791 | $query->where($table . '.n_givn', DB::iLike(), $field_value . '%'); |
||
792 | break; |
||
793 | case 'CONTAINS': |
||
794 | $query->where($table . '.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
795 | break; |
||
796 | case 'SDX_STD': |
||
797 | $sdx = Soundex::russell($field_value); |
||
798 | if ($sdx !== '') { |
||
799 | $this->wherePhonetic($query, $table . '.n_soundex_givn_std', $sdx); |
||
800 | } else { |
||
801 | // No phonetic content? Use a substring match |
||
802 | $query->where($table . '.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
803 | } |
||
804 | break; |
||
805 | case 'SDX': // SDX uses DM by default. |
||
806 | case 'SDX_DM': |
||
807 | $sdx = Soundex::daitchMokotoff($field_value); |
||
808 | if ($sdx !== '') { |
||
809 | $this->wherePhonetic($query, $table . '.n_soundex_givn_dm', $sdx); |
||
810 | } else { |
||
811 | // No phonetic content? Use a substring match |
||
812 | $query->where($table . '.n_givn', DB::iLike(), '%' . $field_value . '%'); |
||
813 | } |
||
814 | break; |
||
815 | } |
||
816 | break; |
||
817 | case 'SURN': |
||
818 | switch ($modifiers[$field_name]) { |
||
819 | case 'EXACT': |
||
820 | $query->where($table . '.n_surn', '=', $field_value); |
||
821 | break; |
||
822 | case 'BEGINS': |
||
823 | $query->where($table . '.n_surn', DB::iLike(), $field_value . '%'); |
||
824 | break; |
||
825 | case 'CONTAINS': |
||
826 | $query->where($table . '.n_surn', DB::iLike(), '%' . $field_value . '%'); |
||
827 | break; |
||
828 | case 'SDX_STD': |
||
829 | $sdx = Soundex::russell($field_value); |
||
830 | if ($sdx !== '') { |
||
831 | $this->wherePhonetic($query, $table . '.n_soundex_surn_std', $sdx); |
||
832 | } else { |
||
833 | // No phonetic content? Use a substring match |
||
834 | $query->where($table . '.n_surn', DB::iLike(), '%' . $field_value . '%'); |
||
835 | } |
||
836 | break; |
||
837 | case 'SDX': // SDX uses DM by default. |
||
838 | case 'SDX_DM': |
||
839 | $sdx = Soundex::daitchMokotoff($field_value); |
||
840 | if ($sdx !== '') { |
||
841 | $this->wherePhonetic($query, $table . '.n_soundex_surn_dm', $sdx); |
||
842 | } else { |
||
843 | // No phonetic content? Use a substring match |
||
844 | $query->where($table . '.n_surn', DB::iLike(), '%' . $field_value . '%'); |
||
845 | } |
||
846 | break; |
||
847 | } |
||
848 | break; |
||
849 | } |
||
850 | unset($fields[$field_name]); |
||
851 | } elseif (str_starts_with($field_name, 'FAM:')) { |
||
852 | // e.g. searches for occupation, religion, note, etc. |
||
853 | // Initial matching only. Need PHP to apply filter. |
||
854 | $query->where('spouse_families.f_gedcom', DB::iLike(), "%\n1 " . $parts[1] . ' %' . $field_value . '%'); |
||
855 | } elseif (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':TYPE')) { |
||
856 | // Initial matching only. Need PHP to apply filter. |
||
857 | $query->where('individuals.i_gedcom', DB::iLike(), "%\n1 " . $parts[1] . "%\n2 TYPE %" . $field_value . '%'); |
||
858 | } elseif (str_starts_with($field_name, 'INDI:')) { |
||
859 | // e.g. searches for occupation, religion, note, etc. |
||
860 | // Initial matching only. Need PHP to apply filter. |
||
861 | $query->where('individuals.i_gedcom', DB::iLike(), "%\n1 " . $parts[1] . '%' . $parts[2] . '%' . $field_value . '%'); |
||
862 | } |
||
863 | } |
||
864 | |||
865 | return $query |
||
866 | ->get() |
||
867 | ->each($this->rowLimiter()) |
||
868 | ->map($this->individualRowMapper()) |
||
869 | ->filter(GedcomRecord::accessFilter()) |
||
870 | ->filter(static function (Individual $individual) use ($fields, $tree): bool { |
||
871 | // Check for searches which were only partially matched by SQL |
||
872 | foreach ($fields as $field_name => $field_value) { |
||
873 | $parts = explode(':', $field_name . '::::'); |
||
874 | |||
875 | if (str_starts_with($field_name, 'INDI:NAME:') && $field_name !== 'INDI:NAME:GIVN' && $field_name !== 'INDI:NAME:SURN') { |
||
876 | $regex = '/\n1 NAME.*(?:\n2.*)*\n2 ' . $parts[2] . ' .*' . preg_quote($field_value, '/') . '/i'; |
||
877 | |||
878 | if (preg_match($regex, $individual->gedcom()) === 1) { |
||
879 | continue; |
||
880 | } |
||
881 | |||
882 | return false; |
||
883 | } |
||
884 | |||
885 | if (str_starts_with($field_name, 'INDI:') && str_ends_with($field_name, ':PLAC')) { |
||
886 | $place = Place::find((int) $field_value, $tree); |
||
887 | |||
888 | foreach ($individual->facts([$parts[1]]) as $fact) { |
||
889 | if ($fact->place()->gedcomName() === $place->gedcomName() || str_ends_with($fact->place()->gedcomName(), ', ' . $place->gedcomName())) { |
||
890 | continue 2; |
||
891 | } |
||
892 | } |
||
893 | return false; |
||
894 | } |
||
895 | |||
896 | if (str_starts_with($field_name, 'FAM:') && str_ends_with($field_name, ':PLAC')) { |
||
897 | $place = Place::find((int) $field_value, $tree); |
||
898 | |||
899 | foreach ($individual->spouseFamilies() as $family) { |
||
900 | foreach ($family->facts([$parts[1]]) as $fact) { |
||
901 | if ($fact->place()->gedcomName() === $place->gedcomName() || str_ends_with($fact->place()->gedcomName(), ', ' . $place->gedcomName())) { |
||
902 | continue 3; |
||
903 | } |
||
904 | } |
||
905 | } |
||
906 | return false; |
||
907 | } |
||
908 | |||
909 | $regex = '/' . preg_quote($field_value, '/') . '/i'; |
||
910 | |||
911 | if ($field_name === 'INDI:FACT:TYPE' || $field_name === 'INDI:EVEN:TYPE' || $field_name === 'INDI:CHAN:_WT_USER') { |
||
912 | foreach ($individual->facts([$parts[1]]) as $fact) { |
||
913 | if (preg_match($regex, $fact->attribute($parts[2])) === 1) { |
||
914 | continue 2; |
||
915 | } |
||
916 | } |
||
917 | |||
918 | return false; |
||
919 | } |
||
920 | |||
921 | if (str_starts_with($field_name, 'INDI:')) { |
||
922 | foreach ($individual->facts([$parts[1]]) as $fact) { |
||
923 | if (preg_match($regex, $fact->value()) === 1) { |
||
924 | continue 2; |
||
925 | } |
||
926 | } |
||
927 | |||
928 | return false; |
||
929 | } |
||
930 | |||
931 | if (str_starts_with($field_name, 'FAM:')) { |
||
932 | foreach ($individual->spouseFamilies() as $family) { |
||
933 | foreach ($family->facts([$parts[1]]) as $fact) { |
||
934 | if (preg_match($regex, $fact->value()) === 1) { |
||
935 | continue 3; |
||
936 | } |
||
937 | } |
||
938 | } |
||
939 | return false; |
||
940 | } |
||
941 | } |
||
942 | |||
943 | return true; |
||
944 | }); |
||
1320 |