Indi::parse()   F
last analyzed

Complexity

Conditions 68
Paths 3

Size

Total Lines 184
Code Lines 161

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 161
c 1
b 0
f 0
dl 0
loc 184
rs 3.3333
cc 68
nc 3
nop 1

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * php-gedcom.
4
 *
5
 * php-gedcom is a library for parsing, manipulating, importing and exporting
6
 * GEDCOM 5.5 files in PHP 5.3+.
7
 *
8
 * @author          Kristopher Wilson <[email protected]>
9
 * @copyright       Copyright (c) 2010-2013, Kristopher Wilson
10
 * @license         MIT
11
 *
12
 * @link            http://github.com/mrkrstphr/php-gedcom
13
 */
14
15
namespace Gedcom\Parser;
16
17
class Indi extends \Gedcom\Parser\Component
18
{
19
    public static function parse(\Gedcom\Parser $parser)
20
    {
21
        $record = $parser->getCurrentLineRecord();
22
        $depth = (int) $record[0];
23
        if (isset($record[1])) {
24
            $identifier = $parser->normalizeIdentifier($record[1]);
25
        } else {
26
            $parser->skipToNextLevel($depth);
27
28
            return null;
29
        }
30
31
        $indi = new \Gedcom\Record\Indi();
32
        $indi->setId($identifier);
33
34
        $parser->getGedcom()->addIndi($indi);
35
36
        $parser->forward();
37
38
        while (!$parser->eof()) {
39
            $record = $parser->getCurrentLineRecord();
40
            $recordType = strtoupper(trim($record[1]));
41
            $currentDepth = (int) $record[0];
42
43
            if ($currentDepth <= $depth) {
44
                $parser->back();
45
                break;
46
            }
47
48
            if ($recordType == 'BURI') {
49
                $a = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $a is dead and can be removed.
Loading history...
50
            }
51
52
            switch ($recordType) {
53
            case '_UID':
54
                $indi->setUid(trim($record[2]));
55
                break;
56
            case 'RESN':
57
                $indi->setResn(trim($record[2]));
58
                break;
59
            case 'NAME':
60
                $name = \Gedcom\Parser\Indi\Name::parse($parser);
61
                $indi->addName($name);
62
                break;
63
            case 'SEX':
64
                $indi->setSex(isset($record[2]) ? trim($record[2]) : '');
65
                break;
66
            case 'ADOP':
67
            case 'BIRT':
68
                $birt = \Gedcom\Parser\Birt::parse($parser);
69
                $indi->setBirt($birt);
70
                break;
71
            case 'BAPM':
72
            case 'BARM':
73
            case 'BASM':
74
            case 'BLES':
75
            case 'BURI':
76
                $buri = \Gedcom\Parser\Buri::parse($parser);
77
                $indi->setBuri($buri);
78
                break;
79
            case 'CENS':
80
            case 'CHR':
81
                $chr = \Gedcom\Parser\Chr::parse($parser);
82
                $indi->setChr($chr);
83
                break;
84
            case 'CHRA':
85
            case 'CONF':
86
            case 'CREM':
87
            case 'DEAT':
88
                $deat = \Gedcom\Parser\Deat::parse($parser);
89
                $indi->setDeat($deat);
90
                break;
91
            case 'EMIG':
92
            case 'FCOM':
93
            case 'GRAD':
94
            case 'IMMI':
95
            case 'NATU':
96
            case 'ORDN':
97
            case 'RETI':
98
            case 'PROB':
99
            case 'WILL':
100
            case 'EVEN':
101
                $className = ucfirst(strtolower($recordType));
102
                $class = '\\Gedcom\\Parser\\Indi\\'.$className;
103
104
                $event = $class::parse($parser);
105
                $indi->addEven($event);
106
                break;
107
            case 'CAST':
108
            case 'DSCR':
109
            case 'EDUC':
110
            case 'IDNO':
111
            case 'NATI':
112
            case 'NCHI':
113
            case 'NMR':
114
            case 'OCCU':
115
            case 'PROP':
116
            case 'RELI':
117
            case 'RESI':
118
            case 'SSN':
119
            case 'TITL':
120
                $className = ucfirst(strtolower($recordType));
121
                $class = '\\Gedcom\\Parser\\Indi\\'.$className;
122
123
                $att = $class::parse($parser);
124
                $indi->addAttr($att);
125
                break;
126
            case 'BAPL':
127
            case 'CONL':
128
            case 'ENDL':
129
            case 'SLGC':
130
                $className = ucfirst(strtolower($recordType));
131
                $class = '\\Gedcom\\Parser\\Indi\\'.$className;
132
133
                $lds = $class::parse($parser);
134
                $indi->{'add'.$recordType}[] = $lds;
135
                break;
136
            case 'FAMC':
137
                $famc = \Gedcom\Parser\Indi\Famc::parse($parser);
138
                if ($famc) {
139
                    $indi->addFamc($famc);
140
                }
141
                break;
142
            case 'FAMS':
143
                $fams = \Gedcom\Parser\Indi\Fams::parse($parser);
144
                if ($fams) {
145
                    $indi->addFams($fams);
146
                }
147
                break;
148
            case 'SUBM':
149
                $indi->addSubm($parser->normalizeIdentifier($record[2]));
150
                break;
151
            case 'ASSO':
152
                $asso = \Gedcom\Parser\Indi\Asso::parse($parser);
153
                $indi->addAsso($asso);
154
                break;
155
            case 'ALIA':
156
                $indi->addAlia($parser->normalizeIdentifier($record[2]));
157
                break;
158
            case 'ANCI':
159
                $indi->addAnci($parser->normalizeIdentifier($record[2]));
160
                break;
161
            case 'DESI':
162
                $indi->addDesi($parser->normalizeIdentifier($record[2]));
163
                break;
164
            case 'RFN':
165
                $indi->setRfn(trim($record[2]));
166
                break;
167
            case 'AFN':
168
                $indi->setAfn(trim($record[2]));
169
                break;
170
            case 'REFN':
171
                $ref = \Gedcom\Parser\Refn::parse($parser);
172
                $indi->addRefn($ref);
173
                break;
174
            case 'RIN':
175
                $indi->setRin(trim($record[2]));
176
                break;
177
            case 'CHAN':
178
                $chan = \Gedcom\Parser\Chan::parse($parser);
179
                $indi->setChan($chan);
0 ignored issues
show
Bug introduced by
$chan of type Gedcom\Record\Chan 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

179
                $indi->setChan(/** @scrutinizer ignore-type */ $chan);
Loading history...
180
                break;
181
            case 'NOTE':
182
                $note = \Gedcom\Parser\NoteRef::parse($parser);
183
                if ($note) {
184
                    $indi->addNote($note);
185
                }
186
                break;
187
            case 'SOUR':
188
                $sour = \Gedcom\Parser\SourRef::parse($parser);
189
                $indi->addSour($sour);
190
                break;
191
            case 'OBJE':
192
                $obje = \Gedcom\Parser\ObjeRef::parse($parser);
193
                $indi->addObje($obje);
194
                break;
195
            default:
196
                $parser->logUnhandledRecord(self::class.' @ '.__LINE__);
197
            }
198
199
            $parser->forward();
200
        }
201
202
        return $indi;
203
    }
204
}
205