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