Passed
Push — master ( e501a6...572009 )
by Curtis
01:50 queued 01:21
created

GedcomGenerator   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 525
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 254
c 3
b 0
f 0
dl 0
loc 525
rs 5.5199
wmc 56

14 Methods

Rating   Name   Duplication   Size   Complexity  
C addDownData() 0 65 14
A __construct() 0 9 1
A setSubM() 0 2 1
A setRepo() 0 2 1
A setObje() 0 2 1
A getGedcomFamily() 0 6 1
A getGedcomPerson() 0 7 1
A setSour() 0 2 1
A setSubn() 0 2 1
A setNote() 0 2 1
D setFam() 0 147 10
F setIndi() 0 81 16
A addUpData() 0 20 6
A setHead() 0 66 1

How to fix   Complexity   

Complex Class

Complex classes like GedcomGenerator 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 GedcomGenerator, and based on these observations, apply Extract Interface, too.

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 FamilyTree365\LaravelGedcom\Models\PersonEvent;
9
use Gedcom\Gedcom;
10
use Gedcom\Record\Fam;
11
use Gedcom\Record\Fam\Even;
12
use Gedcom\Record\Fam\Slgs;
13
use Gedcom\Record\Head;
14
use Gedcom\Record\Head\Sour;
15
use Gedcom\Record\Indi;
16
use Gedcom\Record\Indi\Even as Personal;
17
use Gedcom\Record\Indi\Fams;
18
use Gedcom\Record\Indi\Name;
19
use Gedcom\Record\NoteRef;
20
use Gedcom\Record\ObjeRef;
21
use Gedcom\Record\SourRef;
22
use Gedcom\Record\Subm;
23
use Gedcom\Writer;
24
25
class GedcomGenerator
26
{
27
    protected $family_id;
28
    protected $p_id;
29
    protected $up_nest;
30
    protected $down_nest;
31
    protected $arr_indi_id = [];
32
    protected $arr_fam_id = [];
33
    protected $_gedcom = null;
34
    protected $log = "\n";
35
36
    /**
37
     * Constructor with family_id.
38
     *
39
     * @param int $p_id
40
     * @param int $family_id
41
     * @param int $up_nest
42
     * @param int $down_nest
43
     */
44
    public function __construct($p_id = 0, $family_id = 0, $up_nest = 0, $down_nest = 0)
45
    {
46
        $this->family_id = $family_id;
47
        $this->p_id = $p_id;
48
        $this->up_nest = $up_nest;
49
        $this->down_nest = $down_nest;
50
        $this->arr_indi_id = [];
51
        $this->arr_fam_id = [];
52
        $this->_gedcom = new Gedcom();
53
    }
54
55
    public function getGedcomFamily()
56
    {
57
        $this->setHead();
58
        $writer = new Writer();
59
60
        return $writer->convert($this->_gedcom);
61
    }
62
63
    public function getGedcomPerson()
64
    {
65
        $this->setHead();
66
        $this->addUpData($this->p_id);
67
        $writer = new Writer();
68
69
        return $writer->convert($this->_gedcom);
70
    }
71
72
    public function addUpData($p_id, $nest = 0)
73
    {
74
        if (empty($p_id) || $p_id < 1) {
75
            return;
76
        }
77
78
        if ($this->up_nest < $nest) {
79
            return;
80
        }
81
82
        /* $person = Person::query()->find($p_id);
83
        if ($person == null) {
84
            return;
85
        } */
86
        $persons = Person::query()->get();
87
        if ($persons == null) {
88
            return;
89
        }
90
        foreach ($persons as $key => $person) {
91
            $this->setIndi($person->id);
92
        }
93
94
        // add self to indi
95
        /* if (!in_array($p_id, $this->arr_indi_id)) {
96
            array_push($this->arr_indi_id, $p_id);
97
            $this->setIndi($p_id);
98
        } else {
99
            // already processed this person
100
            return;
101
        } */
102
103
        // process family ( partner, children )
104
        /* $_families = Family::query()->where('husband_id', $p_id)->orwhere('wife_id', $p_id)->get();
105
        foreach ($_families as $item) {
106
            // add family
107
            $f_id = $item->id;
108
            if (!in_array($f_id, $this->arr_fam_id)) {
109
                array_push($this->arr_fam_id, $f_id);
110
                $this->setFam($f_id);
111
            }
112
113
            // add partner to indi
114
            $husb_id = $item->husband_id;
115
            $wife_id = $item->wife_id;
116
            $this->log .= $nest.' f_id='.$f_id."\n";
117
            $this->log .= $nest.' husb_id='.$husb_id."\n";
118
            $this->log .= $nest.' wife_id='.$wife_id."\n";
119
            $this->addUpData($husb_id, $nest);
120
            $this->addUpData($wife_id, $nest);
121
122
            // add chidrent to indi
123
            $children = Person::query()->where('child_in_family_id', $f_id)->get();
124
            foreach ($children as $item2) {
125
                $child_id = $item2->id;
126
                if (!in_array($child_id, $this->arr_indi_id)) {
127
                    array_push($this->arr_indi_id, $child_id);
128
                    $this->setIndi($child_id);
129
                }
130
            }
131
        } */
132
133
        /* $parent_family_id = $person->child_in_family_id;
134
        $p_family = Family::query()->find($parent_family_id);
135
136
        // there is not parent data.
137
        if ($p_family === null) {
138
            return;
139
        }
140
141
        // process siblings
142
        $siblings = Person::query()->where('child_in_family_id', $parent_family_id)->get();
143
        foreach ($siblings as $item3) {
144
            $sibling_id = $item3->id;
145
            if (!in_array($sibling_id, $this->arr_indi_id)) {
146
                array_push($this->arr_indi_id, $sibling_id);
147
                $this->setIndi($sibling_id);
148
            }
149
        }
150
151
        // process parent
152
        $nest++;
153
        $father_id = $p_family->husband_id;
154
        $mother_id = $p_family->wife_id;
155
        $this->addUpData($father_id, $nest);
156
        $this->addUpData($mother_id, $nest); */
157
    }
158
159
    public function addDownData($p_id, $nest = 0)
160
    {
161
        if (empty($p_id) || $p_id < 1) {
162
            return;
163
        }
164
        if ($this->down_nest < $nest) {
165
            return;
166
        }
167
168
        $person = Person::query()->find($p_id);
169
        if ($person == null) {
170
            return;
171
        }
172
173
        // process self
174
        if (!in_array($p_id, $this->arr_indi_id)) {
175
            // add to indi array
176
            array_push($this->arr_indi_id, $p_id);
177
            $this->setIndi($p_id);
178
        }
179
180
        $_families = Family::query()->where('husband_id', $p_id)->orwhere('wife_id', $p_id)->get();
181
        foreach ($_families as $item) {
182
            // add family
183
            $f_id = $item->id;
184
            if (!in_array($f_id, $this->arr_fam_id)) {
185
                array_push($this->arr_fam_id, $f_id);
186
                $this->setFam($f_id);
187
            }
188
            // process partner
189
            $husband_id = $item->husband_id;
190
            $wife_id = $item->wife_id;
191
            $this->addDownData($husband_id, $nest);
192
            $this->addDownData($wife_id, $nest);
193
194
            // process child
195
            $children = Person::query()->where('child_in_family_id', $item->id);
196
            foreach ($children as $item2) {
197
                $child_id = $item2->id;
198
                $nest_next = $nest + 1;
199
                $this->addDownData($child_id, $nest_next);
200
            }
201
        }
202
203
        // process parent
204
        $parent_family_id = $person->child_in_family_id;
205
        $parent_family = Family::query()->find($parent_family_id);
206
        if ($parent_family != null) {
207
            $father_id = $parent_family->husband_id;
208
            $mother_id = $parent_family->wife_id;
209
            if (!in_array($father_id, $this->arr_indi_id)) {
210
                array_push($this->arr_indi_id, $father_id);
211
                $this->setIndi($father_id);
212
            }
213
            if (!in_array($mother_id, $this->arr_indi_id)) {
214
                array_push($this->arr_indi_id, $mother_id);
215
                $this->setIndi($mother_id);
216
            }
217
        }
218
        // process siblings
219
        $siblings = Person::query()->where('child_in_family_id', $parent_family_id)->get();
220
        foreach ($siblings as $item3) {
221
            $sibling_id = $item3->id;
222
            if (!in_array($sibling_id, $this->arr_indi_id)) {
223
                $this->addDownData($sibling_id, $nest);
224
            }
225
        }
226
    }
227
228
    protected function setHead()
229
    {
230
        $head = new Head();
231
        /**
232
         * @var Sour
233
         */
234
        $sour = new Sour();
235
        $sour->setSour(env('APP_NAME', ''));
0 ignored issues
show
Bug introduced by
The function env 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

235
        $sour->setSour(/** @scrutinizer ignore-call */ env('APP_NAME', ''));
Loading history...
236
        $sour->setVersion('1.0');
237
        $head->setSour($sour);
238
        /**
239
         * @var string
240
         */
241
        $dest = null;
242
        $head->setDest($dest);
243
        /**
244
         * @var Head\Date
245
         */
246
        $date = null;
247
        $head->setDate($date);
248
        /**
249
         * @var string
250
         */
251
        $subm = null;
252
        $head->setSubm($subm);
253
        /**
254
         * @var string
255
         */
256
        $subn = null;
257
        $head->setSubn($subn);
258
        /**
259
         * @var string
260
         */
261
        $file = null;
262
        $head->setFile($file);
263
        /**
264
         * @var string
265
         */
266
        $copr = null;
267
        $head->setCopr($copr);
268
        /**
269
         * @var Head\Gedc
270
         */
271
        $gedc = null;
272
        $head->setGedc($gedc);
273
        /**
274
         * @var Head\Char
275
         */
276
        $char = null;
277
        $head->setChar($char);
278
        /**
279
         * @var string
280
         */
281
        $lang = null;
282
        $head->setLang($lang);
283
        /**
284
         * @var Head\Plac
285
         */
286
        $plac = null;
287
        $head->setPlac($plac);
288
        /**
289
         * @var string
290
         */
291
        $note = null;
292
        $head->setNote($note);
293
        $this->_gedcom->setHead($head);
294
    }
295
296
    protected function setIndi($p_id)
297
    {
298
        $indi = new Indi();
299
        $person = Person::query()->find($p_id);
300
        if ($person == null) {
301
            return;
302
        }
303
        /**
304
         * @var string
305
         */
306
        $id = $person->id;
307
        $indi->setId($id);
308
309
        $gid = $person->gid;
310
        $indi->setGid($gid);
311
        
312
        $uid = $person->uid;
313
        $indi->setUid($uid);
314
315
        $_name = new Name();
316
        $_name->setName($person->name);
317
        $_name->setGivn($person->givn);
318
        $_name->setNick($person->nick);
319
        $_name->setSurn($person->surn);
320
        $_name->setNsfx($person->nsfx);
321
        $indi->addName($_name);
322
323
        /**
324
         * @var string
325
         */
326
        $sex = $person->sex;
327
        $indi->setSex($sex);
328
329
        if ($person->birthday || $person->birth_year) {
330
            $birthday = $person->birthday ? strtoupper($person->birthday->format('j M Y')) : $person->birth_year;
331
            $indi->setBirthday($birthday);
332
        }
333
334
        if ($person->deathday || $person->death_year) {
335
            $deathday = $person->deathday ? strtoupper($person->deathday->format('j M Y')) : $person->death_year;
336
            $indi->setDeathday($deathday);
337
        }
338
339
        if ($person->burial_day || $person->burial_year) {
340
            $burialday = $person->burial_day ? strtoupper(Carbon::parse($person->burial_day)->format('j M Y')) : $person->burial_year;
341
            $indi->setBurialday($burialday);
342
        }
343
344
        if ($person->chan) {
345
            $chan = Carbon::parse($person->chan);
346
            $chan = [
347
                strtoupper($chan->format('j M Y')),
348
                $chan->format('H:i:s.v')
349
            ];
350
            $indi->setChan($chan);
0 ignored issues
show
Bug introduced by
$chan of type array<integer,mixed|string> is incompatible with the type string expected by parameter $chan of Gedcom\Record\Indi::setChan(). ( Ignorable by Annotation )

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

350
            $indi->setChan(/** @scrutinizer ignore-type */ $chan);
Loading history...
351
        }
352
353
        $place = PersonEvent::query()->find($p_id);
354
        $_plac = new Personal();
355
        if (!empty($place->type)) {
356
            $_plac->setType($place->type);
357
        }
358
        if (!empty($place->date)) {
359
            $date = \FamilyTree365\LaravelGedcom\Utils\Importer\Date::read('', $place->date);
360
            $_plac->setDate($date);
361
        }
362
        if (!empty($place->type) && !empty($place->date)) {
363
            $indi->getAllEven($_plac);
0 ignored issues
show
Unused Code introduced by
The call to Gedcom\Record\Indi::getAllEven() has too many arguments starting with $_plac. ( Ignorable by Annotation )

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

363
            $indi->/** @scrutinizer ignore-call */ 
364
                   getAllEven($_plac);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
364
        }
365
366
        /**
367
         * @var Fams[]
368
         */
369
        /* $fams = Family::query()->where('husband_id', $p_id)->orwhere('wife_id', $p_id)->get();
370
        foreach ($fams as $item) {
371
            $fam = new Fams();
372
            $fam->setFams($item->id);
373
            $indi->addFams($fam);
374
        } */
375
376
        $this->_gedcom->addIndi($indi);
377
    }
378
379
    protected function setFam($family_id)
380
    {
381
        $famData = Family::query()->where('id', $family_id)->first();
382
        if ($famData == null) {
383
            return null;
384
        }
385
        $fam = new Fam();
386
        $_id = $famData->id;
387
        $fam->setId($_id);
388
389
        $_chan = null;
390
        $fam->setChan($_chan);
391
392
        $_husb = $famData->husband_id;
393
        $fam->setHusb($_husb);
394
395
        // add husb individual
396
        // $this->setIndi($_husb, $family_id);
397
398
        $_wife = $famData->wife_id;
399
        $fam->setWife($_wife);
400
401
        // add wife individual
402
        // $this->setIndi($_wife, $family_id);
403
404
        $_nchi = null;
405
        $fam->setNchi($_nchi);
406
407
        $_chil = Person::query()->where('child_in_family_id', $family_id)->get();
408
        foreach ($_chil as $item) {
409
            $fam->addChil($item->id);
410
            // $this->setIndi($item->id);
411
        }
412
413
        $_even = [];
414
        foreach ($_even as $item) {
415
            $even = new Even();
416
            $_type = null; // string
417
            $_date = null; // string
418
            $_plac = null; // \Gedcom\Record\Indi\Even\Plac
419
            $_caus = null; // string
420
            $_age = null;  // string
0 ignored issues
show
Unused Code introduced by
The assignment to $_age is dead and can be removed.
Loading history...
421
            $_addr = null; // \Gedcom\Record\Addr
422
            $_phon = []; // \Gedcom\Record\Phon
423
            $_agnc = null; // string
424
            $_husb = null; // \Gedcom\Record\Fam\Even\Husb
425
            $_wife = null; // \Gedcom\Record\Fam\Even\Wife
426
            $_obje = []; // \Gedcom\Writer\ObjeRef
427
            $_sour = []; // \Gedcom\Writer\SourRef
428
            $_note = []; // \Gedcom\Writer\NoteRef
429
            $even->setType($_type);
430
            $even->setDate($_date);
431
            $even->setPlac($_plac);
432
            $even->setCaus($_caus);
433
            $even->setAddr($_addr);
434
            $even->setPhon($_phon);
435
            $even->setAgnc($_agnc);
436
            $even->setHusb($_husb);
437
            $even->setWife($_wife);
438
            $even->setObje($_obje);
439
            $even->setSour($_sour);
440
            $even->setNote($_note);
441
            $fam->addEven($even);
442
        }
443
444
        $_slgs = [];
445
        foreach ($_slgs as $item) {
446
            $slgs = new Slgs();
447
            $_stat = null;
448
            $_date = null;
449
            $_plac = null;
450
            $_temp = null;
451
            $_sour = [];
452
            $_note = [];
453
454
            $slgs->setStat($_stat);
455
            $slgs->setDate($_date);
456
            $slgs->setPlac($_plac);
457
            $slgs->setTemp($_temp);
458
            $slgs->setSour($_sour);
459
            $slgs->setNote($_note);
460
            $fam->addSlgs($slgs);
461
        }
462
463
        $_subm = [];
464
        foreach ($_subm as $item) {
465
            $subm = new Subm();
466
            $subm_id = null;
467
            $chan = null; // @var Record\Chan
468
            $name = null;
469
            $addr = null; //@var Record\Addr
470
            $rin = null;
471
            $rfn = null;
472
            $lang = [];
473
            $phon = [];
474
            $obje = [];
475
            $note = [];
476
477
            $subm->setSubm($subm_id);
478
            $subm->setChan($chan);
479
            $subm->setName($name);
480
            $subm->setAddr($addr);
481
            $subm->setRin($rin);
482
            $subm->setRfn($rfn);
483
484
            $subm->setLang($lang);
485
            $subm->setPhon($phon);
486
            $subm->setObje($obje);
487
            $subm->setNote($note);
488
489
            $fam->addSubm($subm);
490
        }
491
492
        $_refn = [];
493
        foreach ($_refn as $item) {
494
            $refn = null;
495
            $type = null;
496
497
            $subm->setRefn($refn);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $subm seems to be defined by a foreach iteration on line 464. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
498
            $subm->setType($type);
499
500
            $fam->addRefn($refn);
501
        }
502
503
        $_rin = null;
504
        $fam->setRin($_rin);
505
506
        $_note = [];
507
        foreach ($_note as $item) {
508
            $note = new NoteRef();
509
            $fam->addNote($note);
510
        }
511
512
        $_sour = [];
513
        foreach ($_sour as $item) {
514
            $sour = new SourRef();
515
            $fam->addSour($sour);
516
        }
517
518
        $_obje = [];
519
        foreach ($_obje as $item) {
520
            $obje = new ObjeRef();
521
            $fam->addObje($obje);
522
        }
523
        $this->_gedcom->addFam($fam);
524
525
        return $fam;
526
    }
527
528
    protected function setSubn()
529
    {
530
    }
531
532
    protected function setSubM()
533
    {
534
    }
535
536
    protected function setSour()
537
    {
538
    }
539
540
    protected function setNote()
541
    {
542
    }
543
544
    protected function setRepo()
545
    {
546
    }
547
548
    protected function setObje()
549
    {
550
    }
551
}
552