ParentData   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 10
Bugs 3 Features 1
Metric Value
eloc 132
dl 0
loc 175
rs 10
c 10
b 3
f 1
wmc 21

2 Methods

Rating   Name   Duplication   Size   Complexity  
F getPerson() 0 150 19
A validateDate() 0 5 2
1
<?php
2
3
namespace FamilyTree365\LaravelGedcom\Utils;
4
5
use Carbon\Carbon;
0 ignored issues
show
Bug introduced by
The type Carbon\Carbon was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use FamilyTree365\LaravelGedcom\Models\Family;
7
use FamilyTree365\LaravelGedcom\Models\Person;
8
use Illuminate\Support\Str;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Str was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Throwable;
10
11
class ParentData
12
{
13
    /**
14
     * Array of persons ID
15
     * key - old GEDCOM ID
16
     * value - new autoincrement ID.
17
     *
18
     * @var string
19
     */
20
    protected $persons_id = [];
21
    protected $subm_ids = [];
22
    protected $sour_ids = [];
23
    protected $obje_ids = [];
24
    protected $note_ids = [];
25
    protected $repo_ids = [];
26
    protected $conn = '';
27
28
    public static function getPerson($conn, $individuals, $obje_ids = [], $sour_ids = [])
29
    {
30
        $ParentData = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ParentData is dead and can be removed.
Loading history...
31
        $a = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $a is dead and can be removed.
Loading history...
32
33
        try {
34
            foreach ($individuals as $k => $individual) {
35
                $g_id = $individual->getId();
0 ignored issues
show
Unused Code introduced by
The assignment to $g_id is dead and can be removed.
Loading history...
36
                $name = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
37
                $givn = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $givn is dead and can be removed.
Loading history...
38
                $surn = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $surn is dead and can be removed.
Loading history...
39
                $name = '';
40
                $npfx = '';
41
                $givn = '';
42
                $nick = '';
43
                $spfx = '';
44
                $surn = '';
45
                $nsfx = '';
46
                $type = '';
47
                $fone = null; // Gedcom/
0 ignored issues
show
Unused Code introduced by
The assignment to $fone is dead and can be removed.
Loading history...
48
                $romn = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $romn is dead and can be removed.
Loading history...
49
                $names = $individual->getName();
50
                $attr = $individual->getAllAttr();
51
                $events = $individual->getAllEven();
0 ignored issues
show
Unused Code introduced by
The assignment to $events is dead and can be removed.
Loading history...
52
                $note = $individual->getNote();
0 ignored issues
show
Unused Code introduced by
The assignment to $note is dead and can be removed.
Loading history...
53
                $indv_sour = $individual->getSour();
0 ignored issues
show
Unused Code introduced by
The assignment to $indv_sour is dead and can be removed.
Loading history...
54
                $alia = $individual->getAlia(); // string array
0 ignored issues
show
Unused Code introduced by
The assignment to $alia is dead and can be removed.
Loading history...
55
                $asso = $individual->getAsso();
0 ignored issues
show
Unused Code introduced by
The assignment to $asso is dead and can be removed.
Loading history...
56
                $subm = $individual->getSubm();
0 ignored issues
show
Unused Code introduced by
The assignment to $subm is dead and can be removed.
Loading history...
57
                $anci = $individual->getAnci();
0 ignored issues
show
Unused Code introduced by
The assignment to $anci is dead and can be removed.
Loading history...
58
                $refn = $individual->getRefn();
0 ignored issues
show
Unused Code introduced by
The assignment to $refn is dead and can be removed.
Loading history...
59
                $obje = $individual->getObje();
0 ignored issues
show
Unused Code introduced by
The assignment to $obje is dead and can be removed.
Loading history...
60
                // object
61
                $bapl = $individual->getBapl();
0 ignored issues
show
Unused Code introduced by
The assignment to $bapl is dead and can be removed.
Loading history...
62
                $conl = $individual->getConl();
0 ignored issues
show
Unused Code introduced by
The assignment to $conl is dead and can be removed.
Loading history...
63
                $endl = $individual->getEndl();
0 ignored issues
show
Unused Code introduced by
The assignment to $endl is dead and can be removed.
Loading history...
64
                $slgc = $individual->getSlgc();
0 ignored issues
show
Unused Code introduced by
The assignment to $slgc is dead and can be removed.
Loading history...
65
                $chan = $individual->getChan();
66
                $g_id = $individual->getId();
67
68
                if (!empty($names)) {
69
                    $name = current($names)->getName() ?: '';
70
                    $npfx = current($names)->getNpfx() ?: '';
71
                    $givn = current($names)->getGivn() ?: '';
72
                    $nick = current($names)->getNick() ?: '';
73
                    $spfx = current($names)->getSpfx() ?: '';
74
                    $surn = current($names)->getSurn() ?: '';
75
                    $nsfx = current($names)->getNsfx() ?: '';
76
                    $type = current($names)->getType() ?: '';
77
                }
78
79
                // array value
80
                $fams = $individual->getFams();  // self family, leave it now, note would be included in family
81
                $famc = $individual->getFamc();  // parent family , leave it now, note and pedi would be included in family
82
83
                // added to database
84
                // string value
85
                $sex = preg_replace('/[^MF]/', '', $individual->getSex());
86
                $uid = $individual->getUid() ?? strtoupper(str_replace('-', '', Str::uuid()));
87
                $resn = $individual->getResn();
88
                $rin = $individual->getRin();
89
                $rfn = $individual->getRfn();
90
                $afn = $individual->getAfn();
91
                $titl = $individual->getAttr();
0 ignored issues
show
Unused Code introduced by
The assignment to $titl is dead and can be removed.
Loading history...
92
93
                $birt = $individual->getBirt();
94
                $birthday = $birt->dateFormatted ?? null;
95
96
                $birth_month = $birt->month ?? null;
97
                $birth_year = $birt->year ?? null;
98
                $birthday_dati = $birt->dati ?? null;
99
                $birthday_plac = $birt->plac ?? null;
100
101
                $deat = $individual->getDeat();
102
                $deathday = $deat->dateFormatted ?? null;
103
                $death_month = $deat->month ?? null;
104
                $death_year = $deat->year ?? null;
105
                $deathday_dati = $deat->dati ?? null;
106
                $deathday_plac = $deat->plac ?? null;
107
                $deathday_caus = $deat->caus ?? null;
108
109
                $buri = $individual->getBuri();
110
                $burial_day = $buri->dateFormatted ?? null;
111
                $burial_month = $buri->month ?? null;
112
                $burial_year = $buri->year ?? null;
113
                $burial_day_dati = $buri->dati ?? null;
114
                $burial_day_plac = $buri->plac ?? null;
115
116
                $chr = $individual->getChr();
117
                $chr = $chr->dateFormatted ?? null;
118
119
                if ($givn == '') {
120
                    $givn = $name;
121
                }
122
123
                $config = json_encode(config('database.connections.'.$conn));
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

123
                $config = json_encode(/** @scrutinizer ignore-call */ config('database.connections.'.$conn));
Loading history...
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
124
                $value = [
125
                    'gid'             => $g_id,
126
                    'name'            => utf8_encode($name),
127
                    'givn'            => utf8_encode($givn),
128
                    'surn'            => utf8_encode($surn),
129
                    'sex'             => $sex,
130
                    'uid'             => $uid,
131
                    'rin'             => $rin,
132
                    'resn'            => $resn,
133
                    'rfn'             => $rfn,
134
                    'afn'             => $afn,
135
                    'nick'            => $nick,
136
                    'type'            => $type,
137
                    'chan'            => $chan ? $chan->getDatetime() : null,
138
                    'nsfx'            => $nsfx,
139
                    'npfx'            => $npfx,
140
                    'spfx'            => $spfx,
141
                    'birthday'        => self::validateDate($birthday) ? $birthday : null,
142
                    'birth_month'     => $birth_month,
143
                    'birth_year'      => $birth_year,
144
                    'birthday_dati'   => utf8_encode($birthday_dati),
0 ignored issues
show
Bug introduced by
It seems like $birthday_dati can also be of type null; however, parameter $string of utf8_encode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
                    'birthday_dati'   => utf8_encode(/** @scrutinizer ignore-type */ $birthday_dati),
Loading history...
145
                    'birthday_plac'   => utf8_encode($birthday_plac),
146
                    'deathday'        => $deathday,
147
                    'death_month'     => $death_month,
148
                    'death_year'      => $death_year,
149
                    'deathday_dati'   => $deathday_dati,
150
                    'deathday_plac'   => utf8_encode($deathday_plac),
151
                    'deathday_caus'   => $deathday_caus,
152
                    'burial_day'      => $burial_day,
153
                    'burial_month'    => $burial_month,
154
                    'burial_year'     => $burial_year,
155
                    'burial_day_dati' => $burial_day_dati,
156
                    'burial_day_plac' => $burial_day_plac,
157
                    'titl'            => array_key_exists('TITL', $attr) ? $attr['TITL'][0]->getAttr('TITL') : null,
158
                    'famc'            => $famc ? $famc[0]->getFamc() : null,
159
                    'fams'            => $fams ? $fams[0]->getFams() : null,
160
                    'chr'             => $chr,
161
                ];
162
163
                $parentData[] = $value;
164
            }
165
166
            $chunk = array_chunk($parentData, 500);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parentData seems to be defined by a foreach iteration on line 34. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
167
168
            foreach ($chunk as $item) {
169
                // it's take only 1 second for 3010 record
170
                $a = Person::on($conn)->upsert($item, ['uid']);
171
            }
172
173
            otherFields::insertOtherFields($conn, $individuals, $obje_ids, $sour_ids);
174
175
            return $parentData;
176
        } catch (\Exception $e) {
177
            return \Log::error($e);
178
        }
179
    }
180
181
    private static function validateDate($date, $format = 'Y-m-d')
182
    {
183
        $d = \DateTime::createFromFormat($format, $date);
184
        // The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
185
        return $d && $d->format($format) === $date;
186
    }
187
}
188