Indi   F
last analyzed

Complexity

Total Complexity 67

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 148
dl 0
loc 170
rs 3.04
c 0
b 0
f 0
wmc 67

1 Method

Rating   Name   Duplication   Size   Complexity  
F parse() 0 168 67

How to fix   Complexity   

Complex Class

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

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 PhpGedcom\Parser;
16
17
class Indi extends \PhpGedcom\Parser\Component
18
{
19
    public static function parse(\PhpGedcom\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 \PhpGedcom\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
            switch ($recordType) {
49
            case '_UID':
50
                $indi->setUid(trim($record[2]));
51
                break;
52
            case 'RESN':
53
                $indi->setResn(trim($record[2]));
54
                break;
55
            case 'NAME':
56
                $name = \PhpGedcom\Parser\Indi\Name::parse($parser);
57
                $indi->addName($name);
58
                break;
59
            case 'SEX':
60
                $indi->setSex(isset($record[2]) ? trim($record[2]) : '');
61
                break;
62
            case 'ADOP':
63
            case 'BIRT':
64
            case 'BAPM':
65
            case 'BARM':
66
            case 'BASM':
67
            case 'BLES':
68
            case 'BURI':
69
            case 'CENS':
70
            case 'CHR':
71
            case 'CHRA':
72
            case 'CONF':
73
            case 'CREM':
74
            case 'DEAT':
75
            case 'EMIG':
76
            case 'FCOM':
77
            case 'GRAD':
78
            case 'IMMI':
79
            case 'NATU':
80
            case 'ORDN':
81
            case 'RETI':
82
            case 'PROB':
83
            case 'WILL':
84
            case 'EVEN':
85
                $className = ucfirst(strtolower($recordType));
86
                $class = '\\PhpGedcom\\Parser\\Indi\\'.$className;
87
88
                $event = $class::parse($parser);
89
                $indi->addEven($event);
90
                break;
91
            case 'CAST':
92
            case 'DSCR':
93
            case 'EDUC':
94
            case 'IDNO':
95
            case 'NATI':
96
            case 'NCHI':
97
            case 'NMR':
98
            case 'OCCU':
99
            case 'PROP':
100
            case 'RELI':
101
            case 'RESI':
102
            case 'SSN':
103
            case 'TITL':
104
                $className = ucfirst(strtolower($recordType));
105
                $class = '\\PhpGedcom\\Parser\\Indi\\'.$className;
106
107
                $att = $class::parse($parser);
108
                $indi->addAttr($att);
109
                break;
110
            case 'BAPL':
111
            case 'CONL':
112
            case 'ENDL':
113
            case 'SLGC':
114
                $className = ucfirst(strtolower($recordType));
115
                $class = '\\PhpGedcom\\Parser\\Indi\\'.$className;
116
117
                $lds = $class::parse($parser);
118
                $indi->{'add'.$recordType}[] = $lds;
119
                break;
120
            case 'FAMC':
121
                $famc = \PhpGedcom\Parser\Indi\Famc::parse($parser);
122
                if ($famc) {
123
                    $indi->addFamc($famc);
124
                }
125
                break;
126
            case 'FAMS':
127
                $fams = \PhpGedcom\Parser\Indi\Fams::parse($parser);
128
                if ($fams) {
129
                    $indi->addFams($fams);
130
                }
131
                break;
132
            case 'SUBM':
133
                $indi->addSubm($parser->normalizeIdentifier($record[2]));
134
                break;
135
            case 'ASSO':
136
                $asso = \PhpGedcom\Parser\Indi\Asso::parse($parser);
137
                $indi->addAsso($asso);
138
                break;
139
            case 'ALIA':
140
                $indi->addAlia($parser->normalizeIdentifier($record[2]));
141
                break;
142
            case 'ANCI':
143
                $indi->addAnci($parser->normalizeIdentifier($record[2]));
144
                break;
145
            case 'DESI':
146
                $indi->addDesi($parser->normalizeIdentifier($record[2]));
147
                break;
148
            case 'RFN':
149
                $indi->setRfn(trim($record[2]));
150
                break;
151
            case 'AFN':
152
                $indi->setAfn(trim($record[2]));
153
                break;
154
            case 'REFN':
155
                $ref = \PhpGedcom\Parser\Refn::parse($parser);
156
                $indi->addRefn($ref);
157
                break;
158
            case 'RIN':
159
                $indi->setRin(trim($record[2]));
160
                break;
161
            case 'CHAN':
162
                $chan = \PhpGedcom\Parser\Chan::parse($parser);
163
                $indi->setChan($chan);
0 ignored issues
show
Bug introduced by
$chan of type PhpGedcom\Record\Chan is incompatible with the type string expected by parameter $chan of PhpGedcom\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

163
                $indi->setChan(/** @scrutinizer ignore-type */ $chan);
Loading history...
164
                break;
165
            case 'NOTE':
166
                $note = \PhpGedcom\Parser\NoteRef::parse($parser);
167
                if ($note) {
168
                    $indi->addNote($note);
169
                }
170
                break;
171
            case 'SOUR':
172
                $sour = \PhpGedcom\Parser\SourRef::parse($parser);
173
                $indi->addSour($sour);
174
                break;
175
            case 'OBJE':
176
                $obje = \PhpGedcom\Parser\ObjeRef::parse($parser);
177
                $indi->addObje($obje);
178
                break;
179
            default:
180
                $parser->logUnhandledRecord(get_class().' @ '.__LINE__);
181
            }
182
183
            $parser->forward();
184
        }
185
186
        return $indi;
187
    }
188
}
189