Even   F
last analyzed

Complexity

Total Complexity 110

Size/Duplication

Total Lines 366
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 300
dl 0
loc 366
rs 2
c 0
b 0
f 0
wmc 110

2 Methods

Rating   Name   Duplication   Size   Complexity  
F read() 0 167 45
F otherField() 0 188 65

How to fix   Complexity   

Complex Class

Complex classes like Even often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Even, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace FamilyTree365\LaravelGedcom\Utils\Exporter\Indi;
4
5
use FamilyTree365\LaravelGedcom\Models\PersonEvent;
6
use Throwable;
7
8
class Even
9
{
10
    /**
11
     * Array of persons ID
12
     * key - old GEDCOM ID
13
     * value - new autoincrement ID.
14
     *
15
     * @var string
16
     */
17
    public static function read($conn, $events, $person, $obje_ids = [])
0 ignored issues
show
Unused Code introduced by
The parameter $obje_ids is not used and could be removed. ( Ignorable by Annotation )

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

17
    public static function read($conn, $events, $person, /** @scrutinizer ignore-unused */ $obje_ids = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
18
    {
19
20
        if(empty($person)) return;
21
        
22
        $eventData = [];
23
        foreach ($events as $event) {
24
            if ($event && count($event) > 0) {
25
                $even = $event[0];
26
                $class_name = get_class($even);
27
                $person_id = $person->id;
28
                $type = $even->getType();
29
                $attr = $even->getAttr();
30
                $_date = $even->getDate();
31
                $date = \FamilyTree365\LaravelGedcom\Utils\Importer\Date::read($conn, $_date);
32
                if (strpos($date, 'BEF') !== false) {
0 ignored issues
show
Bug introduced by
It seems like $date can also be of type null; however, parameter $haystack of strpos() 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

32
                if (strpos(/** @scrutinizer ignore-type */ $date, 'BEF') !== false) {
Loading history...
33
                    $newdate = trim(str_replace('BEF', '', $date));
34
                    $date_cnvert = strtotime($newdate);
35
                } elseif (strpos($date, 'AFT') !== false) {
36
                    $newdate = trim(str_replace('AFT', '', $date));
37
                    $date_cnvert = strtotime($newdate);
38
                } else {
39
                    $date_cnvert = strtotime($date);
0 ignored issues
show
Bug introduced by
It seems like $date can also be of type null; however, parameter $datetime of strtotime() 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

39
                    $date_cnvert = strtotime(/** @scrutinizer ignore-type */ $date);
Loading history...
40
                }
41
                $_plac = $even->getPlac();
42
                $plac = \FamilyTree365\LaravelGedcom\Utils\Importer\Indi\Even\Plac::read($conn, $_plac);
43
                $_phon = $even->getPhon();
44
                $phon = \FamilyTree365\LaravelGedcom\Utils\Importer\Phon::read($conn, $_phon);
45
                $_addr = $even->getAddr();
46
                //$addr_id = \FamilyTree365\LaravelGedcom\Utils\Importer\Addr::read($conn, $_addr);
47
                if (!empty($_addr)) {
48
                    $addr_id = \FamilyTree365\LaravelGedcom\Utils\Importer\Addr::read($conn, $_addr);
49
                } else {
50
                    $addr_id = null;
51
                }
52
53
                $caus = $even->getCaus();
54
                $age = $even->getAge();
55
                $agnc = $even->getAgnc();
56
                $adop = '';
57
                $adop_famc = '';
58
                $birt_famc = '';
59
                switch ($class_name) {
60
                case 'Adop':
61
                    $adop = $even->getAdop();
0 ignored issues
show
Unused Code introduced by
The assignment to $adop is dead and can be removed.
Loading history...
62
                    $adop_famc = $even->getFamc();
0 ignored issues
show
Unused Code introduced by
The assignment to $adop_famc is dead and can be removed.
Loading history...
63
                break;
64
                case 'Birt':
65
                    $birt_famc = $even->getFamc();
0 ignored issues
show
Unused Code introduced by
The assignment to $birt_famc is dead and can be removed.
Loading history...
66
                break;
67
                case 'Bapm':
68
                break;
69
                case 'Barm':
70
                break;
71
                case 'Basm':
72
                break;
73
                case 'Bles':
74
                break;
75
                case 'Buri':
76
                break;
77
                case 'Cast':
78
                break;
79
                case 'Cens':
80
                break;
81
                case 'Chr':
82
                    $chr_famc = $even->getFamc();
0 ignored issues
show
Unused Code introduced by
The assignment to $chr_famc is dead and can be removed.
Loading history...
83
                break;
84
                case 'Chra':
85
                break;
86
                case 'Conf':
87
                break;
88
                case 'Crem':
89
                break;
90
                case 'Dscr':
91
                break;
92
                case 'Deat':
93
                break;
94
                case 'Educ':
95
                break;
96
                case 'Emig':
97
                break;
98
                case 'Fcom':
99
                break;
100
                case 'Grad':
101
                break;
102
                case 'Idno':
103
                break;
104
                case 'Immi':
105
                break;
106
                case 'Nati':
107
                break;
108
                case 'Nchi':
109
                break;
110
                case 'Natu':
111
                break;
112
                case 'Nmr':
113
                break;
114
                case 'Occu':
115
                break;
116
                case 'Ordn':
117
                break;
118
                case 'Reti':
119
                break;
120
                case 'Prob':
121
                break;
122
                case 'Prop':
123
                break;
124
                case 'Reli':
125
                break;
126
                case 'Resi':
127
                break;
128
                case 'Ssn':
129
                break;
130
                case 'Titl':
131
                break;
132
                case 'Will':
133
                break;
134
                case 'Even':
135
                break;
136
                default:
137
            }
138
                $adop = '';
139
                $adop_famc = '';
140
                $birt_famc = '';
141
                // store Even
142
                $key = [
143
                    ['person_id', $person_id],
144
                    ['title', $class_name],
145
                    ['type', $type],
146
                    ['attr', $attr],
147
                    ['date', $date],
148
                    ['plac', $plac],
149
                    ['phon', $phon],
150
                    ['caus', $caus],
151
                    ['age', $age],
152
                    ['agnc', $agnc],
153
                    ['adop', $adop],
154
                    ['adop_famc', $adop_famc],
155
                    ['birt_famc', $birt_famc],
156
                ];
157
                $check = PersonEvent::on($conn)->where($key)->first();
158
                if (empty($check)) {
159
                    $data = [
160
                        'person_id'      => $person_id,
161
                        'title'          => $class_name,
162
                        'type'           => $type, //
163
                        'attr'           => $attr, //
164
                        'date'           => $date,
165
                        'converted_date' => $date_cnvert,
166
                        'plac'           => $plac, //
167
                        'addr_id'        => $addr_id, //
168
                        'phon'           => $phon, //
169
                        'caus'           => $caus, //
170
                        'age'            => $age, //
171
                        'agnc'           => $agnc, //
172
                        'adop'           => $adop, //
173
                        'adop_famc'      => $adop_famc,  //
174
                        'birt_famc'      => $birt_famc,  //
175
                    ];
176
177
                    $eventData[] = $data;
178
                }
179
            }
180
        }
181
        PersonEvent::on($conn)->insert($eventData);
182
        $new = new Even();
183
        $new->otherField($conn, $events, $person);
184
    }
185
186
    public static function otherField($conn, $events, $person)
187
    {
188
        try
189
        {
190
            foreach ($events as $event) {
191
                
192
                if ($event && count($event) > 0) {
193
                    $even = $event[0];
194
                    $class_name = get_class($even);
195
                    $person_id = $person->id;
196
                    $type = $even->getType();
197
                    $attr = $even->getAttr();
198
                    $_date = $even->getDate();
199
                    $date = \FamilyTree365\LaravelGedcom\Utils\Importer\Date::read($conn, $_date);
200
                    $_plac = $even->getPlac();
201
                    $plac = \FamilyTree365\LaravelGedcom\Utils\Importer\Indi\Even\Plac::read($conn, $_plac);
202
                    $_phon = $even->getPhon();
203
                    $phon = \FamilyTree365\LaravelGedcom\Utils\Importer\Phon::read($conn, $_phon);
204
                    $_addr = $even->getAddr();
205
                    //$addr_id = \FamilyTree365\LaravelGedcom\Utils\Importer\Addr::read($conn, $_addr);
206
                    if (!empty($_addr)) {
207
                        $addr_id = \FamilyTree365\LaravelGedcom\Utils\Importer\Addr::read($conn, $_addr);
0 ignored issues
show
Unused Code introduced by
The assignment to $addr_id is dead and can be removed.
Loading history...
208
                    } else {
209
                        $addr_id = null;
210
                    }
211
212
                    $caus = $even->getCaus();
213
                    $age = $even->getAge();
214
                    $agnc = $even->getAgnc();
215
                    $adop = '';
216
                    $adop_famc = '';
217
                    $birt_famc = '';
218
                    switch ($class_name) {
219
                    case 'Adop':
220
                        $adop = $even->getAdop();
0 ignored issues
show
Unused Code introduced by
The assignment to $adop is dead and can be removed.
Loading history...
221
                        $adop_famc = $even->getFamc();
0 ignored issues
show
Unused Code introduced by
The assignment to $adop_famc is dead and can be removed.
Loading history...
222
                    break;
223
                    case 'Birt':
224
                        $birt_famc = $even->getFamc();
0 ignored issues
show
Unused Code introduced by
The assignment to $birt_famc is dead and can be removed.
Loading history...
225
                    break;
226
                    case 'Bapm':
227
                    break;
228
                    case 'Barm':
229
                    break;
230
                    case 'Basm':
231
                    break;
232
                    case 'Bles':
233
                    break;
234
                    case 'Buri':
235
                    break;
236
                    case 'Cast':
237
                    break;
238
                    case 'Cens':
239
                    break;
240
                    case 'Chr':
241
                        $chr_famc = $even->getFamc();
0 ignored issues
show
Unused Code introduced by
The assignment to $chr_famc is dead and can be removed.
Loading history...
242
                    break;
243
                    case 'Chra':
244
                    break;
245
                    case 'Conf':
246
                    break;
247
                    case 'Crem':
248
                    break;
249
                    case 'Dscr':
250
                    break;
251
                    case 'Deat':
252
                    break;
253
                    case 'Educ':
254
                    break;
255
                    case 'Emig':
256
                    break;
257
                    case 'Fcom':
258
                    break;
259
                    case 'Grad':
260
                    break;
261
                    case 'Idno':
262
                    break;
263
                    case 'Immi':
264
                    break;
265
                    case 'Nati':
266
                    break;
267
                    case 'Nchi':
268
                    break;
269
                    case 'Natu':
270
                    break;
271
                    case 'Nmr':
272
                    break;
273
                    case 'Occu':
274
                    break;
275
                    case 'Ordn':
276
                    break;
277
                    case 'Reti':
278
                    break;
279
                    case 'Prob':
280
                    break;
281
                    case 'Prop':
282
                    break;
283
                    case 'Reli':
284
                    break;
285
                    case 'Resi':
286
                    break;
287
                    case 'Ssn':
288
                    break;
289
                    case 'Titl':
290
                    break;
291
                    case 'Will':
292
                    break;
293
                    case 'Even':
294
                    break;
295
                    default:
296
                }
297
                    $adop = '';
298
                    $adop_famc = '';
299
                    $birt_famc = '';
300
                    // store Even
301
                    $key = [
302
                        ['person_id', $person_id],
303
                        ['title', $class_name],
304
                        ['type', $type],
305
                        ['attr', $attr],
306
                        ['date', $date],
307
                        ['plac', $plac],
308
                        ['phon', $phon],
309
                        ['caus', $caus],
310
                        ['age', $age],
311
                        ['agnc', $agnc],
312
                        ['adop', $adop],
313
                        ['adop_famc', $adop_famc],
314
                        ['birt_famc', $birt_famc],
315
                    ];
316
317
                    // update person's record
318
                    if ($class_name == 'BIRT' && !empty($date)) {
319
                        $person->birthday = date('Y-m-d', strtotime($date));
320
                    }
321
                    // add deathyear to person table ( for form builder )
322
                    if ($class_name == 'DEAT' && !empty($date)) {
323
                        $person->deathday = date('Y-m-d', strtotime($date));
324
                    }
325
                    $person->save();
326
327
                    $sour = $even->getSour();
328
                    $notes = $even->getNote();
329
                    $obje = $even->getObje();
330
                    $_chan = $even->getChan() ?? null;
331
                    if ((!empty($sour) && count($sour) > 0) || (!empty($obje) && count($obje) > 0) || (!empty($notes) && count($notes) > 0) || !empty($_chan)) {
332
                        $record = PersonEvent::on($conn)->where($key)->first();
333
                        $_group = 'indi_even';
334
                        $_gid = $record->id;
335
                    }
336
337
                    // array
338
                    //$sour = $even->getSour();
339
                    if ($sour && count($sour) > 0) {
340
                        foreach ($sour as $item) {
341
                            if ($item) {
342
                                \FamilyTree365\LaravelGedcom\Utils\Importer\SourRef::read($conn, $item, $_group, $_gid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_group does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $_gid does not seem to be defined for all execution paths leading up to this point.
Loading history...
343
                            }
344
                        }
345
                    }
346
                    //$obje = $even->getObje();
347
                    if ($obje && count($obje) > 0) {
348
                        foreach ($obje as $item) {
349
                            if ($item) {
350
                                \FamilyTree365\LaravelGedcom\Utils\Importer\ObjeRef::read($conn, $item, $_group, $_gid, $obje);
351
                            }
352
                        }
353
                    }
354
                    //$notes = $even->getNote();
355
                    if ($notes && count($notes) > 0) {
356
                        foreach ($notes as $item) {
357
                            \FamilyTree365\LaravelGedcom\Utils\Importer\NoteRef::read($conn, $item, $_group, $_gid);
358
                        }
359
                    }
360
                    // object
361
                    //$_chan = $even->getChan() ?? null;
362
                    if ($_chan !== null) {
363
                        \FamilyTree365\LaravelGedcom\Utils\Importer\Chan::read($conn, $_chan, $_group, $_gid);
364
                    }
365
366
                    // $_type = $even->getType();
367
                // $person->addEvent($_type, $date, $plac);
368
                }
369
            }
370
        }
371
        catch(Throwable $e)
372
        {
373
            report($e);
0 ignored issues
show
Bug introduced by
The function report 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

373
            /** @scrutinizer ignore-call */ 
374
            report($e);
Loading history...
374
        }
375
    }
376
}
377