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